Skip to content

Commit

Permalink
add goimports fuzzy matching
Browse files Browse the repository at this point in the history
goimports supports fuzzy matching(use '_'). e.g. : ok._   or  grpc._
  • Loading branch information
lujinda committed Mar 20, 2018
1 parent 2226533 commit 41faba6
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions imports/fix.go
Expand Up @@ -901,7 +901,7 @@ func findImportGoPath(pkgName string, symbols map[string]bool, filename string)
// If it doesn't have the right
// symbols, send nil to mean no match.
for symbol := range symbols {
if !exports[symbol] {
if (symbol != "_") && (!exports[symbol]) {
pkg = nil
break
}
Expand Down Expand Up @@ -1057,10 +1057,26 @@ func (fn visitFn) Visit(node ast.Node) ast.Visitor {
return fn(node)
}

// findImportStdlibByFuzzy fuzzy matching pkg path
func findImportStdlibByFuzzy(shortPkg string) (importPath string, ok bool) {
pkgPrefix := fmt.Sprintf("%s.", shortPkg)
for key, value := range stdlib {
if strings.HasPrefix(key, pkgPrefix) {
return value, true
}
}
return "", false
}

func findImportStdlib(shortPkg string, symbols map[string]bool) (importPath string, ok bool) {
var path string
for symbol := range symbols {
key := shortPkg + "." + symbol
path := stdlib[key]
if symbol == "_" {
path, _ = findImportStdlibByFuzzy(shortPkg)

} else {
path = stdlib[key]
if path == "" {
if key == "rand.Read" {
continue
Expand Down

0 comments on commit 41faba6

Please sign in to comment.