From 66af5afb1625d129f8bfad6f7d860d7750410e9d Mon Sep 17 00:00:00 2001 From: Heschi Kreinick Date: Wed, 13 Nov 2019 17:03:07 -0500 Subject: [PATCH] internal/lsp/source: give more imports names Expose ImportPathToAssumedName (internally) and use it in an LSP completion case that doesn't go through the usual imports code. Fixes golang/go#35401. Change-Id: If87912072e11e22c542f7474841e53467a33ef2b Reviewed-on: https://go-review.googlesource.com/c/tools/+/206890 Run-TryBot: Heschi Kreinick Reviewed-by: Rebecca Stambler --- internal/imports/fix.go | 12 ++++++------ internal/lsp/source/completion.go | 9 +++++++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/internal/imports/fix.go b/internal/imports/fix.go index cdaa57b9bd..f531024da9 100644 --- a/internal/imports/fix.go +++ b/internal/imports/fix.go @@ -302,7 +302,7 @@ func (p *pass) importIdentifier(imp *ImportInfo) string { if known != nil && known.name != "" { return known.name } - return importPathToAssumedName(imp.ImportPath) + return ImportPathToAssumedName(imp.ImportPath) } // load reads in everything necessary to run a pass, and reports whether the @@ -435,7 +435,7 @@ func (p *pass) importSpecName(imp *ImportInfo) string { } ident := p.importIdentifier(imp) - if ident == importPathToAssumedName(imp.ImportPath) { + if ident == ImportPathToAssumedName(imp.ImportPath) { return "" // ident not needed since the assumed and real names are the same. } return ident @@ -644,7 +644,7 @@ func getCandidatePkgs(pkgName, filename string, env *ProcessEnv) ([]*pkg, error) } func candidateImportName(pkg *pkg) string { - if importPathToAssumedName(pkg.importPathShort) != pkg.packageName { + if ImportPathToAssumedName(pkg.importPathShort) != pkg.packageName { return pkg.packageName } return "" @@ -884,7 +884,7 @@ func (r *goPackagesResolver) loadPackageNames(importPaths []string, srcDir strin if _, ok := names[path]; ok { continue } - names[path] = importPathToAssumedName(path) + names[path] = ImportPathToAssumedName(path) } return names, nil @@ -1006,7 +1006,7 @@ func notIdentifier(ch rune) bool { ch >= utf8.RuneSelf && (unicode.IsLetter(ch) || unicode.IsDigit(ch))) } -// importPathToAssumedName returns the assumed package name of an import path. +// ImportPathToAssumedName returns the assumed package name of an import path. // It does this using only string parsing of the import path. // It picks the last element of the path that does not look like a major // version, and then picks the valid identifier off the start of that element. @@ -1014,7 +1014,7 @@ func notIdentifier(ch rune) bool { // clarity. // This function could be moved to a standard package and exported if we want // for use in other tools. -func importPathToAssumedName(importPath string) string { +func ImportPathToAssumedName(importPath string) string { base := path.Base(importPath) if strings.HasPrefix(base, "v") { if _, err := strconv.Atoi(base[1:]); err == nil { diff --git a/internal/lsp/source/completion.go b/internal/lsp/source/completion.go index 88aeb73640..b2c32b52cd 100644 --- a/internal/lsp/source/completion.go +++ b/internal/lsp/source/completion.go @@ -15,6 +15,7 @@ import ( "time" "golang.org/x/tools/go/ast/astutil" + "golang.org/x/tools/internal/imports" "golang.org/x/tools/internal/lsp/fuzzy" "golang.org/x/tools/internal/lsp/protocol" "golang.org/x/tools/internal/lsp/snippet" @@ -715,9 +716,13 @@ func (c *completer) lexical() error { if _, ok := seen[pkg.Name()]; !ok && pkg != c.pkg.GetTypes() && !alreadyImports(c.file, pkg.Path()) { seen[pkg.Name()] = struct{}{} obj := types.NewPkgName(0, nil, pkg.Name(), pkg) - c.found(obj, stdScore, &importInfo{ + imp := &importInfo{ importPath: pkg.Path(), - }) + } + if imports.ImportPathToAssumedName(pkg.Path()) != pkg.Name() { + imp.name = pkg.Name() + } + c.found(obj, stdScore, imp) } } }