| 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.manager; | 5 library services.completion.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, CompletionContributorFactory, CompletionRequest,
CompletionResult; | 11 show |
| 12 CompletionContributor, |
| 13 CompletionContributorFactory, |
| 14 CompletionRequest, |
| 15 CompletionResult; |
| 12 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; | 16 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; |
| 13 import 'package:analysis_server/src/services/search/search_engine.dart'; | 17 import 'package:analysis_server/src/services/search/search_engine.dart'; |
| 14 import 'package:analyzer/src/generated/engine.dart'; | 18 import 'package:analyzer/src/generated/engine.dart'; |
| 15 import 'package:analyzer/src/generated/source.dart'; | 19 import 'package:analyzer/src/generated/source.dart'; |
| 16 | 20 |
| 17 /** | 21 /** |
| 18 * Manages completion contributors for a given completion request. | 22 * Manages completion contributors for a given completion request. |
| 19 */ | 23 */ |
| 20 abstract class CompletionManager { | 24 abstract class CompletionManager { |
| 21 /** | 25 /** |
| 22 * The context in which the completion was computed. | 26 * The context in which the completion was computed. |
| 23 */ | 27 */ |
| 24 final AnalysisContext context; | 28 final AnalysisContext context; |
| 25 | 29 |
| 26 /** | 30 /** |
| 27 * The source in which the completion was computed. | 31 * The source in which the completion was computed. |
| 28 */ | 32 */ |
| 29 final Source source; | 33 final Source source; |
| 30 | 34 |
| 31 /** | |
| 32 * The controller used for returning completion results. | |
| 33 */ | |
| 34 StreamController<CompletionResult> controller; | |
| 35 | |
| 36 CompletionManager(this.context, this.source); | 35 CompletionManager(this.context, this.source); |
| 37 | 36 |
| 38 /** | 37 /** |
| 39 * Create a manager for the given request. | 38 * Create a manager for the given request. |
| 40 */ | 39 */ |
| 41 factory CompletionManager.create( | 40 factory CompletionManager.create( |
| 42 AnalysisContext context, | 41 AnalysisContext context, |
| 43 Source source, | 42 Source source, |
| 44 SearchEngine searchEngine, | 43 SearchEngine searchEngine, |
| 45 Iterable<CompletionContributor> newContributors) { | 44 Iterable<CompletionContributor> newContributors) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 64 return new Future.value(true); | 63 return new Future.value(true); |
| 65 } | 64 } |
| 66 | 65 |
| 67 /** | 66 /** |
| 68 * Compute completion results for the given reqeust and append them to the str
eam. | 67 * Compute completion results for the given reqeust and append them to the str
eam. |
| 69 * Clients should not call this method directly as it is automatically called | 68 * Clients should not call this method directly as it is automatically called |
| 70 * when a client listens to the stream returned by [results]. | 69 * when a client listens to the stream returned by [results]. |
| 71 * Subclasses should override this method, append at least one result | 70 * Subclasses should override this method, append at least one result |
| 72 * to the [controller], and close the controller stream once complete. | 71 * to the [controller], and close the controller stream once complete. |
| 73 */ | 72 */ |
| 74 void computeSuggestions(CompletionRequest request); | 73 Future<CompletionResult> computeSuggestions(CompletionRequest request); |
| 75 | 74 |
| 76 /** | 75 /** |
| 77 * Discard any pending operations. | 76 * Discard any pending operations. |
| 78 * Subclasses may override but should call super.dispose | 77 * Subclasses may override but should call super.dispose |
| 79 */ | 78 */ |
| 80 void dispose() {} | 79 void dispose() {} |
| 81 | |
| 82 /** | |
| 83 * Generate a stream of code completion results. | |
| 84 */ | |
| 85 Stream<CompletionResult> results(CompletionRequest request) { | |
| 86 controller = new StreamController<CompletionResult>(onListen: () { | |
| 87 scheduleMicrotask(() { | |
| 88 computeSuggestions(request); | |
| 89 }); | |
| 90 }); | |
| 91 return controller.stream; | |
| 92 } | |
| 93 } | 80 } |
| 94 | 81 |
| 95 /** | 82 /** |
| 96 * Overall performance of a code completion operation. | 83 * Overall performance of a code completion operation. |
| 97 */ | 84 */ |
| 98 class CompletionPerformance { | 85 class CompletionPerformance { |
| 99 final DateTime start = new DateTime.now(); | 86 final DateTime start = new DateTime.now(); |
| 100 final Map<String, Duration> _startTimes = new Map<String, Duration>(); | 87 final Map<String, Duration> _startTimes = new Map<String, Duration>(); |
| 101 final Stopwatch _stopwatch = new Stopwatch(); | 88 final Stopwatch _stopwatch = new Stopwatch(); |
| 102 final List<OperationPerformance> operations = <OperationPerformance>[]; | 89 final List<OperationPerformance> operations = <OperationPerformance>[]; |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 * portion of an identifier before the original offset. In particular, the | 200 * portion of an identifier before the original offset. In particular, the |
| 214 * replacementOffset will be the offset of the beginning of said identifier. | 201 * replacementOffset will be the offset of the beginning of said identifier. |
| 215 */ | 202 */ |
| 216 final int replacementOffset; | 203 final int replacementOffset; |
| 217 | 204 |
| 218 /** | 205 /** |
| 219 * The suggested completions. | 206 * The suggested completions. |
| 220 */ | 207 */ |
| 221 final List<CompletionSuggestion> suggestions; | 208 final List<CompletionSuggestion> suggestions; |
| 222 | 209 |
| 223 /** | 210 CompletionResultImpl( |
| 224 * `true` if this is that last set of results that will be returned | 211 this.replacementOffset, this.replacementLength, this.suggestions); |
| 225 * for the indicated completion. | |
| 226 */ | |
| 227 final bool last; | |
| 228 | |
| 229 CompletionResultImpl(this.replacementOffset, this.replacementLength, | |
| 230 this.suggestions, this.last); | |
| 231 | |
| 232 /** | |
| 233 * Return `true` if this is the last completion result that will be produced. | |
| 234 */ | |
| 235 bool get isLast => last; | |
| 236 } | 212 } |
| 237 | 213 |
| 238 class NoOpCompletionManager extends CompletionManager { | 214 class NoOpCompletionManager extends CompletionManager { |
| 239 NoOpCompletionManager(Source source) : super(null, source); | 215 NoOpCompletionManager(Source source) : super(null, source); |
| 240 | 216 |
| 241 @override | 217 @override |
| 242 void computeSuggestions(CompletionRequest request) { | 218 Future<CompletionResult> computeSuggestions(CompletionRequest request) async { |
| 243 controller.add(new CompletionResultImpl(request.offset, 0, [], true)); | 219 return new CompletionResultImpl(request.offset, 0, []); |
| 244 } | 220 } |
| 245 } | 221 } |
| 246 | 222 |
| 247 /** | 223 /** |
| 248 * The performance of an operation when computing code completion. | 224 * The performance of an operation when computing code completion. |
| 249 */ | 225 */ |
| 250 class OperationPerformance { | 226 class OperationPerformance { |
| 251 /** | 227 /** |
| 252 * The name of the operation | 228 * The name of the operation |
| 253 */ | 229 */ |
| 254 final String name; | 230 final String name; |
| 255 | 231 |
| 256 /** | 232 /** |
| 257 * The elapse time or `null` if undefined. | 233 * The elapse time or `null` if undefined. |
| 258 */ | 234 */ |
| 259 final Duration elapsed; | 235 final Duration elapsed; |
| 260 | 236 |
| 261 OperationPerformance(this.name, this.elapsed); | 237 OperationPerformance(this.name, this.elapsed); |
| 262 } | 238 } |
| OLD | NEW |