Skip to content

Commit

Permalink
format: decl group allow first comment
Browse files Browse the repository at this point in the history
fixes #212
  • Loading branch information
Oiyoo committed Apr 3, 2022
1 parent 6b144e8 commit 51b71fc
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 9 deletions.
39 changes: 34 additions & 5 deletions format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,23 +316,39 @@ func (f *fumpter) applyPre(c *astutil.Cursor) {
switch node := c.Node().(type) {
case *ast.File:
// Join contiguous lone var/const/import lines.
// Abort if there are empty lines or comments in between,
// including a leading comment, which could be a directive.
// Abort if there are empty lines in between,
// including a leading comment if it's a directive.
newDecls := make([]ast.Decl, 0, len(node.Decls))
for i := 0; i < len(node.Decls); {
newDecls = append(newDecls, node.Decls[i])
start, ok := node.Decls[i].(*ast.GenDecl)
if !ok || isCgoImport(start) || start.Doc != nil {
if !ok || isCgoImport(start) || containsAnyDirective(start.Doc) {
i++
continue
}
lastPos := start.Pos()
contLoop:
for i++; i < len(node.Decls); {
cont, ok := node.Decls[i].(*ast.GenDecl)
if !ok || cont.Tok != start.Tok || cont.Lparen != token.NoPos ||
f.Line(lastPos) < f.Line(cont.Pos())-1 || isCgoImport(cont) {
if !ok || cont.Tok != start.Tok || cont.Lparen != token.NoPos || isCgoImport(cont) {
break
}
// Are there things between these two declarations? e.g. empty lines, comments, directives
// If so, break the chain on empty lines and directives, continue below for comments.
if f.Line(lastPos) < f.Line(cont.Pos())-1 {
// break on empty line
if cont.Doc == nil {
break
}
// break on directive
for i, comment := range cont.Doc.List {
if f.Line(comment.Slash) != f.Line(lastPos)+1+i || rxCommentDirective.MatchString(strings.TrimPrefix(comment.Text, "//")) {
break contLoop
}
}
// continue below for comments
}

start.Specs = append(start.Specs, cont.Specs...)
if c := f.inlineComment(cont.End()); c != nil {
// don't move an inline comment outside
Expand Down Expand Up @@ -1018,3 +1034,16 @@ func setPos(v reflect.Value, pos token.Pos) {
}
}
}

func containsAnyDirective(group *ast.CommentGroup) bool {
if group == nil {
return false
}
for _, comment := range group.List {
body := strings.TrimPrefix(comment.Text, "//")
if rxCommentDirective.MatchString(body) {
return true
}
}
return false
}
34 changes: 30 additions & 4 deletions testdata/scripts/decl-group-many.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,26 @@ const four = 'r'
var not = 'a'

var v1 = 's'
// comment, e.g. directive
//go:embed hello.txt
var v2 = 'd'

var v1 = 's'
// comment line 1
// comment line 2
var v2 = 'd'

var v1 = "mixed"
const c1 = "mixed"

// comment, e.g. directive
//go:embed hello.txt
var v1 = 's'
var v2 = 'd'
var v3 = 'd'

// comment
var v1 = 's'
var v2 = 'd'
/* comment */
var v3 = 'd'

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

var v1 = 's'

// comment, e.g. directive
//go:embed hello.txt
var v2 = 'd'

var (
v1 = 's'
// comment line 1
// comment line 2
v2 = 'd'
)

var v1 = "mixed"

const c1 = "mixed"

// comment, e.g. directive
//go:embed hello.txt
var v1 = 's'

var (
v2 = 'd'
v3 = 'd'
)

// comment
var (
v1 = 's'
v2 = 'd'
/* comment */
v3 = 'd'
)

const (
inline1 = "s1" // c1
inline2 = "s2" // c2
Expand Down

0 comments on commit 51b71fc

Please sign in to comment.