Skip to content

Commit

Permalink
More Comment Formatting Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cwarden committed Feb 12, 2025
1 parent a431091 commit c1c720e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
27 changes: 27 additions & 0 deletions formatter/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,33 @@ public class A {}`,
}`,
`public class TestClass {}`,
},
{
` public class TestClass {
public static void getInquiryMarketingOwners(Set<Id> inqIds,/**first**/
Map<Id, Set<Id>> mapInqIdMarketingOwnerIds, /**second**/
Set<Id> inquiryMarketingOwnerIds /**third**/) {
}
}`,
`public class TestClass {
public static void getInquiryMarketingOwners(
Set<Id> inqIds,/**first**/
Map<Id, Set<Id>> mapInqIdMarketingOwnerIds, /**second**/
Set<Id> inquiryMarketingOwnerIds /**third**/
) {}
}`,
},
{
`public class TestClass {
public static void myTest(Set<Id> ids) { // end of line comment
List<Id> moreIds = new List<Id>();
}
}`,
`public class TestClass {
public static void myTest(Set<Id> ids) { // end of line comment
List<Id> moreIds = new List<Id>();
}
}`,
},
}
dmp := diffmatchpatch.New()
for i, tt := range tests {
Expand Down
28 changes: 18 additions & 10 deletions formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,35 +163,43 @@ func removeIndentationFromComment(comment string) string {
func removeExtraCommentIndentation(input string) string {
log.Trace(fmt.Sprintf("ADJUSTING : %q", input))
// Remove extra grammar-specific newlines added unaware of newline-preserving comments injected
newlinePrefixedMultilineComment := regexp.MustCompile("[\n ]*(\t*\uFFFA)")
newlinePrefixedMultilineComment := regexp.MustCompile("[\n ]*(\t*[\uFFFA\uFFF9])")
input = newlinePrefixedMultilineComment.ReplaceAllString(input, "$1")
log.Trace(fmt.Sprintf("ADJUSTED(1): %q", input))

indentedInlineComment := regexp.MustCompile("([^\n\uFFFB])\t+\uFFFA([^\n])")
input = indentedInlineComment.ReplaceAllString(input, "$1\uFFFA$2")
log.Trace(fmt.Sprintf("ADJUSTED(2): %q", input))

// Remove extra grammar-specific space added unaware of newline-preserving comments injected
spacePaddedMultilineComment := regexp.MustCompile(`(` + "\uFFFB\n*\t*" + `) +`)
input = spacePaddedMultilineComment.ReplaceAllString(input, "$1")
log.Trace(fmt.Sprintf("ADJUSTED(2): %q", input))
log.Trace(fmt.Sprintf("ADJUSTED(3): %q", input))

// Remove extra indent-injected newlines
indentInjectedNewlines := regexp.MustCompile("\uFFFB\n+")
input = indentInjectedNewlines.ReplaceAllString(input, "\uFFFB\n")
log.Trace(fmt.Sprintf("ADJUSTED(3): %q", input))
log.Trace(fmt.Sprintf("ADJUSTED(4): %q", input))

input = strings.ReplaceAll(input, "\n\uFFFB\n", "\n\uFFFB")
log.Trace(fmt.Sprintf("ADJUSTED(4): %q", input))
log.Trace(fmt.Sprintf("ADJUSTED(5): %q", input))

doubleCapturedNewlines := regexp.MustCompile("\n(\ufffb\t*\ufffa\n)")
doubleCapturedNewlines := regexp.MustCompile("\n(\uFFFB\t*\uFFFA\n)")
input = doubleCapturedNewlines.ReplaceAllString(input, "$1")
log.Trace(fmt.Sprintf("ADJUSTED(5): %q", input))
log.Trace(fmt.Sprintf("ADJUSTED(6): %q", input))

newlinePrefixedInlineComment := regexp.MustCompile("\n\t*\uFFF9\n")
input = newlinePrefixedInlineComment.ReplaceAllString(input, "\uFFF9\n")
log.Trace(fmt.Sprintf("ADJUSTED(6): %q", input))

tabPrefixedInlineComment := regexp.MustCompile(`([\w,]+)` + "\t+\uFFF9")
input = tabPrefixedInlineComment.ReplaceAllString(input, "$1 \uFFF9")
log.Trace(fmt.Sprintf("ADJUSTED(7): %q", input))

tabPrefixedInlineComment := regexp.MustCompile(`([\w,{]+)` + "\t+\uFFF9")
input = tabPrefixedInlineComment.ReplaceAllString(input, "$1\uFFF9")
log.Trace(fmt.Sprintf("ADJUSTED(8): %q", input))

extraSpace := regexp.MustCompile(" \uFFF9 ")
input = extraSpace.ReplaceAllString(input, "\uFFF9 ")
log.Trace(fmt.Sprintf("ADJUSTED(9): %q", input))

// Remove inline comment delimeters
inlineCommentPattern := regexp.MustCompile(`(?s)` + "\uFFF9" + `(.*?)` + "\uFFFB")
input = inlineCommentPattern.ReplaceAllString(input, "$1")
Expand Down
2 changes: 1 addition & 1 deletion formatter/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ func appendHiddenTokens(v *FormatVisitor, result interface{}, tokens []antlr.Tok
leading = strings.Repeat("\n", 2)
} else if countNewlines(leadingWhitespace) == 1 {
leading = "\n"
} else if len(leadingWhitespace) > 0 && position == PositionAfter {
} else if len(leadingWhitespace) > 0 {
leading = " "
}
// Strip leading whitespace so the comment can be indented to the right location
Expand Down

0 comments on commit c1c720e

Please sign in to comment.