Skip to content

Commit 05ac8b3

Browse files
committed
fix a case where we joined a comment with the previous line
Fixes #256.
1 parent 8b01b13 commit 05ac8b3

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

format/format.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,13 +467,19 @@ func (f *fumpter) applyPre(c *astutil.Cursor) {
467467
specEnd := node.Specs[0].End()
468468

469469
if len(f.commentsBetween(node.TokPos, specPos)) > 0 {
470-
// If the single spec has any comment, it must
471-
// go before the entire declaration now.
470+
// If the single spec has a comment on the line above,
471+
// the comment must go before the entire declaration now.
472472
node.TokPos = specPos
473473
} else {
474474
f.removeLines(f.Line(node.TokPos), f.Line(specPos))
475475
}
476-
f.removeLines(f.Line(specEnd), f.Line(node.Rparen))
476+
if len(f.commentsBetween(specEnd, node.Rparen)) > 0 {
477+
// Leave one newline to not force a comment on the next line to
478+
// become an inline comment.
479+
f.removeLines(f.Line(specEnd)+1, f.Line(node.Rparen))
480+
} else {
481+
f.removeLines(f.Line(specEnd), f.Line(node.Rparen))
482+
}
477483

478484
// Remove the parentheses. go/printer will automatically
479485
// get rid of the newlines.

testdata/script/decl-group-single.txtar

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ var (
4141
"bar",
4242
}
4343
)
44+
45+
var (
46+
foo = "foo"
47+
// bar = "bar"
48+
// baz = "baz"
49+
)
4450
-- f1.go.golden --
4551
package p
4652

@@ -73,6 +79,11 @@ var multiline = []string{
7379
"foo",
7480
"bar",
7581
}
82+
83+
var foo = "foo"
84+
85+
// bar = "bar"
86+
// baz = "baz"
7687
-- f2.go --
7788
package p
7889

0 commit comments

Comments
 (0)