Skip to content

Commit

Permalink
formatting for struct (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
incu6us authored May 4, 2021
1 parent ccaf4ae commit 88e115f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
38 changes: 23 additions & 15 deletions reviser/reviser.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,25 +122,33 @@ func formatDecls(f *ast.File, options Options) {
}

for _, decl := range f.Decls {
if dd, ok := decl.(*ast.FuncDecl); ok {
var formattedComments []*ast.Comment
if dd.Doc != nil {
formattedComments = make([]*ast.Comment, len(dd.Doc.List))
}
switch dd := decl.(type) {
case *ast.GenDecl:
dd.Doc = fixCommentGroup(dd.Doc)
case *ast.FuncDecl:
dd.Doc = fixCommentGroup(dd.Doc)
}
}
}

formattedDoc := &ast.CommentGroup{
List: formattedComments,
}
func fixCommentGroup(commentGroup *ast.CommentGroup) *ast.CommentGroup {
if commentGroup == nil {
formattedDoc := &ast.CommentGroup{
List: []*ast.Comment{},
}

if dd.Doc != nil {
for i, comment := range dd.Doc.List {
formattedDoc.List[i] = comment
}
}
return formattedDoc
}

dd.Doc = formattedDoc
}
formattedDoc := &ast.CommentGroup{
List: make([]*ast.Comment, len(commentGroup.List)),
}

for i, comment := range commentGroup.List {
formattedDoc.List[i] = comment
}

return formattedDoc
}

func groupImports(
Expand Down
14 changes: 14 additions & 0 deletions reviser/reviser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -954,12 +954,26 @@ func TestExecute_WithFormat(t *testing.T) {
projectName: "github.com/incu6us/goimports-reviser",
filePath: "./testdata/example.go",
fileContent: `package testdata
type SomeStruct struct{}
type SomeStruct1 struct{}
// SomeStruct2 comments
type SomeStruct2 struct{}
func (s *SomeStruct2) test() {}
func test(){}
func test1(){}
`,
},
want: `package testdata
type SomeStruct struct{}
type SomeStruct1 struct{}
// SomeStruct2 comments
type SomeStruct2 struct{}
func (s *SomeStruct2) test() {}
func test() {}
func test1() {}
Expand Down

0 comments on commit 88e115f

Please sign in to comment.