Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Side by Side Diff: pkg/analysis_server/lib/src/domain_completion.dart

Issue 2842243003: Add completion support for plugins. (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | pkg/analysis_server/test/domain_completion_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 domain.completion; 5 library domain.completion;
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/analysis_server.dart'; 10 import 'package:analysis_server/src/analysis_server.dart';
11 import 'package:analysis_server/src/constants.dart'; 11 import 'package:analysis_server/src/constants.dart';
12 import 'package:analysis_server/src/domain_abstract.dart';
13 import 'package:analysis_server/src/plugin/plugin_manager.dart';
14 import 'package:analysis_server/src/plugin/result_converter.dart';
12 import 'package:analysis_server/src/provisional/completion/completion_core.dart' ; 15 import 'package:analysis_server/src/provisional/completion/completion_core.dart' ;
13 import 'package:analysis_server/src/services/completion/completion_core.dart'; 16 import 'package:analysis_server/src/services/completion/completion_core.dart';
14 import 'package:analysis_server/src/services/completion/completion_performance.d art'; 17 import 'package:analysis_server/src/services/completion/completion_performance.d art';
15 import 'package:analyzer/src/dart/analysis/driver.dart'; 18 import 'package:analyzer/src/dart/analysis/driver.dart';
16 import 'package:analyzer/src/generated/engine.dart' hide AnalysisResult; 19 import 'package:analyzer/src/generated/engine.dart' hide AnalysisResult;
20 import 'package:analyzer/src/generated/source.dart';
17 import 'package:analyzer/src/source/source_resource.dart'; 21 import 'package:analyzer/src/source/source_resource.dart';
18 import 'package:analyzer/src/generated/source.dart'; 22 import 'package:analyzer_plugin/protocol/protocol.dart' as plugin;
23 import 'package:analyzer_plugin/protocol/protocol_constants.dart' as plugin;
24 import 'package:analyzer_plugin/protocol/protocol_generated.dart' as plugin;
19 25
20 /** 26 /**
21 * Instances of the class [CompletionDomainHandler] implement a [RequestHandler] 27 * Instances of the class [CompletionDomainHandler] implement a [RequestHandler]
22 * that handles requests in the completion domain. 28 * that handles requests in the completion domain.
23 */ 29 */
24 class CompletionDomainHandler implements RequestHandler { 30 class CompletionDomainHandler extends AbstractRequestHandler {
25 /** 31 /**
26 * The maximum number of performance measurements to keep. 32 * The maximum number of performance measurements to keep.
27 */ 33 */
28 static const int performanceListMaxLength = 50; 34 static const int performanceListMaxLength = 50;
29 35
30 /** 36 /**
31 * The analysis server that is using this handler to process requests.
32 */
33 final AnalysisServer server;
34
35 /**
36 * The next completion response id. 37 * The next completion response id.
37 */ 38 */
38 int _nextCompletionId = 0; 39 int _nextCompletionId = 0;
39 40
40 /** 41 /**
41 * Code completion performance for the last completion operation. 42 * Code completion performance for the last completion operation.
42 */ 43 */
43 CompletionPerformance performance; 44 CompletionPerformance performance;
44 45
45 /** 46 /**
46 * A list of code completion performance measurements for the latest 47 * A list of code completion performance measurements for the latest
47 * completion operation up to [performanceListMaxLength] measurements. 48 * completion operation up to [performanceListMaxLength] measurements.
48 */ 49 */
49 final List<CompletionPerformance> performanceList = 50 final List<CompletionPerformance> performanceList =
50 new List<CompletionPerformance>(); 51 new List<CompletionPerformance>();
51 52
52 /** 53 /**
53 * Performance for the last priority change event. 54 * Performance for the last priority change event.
54 */ 55 */
55 CompletionPerformance computeCachePerformance; 56 CompletionPerformance computeCachePerformance;
56 57
57 /** 58 /**
58 * The current request being processed or `null` if none. 59 * The current request being processed or `null` if none.
59 */ 60 */
60 CompletionRequestImpl _currentRequest; 61 CompletionRequestImpl _currentRequest;
61 62
62 /** 63 /**
63 * Initialize a new request handler for the given [server]. 64 * Initialize a new request handler for the given [server].
64 */ 65 */
65 CompletionDomainHandler(this.server); 66 CompletionDomainHandler(AnalysisServer server) : super(server);
66 67
67 /** 68 /**
68 * Compute completion results for the given request and append them to the str eam. 69 * Compute completion results for the given request and append them to the str eam.
69 * Clients should not call this method directly as it is automatically called 70 * Clients should not call this method directly as it is automatically called
70 * when a client listens to the stream returned by [results]. 71 * when a client listens to the stream returned by [results].
71 * Subclasses should override this method, append at least one result 72 * Subclasses should override this method, append at least one result
72 * to the [controller], and close the controller stream once complete. 73 * to the [controller], and close the controller stream once complete.
73 */ 74 */
74 Future<CompletionResult> computeSuggestions( 75 Future<CompletionResult> computeSuggestions(CompletionRequestImpl request,
75 CompletionRequestImpl request) async { 76 CompletionGetSuggestionsParams params) async {
77 //
78 // Allow plugins to start computing fixes.
79 //
80 Map<PluginInfo, Future<plugin.Response>> pluginFutures;
81 plugin.CompletionGetSuggestionsParams requestParams;
82 if (server.options.enableNewAnalysisDriver) {
83 String file = params.file;
84 int offset = params.offset;
85 AnalysisDriver driver = server.getAnalysisDriver(file);
86 requestParams = new plugin.CompletionGetSuggestionsParams(file, offset);
87 pluginFutures = server.pluginManager
88 .broadcastRequest(requestParams, contextRoot: driver.contextRoot);
89 }
90 //
91 // Compute completions generated by server.
92 //
76 Iterable<CompletionContributor> newContributors = 93 Iterable<CompletionContributor> newContributors =
77 server.serverPlugin.completionContributors; 94 server.serverPlugin.completionContributors;
78 List<CompletionSuggestion> suggestions = <CompletionSuggestion>[]; 95 List<CompletionSuggestion> suggestions = <CompletionSuggestion>[];
79 96
80 const COMPUTE_SUGGESTIONS_TAG = 'computeSuggestions'; 97 const COMPUTE_SUGGESTIONS_TAG = 'computeSuggestions';
81 performance.logStartTime(COMPUTE_SUGGESTIONS_TAG); 98 performance.logStartTime(COMPUTE_SUGGESTIONS_TAG);
82 99
83 for (CompletionContributor contributor in newContributors) { 100 for (CompletionContributor contributor in newContributors) {
84 String contributorTag = 'computeSuggestions - ${contributor.runtimeType}'; 101 String contributorTag = 'computeSuggestions - ${contributor.runtimeType}';
85 performance.logStartTime(contributorTag); 102 performance.logStartTime(contributorTag);
86 try { 103 try {
87 suggestions.addAll(await contributor.computeSuggestions(request)); 104 suggestions.addAll(await contributor.computeSuggestions(request));
88 } on AbortCompletion { 105 } on AbortCompletion {
89 suggestions.clear(); 106 suggestions.clear();
90 break; 107 break;
91 } 108 }
92 performance.logElapseTime(contributorTag); 109 performance.logElapseTime(contributorTag);
93 } 110 }
94 111
95 performance.logElapseTime(COMPUTE_SUGGESTIONS_TAG); 112 performance.logElapseTime(COMPUTE_SUGGESTIONS_TAG);
96 113
97 // TODO (danrubel) if request is obsolete 114 // TODO (danrubel) if request is obsolete (processAnalysisRequest returns
98 // (processAnalysisRequest returns false) 115 // false) then send empty results
99 // then send empty results
100 116
117 //
118 // Add the fixes produced by plugins to the server-generated fixes.
119 //
120 if (pluginFutures != null) {
121 List<plugin.Response> responses = await waitForResponses(pluginFutures,
122 requestParameters: requestParams);
123 ResultConverter converter = new ResultConverter();
124 for (plugin.Response response in responses) {
125 plugin.CompletionGetSuggestionsResult result =
126 new plugin.CompletionGetSuggestionsResult.fromResponse(response);
127 suggestions
128 .addAll(result.results.map(converter.convertCompletionSuggestion));
129 }
130 }
131 //
132 // Return the result.
133 //
101 return new CompletionResult( 134 return new CompletionResult(
102 request.replacementOffset, request.replacementLength, suggestions); 135 request.replacementOffset, request.replacementLength, suggestions);
103 } 136 }
104 137
105 @override 138 @override
106 Response handleRequest(Request request) { 139 Response handleRequest(Request request) {
107 if (server.searchEngine == null) { 140 if (server.searchEngine == null) {
108 return new Response.noIndexGenerated(request); 141 return new Response.noIndexGenerated(request);
109 } 142 }
110 return runZoned(() { 143 return runZoned(() {
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 String completionId = (_nextCompletionId++).toString(); 223 String completionId = (_nextCompletionId++).toString();
191 224
192 _abortCurrentRequest(); 225 _abortCurrentRequest();
193 _currentRequest = completionRequest; 226 _currentRequest = completionRequest;
194 227
195 // initial response without results 228 // initial response without results
196 server.sendResponse(new CompletionGetSuggestionsResult(completionId) 229 server.sendResponse(new CompletionGetSuggestionsResult(completionId)
197 .toResponse(request.id)); 230 .toResponse(request.id));
198 231
199 // Compute suggestions in the background 232 // Compute suggestions in the background
200 computeSuggestions(completionRequest).then((CompletionResult result) { 233 computeSuggestions(completionRequest, params)
234 .then((CompletionResult result) {
201 const SEND_NOTIFICATION_TAG = 'send notification'; 235 const SEND_NOTIFICATION_TAG = 'send notification';
202 performance.logStartTime(SEND_NOTIFICATION_TAG); 236 performance.logStartTime(SEND_NOTIFICATION_TAG);
203 sendCompletionNotification(completionId, result.replacementOffset, 237 sendCompletionNotification(completionId, result.replacementOffset,
204 result.replacementLength, result.suggestions); 238 result.replacementLength, result.suggestions);
205 performance.logElapseTime(SEND_NOTIFICATION_TAG); 239 performance.logElapseTime(SEND_NOTIFICATION_TAG);
206 240
207 performance.notificationCount = 1; 241 performance.notificationCount = 1;
208 performance.logFirstNotificationComplete('notification 1 complete'); 242 performance.logFirstNotificationComplete('notification 1 complete');
209 performance.suggestionCountFirst = result.suggestions.length; 243 performance.suggestionCountFirst = result.suggestions.length;
210 performance.suggestionCountLast = result.suggestions.length; 244 performance.suggestionCountLast = result.suggestions.length;
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 final int replacementOffset; 312 final int replacementOffset;
279 313
280 /** 314 /**
281 * The suggested completions. 315 * The suggested completions.
282 */ 316 */
283 final List<CompletionSuggestion> suggestions; 317 final List<CompletionSuggestion> suggestions;
284 318
285 CompletionResult( 319 CompletionResult(
286 this.replacementOffset, this.replacementLength, this.suggestions); 320 this.replacementOffset, this.replacementLength, this.suggestions);
287 } 321 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analysis_server/test/domain_completion_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698