Skip to content

Commit 3af9f3e

Browse files
heschistamblerre
authored andcommitted
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 <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> (cherry picked from commit 689d0f0) Reviewed-on: https://go-review.googlesource.com/c/tools/+/205660 Reviewed-by: Heschi Kreinick <[email protected]>
1 parent 181573a commit 3af9f3e

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

internal/imports/fix.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,13 @@ func getCandidatePkgs(pkgName, filename string, env *ProcessEnv) ([]*pkg, error)
643643
return result, nil
644644
}
645645

646+
func candidateImportName(pkg *pkg) string {
647+
if importPathToAssumedName(pkg.importPathShort) != pkg.packageName {
648+
return pkg.packageName
649+
}
650+
return ""
651+
}
652+
646653
// getAllCandidates gets all of the candidates to be imported, regardless of if they are needed.
647654
func getAllCandidates(filename string, env *ProcessEnv) ([]ImportFix, error) {
648655
pkgs, err := getCandidatePkgs("", filename, env)
@@ -654,6 +661,7 @@ func getAllCandidates(filename string, env *ProcessEnv) ([]ImportFix, error) {
654661
result = append(result, ImportFix{
655662
StmtInfo: ImportInfo{
656663
ImportPath: pkg.importPathShort,
664+
Name: candidateImportName(pkg),
657665
},
658666
IdentName: pkg.packageName,
659667
FixType: AddImport,
@@ -679,6 +687,7 @@ func getPackageExports(completePackage, filename string, env *ProcessEnv) ([]Pac
679687
fix := &ImportFix{
680688
StmtInfo: ImportInfo{
681689
ImportPath: pkg.importPathShort,
690+
Name: candidateImportName(pkg),
682691
},
683692
IdentName: pkg.packageName,
684693
FixType: AddImport,

0 commit comments

Comments
 (0)