From 29854455ffdf6ea839dc1ae73d968e8aaec261fe Mon Sep 17 00:00:00 2001 From: Sean Chittenden Date: Tue, 6 Aug 2024 13:54:37 -0700 Subject: [PATCH 1/3] import: add goimports LocalPrefix support --- main.go | 48 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/main.go b/main.go index ee59e40..be46b23 100644 --- a/main.go +++ b/main.go @@ -43,6 +43,7 @@ var ( groupImports = flag.Bool("groupimports", true, "group imports by type") printDiff = flag.Bool("diff", true, "print diffs") ignore = flag.String("ignore", "", "regex matching files to skip") + localPrefix = flag.String("local", "", "local imports path") srcDir = flag.String("srcdir", "", "resolve imports as if the source file is from the given directory (if a file is given, the parent directory is used)") ) @@ -161,6 +162,10 @@ func checkBuf(path string, src []byte) ([]byte, error) { FormatOnly: false, } + if localPrefix != nil && *localPrefix != "" { + imports.LocalPrefix = *localPrefix + } + pathForImports := path if *srcDir != "" { filename := filepath.Base(path) @@ -272,6 +277,7 @@ func checkBuf(path string, src []byte) ([]byte, error) { lastPos = typ.End() } } + output.Write(src[file.Offset(lastPos):]) return output.Bytes(), nil } @@ -287,23 +293,39 @@ func checkBuf(path string, src []byte) ([]byte, error) { // exception is made for cgo, whose "C" psuedo-imports are extracted into // separate import declarations. func remapImports(file *parser.File) map[*parser.ImportDecl][]render.ImportBlock { - var ( - stdlibImports []parser.ImportSpec - otherImports []parser.ImportSpec - ) - - for _, imp := range file.ImportSpecs() { - switch impPath := imp.Path(); { - case impPath == "C": - continue - case strings.Contains(impPath, "."): + imports := file.ImportSpecs() + stdlibImports := make([]parser.ImportSpec, 0, len(imports)) + otherImports := make([]parser.ImportSpec, 0, len(imports)) + localImports := make([]parser.ImportSpec, 0, len(imports)) + +NEXT_IMPORT: + for _, imp := range imports { + impPath := imp.Path() + if impPath == "C" { + continue NEXT_IMPORT + } + + if localPrefix != nil && *localPrefix != "" { + for _, lp := range strings.Split(*localPrefix, ",") { + if !strings.HasSuffix(lp, "/") { + lp += "/" + } + if strings.HasPrefix(impPath, lp) { + localImports = append(localImports, imp) + continue NEXT_IMPORT + } + } + } + + if strings.Contains(impPath, ".") { otherImports = append(otherImports, imp) - default: - stdlibImports = append(stdlibImports, imp) + continue NEXT_IMPORT } + + stdlibImports = append(stdlibImports, imp) } - mainBlock := render.ImportBlock{stdlibImports, otherImports} + mainBlock := render.ImportBlock{stdlibImports, otherImports, localImports} needMainBlock := mainBlock.Size() > 0 mapping := map[*parser.ImportDecl][]render.ImportBlock{} From 1c509862fbef2ea4657f0e1d46e3b4578ac5d1ae Mon Sep 17 00:00:00 2001 From: Sean Chittenden Date: Tue, 6 Aug 2024 14:00:21 -0700 Subject: [PATCH 2/3] imports: match goimports help string for the -local flag --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index be46b23..a739a59 100644 --- a/main.go +++ b/main.go @@ -43,7 +43,7 @@ var ( groupImports = flag.Bool("groupimports", true, "group imports by type") printDiff = flag.Bool("diff", true, "print diffs") ignore = flag.String("ignore", "", "regex matching files to skip") - localPrefix = flag.String("local", "", "local imports path") + localPrefix = flag.String("local", "", "put imports beginning with this string after 3rd-party packages; comma-separated list") srcDir = flag.String("srcdir", "", "resolve imports as if the source file is from the given directory (if a file is given, the parent directory is used)") ) From 7dd5fef4b69869b8b1feeb386205280f077b805b Mon Sep 17 00:00:00 2001 From: Sean Chittenden Date: Tue, 6 Aug 2024 15:36:08 -0700 Subject: [PATCH 3/3] imports: cache the split version of local prefixes --- main.go | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index a739a59..5c856e7 100644 --- a/main.go +++ b/main.go @@ -298,6 +298,18 @@ func remapImports(file *parser.File) map[*parser.ImportDecl][]render.ImportBlock otherImports := make([]parser.ImportSpec, 0, len(imports)) localImports := make([]parser.ImportSpec, 0, len(imports)) + localPrefixes := []string{} + if localPrefix != nil && *localPrefix != "" { + lps := strings.Split(*localPrefix, ",") + localPrefixes = make([]string, 0, len(lps)) + for _, lp := range lps { + if !strings.HasSuffix(lp, "/") { + lp += "/" + } + localPrefixes = append(localPrefixes, lp) + } + } + NEXT_IMPORT: for _, imp := range imports { impPath := imp.Path() @@ -305,15 +317,10 @@ NEXT_IMPORT: continue NEXT_IMPORT } - if localPrefix != nil && *localPrefix != "" { - for _, lp := range strings.Split(*localPrefix, ",") { - if !strings.HasSuffix(lp, "/") { - lp += "/" - } - if strings.HasPrefix(impPath, lp) { - localImports = append(localImports, imp) - continue NEXT_IMPORT - } + for _, lp := range localPrefixes { + if strings.HasPrefix(impPath, lp) { + localImports = append(localImports, imp) + continue NEXT_IMPORT } }