OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library services.completion.computer.dart.relevance; | 5 library services.completion.computer.dart.relevance; |
6 | 6 |
| 7 import 'package:analysis_server/completion/completion_dart.dart'; |
7 import 'package:analysis_server/src/protocol_server.dart' as protocol; | 8 import 'package:analysis_server/src/protocol_server.dart' as protocol; |
8 import 'package:analysis_server/src/protocol_server.dart' | 9 import 'package:analysis_server/src/protocol_server.dart' |
9 show CompletionSuggestion, CompletionSuggestionKind; | 10 show CompletionSuggestion, CompletionSuggestionKind; |
10 import 'package:analysis_server/src/services/completion/contribution_sorter.dart
'; | 11 import 'package:analysis_server/src/services/completion/contribution_sorter.dart
'; |
11 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; | 12 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart' |
| 13 show DART_RELEVANCE_COMMON_USAGE; |
12 import 'package:analyzer/src/generated/ast.dart'; | 14 import 'package:analyzer/src/generated/ast.dart'; |
13 import 'package:analyzer/src/generated/element.dart'; | 15 import 'package:analyzer/src/generated/element.dart'; |
14 | 16 |
15 part 'common_usage_generated.dart'; | 17 part 'common_usage_generated.dart'; |
16 | 18 |
17 /** | 19 /** |
18 * A computer for adjusting the relevance of completions computed by others | 20 * A computer for adjusting the relevance of completions computed by others |
19 * based upon common Dart usage patterns. This is a long-lived object | 21 * based upon common Dart usage patterns. This is a long-lived object |
20 * that should not maintain state between calls to it's [sort] method. | 22 * that should not maintain state between calls to it's [sort] method. |
21 */ | 23 */ |
22 class CommonUsageComputer implements ContributionSorter { | 24 class CommonUsageComputer implements ContributionSorter { |
23 /** | 25 /** |
24 * A map of <library>.<classname> to an ordered list of method names, | 26 * A map of <library>.<classname> to an ordered list of method names, |
25 * field names, getter names, and named constructors. | 27 * field names, getter names, and named constructors. |
26 * The names are ordered from most relevant to least relevant. | 28 * The names are ordered from most relevant to least relevant. |
27 * Names not listed are considered equally less relevant than those listed. | 29 * Names not listed are considered equally less relevant than those listed. |
28 */ | 30 */ |
29 Map<String, List<String>> selectorRelevance; | 31 Map<String, List<String>> selectorRelevance; |
30 | 32 |
31 CommonUsageComputer([this.selectorRelevance = defaultSelectorRelevance]); | 33 CommonUsageComputer([this.selectorRelevance = defaultSelectorRelevance]); |
32 | 34 |
33 @override | 35 @override |
34 void sort(DartCompletionRequest request) { | 36 void sort(DartCompletionRequest request, |
35 _update(request); | 37 Iterable<CompletionSuggestion> suggestions) { |
| 38 _update(request, suggestions); |
36 } | 39 } |
37 | 40 |
38 /** | 41 /** |
39 * Adjusts the relevance based on the given completion context. | 42 * Adjusts the relevance based on the given completion context. |
40 * The compilation unit and completion node | 43 * The compilation unit and completion node |
41 * in the given completion context may not be resolved. | 44 * in the given completion context may not be resolved. |
42 */ | 45 */ |
43 void _update(DartCompletionRequest request) { | 46 void _update(DartCompletionRequest request, |
| 47 Iterable<CompletionSuggestion> suggestions) { |
44 var visitor = new _BestTypeVisitor(request.target.entity); | 48 var visitor = new _BestTypeVisitor(request.target.entity); |
45 DartType type = request.target.containingNode.accept(visitor); | 49 DartType type = request.target.containingNode.accept(visitor); |
46 if (type != null) { | 50 if (type != null) { |
47 Element typeElem = type.element; | 51 Element typeElem = type.element; |
48 if (typeElem != null) { | 52 if (typeElem != null) { |
49 LibraryElement libElem = typeElem.library; | 53 LibraryElement libElem = typeElem.library; |
50 if (libElem != null) { | 54 if (libElem != null) { |
51 _updateInvocationRelevance(request, type, libElem); | 55 _updateInvocationRelevance(request, type, libElem, suggestions); |
52 } | 56 } |
53 } | 57 } |
54 } | 58 } |
55 } | 59 } |
56 | 60 |
57 /** | 61 /** |
58 * Adjusts the relevance of all method suggestions based upon the given | 62 * Adjusts the relevance of all method suggestions based upon the given |
59 * target type and library. | 63 * target type and library. |
60 */ | 64 */ |
61 void _updateInvocationRelevance( | 65 void _updateInvocationRelevance(DartCompletionRequest request, DartType type, |
62 DartCompletionRequest request, DartType type, LibraryElement libElem) { | 66 LibraryElement libElem, Iterable<CompletionSuggestion> suggestions) { |
63 String typeName = type.name; | 67 String typeName = type.name; |
64 List<String> selectors = selectorRelevance['${libElem.name}.${typeName}']; | 68 List<String> selectors = selectorRelevance['${libElem.name}.${typeName}']; |
65 if (selectors != null) { | 69 if (selectors != null) { |
66 for (CompletionSuggestion suggestion in request.suggestions) { | 70 for (CompletionSuggestion suggestion in suggestions) { |
67 protocol.Element element = suggestion.element; | 71 protocol.Element element = suggestion.element; |
68 if (element != null && | 72 if (element != null && |
69 (element.kind == protocol.ElementKind.CONSTRUCTOR || | 73 (element.kind == protocol.ElementKind.CONSTRUCTOR || |
70 element.kind == protocol.ElementKind.FIELD || | 74 element.kind == protocol.ElementKind.FIELD || |
71 element.kind == protocol.ElementKind.GETTER || | 75 element.kind == protocol.ElementKind.GETTER || |
72 element.kind == protocol.ElementKind.METHOD || | 76 element.kind == protocol.ElementKind.METHOD || |
73 element.kind == protocol.ElementKind.SETTER) && | 77 element.kind == protocol.ElementKind.SETTER) && |
74 suggestion.kind == CompletionSuggestionKind.INVOCATION && | 78 suggestion.kind == CompletionSuggestionKind.INVOCATION && |
75 suggestion.declaringType == typeName) { | 79 suggestion.declaringType == typeName) { |
76 int index = selectors.indexOf(suggestion.completion); | 80 int index = selectors.indexOf(suggestion.completion); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 DartType visitPropertyAccess(PropertyAccess node) { | 128 DartType visitPropertyAccess(PropertyAccess node) { |
125 if (node.propertyName == entity) { | 129 if (node.propertyName == entity) { |
126 Expression target = node.realTarget; | 130 Expression target = node.realTarget; |
127 if (target != null) { | 131 if (target != null) { |
128 return target.bestType; | 132 return target.bestType; |
129 } | 133 } |
130 } | 134 } |
131 return null; | 135 return null; |
132 } | 136 } |
133 } | 137 } |
OLD | NEW |