Chromium Code Reviews| 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/src/protocol_server.dart' as protocol; | 7 import 'package:analysis_server/src/protocol_server.dart' as protocol; |
| 8 import 'package:analysis_server/src/protocol_server.dart' | 8 import 'package:analysis_server/src/protocol_server.dart' |
| 9 show CompletionSuggestion, CompletionSuggestionKind; | 9 show CompletionSuggestion, CompletionSuggestionKind; |
| 10 import 'package:analysis_server/src/provisional/completion/completion_dart.dart' ; | 10 import 'package:analysis_server/src/provisional/completion/completion_dart.dart' ; |
| 11 import 'package:analysis_server/src/services/completion/contribution_sorter.dart '; | 11 import 'package:analysis_server/src/services/completion/contribution_sorter.dart '; |
| 12 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; | 13 show DART_RELEVANCE_COMMON_USAGE; |
| 14 import 'package:analyzer/src/generated/ast.dart'; | 14 import 'package:analyzer/src/generated/ast.dart'; |
| 15 import 'package:analyzer/src/generated/element.dart'; | 15 import 'package:analyzer/src/generated/element.dart'; |
| 16 import 'package:analysis_server/src/provisional/completion/completion_core.dart' ; | |
| 17 import 'package:analysis_server/src/provisional/completion/dart/completion_targe t.dart'; | |
| 18 import 'package:analyzer/task/dart.dart'; | |
| 19 import 'package:analyzer/src/task/dart.dart'; | |
| 16 | 20 |
| 17 part 'common_usage_generated.dart'; | 21 part 'common_usage_generated.dart'; |
| 18 | 22 |
| 19 /** | 23 /** |
| 20 * A computer for adjusting the relevance of completions computed by others | 24 * A computer for adjusting the relevance of completions computed by others |
| 21 * based upon common Dart usage patterns. This is a long-lived object | 25 * based upon common Dart usage patterns. This is a long-lived object |
| 22 * that should not maintain state between calls to it's [sort] method. | 26 * that should not maintain state between calls to it's [sort] method. |
| 23 */ | 27 */ |
| 24 class CommonUsageComputer implements ContributionSorter { | 28 class CommonUsageComputer implements ContributionSorter { |
| 25 /** | 29 /** |
| 26 * A map of <library>.<classname> to an ordered list of method names, | 30 * A map of <library>.<classname> to an ordered list of method names, |
| 27 * field names, getter names, and named constructors. | 31 * field names, getter names, and named constructors. |
| 28 * The names are ordered from most relevant to least relevant. | 32 * The names are ordered from most relevant to least relevant. |
| 29 * Names not listed are considered equally less relevant than those listed. | 33 * Names not listed are considered equally less relevant than those listed. |
| 30 */ | 34 */ |
| 31 Map<String, List<String>> selectorRelevance; | 35 Map<String, List<String>> selectorRelevance; |
| 32 | 36 |
| 33 CommonUsageComputer([this.selectorRelevance = defaultSelectorRelevance]); | 37 CommonUsageComputer([this.selectorRelevance = defaultSelectorRelevance]); |
| 34 | 38 |
| 35 @override | 39 @override |
| 36 void sort(DartCompletionRequest request, | 40 AnalysisRequest sort( |
| 37 Iterable<CompletionSuggestion> suggestions) { | 41 CompletionRequest request, Iterable<CompletionSuggestion> suggestions) { |
| 38 _update(request, suggestions); | 42 _update(request, suggestions); |
| 43 return null; | |
| 44 } | |
| 45 | |
| 46 CompletionTarget _getCompletionTarget(CompletionRequest request) { | |
| 47 // TODO (danrubel) get cached completion target | |
| 48 var libElem = request.context.getResult(request.source, LIBRARY_ELEMENT1); | |
|
Brian Wilkerson
2015/11/14 16:59:24
If `request.source` is a part file, then `libElem`
danrubel
2015/11/17 06:58:45
Good point. Fixed.
| |
| 49 if (libElem is LibraryElement) { | |
|
Brian Wilkerson
2015/11/14 16:59:24
The value of `libElem` can only be a LibraryElemen
| |
| 50 var unit = request.context.getResult( | |
| 51 new LibrarySpecificUnit(libElem.source, request.source), | |
| 52 RESOLVED_UNIT3); | |
| 53 if (unit is CompilationUnit){ | |
|
Brian Wilkerson
2015/11/14 16:59:24
"){"? Did the formatter leave the code this way?
danrubel
2015/11/17 06:58:45
Oops. Must forgotten to run the formatter. Done.
| |
| 54 return new CompletionTarget.forOffset(unit, request.offset); | |
| 55 } | |
| 56 } | |
| 57 return null; | |
| 39 } | 58 } |
| 40 | 59 |
| 41 /** | 60 /** |
| 42 * Adjusts the relevance based on the given completion context. | 61 * Adjusts the relevance based on the given completion context. |
| 43 * The compilation unit and completion node | 62 * The compilation unit and completion node |
| 44 * in the given completion context may not be resolved. | 63 * in the given completion context may not be resolved. |
| 45 */ | 64 */ |
| 46 void _update(DartCompletionRequest request, | 65 void _update( |
| 47 Iterable<CompletionSuggestion> suggestions) { | 66 CompletionRequest request, Iterable<CompletionSuggestion> suggestions) { |
| 48 var visitor = new _BestTypeVisitor(request.target.entity); | 67 var target = _getCompletionTarget(request); |
| 49 DartType type = request.target.containingNode.accept(visitor); | 68 if (target != null) { |
| 50 if (type != null) { | 69 var visitor = new _BestTypeVisitor(target.entity); |
| 51 Element typeElem = type.element; | 70 DartType type = target.containingNode.accept(visitor); |
| 52 if (typeElem != null) { | 71 if (type != null) { |
| 53 LibraryElement libElem = typeElem.library; | 72 Element typeElem = type.element; |
| 54 if (libElem != null) { | 73 if (typeElem != null) { |
| 55 _updateInvocationRelevance(request, type, libElem, suggestions); | 74 LibraryElement libElem = typeElem.library; |
| 75 if (libElem != null) { | |
| 76 _updateInvocationRelevance(type, libElem, suggestions); | |
| 77 } | |
| 56 } | 78 } |
| 57 } | 79 } |
| 58 } | 80 } |
| 59 } | 81 } |
| 60 | 82 |
| 61 /** | 83 /** |
| 62 * Adjusts the relevance of all method suggestions based upon the given | 84 * Adjusts the relevance of all method suggestions based upon the given |
| 63 * target type and library. | 85 * target type and library. |
| 64 */ | 86 */ |
| 65 void _updateInvocationRelevance(DartCompletionRequest request, DartType type, | 87 void _updateInvocationRelevance(DartType type, LibraryElement libElem, |
| 66 LibraryElement libElem, Iterable<CompletionSuggestion> suggestions) { | 88 Iterable<CompletionSuggestion> suggestions) { |
| 67 String typeName = type.name; | 89 String typeName = type.name; |
| 68 List<String> selectors = selectorRelevance['${libElem.name}.${typeName}']; | 90 List<String> selectors = selectorRelevance['${libElem.name}.${typeName}']; |
| 69 if (selectors != null) { | 91 if (selectors != null) { |
| 70 for (CompletionSuggestion suggestion in suggestions) { | 92 for (CompletionSuggestion suggestion in suggestions) { |
| 71 protocol.Element element = suggestion.element; | 93 protocol.Element element = suggestion.element; |
| 72 if (element != null && | 94 if (element != null && |
| 73 (element.kind == protocol.ElementKind.CONSTRUCTOR || | 95 (element.kind == protocol.ElementKind.CONSTRUCTOR || |
| 74 element.kind == protocol.ElementKind.FIELD || | 96 element.kind == protocol.ElementKind.FIELD || |
| 75 element.kind == protocol.ElementKind.GETTER || | 97 element.kind == protocol.ElementKind.GETTER || |
| 76 element.kind == protocol.ElementKind.METHOD || | 98 element.kind == protocol.ElementKind.METHOD || |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 128 DartType visitPropertyAccess(PropertyAccess node) { | 150 DartType visitPropertyAccess(PropertyAccess node) { |
| 129 if (node.propertyName == entity) { | 151 if (node.propertyName == entity) { |
| 130 Expression target = node.realTarget; | 152 Expression target = node.realTarget; |
| 131 if (target != null) { | 153 if (target != null) { |
| 132 return target.bestType; | 154 return target.bestType; |
| 133 } | 155 } |
| 134 } | 156 } |
| 135 return null; | 157 return null; |
| 136 } | 158 } |
| 137 } | 159 } |
| OLD | NEW |