Fix some issues with whitespace in formatter.
1) Insert a whitespace after the forward slash of a single line comment e.g:
//hello. -> // hello.
2) Remove whitespace above the first element in a container:
struct Hello {
enum Something {
BLAH,
};
};
becomes:
struct Hello {
enum Something {
BLAH,
};
};
R=rudominer@chromium.org, rudominer
BUG= #592
Review URL: https://codereview.chromium.org/1819203002 .
diff --git a/mojo/public/tools/bindings/mojom_tool/bin/linux64/mojom.sha1 b/mojo/public/tools/bindings/mojom_tool/bin/linux64/mojom.sha1
index 5bc7186..4e1717c 100644
--- a/mojo/public/tools/bindings/mojom_tool/bin/linux64/mojom.sha1
+++ b/mojo/public/tools/bindings/mojom_tool/bin/linux64/mojom.sha1
@@ -1 +1 @@
-b543e123fc4b0eff0e6b353c5403ec4dd58a4407
\ No newline at end of file
+f08221cad306516a10689f6d65e774e646826c9c
\ No newline at end of file
diff --git a/mojo/public/tools/bindings/mojom_tool/bin/mac64/mojom.sha1 b/mojo/public/tools/bindings/mojom_tool/bin/mac64/mojom.sha1
index f8f67ea..24d5f22 100644
--- a/mojo/public/tools/bindings/mojom_tool/bin/mac64/mojom.sha1
+++ b/mojo/public/tools/bindings/mojom_tool/bin/mac64/mojom.sha1
@@ -1 +1 @@
-63db0676e6002170cb875ddc287e1f75a5e4fdbd
\ No newline at end of file
+5bf2707796bf2f79b4afe80c82a87515f95e614e
\ No newline at end of file
diff --git a/mojom/mojom_parser/formatter/printer.go b/mojom/mojom_parser/formatter/printer.go
index 973d0e9..821defa 100644
--- a/mojom/mojom_parser/formatter/printer.go
+++ b/mojom/mojom_parser/formatter/printer.go
@@ -96,7 +96,7 @@
}
if mojomFile.FinalComments != nil {
- finalComments := trimEmptyLines(mojomFile.FinalComments)
+ finalComments := trimEmptyLinesEnd(mojomFile.FinalComments)
if len(finalComments) > 0 {
p.nl()
p.writeCommentBlocks(finalComments, true)
@@ -375,6 +375,9 @@
p.nl()
declaredObjects := container.GetDeclaredObjects()
for i, declaredObject := range declaredObjects {
+ if i == 0 {
+ trimEmptyLinesDeclaredObject(declaredObject)
+ }
p.writeDeclaredObject(declaredObject)
if i < len(declaredObjects)-1 {
p.nl()
@@ -632,7 +635,7 @@
} else if comment.Text == "// end-no-format" {
p.endNoFormat(comment)
}
- p.write(comment.Text)
+ p.writeSingleLineComment(comment)
if finalEol || i < len(comments)-1 {
p.nl()
}
@@ -653,6 +656,21 @@
}
}
+func (p *printer) writeSingleLineComment(comment lexer.Token) {
+ if comment.Kind != lexer.SingleLineComment {
+ panic(fmt.Sprintf("This is not a SingleLineComment: %s", comment))
+ }
+ commentText := comment.Text
+
+ // We expect that the first 2 characters are // followed by a space or tab.
+ // If the third character is not a space or tab, we insert a space.
+ space := commentText[2]
+ if space != ' ' && space != '\t' {
+ commentText = "// " + commentText[2:]
+ }
+ p.write(commentText)
+}
+
func (p *printer) writeMultiLineComment(comment lexer.Token) {
if comment.Kind != lexer.MultiLineComment {
panic(fmt.Sprintf("This is not a MultiLineComment: %s", comment))
@@ -734,7 +752,8 @@
// Before going to the next line, print the last comment on the line.
if p.eolComment != nil {
if !p.noFormat {
- p.writef(" %s", p.eolComment.Text)
+ p.write(" ")
+ p.writeSingleLineComment(*p.eolComment)
}
p.eolComment = nil
}
@@ -868,8 +887,8 @@
return false
}
-// trimEmptyLines trims out empty line comments at the end of a slice of comments.
-func trimEmptyLines(comments []lexer.Token) []lexer.Token {
+// trimEmptyLinesEnd trims out empty line comments at the end of a slice of comments.
+func trimEmptyLinesEnd(comments []lexer.Token) []lexer.Token {
lastNonEmpty := -1
for i, comment := range comments {
if comment.Kind != lexer.EmptyLine {
@@ -879,6 +898,35 @@
return comments[:lastNonEmpty+1]
}
+// trimEmptyLinesBegin trims out empty line comments at the beginning of a slice of comments.
+func trimEmptyLinesBegin(comments []lexer.Token) []lexer.Token {
+ for i, comment := range comments {
+ if comment.Kind != lexer.EmptyLine {
+ return comments[i:]
+ }
+ }
+ return comments[len(comments):]
+}
+
+// trimEmptyLinesDeclaredObject trims out empty lines from the beginning of the
+// comments on a DeclaredObject or its Attributes.
+func trimEmptyLinesDeclaredObject(declaredObject mojom.DeclaredObject) {
+ if declaredObject.Attributes() != nil {
+ comments := declaredObject.Attributes().AttachedComments()
+ if comments == nil {
+ return
+ }
+ comments.Above = trimEmptyLinesBegin(comments.Above)
+ return
+ }
+ comments := declaredObject.AttachedComments()
+
+ if comments == nil || len(comments.Above) == 0 {
+ return
+ }
+ comments.Above = trimEmptyLinesBegin(comments.Above)
+}
+
// Following is a utility to sort slices of |ImportedFile|s.
// sortImportedFiles sorts the slice of imported files it receives.
diff --git a/mojom/mojom_parser/formatter/printer_test.go b/mojom/mojom_parser/formatter/printer_test.go
index 6014009..2e2888a 100644
--- a/mojom/mojom_parser/formatter/printer_test.go
+++ b/mojom/mojom_parser/formatter/printer_test.go
@@ -140,6 +140,22 @@
checkEq(t, expected, p.result())
}
+func TestWriteSingleLineComment(t *testing.T) {
+ commentText := "// Hello world."
+ token := lexer.Token{Kind: lexer.SingleLineComment, Text: commentText}
+ p := getNewPrinter()
+ p.writeSingleLineComment(token)
+ checkEq(t, commentText, p.result())
+}
+
+func TestWriteSingleLineCommentAddSpace(t *testing.T) {
+ commentText := "//Hello world."
+ token := lexer.Token{Kind: lexer.SingleLineComment, Text: commentText}
+ p := getNewPrinter()
+ p.writeSingleLineComment(token)
+ checkEq(t, "// Hello world.", p.result())
+}
+
func TestWriteMultilineComments(t *testing.T) {
commentText := `/*
* Some comment.