Skip to content

Commit

Permalink
format: decl group allow first comment
Browse files Browse the repository at this point in the history
fixes mvdan#212
  • Loading branch information
Oiyoo committed Mar 30, 2022
1 parent f04017f commit 6e1ed83
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 8 deletions.
39 changes: 35 additions & 4 deletions format/format.go
Expand Up @@ -317,22 +317,33 @@ func (f *fumpter) applyPre(c *astutil.Cursor) {
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.
// 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) || isCommentGrpDirective(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
}
if f.Line(lastPos) < f.Line(cont.Pos())-1 {
if cont.Doc == nil {
break
}
for i, comment := range cont.Doc.List {
if f.Line(comment.Slash) != f.Line(lastPos)+1+i || isCommentDirective(comment) {
break contLoop
}
}
}

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 +1029,23 @@ func setPos(v reflect.Value, pos token.Pos) {
}
}
}

func isCommentGrpDirective(cg *ast.CommentGroup) bool {
if cg == nil {
return false
}
for _, comment := range cg.List {
if isCommentDirective(comment) {
return true
}
}
return false
}

func isCommentDirective(comment *ast.Comment) bool {
if comment == nil {
return false
}
body := strings.TrimPrefix(comment.Text, "//")
return rxCommentDirective.MatchString(body)
}
32 changes: 28 additions & 4 deletions testdata/scripts/decl-group-many.txt
Expand Up @@ -18,13 +18,23 @@ 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'
var v3 = 'd'
Expand All @@ -51,21 +61,35 @@ 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'
v3 = 'd'
)

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

0 comments on commit 6e1ed83

Please sign in to comment.