@@ -16,7 +16,10 @@ import 'package:analysis_server/src/provisional/completion/completion_core.dart'
16
16
import 'package:analysis_server/src/services/completion/completion_core.dart' ;
17
17
import 'package:analysis_server/src/services/completion/completion_performance.dart' ;
18
18
import 'package:analysis_server/src/services/completion/dart/completion_manager.dart' ;
19
+ import 'package:analysis_server/src/services/completion/filtering/fuzzy_matcher.dart' ;
19
20
import 'package:analyzer/dart/analysis/results.dart' ;
21
+ import 'package:analyzer/dart/ast/ast.dart' show SimpleIdentifier;
22
+ import 'package:analyzer/dart/ast/visitor.dart' show RecursiveAstVisitor;
20
23
import 'package:analyzer/source/line_info.dart' ;
21
24
import 'package:analyzer/src/services/available_declarations.dart' ;
22
25
import 'package:analyzer_plugin/protocol/protocol_common.dart' ;
@@ -189,6 +192,10 @@ class CompletionHandler
189
192
190
193
final completionRequest = CompletionRequestImpl (
191
194
unit, offset, server.options.useNewRelevance, performance);
195
+ final directiveInfo =
196
+ server.getDartdocDirectiveInfoFor (completionRequest.result);
197
+ final dartCompletionRequest =
198
+ await DartCompletionRequestImpl .from (completionRequest, directiveInfo);
192
199
193
200
Set <ElementKind > includedElementKinds;
194
201
Set <String > includedElementNames;
@@ -201,8 +208,7 @@ class CompletionHandler
201
208
202
209
try {
203
210
CompletionContributor contributor = DartCompletionManager (
204
- dartdocDirectiveInfo:
205
- server.getDartdocDirectiveInfoFor (completionRequest.result),
211
+ dartdocDirectiveInfo: directiveInfo,
206
212
includedElementKinds: includedElementKinds,
207
213
includedElementNames: includedElementNames,
208
214
includedSuggestionRelevanceTags: includedSuggestionRelevanceTags,
@@ -312,12 +318,21 @@ class CompletionHandler
312
318
results.addAll (setResults);
313
319
});
314
320
321
+ // Perform fuzzy matching based on the identifier in front of the caret to
322
+ // reduce the size of the payload.
323
+ final fuzzyPattern = _prefixMatchingPattern (dartCompletionRequest);
324
+ final fuzzyMatcher =
325
+ FuzzyMatcher (fuzzyPattern, matchStyle: MatchStyle .TEXT );
326
+
327
+ final matchingResults =
328
+ results.where ((e) => fuzzyMatcher.score (e.label) > 0 ).toList ();
329
+
315
330
performance.notificationCount = 1 ;
316
331
performance.suggestionCountFirst = results.length;
317
332
performance.suggestionCountLast = results.length;
318
333
performance.complete ();
319
334
320
- return success (results );
335
+ return success (matchingResults );
321
336
} on AbortCompletion {
322
337
return success ([]);
323
338
}
@@ -342,4 +357,29 @@ class CompletionHandler
342
357
);
343
358
});
344
359
}
360
+
361
+ /// Return the pattern to match suggestions against, from the identifier
362
+ /// to the left of the caret. Return the empty string if cannot find the
363
+ /// identifier.
364
+ String _prefixMatchingPattern (DartCompletionRequestImpl request) {
365
+ final nodeAtOffsetVisitor =
366
+ _IdentifierEndingAtOffsetVisitor (request.offset);
367
+ request.target.containingNode.accept (nodeAtOffsetVisitor);
368
+
369
+ return nodeAtOffsetVisitor.matchingNode? .name ?? '' ;
370
+ }
371
+ }
372
+
373
+ /// An AST visitor to locate a [SimpleIdentifier] that ends at the provided offset.
374
+ class _IdentifierEndingAtOffsetVisitor extends RecursiveAstVisitor <void > {
375
+ final int offset;
376
+ SimpleIdentifier _matchingNode;
377
+ _IdentifierEndingAtOffsetVisitor (this .offset);
378
+ SimpleIdentifier get matchingNode => _matchingNode;
379
+ @override
380
+ void visitSimpleIdentifier (SimpleIdentifier node) {
381
+ if (node.end == offset) {
382
+ _matchingNode = node;
383
+ }
384
+ }
345
385
}
0 commit comments