1
0
mirror of https://github.com/golang/go synced 2024-11-18 17:54:57 -07:00

internal/imports: set import names on completion candidates

I forgot to set .Name on completion candidates. That meant that if the
package name didn't match the import path, you'd first get an import
without a name, then it would be added when you organized imports.

Change-Id: Ic374de872324effa6bc04c1440c659d7a182d17f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/205503
Run-TryBot: Heschi Kreinick <heschi@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Heschi Kreinick 2019-11-05 17:53:32 -05:00
parent ea203083f5
commit 689d0f08e6

View File

@ -643,6 +643,13 @@ func getCandidatePkgs(pkgName, filename string, env *ProcessEnv) ([]*pkg, error)
return result, nil
}
func candidateImportName(pkg *pkg) string {
if importPathToAssumedName(pkg.importPathShort) != pkg.packageName {
return pkg.packageName
}
return ""
}
// getAllCandidates gets all of the candidates to be imported, regardless of if they are needed.
func getAllCandidates(filename string, env *ProcessEnv) ([]ImportFix, error) {
pkgs, err := getCandidatePkgs("", filename, env)
@ -654,6 +661,7 @@ func getAllCandidates(filename string, env *ProcessEnv) ([]ImportFix, error) {
result = append(result, ImportFix{
StmtInfo: ImportInfo{
ImportPath: pkg.importPathShort,
Name: candidateImportName(pkg),
},
IdentName: pkg.packageName,
FixType: AddImport,
@ -679,6 +687,7 @@ func getPackageExports(completePackage, filename string, env *ProcessEnv) ([]Pac
fix := &ImportFix{
StmtInfo: ImportInfo{
ImportPath: pkg.importPathShort,
Name: candidateImportName(pkg),
},
IdentName: pkg.packageName,
FixType: AddImport,