| 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 libSrcs = request.context.getLibrariesContaining(request.source); |
| 49 if (libSrcs.length == 0) { |
| 50 return null; |
| 51 } |
| 52 var libElem = request.context.getResult(libSrcs[0], LIBRARY_ELEMENT1); |
| 53 if (libElem is LibraryElement) { |
| 54 var unit = request.context.getResult( |
| 55 new LibrarySpecificUnit(libElem.source, request.source), |
| 56 RESOLVED_UNIT3); |
| 57 if (unit is CompilationUnit) { |
| 58 return new CompletionTarget.forOffset(unit, request.offset); |
| 59 } |
| 60 } |
| 61 return null; |
| 39 } | 62 } |
| 40 | 63 |
| 41 /** | 64 /** |
| 42 * Adjusts the relevance based on the given completion context. | 65 * Adjusts the relevance based on the given completion context. |
| 43 * The compilation unit and completion node | 66 * The compilation unit and completion node |
| 44 * in the given completion context may not be resolved. | 67 * in the given completion context may not be resolved. |
| 45 */ | 68 */ |
| 46 void _update(DartCompletionRequest request, | 69 void _update( |
| 47 Iterable<CompletionSuggestion> suggestions) { | 70 CompletionRequest request, Iterable<CompletionSuggestion> suggestions) { |
| 48 var visitor = new _BestTypeVisitor(request.target.entity); | 71 var target = _getCompletionTarget(request); |
| 49 DartType type = request.target.containingNode.accept(visitor); | 72 if (target != null) { |
| 50 if (type != null) { | 73 var visitor = new _BestTypeVisitor(target.entity); |
| 51 Element typeElem = type.element; | 74 DartType type = target.containingNode.accept(visitor); |
| 52 if (typeElem != null) { | 75 if (type != null) { |
| 53 LibraryElement libElem = typeElem.library; | 76 Element typeElem = type.element; |
| 54 if (libElem != null) { | 77 if (typeElem != null) { |
| 55 _updateInvocationRelevance(request, type, libElem, suggestions); | 78 LibraryElement libElem = typeElem.library; |
| 79 if (libElem != null) { |
| 80 _updateInvocationRelevance(type, libElem, suggestions); |
| 81 } |
| 56 } | 82 } |
| 57 } | 83 } |
| 58 } | 84 } |
| 59 } | 85 } |
| 60 | 86 |
| 61 /** | 87 /** |
| 62 * Adjusts the relevance of all method suggestions based upon the given | 88 * Adjusts the relevance of all method suggestions based upon the given |
| 63 * target type and library. | 89 * target type and library. |
| 64 */ | 90 */ |
| 65 void _updateInvocationRelevance(DartCompletionRequest request, DartType type, | 91 void _updateInvocationRelevance(DartType type, LibraryElement libElem, |
| 66 LibraryElement libElem, Iterable<CompletionSuggestion> suggestions) { | 92 Iterable<CompletionSuggestion> suggestions) { |
| 67 String typeName = type.name; | 93 String typeName = type.name; |
| 68 List<String> selectors = selectorRelevance['${libElem.name}.${typeName}']; | 94 List<String> selectors = selectorRelevance['${libElem.name}.${typeName}']; |
| 69 if (selectors != null) { | 95 if (selectors != null) { |
| 70 for (CompletionSuggestion suggestion in suggestions) { | 96 for (CompletionSuggestion suggestion in suggestions) { |
| 71 protocol.Element element = suggestion.element; | 97 protocol.Element element = suggestion.element; |
| 72 if (element != null && | 98 if (element != null && |
| 73 (element.kind == protocol.ElementKind.CONSTRUCTOR || | 99 (element.kind == protocol.ElementKind.CONSTRUCTOR || |
| 74 element.kind == protocol.ElementKind.FIELD || | 100 element.kind == protocol.ElementKind.FIELD || |
| 75 element.kind == protocol.ElementKind.GETTER || | 101 element.kind == protocol.ElementKind.GETTER || |
| 76 element.kind == protocol.ElementKind.METHOD || | 102 element.kind == protocol.ElementKind.METHOD || |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 DartType visitPropertyAccess(PropertyAccess node) { | 154 DartType visitPropertyAccess(PropertyAccess node) { |
| 129 if (node.propertyName == entity) { | 155 if (node.propertyName == entity) { |
| 130 Expression target = node.realTarget; | 156 Expression target = node.realTarget; |
| 131 if (target != null) { | 157 if (target != null) { |
| 132 return target.bestType; | 158 return target.bestType; |
| 133 } | 159 } |
| 134 } | 160 } |
| 135 return null; | 161 return null; |
| 136 } | 162 } |
| 137 } | 163 } |
| OLD | NEW |