Skip to content
Permalink
Browse files

add spaces to comments less aggressively

If any of the lines in a comment group look like shouldn't be touched,
don't touch any of the group at all.

See the added test case and comments.

Fixes #43.
  • Loading branch information
mvdan committed Mar 20, 2020
1 parent 7590cc6 commit 4bdc928868e60b5ac1a986e3565f24c579868f80
Showing with 21 additions and 5 deletions.
  1. +13 −5 internal/gofumpt.go
  2. +8 −0 testdata/scripts/comment-spaced.txt
@@ -231,22 +231,30 @@ func (f *fumpter) applyPre(c *astutil.Cursor) {
node.Decls = newDecls

// Comments aren't nodes, so they're not walked by default.
groupLoop:
for _, group := range node.Comments {
for _, comment := range group.List {
body := strings.TrimPrefix(comment.Text, "//")
if body == comment.Text {
// /*-style comment
break
continue groupLoop
}
if rxCommentDirective.MatchString(body) {
// this comment is a directive
break
// this line is a directive
continue groupLoop
}
r, _ := utf8.DecodeRuneInString(body)
if unicode.IsLetter(r) || unicode.IsNumber(r) {
comment.Text = "// " + body
if !unicode.IsLetter(r) && !unicode.IsNumber(r) {
// this line could be code like "//{",
// or be already spaced.
continue groupLoop
}
}
// If none of the comment group's lines look like a
// directive or code, nor are spaced, add spaces.
for _, comment := range group.List {
comment.Text = "// " + strings.TrimPrefix(comment.Text, "//")
}
}

case *ast.DeclStmt:
@@ -37,6 +37,10 @@ type foo int
//comment group
//123 numbers too

//{
//this is probably code
//}

////////////
// ascii art
//----------
@@ -78,6 +82,10 @@ type foo int
// comment group
// 123 numbers too

//{
//this is probably code
//}

////////////
// ascii art
//----------

0 comments on commit 4bdc928

Please sign in to comment.
You can’t perform that action at this time.