@@ -862,7 +862,7 @@ func (c *completer) unimportedMembers(ctx context.Context, id *ast.Ident) error
862
862
if imports .ImportPathToAssumedName (path ) != pkg .GetTypes ().Name () {
863
863
imp .name = pkg .GetTypes ().Name ()
864
864
}
865
- c .packageMembers (ctx , pkg .GetTypes (), stdScore + .1 * float64 (relevance ), imp )
865
+ c .packageMembers (ctx , pkg .GetTypes (), unimportedScore (relevance ), imp )
866
866
if len (c .items ) >= unimportedMemberTarget {
867
867
return nil
868
868
}
@@ -882,7 +882,7 @@ func (c *completer) unimportedMembers(ctx context.Context, id *ast.Ident) error
882
882
// Continue with untyped proposals.
883
883
pkg := types .NewPackage (pkgExport .Fix .StmtInfo .ImportPath , pkgExport .Fix .IdentName )
884
884
for _ , export := range pkgExport .Exports {
885
- score := stdScore + 0.1 * float64 (pkgExport .Fix .Relevance )
885
+ score := unimportedScore (pkgExport .Fix .Relevance )
886
886
c .found (ctx , candidate {
887
887
obj : types .NewVar (0 , pkg , export , nil ),
888
888
score : score ,
@@ -901,6 +901,12 @@ func (c *completer) unimportedMembers(ctx context.Context, id *ast.Ident) error
901
901
})
902
902
}
903
903
904
+ // unimportedScore returns a score for an unimported package that is generally
905
+ // lower than other candidates.
906
+ func unimportedScore (relevance int ) float64 {
907
+ return (stdScore + .1 * float64 (relevance )) / 2
908
+ }
909
+
904
910
func (c * completer ) packageMembers (ctx context.Context , pkg * types.Package , score float64 , imp * importInfo ) {
905
911
scope := pkg .Scope ()
906
912
for _ , name := range scope .Names () {
@@ -1107,15 +1113,15 @@ func (c *completer) unimportedPackages(ctx context.Context, seen map[string]stru
1107
1113
if c .surrounding != nil {
1108
1114
prefix = c .surrounding .Prefix ()
1109
1115
}
1110
- initialItemCount := len ( c . items )
1116
+ count := 0
1111
1117
1112
1118
known , err := c .snapshot .CachedImportPaths (ctx )
1113
1119
if err != nil {
1114
1120
return err
1115
1121
}
1116
1122
var paths []string
1117
- for path := range known {
1118
- if ! strings .HasPrefix (path , prefix ) {
1123
+ for path , pkg := range known {
1124
+ if ! strings .HasPrefix (pkg . GetTypes (). Name () , prefix ) {
1119
1125
continue
1120
1126
}
1121
1127
paths = append (paths , path )
@@ -1131,22 +1137,25 @@ func (c *completer) unimportedPackages(ctx context.Context, seen map[string]stru
1131
1137
1132
1138
for path , relevance := range relevances {
1133
1139
pkg := known [path ]
1140
+ if _ , ok := seen [pkg .GetTypes ().Name ()]; ok {
1141
+ continue
1142
+ }
1134
1143
imp := & importInfo {
1135
1144
importPath : path ,
1136
1145
pkg : pkg ,
1137
1146
}
1138
1147
if imports .ImportPathToAssumedName (path ) != pkg .GetTypes ().Name () {
1139
1148
imp .name = pkg .GetTypes ().Name ()
1140
1149
}
1141
- score := 0.01 * float64 (relevance )
1150
+ if count >= maxUnimportedPackageNames {
1151
+ return nil
1152
+ }
1142
1153
c .found (ctx , candidate {
1143
1154
obj : types .NewPkgName (0 , nil , pkg .GetTypes ().Name (), pkg .GetTypes ()),
1144
- score : score ,
1155
+ score : unimportedScore ( relevance ) ,
1145
1156
imp : imp ,
1146
1157
})
1147
- if len (c .items )- initialItemCount >= maxUnimportedPackageNames {
1148
- return nil
1149
- }
1158
+ count ++
1150
1159
}
1151
1160
1152
1161
ctx , cancel := context .WithCancel (ctx )
@@ -1159,26 +1168,28 @@ func (c *completer) unimportedPackages(ctx context.Context, seen map[string]stru
1159
1168
if _ , ok := seen [pkg .IdentName ]; ok {
1160
1169
return
1161
1170
}
1171
+ if _ , ok := relevances [pkg .StmtInfo .ImportPath ]; ok {
1172
+ return
1173
+ }
1162
1174
1163
- if len ( c . items ) - initialItemCount >= maxUnimportedPackageNames {
1175
+ if count >= maxUnimportedPackageNames {
1164
1176
cancel ()
1165
1177
return
1166
1178
}
1167
- // Rank unimported packages significantly lower than other results.
1168
- score := 0.01 * float64 (pkg .Relevance )
1169
1179
1170
1180
// Do not add the unimported packages to seen, since we can have
1171
1181
// multiple packages of the same name as completion suggestions, since
1172
1182
// only one will be chosen.
1173
1183
obj := types .NewPkgName (0 , nil , pkg .IdentName , types .NewPackage (pkg .StmtInfo .ImportPath , pkg .IdentName ))
1174
1184
c .found (ctx , candidate {
1175
1185
obj : obj ,
1176
- score : score ,
1186
+ score : unimportedScore ( pkg . Relevance ) ,
1177
1187
imp : & importInfo {
1178
1188
importPath : pkg .StmtInfo .ImportPath ,
1179
1189
name : pkg .StmtInfo .Name ,
1180
1190
},
1181
1191
})
1192
+ count ++
1182
1193
}
1183
1194
return c .snapshot .View ().RunProcessEnvFunc (ctx , func (opts * imports.Options ) error {
1184
1195
return imports .GetAllCandidates (ctx , add , prefix , c .filename , c .pkg .GetTypes ().Name (), opts )
0 commit comments