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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
vendor
.vscode
golines
.idea

coverage.out
22 changes: 13 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ var (
"chain-split-dots",
"Split chained methods on the dots as opposed to the arguments").
Default("true").Bool()
compactFunctionArgs = kingpin.Flag(
"compact-function-args",
"Compact function arguments to single line in signatures and calls").Bool()
debug = kingpin.Flag(
"debug",
"Show debug output").Short('d').Default("false").Bool()
Expand Down Expand Up @@ -117,15 +120,16 @@ func main() {

func run() error {
config := ShortenerConfig{
MaxLen: *maxLen,
TabLen: *tabLen,
KeepAnnotations: *keepAnnotations,
ShortenComments: *shortenComments,
ReformatTags: *reformatTags,
IgnoreGenerated: *ignoreGenerated,
DotFile: *dotFile,
BaseFormatterCmd: *baseFormatterCmd,
ChainSplitDots: *chainSplitDots,
MaxLen: *maxLen,
TabLen: *tabLen,
KeepAnnotations: *keepAnnotations,
ShortenComments: *shortenComments,
ReformatTags: *reformatTags,
IgnoreGenerated: *ignoreGenerated,
DotFile: *dotFile,
BaseFormatterCmd: *baseFormatterCmd,
ChainSplitDots: *chainSplitDots,
CompactFunctionArgs: *compactFunctionArgs,
}
shortener := NewShortener(config)

Expand Down
73 changes: 53 additions & 20 deletions shortener.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ const maxRounds = 20

// ShortenerConfig stores the configuration options exposed by a Shortener instance.
type ShortenerConfig struct {
MaxLen int // Max target width for each line
TabLen int // Width of a tab character
KeepAnnotations bool // Whether to keep annotations in final result (for debugging only)
ShortenComments bool // Whether to shorten comments
ReformatTags bool // Whether to reformat struct tags in addition to shortening long lines
IgnoreGenerated bool // Whether to ignore generated files
DotFile string // Path to write dot-formatted output to (for debugging only)
ChainSplitDots bool // Whether to split chain methods by putting dots at ends of lines
MaxLen int // Max target width for each line
TabLen int // Width of a tab character
KeepAnnotations bool // Whether to keep annotations in final result (for debugging only)
ShortenComments bool // Whether to shorten comments
ReformatTags bool // Whether to reformat struct tags in addition to shortening long lines
IgnoreGenerated bool // Whether to ignore generated files
DotFile string // Path to write dot-formatted output to (for debugging only)
ChainSplitDots bool // Whether to split chain methods by putting dots at ends of lines
CompactFunctionArgs bool // Whether to compact function arguments to single line in signatures and calls

// Formatter that will be run before and after main shortening process. If empty,
// defaults to goimports (if found), otherwise gofmt.
Expand Down Expand Up @@ -401,14 +402,28 @@ func (s *Shortener) formatDecl(decl dst.Decl) {

// formatFieldList formats a field list in a function declaration.
func (s *Shortener) formatFieldList(fieldList *dst.FieldList) {
for f, field := range fieldList.List {
if f == 0 {
field.Decorations().Before = dst.NewLine
} else {
if s.config.CompactFunctionArgs {
for i, field := range fieldList.List {
field.Decorations().Before = dst.None
field.Decorations().After = dst.None

if i == 0 {
field.Decorations().Before = dst.NewLine
}
if i == len(fieldList.List)-1 {
field.Decorations().After = dst.NewLine
}
}
} else {
for f, field := range fieldList.List {
if f == 0 {
field.Decorations().Before = dst.NewLine
} else {
field.Decorations().Before = dst.None
}

field.Decorations().After = dst.NewLine
field.Decorations().After = dst.NewLine
}
}
}

Expand Down Expand Up @@ -514,17 +529,35 @@ func (s *Shortener) formatExpr(expr dst.Expr, force bool, isChain bool) {
} else {
shortenChildArgs := shouldShorten || HasAnnotationRecursive(e)

for a, arg := range e.Args {
if shortenChildArgs {
if a == 0 {
arg.Decorations().Before = dst.NewLine
} else {
if s.config.CompactFunctionArgs {
for i, arg := range e.Args {
if shortenChildArgs {
arg.Decorations().Before = dst.None
arg.Decorations().After = dst.None

if i == 0 {
arg.Decorations().Before = dst.NewLine
}
if i == len(e.Args)-1 {
arg.Decorations().After = dst.NewLine
}
}
s.formatExpr(arg, false, isChain)
}
} else {
for a, arg := range e.Args {
if shortenChildArgs {
if a == 0 {
arg.Decorations().Before = dst.NewLine
} else {
arg.Decorations().After = dst.None
}
arg.Decorations().After = dst.NewLine
}
arg.Decorations().After = dst.NewLine
s.formatExpr(arg, false, isChain)
}
s.formatExpr(arg, false, isChain)
}

s.formatExpr(e.Fun, shouldShorten, isChain)
}
case *dst.CompositeLit:
Expand Down