@@ -179,6 +179,11 @@ type completer struct {
179
179
// items is the list of completion items returned.
180
180
items []CompletionItem
181
181
182
+ // completionCallbacks is a list of callbacks to collect completions that
183
+ // require expensive operations. This includes operations where we search
184
+ // through the entire module cache.
185
+ completionCallbacks []func (opts * imports.Options ) error
186
+
182
187
// surrounding describes the identifier surrounding the position.
183
188
surrounding * Selection
184
189
@@ -516,6 +521,17 @@ func Completion(ctx context.Context, snapshot source.Snapshot, fh source.FileHan
516
521
517
522
// Deep search collected candidates and their members for more candidates.
518
523
c .deepSearch (ctx )
524
+ c .deepState .searchQueue = nil
525
+
526
+ for _ , callback := range c .completionCallbacks {
527
+ if err := c .snapshot .View ().RunProcessEnvFunc (ctx , callback ); err != nil {
528
+ return nil , nil , err
529
+ }
530
+ }
531
+
532
+ // Search candidates populated by expensive operations like
533
+ // unimportedMembers etc. for more completion items.
534
+ c .deepSearch (ctx )
519
535
520
536
// Statement candidates offer an entire statement in certain contexts, as
521
537
// opposed to a single object. Add statement candidates last because they
@@ -799,9 +815,10 @@ func (c *completer) populateImportCompletions(ctx context.Context, searchImport
799
815
}
800
816
}
801
817
802
- return c . snapshot . View (). RunProcessEnvFunc ( ctx , func (opts * imports.Options ) error {
818
+ c . completionCallbacks = append ( c . completionCallbacks , func (opts * imports.Options ) error {
803
819
return imports .GetImportPaths (ctx , searchImports , prefix , c .filename , c .pkg .GetTypes ().Name (), opts .Env )
804
820
})
821
+ return nil
805
822
}
806
823
807
824
// populateCommentCompletions yields completions for comments preceding or in declarations.
@@ -1126,7 +1143,6 @@ func (c *completer) unimportedMembers(ctx context.Context, id *ast.Ident) error
1126
1143
}
1127
1144
1128
1145
ctx , cancel := context .WithCancel (ctx )
1129
- defer cancel ()
1130
1146
1131
1147
var mu sync.Mutex
1132
1148
add := func (pkgExport imports.PackageExport ) {
@@ -1153,9 +1169,12 @@ func (c *completer) unimportedMembers(ctx context.Context, id *ast.Ident) error
1153
1169
cancel ()
1154
1170
}
1155
1171
}
1156
- return c .snapshot .View ().RunProcessEnvFunc (ctx , func (opts * imports.Options ) error {
1172
+
1173
+ c .completionCallbacks = append (c .completionCallbacks , func (opts * imports.Options ) error {
1174
+ defer cancel ()
1157
1175
return imports .GetPackageExports (ctx , add , id .Name , c .filename , c .pkg .GetTypes ().Name (), opts .Env )
1158
1176
})
1177
+ return nil
1159
1178
}
1160
1179
1161
1180
// unimportedScore returns a score for an unimported package that is generally
@@ -1422,7 +1441,6 @@ func (c *completer) unimportedPackages(ctx context.Context, seen map[string]stru
1422
1441
}
1423
1442
1424
1443
ctx , cancel := context .WithCancel (ctx )
1425
- defer cancel ()
1426
1444
1427
1445
var mu sync.Mutex
1428
1446
add := func (pkg imports.ImportFix ) {
@@ -1454,9 +1472,11 @@ func (c *completer) unimportedPackages(ctx context.Context, seen map[string]stru
1454
1472
})
1455
1473
count ++
1456
1474
}
1457
- return c .snapshot .View ().RunProcessEnvFunc (ctx , func (opts * imports.Options ) error {
1475
+ c .completionCallbacks = append (c .completionCallbacks , func (opts * imports.Options ) error {
1476
+ defer cancel ()
1458
1477
return imports .GetAllCandidates (ctx , add , prefix , c .filename , c .pkg .GetTypes ().Name (), opts .Env )
1459
1478
})
1479
+ return nil
1460
1480
}
1461
1481
1462
1482
// alreadyImports reports whether f has an import with the specified path.
0 commit comments