Skip to content

Commit 51b71fc

Browse files
committed
format: decl group allow first comment
fixes #212
1 parent 6b144e8 commit 51b71fc

File tree

2 files changed

+64
-9
lines changed

2 files changed

+64
-9
lines changed

format/format.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,23 +316,39 @@ func (f *fumpter) applyPre(c *astutil.Cursor) {
316316
switch node := c.Node().(type) {
317317
case *ast.File:
318318
// Join contiguous lone var/const/import lines.
319-
// Abort if there are empty lines or comments in between,
320-
// including a leading comment, which could be a directive.
319+
// Abort if there are empty lines in between,
320+
// including a leading comment if it's a directive.
321321
newDecls := make([]ast.Decl, 0, len(node.Decls))
322322
for i := 0; i < len(node.Decls); {
323323
newDecls = append(newDecls, node.Decls[i])
324324
start, ok := node.Decls[i].(*ast.GenDecl)
325-
if !ok || isCgoImport(start) || start.Doc != nil {
325+
if !ok || isCgoImport(start) || containsAnyDirective(start.Doc) {
326326
i++
327327
continue
328328
}
329329
lastPos := start.Pos()
330+
contLoop:
330331
for i++; i < len(node.Decls); {
331332
cont, ok := node.Decls[i].(*ast.GenDecl)
332-
if !ok || cont.Tok != start.Tok || cont.Lparen != token.NoPos ||
333-
f.Line(lastPos) < f.Line(cont.Pos())-1 || isCgoImport(cont) {
333+
if !ok || cont.Tok != start.Tok || cont.Lparen != token.NoPos || isCgoImport(cont) {
334334
break
335335
}
336+
// Are there things between these two declarations? e.g. empty lines, comments, directives
337+
// If so, break the chain on empty lines and directives, continue below for comments.
338+
if f.Line(lastPos) < f.Line(cont.Pos())-1 {
339+
// break on empty line
340+
if cont.Doc == nil {
341+
break
342+
}
343+
// break on directive
344+
for i, comment := range cont.Doc.List {
345+
if f.Line(comment.Slash) != f.Line(lastPos)+1+i || rxCommentDirective.MatchString(strings.TrimPrefix(comment.Text, "//")) {
346+
break contLoop
347+
}
348+
}
349+
// continue below for comments
350+
}
351+
336352
start.Specs = append(start.Specs, cont.Specs...)
337353
if c := f.inlineComment(cont.End()); c != nil {
338354
// don't move an inline comment outside
@@ -1018,3 +1034,16 @@ func setPos(v reflect.Value, pos token.Pos) {
10181034
}
10191035
}
10201036
}
1037+
1038+
func containsAnyDirective(group *ast.CommentGroup) bool {
1039+
if group == nil {
1040+
return false
1041+
}
1042+
for _, comment := range group.List {
1043+
body := strings.TrimPrefix(comment.Text, "//")
1044+
if rxCommentDirective.MatchString(body) {
1045+
return true
1046+
}
1047+
}
1048+
return false
1049+
}

testdata/scripts/decl-group-many.txt

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,26 @@ const four = 'r'
1818
var not = 'a'
1919

2020
var v1 = 's'
21-
// comment, e.g. directive
21+
//go:embed hello.txt
22+
var v2 = 'd'
23+
24+
var v1 = 's'
25+
// comment line 1
26+
// comment line 2
2227
var v2 = 'd'
2328

2429
var v1 = "mixed"
2530
const c1 = "mixed"
2631

27-
// comment, e.g. directive
32+
//go:embed hello.txt
33+
var v1 = 's'
34+
var v2 = 'd'
35+
var v3 = 'd'
36+
37+
// comment
2838
var v1 = 's'
2939
var v2 = 'd'
40+
/* comment */
3041
var v3 = 'd'
3142

3243
const inline1 = "s1" // c1
@@ -51,21 +62,36 @@ var not = 'a'
5162

5263
var v1 = 's'
5364

54-
// comment, e.g. directive
65+
//go:embed hello.txt
5566
var v2 = 'd'
5667

68+
var (
69+
v1 = 's'
70+
// comment line 1
71+
// comment line 2
72+
v2 = 'd'
73+
)
74+
5775
var v1 = "mixed"
5876

5977
const c1 = "mixed"
6078

61-
// comment, e.g. directive
79+
//go:embed hello.txt
6280
var v1 = 's'
6381

6482
var (
6583
v2 = 'd'
6684
v3 = 'd'
6785
)
6886

87+
// comment
88+
var (
89+
v1 = 's'
90+
v2 = 'd'
91+
/* comment */
92+
v3 = 'd'
93+
)
94+
6995
const (
7096
inline1 = "s1" // c1
7197
inline2 = "s2" // c2

0 commit comments

Comments
 (0)