Skip to content

feat: Add option to ignore lines before indentation change #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
23 changes: 13 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
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,
IgnoreBeforeIndentChange: *ignoreBeforeIndentChange,
}
shortener := NewShortener(config)

Expand Down Expand Up @@ -258,5 +262,4 @@ func handleOutput(path string, contents []byte, result []byte) error {

fmt.Print(string(result))
return nil

}
28 changes: 17 additions & 11 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
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.
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down