1
0
mirror of https://github.com/golang/go synced 2024-11-18 16:14:46 -07:00

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 <heschi@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Heschi Kreinick 2019-11-13 17:03:07 -05:00
parent 59b2cd78f8
commit 66af5afb16
2 changed files with 13 additions and 8 deletions

View File

@ -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 {

View File

@ -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)
}
}
}