Skip to content
Open
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
15 changes: 15 additions & 0 deletions _fixtures/struct_interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package fixtures

type MyStruct struct {
i Int
LongInterface interface {
SuperLongFunctionName(anArgument string, anotherReallyLongArgument string, superDuperLongArgument string, definitelyTheLongestArgument string) error
}
}

type MyShortStruct struct {
s string
ShortInterface interface {
SuperShortFunctionName(shortArg string) error
}
}
20 changes: 20 additions & 0 deletions _fixtures/struct_interface__exp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package fixtures

type MyStruct struct {
i Int
LongInterface interface {
SuperLongFunctionName(
anArgument string,
anotherReallyLongArgument string,
superDuperLongArgument string,
definitelyTheLongestArgument string,
) error
}
}

type MyShortStruct struct {
s string
ShortInterface interface {
SuperShortFunctionName(shortArg string) error
}
}
24 changes: 24 additions & 0 deletions annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/dave/dst"
log "github.com/sirupsen/logrus"
)

const annotationPrefix = "// __golines:shorten:"
Expand Down Expand Up @@ -51,6 +52,12 @@ func HasAnnotationRecursive(node dst.Node) bool {
}

switch n := node.(type) {
case *dst.GenDecl:
for _, spec := range n.Specs {
if HasAnnotationRecursive(spec) {
return true
}
}
case *dst.FuncDecl:
if n.Type != nil && n.Type.Params != nil {
for _, item := range n.Type.Params.List {
Expand All @@ -59,8 +66,23 @@ func HasAnnotationRecursive(node dst.Node) bool {
}
}
}
case *dst.StructType:
return HasAnnotationRecursive(n.Fields)
case *dst.FuncType:
hasAny := n.Params != nil && HasAnnotationRecursive(n.Params)
hasAny = hasAny || (n.TypeParams != nil && HasAnnotationRecursive(n.TypeParams))
hasAny = hasAny || (n.Results != nil && HasAnnotationRecursive(n.Results))
return hasAny
case *dst.TypeSpec:
return HasAnnotationRecursive(n.Type)
case *dst.Field:
return HasTailAnnotation(n) || HasAnnotationRecursive(n.Type)
case *dst.FieldList:
for _, field := range n.List {
if HasAnnotationRecursive(field) {
return true
}
}
case *dst.SelectorExpr:
return HasAnnotation(n.Sel) || HasAnnotation(n.X)
case *dst.CallExpr:
Expand All @@ -79,6 +101,8 @@ func HasAnnotationRecursive(node dst.Node) bool {
return true
}
}
default:
log.Debugf("Couldn't analyze type for annotations: %+v", n)
}

return false
Expand Down
5 changes: 5 additions & 0 deletions shortener.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,11 @@ func (s *Shortener) formatExpr(expr dst.Expr, force bool, isChain bool) {
case *dst.SelectorExpr:
s.formatExpr(e.X, shouldShorten, isChain)
case *dst.StructType:
if HasAnnotationRecursive(e) && e.Fields != nil {
for _, field := range e.Fields.List {
s.formatExpr(field.Type, false, isChain)
}
}
if s.config.ReformatTags {
FormatStructTags(e.Fields)
}
Expand Down
2 changes: 2 additions & 0 deletions tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/dave/dst"
"github.com/fatih/structtag"
log "github.com/sirupsen/logrus"
)

var structTagRegexp = regexp.MustCompile("`([ ]*[a-zA-Z0-9_-]+:\".*\"[ ]*){2,}`")
Expand Down Expand Up @@ -213,5 +214,6 @@ func getWidth(node dst.Node) (int, error) {
return 1 + xWidth, nil
}

log.Debugf("Could not get width of node %+v", node)
return 0, fmt.Errorf("Could not get width of node %+v", node)
}