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>
diff --git a/internal/imports/fix.go b/internal/imports/fix.go
index cdaa57b..f531024 100644
--- a/internal/imports/fix.go
+++ b/internal/imports/fix.go
@@ -302,7 +302,7 @@
 	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 @@
 	}
 
 	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 candidateImportName(pkg *pkg) string {
-	if importPathToAssumedName(pkg.importPathShort) != pkg.packageName {
+	if ImportPathToAssumedName(pkg.importPathShort) != pkg.packageName {
 		return pkg.packageName
 	}
 	return ""
@@ -884,7 +884,7 @@
 		if _, ok := names[path]; ok {
 			continue
 		}
-		names[path] = importPathToAssumedName(path)
+		names[path] = ImportPathToAssumedName(path)
 	}
 	return names, nil
 
@@ -1006,7 +1006,7 @@
 		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 @@
 // 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 88aeb73..b2c32b5 100644
--- a/internal/lsp/source/completion.go
+++ b/internal/lsp/source/completion.go
@@ -15,6 +15,7 @@
 	"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 @@
 				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)
 				}
 			}
 		}