Skip to content

Commit

Permalink
fix a case where we joined a comment with the previous line
Browse files Browse the repository at this point in the history
Fixes #256.
  • Loading branch information
mvdan committed Apr 8, 2023
1 parent 8b01b13 commit 05ac8b3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
12 changes: 9 additions & 3 deletions format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,13 +467,19 @@ func (f *fumpter) applyPre(c *astutil.Cursor) {
specEnd := node.Specs[0].End()

if len(f.commentsBetween(node.TokPos, specPos)) > 0 {
// If the single spec has any comment, it must
// go before the entire declaration now.
// If the single spec has a comment on the line above,
// the comment must go before the entire declaration now.
node.TokPos = specPos
} else {
f.removeLines(f.Line(node.TokPos), f.Line(specPos))
}
f.removeLines(f.Line(specEnd), f.Line(node.Rparen))
if len(f.commentsBetween(specEnd, node.Rparen)) > 0 {
// Leave one newline to not force a comment on the next line to
// become an inline comment.
f.removeLines(f.Line(specEnd)+1, f.Line(node.Rparen))
} else {
f.removeLines(f.Line(specEnd), f.Line(node.Rparen))
}

// Remove the parentheses. go/printer will automatically
// get rid of the newlines.
Expand Down
11 changes: 11 additions & 0 deletions testdata/script/decl-group-single.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ var (
"bar",
}
)

var (
foo = "foo"
// bar = "bar"
// baz = "baz"
)
-- f1.go.golden --
package p

Expand Down Expand Up @@ -73,6 +79,11 @@ var multiline = []string{
"foo",
"bar",
}

var foo = "foo"

// bar = "bar"
// baz = "baz"
-- f2.go --
package p

Expand Down

0 comments on commit 05ac8b3

Please sign in to comment.