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.dart.manager; | 5 library services.completion.dart.manager; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:analysis_server/plugin/protocol/protocol.dart'; | 9 import 'package:analysis_server/plugin/protocol/protocol.dart'; |
10 import 'package:analysis_server/src/provisional/completion/completion_core.dart' | 10 import 'package:analysis_server/src/provisional/completion/completion_core.dart' |
11 show CompletionContributor, CompletionRequest; | 11 show CompletionContributor, CompletionRequest; |
12 import 'package:analysis_server/src/provisional/completion/dart/completion_dart.
dart'; | 12 import 'package:analysis_server/src/provisional/completion/dart/completion_dart.
dart'; |
13 import 'package:analysis_server/src/provisional/completion/dart/completion_plugi
n.dart'; | 13 import 'package:analysis_server/src/provisional/completion/dart/completion_plugi
n.dart'; |
14 import 'package:analysis_server/src/provisional/completion/dart/completion_targe
t.dart'; | 14 import 'package:analysis_server/src/provisional/completion/dart/completion_targe
t.dart'; |
15 import 'package:analysis_server/src/services/completion/completion_core.dart'; | 15 import 'package:analysis_server/src/services/completion/completion_core.dart'; |
16 import 'package:analysis_server/src/services/completion/optype.dart'; | 16 import 'package:analysis_server/src/services/completion/optype.dart'; |
17 import 'package:analysis_server/src/services/search/search_engine.dart'; | 17 import 'package:analysis_server/src/services/search/search_engine.dart'; |
18 import 'package:analyzer/dart/element/element.dart'; | 18 import 'package:analyzer/dart/element/element.dart'; |
19 import 'package:analyzer/dart/element/type.dart'; | 19 import 'package:analyzer/dart/element/type.dart'; |
20 import 'package:analyzer/file_system/file_system.dart'; | 20 import 'package:analyzer/file_system/file_system.dart'; |
21 import 'package:analyzer/src/context/context.dart' | 21 import 'package:analyzer/src/context/context.dart' |
22 show AnalysisFutureHelper, AnalysisContextImpl; | 22 show AnalysisFutureHelper, AnalysisContextImpl; |
23 import 'package:analyzer/src/generated/ast.dart'; | 23 import 'package:analyzer/src/generated/ast.dart'; |
24 import 'package:analyzer/src/generated/engine.dart' hide AnalysisContextImpl; | 24 import 'package:analyzer/src/generated/engine.dart' hide AnalysisContextImpl; |
25 import 'package:analyzer/src/generated/source.dart'; | 25 import 'package:analyzer/src/generated/source.dart'; |
26 import 'package:analyzer/src/task/dart.dart'; | 26 import 'package:analyzer/src/task/dart.dart'; |
27 import 'package:analyzer/task/dart.dart'; | 27 import 'package:analyzer/task/dart.dart'; |
28 import 'package:analyzer/src/generated/scanner.dart'; | 28 import 'package:analyzer/src/generated/scanner.dart'; |
| 29 import 'package:analysis_server/src/services/completion/dart/contribution_sorter
.dart'; |
| 30 import 'package:analysis_server/src/services/completion/dart/common_usage_sorter
.dart'; |
29 | 31 |
30 /** | 32 /** |
31 * [DartCompletionManager] determines if a completion request is Dart specific | 33 * [DartCompletionManager] determines if a completion request is Dart specific |
32 * and forwards those requests to all [DartCompletionContributor]s. | 34 * and forwards those requests to all [DartCompletionContributor]s. |
33 */ | 35 */ |
34 class DartCompletionManager implements CompletionContributor { | 36 class DartCompletionManager implements CompletionContributor { |
| 37 /** |
| 38 * The [contributionSorter] is a long-lived object that isn't allowed |
| 39 * to maintain state between calls to [DartContributionSorter#sort(...)]. |
| 40 */ |
| 41 static DartContributionSorter contributionSorter = new CommonUsageSorter(); |
| 42 |
35 @override | 43 @override |
36 Future<List<CompletionSuggestion>> computeSuggestions( | 44 Future<List<CompletionSuggestion>> computeSuggestions( |
37 CompletionRequest request) async { | 45 CompletionRequest request) async { |
38 if (!AnalysisEngine.isDartFileName(request.source.shortName)) { | 46 if (!AnalysisEngine.isDartFileName(request.source.shortName)) { |
39 return EMPTY_LIST; | 47 return EMPTY_LIST; |
40 } | 48 } |
41 | 49 |
42 DartCompletionRequestImpl dartRequest = | 50 DartCompletionRequestImpl dartRequest = |
43 await DartCompletionRequestImpl.from(request); | 51 await DartCompletionRequestImpl.from(request); |
44 ReplacementRange range = | 52 ReplacementRange range = |
45 new ReplacementRange.compute(dartRequest.offset, dartRequest.target); | 53 new ReplacementRange.compute(dartRequest.offset, dartRequest.target); |
46 (request as CompletionRequestImpl) | 54 (request as CompletionRequestImpl) |
47 ..replacementOffset = range.offset | 55 ..replacementOffset = range.offset |
48 ..replacementLength = range.length; | 56 ..replacementLength = range.length; |
49 | 57 |
50 // Don't suggest in comments. | 58 // Don't suggest in comments. |
51 if (dartRequest.target.isCommentText) { | 59 if (dartRequest.target.isCommentText) { |
52 return EMPTY_LIST; | 60 return EMPTY_LIST; |
53 } | 61 } |
54 | 62 |
55 // Request Dart specific completions from each contributor | 63 // Request Dart specific completions from each contributor |
56 Map<String, CompletionSuggestion> suggestionMap = | 64 Map<String, CompletionSuggestion> suggestionMap = |
57 <String, CompletionSuggestion>{}; | 65 <String, CompletionSuggestion>{}; |
58 for (DartCompletionContributor contributor | 66 for (DartCompletionContributor contributor |
59 in dartCompletionPlugin.contributors) { | 67 in dartCompletionPlugin.contributors) { |
60 for (CompletionSuggestion newSuggestion | 68 List<CompletionSuggestion> contributorSuggestions = |
61 in await contributor.computeSuggestions(dartRequest)) { | 69 await contributor.computeSuggestions(dartRequest); |
| 70 |
| 71 for (CompletionSuggestion newSuggestion in contributorSuggestions) { |
62 var oldSuggestion = suggestionMap.putIfAbsent( | 72 var oldSuggestion = suggestionMap.putIfAbsent( |
63 newSuggestion.completion, () => newSuggestion); | 73 newSuggestion.completion, () => newSuggestion); |
64 if (newSuggestion != oldSuggestion && | 74 if (newSuggestion != oldSuggestion && |
65 newSuggestion.relevance > oldSuggestion.relevance) { | 75 newSuggestion.relevance > oldSuggestion.relevance) { |
66 suggestionMap[newSuggestion.completion] = newSuggestion; | 76 suggestionMap[newSuggestion.completion] = newSuggestion; |
67 } | 77 } |
68 } | 78 } |
69 } | 79 } |
70 return suggestionMap.values.toList(); | 80 |
| 81 // Adjust suggestion relevance before returning |
| 82 List<CompletionSuggestion> suggestions = suggestionMap.values.toList(); |
| 83 await contributionSorter.sort(dartRequest, suggestions); |
| 84 return suggestions; |
71 } | 85 } |
72 } | 86 } |
73 | 87 |
74 /** | 88 /** |
75 * The information about a requested list of completions within a Dart file. | 89 * The information about a requested list of completions within a Dart file. |
76 */ | 90 */ |
77 class DartCompletionRequestImpl extends CompletionRequestImpl | 91 class DartCompletionRequestImpl extends CompletionRequestImpl |
78 implements DartCompletionRequest { | 92 implements DartCompletionRequest { |
79 /** | 93 /** |
80 * The [LibraryElement] representing dart:core | 94 * The [LibraryElement] representing dart:core |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 // Replacement range for import URI | 347 // Replacement range for import URI |
334 return new ReplacementRange(start, end - start); | 348 return new ReplacementRange(start, end - start); |
335 } | 349 } |
336 } | 350 } |
337 } | 351 } |
338 } | 352 } |
339 } | 353 } |
340 return new ReplacementRange(requestOffset, 0); | 354 return new ReplacementRange(requestOffset, 0); |
341 } | 355 } |
342 } | 356 } |
OLD | NEW |