@@ -16,7 +16,10 @@ import 'package:analysis_server/src/provisional/completion/completion_core.dart'
1616import 'package:analysis_server/src/services/completion/completion_core.dart' ;
1717import 'package:analysis_server/src/services/completion/completion_performance.dart' ;
1818import 'package:analysis_server/src/services/completion/dart/completion_manager.dart' ;
19+ import 'package:analysis_server/src/services/completion/filtering/fuzzy_matcher.dart' ;
1920import '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;
2023import 'package:analyzer/source/line_info.dart' ;
2124import 'package:analyzer/src/services/available_declarations.dart' ;
2225import 'package:analyzer_plugin/protocol/protocol_common.dart' ;
@@ -189,6 +192,10 @@ class CompletionHandler
189192
190193 final completionRequest = CompletionRequestImpl (
191194 unit, offset, server.options.useNewRelevance, performance);
195+ final directiveInfo =
196+ server.getDartdocDirectiveInfoFor (completionRequest.result);
197+ final dartCompletionRequest =
198+ await DartCompletionRequestImpl .from (completionRequest, directiveInfo);
192199
193200 Set <ElementKind > includedElementKinds;
194201 Set <String > includedElementNames;
@@ -201,8 +208,7 @@ class CompletionHandler
201208
202209 try {
203210 CompletionContributor contributor = DartCompletionManager (
204- dartdocDirectiveInfo:
205- server.getDartdocDirectiveInfoFor (completionRequest.result),
211+ dartdocDirectiveInfo: directiveInfo,
206212 includedElementKinds: includedElementKinds,
207213 includedElementNames: includedElementNames,
208214 includedSuggestionRelevanceTags: includedSuggestionRelevanceTags,
@@ -312,12 +318,21 @@ class CompletionHandler
312318 results.addAll (setResults);
313319 });
314320
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+
315330 performance.notificationCount = 1 ;
316331 performance.suggestionCountFirst = results.length;
317332 performance.suggestionCountLast = results.length;
318333 performance.complete ();
319334
320- return success (results );
335+ return success (matchingResults );
321336 } on AbortCompletion {
322337 return success ([]);
323338 }
@@ -342,4 +357,29 @@ class CompletionHandler
342357 );
343358 });
344359 }
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+ }
345385}
0 commit comments