@@ -14,7 +14,6 @@ import (
14
14
"time"
15
15
16
16
"golang.org/x/tools/go/ast/astutil"
17
- "golang.org/x/tools/internal/imports"
18
17
"golang.org/x/tools/internal/lsp/fuzzy"
19
18
"golang.org/x/tools/internal/lsp/protocol"
20
19
"golang.org/x/tools/internal/lsp/snippet"
@@ -221,6 +220,12 @@ type compLitInfo struct {
221
220
maybeInFieldName bool
222
221
}
223
222
223
+ type importInfo struct {
224
+ importPath string
225
+ name string
226
+ pkg Package
227
+ }
228
+
224
229
type methodSetKey struct {
225
230
typ types.Type
226
231
addressable bool
@@ -284,7 +289,7 @@ func (c *completer) getSurrounding() *Selection {
284
289
285
290
// found adds a candidate completion. We will also search through the object's
286
291
// members for more candidates.
287
- func (c * completer ) found (obj types.Object , score float64 , imp * imports. ImportInfo ) {
292
+ func (c * completer ) found (obj types.Object , score float64 , imp * importInfo ) {
288
293
if obj .Pkg () != nil && obj .Pkg () != c .pkg .GetTypes () && ! obj .Exported () {
289
294
// obj is not accessible because it lives in another package and is not
290
295
// exported. Don't treat it as a completion candidate.
@@ -370,7 +375,7 @@ type candidate struct {
370
375
371
376
// imp is the import that needs to be added to this package in order
372
377
// for this candidate to be valid. nil if no import needed.
373
- imp * imports. ImportInfo
378
+ imp * importInfo
374
379
}
375
380
376
381
// ErrIsDefinition is an error that informs the user they got no
@@ -591,28 +596,35 @@ func (c *completer) selector(sel *ast.SelectorExpr) error {
591
596
for _ , pkgExport := range pkgExports {
592
597
// If we've seen this import path, use the fully-typed version.
593
598
if knownPkg , ok := known [pkgExport .Fix .StmtInfo .ImportPath ]; ok {
594
- c .packageMembers (knownPkg .GetTypes (), & pkgExport .Fix .StmtInfo )
599
+ c .packageMembers (knownPkg .GetTypes (), & importInfo {
600
+ importPath : pkgExport .Fix .StmtInfo .ImportPath ,
601
+ name : pkgExport .Fix .StmtInfo .Name ,
602
+ pkg : knownPkg ,
603
+ })
595
604
continue
596
605
}
597
606
598
607
// Otherwise, continue with untyped proposals.
599
608
pkg := types .NewPackage (pkgExport .Fix .StmtInfo .ImportPath , pkgExport .Fix .IdentName )
600
609
for _ , export := range pkgExport .Exports {
601
- c .found (types .NewVar (0 , pkg , export , nil ), 0.07 , & pkgExport .Fix .StmtInfo )
610
+ c .found (types .NewVar (0 , pkg , export , nil ), 0.07 , & importInfo {
611
+ importPath : pkgExport .Fix .StmtInfo .ImportPath ,
612
+ name : pkgExport .Fix .StmtInfo .Name ,
613
+ })
602
614
}
603
615
}
604
616
}
605
617
return nil
606
618
}
607
619
608
- func (c * completer ) packageMembers (pkg * types.Package , imp * imports. ImportInfo ) {
620
+ func (c * completer ) packageMembers (pkg * types.Package , imp * importInfo ) {
609
621
scope := pkg .Scope ()
610
622
for _ , name := range scope .Names () {
611
623
c .found (scope .Lookup (name ), stdScore , imp )
612
624
}
613
625
}
614
626
615
- func (c * completer ) methodsAndFields (typ types.Type , addressable bool , imp * imports. ImportInfo ) error {
627
+ func (c * completer ) methodsAndFields (typ types.Type , addressable bool , imp * importInfo ) error {
616
628
mset := c .methodSetCache [methodSetKey {typ , addressable }]
617
629
if mset == nil {
618
630
if addressable && ! types .IsInterface (typ ) && ! isPointer (typ ) {
@@ -716,8 +728,9 @@ func (c *completer) lexical() error {
716
728
if _ , ok := seen [pkg .Name ()]; ! ok && pkg != c .pkg .GetTypes () && ! alreadyImports (c .file , pkg .Path ()) {
717
729
seen [pkg .Name ()] = struct {}{}
718
730
obj := types .NewPkgName (0 , nil , pkg .Name (), pkg )
719
- c .found (obj , stdScore , & imports.ImportInfo {
720
- ImportPath : pkg .Path (),
731
+ c .found (obj , stdScore , & importInfo {
732
+ importPath : pkg .Path (),
733
+ name : pkg .Name (),
721
734
})
722
735
}
723
736
}
@@ -739,7 +752,10 @@ func (c *completer) lexical() error {
739
752
// multiple packages of the same name as completion suggestions, since
740
753
// only one will be chosen.
741
754
obj := types .NewPkgName (0 , nil , pkg .IdentName , types .NewPackage (pkg .StmtInfo .ImportPath , pkg .IdentName ))
742
- c .found (obj , score , & pkg .StmtInfo )
755
+ c .found (obj , score , & importInfo {
756
+ importPath : pkg .StmtInfo .ImportPath ,
757
+ name : pkg .StmtInfo .Name ,
758
+ })
743
759
}
744
760
}
745
761
}
0 commit comments