Skip to content

Commit

Permalink
Fix Extra Whitespace When Comments Are Within List Initializers
Browse files Browse the repository at this point in the history
Treat single-line comments that include wrapping newlines as multiline
comments.

Remove the space added around list initialization elements when injected
comments contain newlines.
  • Loading branch information
cwarden committed Jan 7, 2025
1 parent 14e8185 commit 6da262b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
61 changes: 43 additions & 18 deletions formatter/format_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package formatter

import (
"fmt"
"strconv"
"testing"

Expand All @@ -22,7 +23,6 @@ func (e *testErrorListener) SyntaxError(_ antlr.Recognizer, _ interface{}, line,
func TestStatement(t *testing.T) {
if testing.Verbose() {
log.SetLevel(log.DebugLevel)

}
tests :=
[]struct {
Expand Down Expand Up @@ -427,7 +427,9 @@ func TestMemberDeclaration(t *testing.T) {
func TestCompilationUnit(t *testing.T) {
if testing.Verbose() {
log.SetLevel(log.DebugLevel)

log.SetFormatter(&log.TextFormatter{
DisableQuote: true,
})
}
tests :=
[]struct {
Expand Down Expand Up @@ -561,27 +563,50 @@ public class Top {
/* comment
*/
public String it;
}`,
},
{
`class TestClass {
List<String> vals = new List<String>{
// test comment 1
'val1', 'val2',
// test comment 2
'val3'
// test comment 3
};
}`,
`class TestClass {
List<String> vals = new List<String>{
// test comment 1
'val1', 'val2',
// test comment 2
'val3'
// test comment 3
};
}`,
},
}
for _, tt := range tests {
input := antlr.NewInputStream(tt.input)
lexer := parser.NewApexLexer(input)
stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
for i, tt := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {

p := parser.NewApexParser(stream)
p.RemoveErrorListeners()
p.AddErrorListener(&testErrorListener{t: t})
input := antlr.NewInputStream(tt.input)
lexer := parser.NewApexLexer(input)
stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)

v := NewFormatVisitor(stream)
out, ok := v.visitRule(p.CompilationUnit()).(string)
if !ok {
t.Errorf("Unexpected result parsing apex")
}
out = removeExtraCommentIndentation(out)
if out != tt.output {
t.Errorf("unexpected format. expected:\n%q\ngot:\n%q\n", tt.output, out)
}
p := parser.NewApexParser(stream)
p.RemoveErrorListeners()
p.AddErrorListener(&testErrorListener{t: t})

v := NewFormatVisitor(stream)
out, ok := v.visitRule(p.CompilationUnit()).(string)
if !ok {
t.Errorf("Unexpected result parsing apex")
}
out = removeExtraCommentIndentation(out)
if out != tt.output {
t.Errorf("unexpected format. expected:\n%q\ngot:\n%q\n", tt.output, out)
}
})
}
}

Expand Down
6 changes: 5 additions & 1 deletion formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,13 @@ func removeIndentationFromComment(comment string) string {
// comment.
func removeExtraCommentIndentation(input string) string {
// Remove extra grammar-specific newlines added unaware of newline-preserving comments injected
newlinePrefixedMultilineComment := regexp.MustCompile("\n(\t*\uFFFA)")
newlinePrefixedMultilineComment := regexp.MustCompile("[\n ]*(\t*\uFFFA)")
input = newlinePrefixedMultilineComment.ReplaceAllString(input, "$1")

// Remove extra grammar-specific space added unaware of newline-preserving comments injected
spacePaddedMultilineComment := regexp.MustCompile(`(` + "\uFFFB\n*\t*" + `) +`)
input = spacePaddedMultilineComment.ReplaceAllString(input, "$1")

newlinePrefixedInlineComment := regexp.MustCompile("\n\t*\uFFF9\n")
input = newlinePrefixedInlineComment.ReplaceAllString(input, "\uFFF9\n")

Expand Down
4 changes: 2 additions & 2 deletions formatter/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,8 @@ func appendHiddenTokens(v *FormatVisitor, result interface{}, tokens []antlr.Tok
trailing = " "
}
// Strip leading whitespace so the comment can be indented to the right location
text = strings.TrimSpace(text)
containsNewline := strings.Contains(text, "\n")
text = strings.TrimSpace(text)

text = fmt.Sprintf("%s%s%s", leading, text, trailing)
log.Debug(fmt.Sprintf("NORMALIZED COMMENT: %q\n", text))
Expand All @@ -353,7 +353,7 @@ func appendHiddenTokens(v *FormatVisitor, result interface{}, tokens []antlr.Tok
text = "\n" // Replace multiple blank lines with a single blank line
} else {
// whitespace to ignore
text = ""
continue
}

tokenLines = append(tokenLines, text)
Expand Down

0 comments on commit 6da262b

Please sign in to comment.