Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format: decl group allow first comment #214

Merged
merged 1 commit into from
Apr 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
mvdan marked this conversation as resolved.
Show resolved Hide resolved
// 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