diff --git a/main.go b/main.go index 1e5fd6c..a65c714 100644 --- a/main.go +++ b/main.go @@ -72,6 +72,9 @@ var ( writeOutput = kingpin.Flag( "write-output", "Write output to source instead of stdout").Short('w').Default("false").Bool() + ignoreBeforeIndentChange = kingpin.Flag( + "ignore-indent-change", + "Ignore line length before indent change").Short('i').Default("false").Bool() // Args paths = kingpin.Arg( @@ -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, + IgnoreBeforeIndentChange: *ignoreBeforeIndentChange, } shortener := NewShortener(config) @@ -258,5 +262,4 @@ func handleOutput(path string, contents []byte, result []byte) error { fmt.Print(string(result)) return nil - } diff --git a/shortener.go b/shortener.go index b3a14e8..b49763e 100644 --- a/shortener.go +++ b/shortener.go @@ -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 + IgnoreBeforeIndentChange bool // Whether to ignore line length before indent change // Formatter that will be run before and after main shortening process. If empty, // defaults to goimports (if found), otherwise gofmt. @@ -381,7 +382,7 @@ func (s *Shortener) formatNode(node dst.Node) { func (s *Shortener) formatDecl(decl dst.Decl) { switch d := decl.(type) { case *dst.FuncDecl: - if HasAnnotationRecursive(decl) { + if !s.config.IgnoreBeforeIndentChange && HasAnnotationRecursive(decl) { if d.Type != nil && d.Type.Params != nil { s.formatFieldList(d.Type.Params) } @@ -433,7 +434,7 @@ func (s *Shortener) formatStmt(stmt dst.Stmt) { s.formatStmt(stmt) } case *dst.CaseClause: - if shouldShorten { + if shouldShorten && !s.config.IgnoreBeforeIndentChange { for _, arg := range st.List { arg.Decorations().After = dst.NewLine s.formatExpr(arg, false, false) @@ -458,7 +459,9 @@ func (s *Shortener) formatStmt(stmt dst.Stmt) { case *dst.GoStmt: s.formatExpr(st.Call, shouldShorten, false) case *dst.IfStmt: - s.formatExpr(st.Cond, shouldShorten, false) + if !s.config.IgnoreBeforeIndentChange { + s.formatExpr(st.Cond, shouldShorten, false) + } s.formatStmt(st.Body) case *dst.RangeStmt: s.formatStmt(st.Body) @@ -547,6 +550,9 @@ func (s *Shortener) formatExpr(expr dst.Expr, force bool, isChain bool) { s.formatFieldList(e.Params) } case *dst.InterfaceType: + if s.config.IgnoreBeforeIndentChange { + return + } for _, method := range e.Methods.List { if HasAnnotation(method) { s.formatExpr(method.Type, true, isChain)