OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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; | 5 library services.completion.dart; |
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 AnalysisRequest, CompletionContributor, CompletionRequest; | 11 show AnalysisRequest, CompletionContributor, CompletionRequest; |
12 import 'package:analysis_server/src/services/completion/completion_core.dart'; | 12 import 'package:analysis_server/src/services/completion/completion_core.dart'; |
13 import 'package:analysis_server/src/services/completion/completion_manager.dart'
; | 13 import 'package:analysis_server/src/services/completion/completion_manager.dart'
; |
14 import 'package:analysis_server/src/services/completion/dart/common_usage_sorter
.dart'; | |
15 import 'package:analysis_server/src/services/completion/dart/contribution_sorter
.dart'; | |
16 import 'package:analysis_server/src/services/search/search_engine.dart'; | 14 import 'package:analysis_server/src/services/search/search_engine.dart'; |
17 import 'package:analyzer/file_system/file_system.dart'; | 15 import 'package:analyzer/file_system/file_system.dart'; |
18 import 'package:analyzer/src/generated/ast.dart'; | 16 import 'package:analyzer/src/generated/ast.dart'; |
19 import 'package:analyzer/src/generated/engine.dart'; | 17 import 'package:analyzer/src/generated/engine.dart'; |
20 import 'package:analyzer/src/generated/source.dart'; | 18 import 'package:analyzer/src/generated/source.dart'; |
21 | 19 |
22 /** | 20 /** |
23 * The base class for contributing code completion suggestions. | 21 * The base class for contributing code completion suggestions. |
24 */ | 22 */ |
25 abstract class DartCompletionContributor { | 23 abstract class DartCompletionContributor { |
(...skipping 13 matching lines...) Expand all Loading... |
39 * in the given completion context are resolved. | 37 * in the given completion context are resolved. |
40 * Returns `true` if the receiver modified the list of suggestions. | 38 * Returns `true` if the receiver modified the list of suggestions. |
41 */ | 39 */ |
42 Future<bool> computeFull(DartCompletionRequest request); | 40 Future<bool> computeFull(DartCompletionRequest request); |
43 } | 41 } |
44 | 42 |
45 /** | 43 /** |
46 * Manages code completion for a given Dart file completion request. | 44 * Manages code completion for a given Dart file completion request. |
47 */ | 45 */ |
48 class DartCompletionManager extends CompletionManager { | 46 class DartCompletionManager extends CompletionManager { |
49 /** | |
50 * The [defaultContributionSorter] is a long-lived object that isn't allowed | |
51 * to maintain state between calls to [ContributionSorter#sort(...)]. | |
52 */ | |
53 static DartContributionSorter defaultContributionSorter = | |
54 new CommonUsageSorter(); | |
55 | |
56 final SearchEngine searchEngine; | 47 final SearchEngine searchEngine; |
57 Iterable<CompletionContributor> newContributors; | 48 Iterable<CompletionContributor> newContributors; |
58 DartContributionSorter contributionSorter; | |
59 | 49 |
60 DartCompletionManager( | 50 DartCompletionManager( |
61 AnalysisContext context, this.searchEngine, Source source, | 51 AnalysisContext context, this.searchEngine, Source source, |
62 [this.newContributors, this.contributionSorter]) | 52 [this.newContributors]) |
63 : super(context, source) { | 53 : super(context, source) { |
64 if (newContributors == null) { | 54 if (newContributors == null) { |
65 newContributors = <CompletionContributor>[]; | 55 newContributors = <CompletionContributor>[]; |
66 } | 56 } |
67 if (contributionSorter == null) { | |
68 contributionSorter = defaultContributionSorter; | |
69 } | |
70 } | 57 } |
71 | 58 |
72 /** | 59 /** |
73 * Create a new initialized Dart source completion manager | 60 * Create a new initialized Dart source completion manager |
74 */ | 61 */ |
75 factory DartCompletionManager.create( | 62 factory DartCompletionManager.create( |
76 AnalysisContext context, | 63 AnalysisContext context, |
77 SearchEngine searchEngine, | 64 SearchEngine searchEngine, |
78 Source source, | 65 Source source, |
79 Iterable<CompletionContributor> newContributors) { | 66 Iterable<CompletionContributor> newContributors) { |
(...skipping 28 matching lines...) Expand all Loading... |
108 List<CompletionSuggestion> newSuggestions = | 95 List<CompletionSuggestion> newSuggestions = |
109 await contributor.computeSuggestions(request); | 96 await contributor.computeSuggestions(request); |
110 for (CompletionSuggestion suggestion in newSuggestions) { | 97 for (CompletionSuggestion suggestion in newSuggestions) { |
111 request.addSuggestion(suggestion); | 98 request.addSuggestion(suggestion); |
112 } | 99 } |
113 performance.logElapseTime(contributorTag); | 100 performance.logElapseTime(contributorTag); |
114 } | 101 } |
115 performance.logElapseTime('computeSuggestions'); | 102 performance.logElapseTime('computeSuggestions'); |
116 performance.logStartTime('waitForAnalysis'); | 103 performance.logStartTime('waitForAnalysis'); |
117 | 104 |
118 // TODO(danrubel) current sorter requires no additional analysis, | |
119 // but need to handle the returned future the same way that futures | |
120 // returned from contributors are handled once this method is refactored | |
121 // to be async. | |
122 /* await */ contributionSorter.sort(request, request.suggestions); | |
123 // TODO (danrubel) if request is obsolete | 105 // TODO (danrubel) if request is obsolete |
124 // (processAnalysisRequest returns false) | 106 // (processAnalysisRequest returns false) |
125 // then send empty results | 107 // then send empty results |
126 sendResults(request, true); | 108 sendResults(request, true); |
127 return new Future.value(); | 109 return new Future.value(); |
128 } | 110 } |
129 | 111 |
130 @override | 112 @override |
131 void computeSuggestions(CompletionRequest completionRequest) { | 113 void computeSuggestions(CompletionRequest completionRequest) { |
132 DartCompletionRequest request = | 114 DartCompletionRequest request = |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 /** | 191 /** |
210 * Add the given suggestion to the list that is returned to the client as long | 192 * Add the given suggestion to the list that is returned to the client as long |
211 * as a suggestion with an identical completion has not already been added. | 193 * as a suggestion with an identical completion has not already been added. |
212 */ | 194 */ |
213 void addSuggestion(CompletionSuggestion suggestion) { | 195 void addSuggestion(CompletionSuggestion suggestion) { |
214 if (_completions.add(suggestion.completion)) { | 196 if (_completions.add(suggestion.completion)) { |
215 _suggestions.add(suggestion); | 197 _suggestions.add(suggestion); |
216 } | 198 } |
217 } | 199 } |
218 } | 200 } |
OLD | NEW |