| 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.computer; | 5 library services.completion.computer; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/src/protocol.dart'; | 9 import 'package:analysis_server/src/protocol.dart'; |
| 10 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; | 10 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 | 30 |
| 31 CompletionCache(this.context, this.source); | 31 CompletionCache(this.context, this.source); |
| 32 } | 32 } |
| 33 | 33 |
| 34 /** | 34 /** |
| 35 * Manages `CompletionComputer`s for a given completion request. | 35 * Manages `CompletionComputer`s for a given completion request. |
| 36 */ | 36 */ |
| 37 abstract class CompletionManager { | 37 abstract class CompletionManager { |
| 38 | 38 |
| 39 /** | 39 /** |
| 40 * The context in which the completion was computed. |
| 41 */ |
| 42 final AnalysisContext context; |
| 43 |
| 44 /** |
| 45 * The source in which the completion was computed. |
| 46 */ |
| 47 final Source source; |
| 48 |
| 49 /** |
| 40 * The controller used for returning completion results. | 50 * The controller used for returning completion results. |
| 41 */ | 51 */ |
| 42 StreamController<CompletionResult> controller; | 52 StreamController<CompletionResult> controller; |
| 43 | 53 |
| 44 CompletionManager(); | 54 CompletionManager(this.context, this.source); |
| 45 | 55 |
| 46 /** | 56 /** |
| 47 * Create a manager for the given request. | 57 * Create a manager for the given request. |
| 48 */ | 58 */ |
| 49 factory CompletionManager.create(AnalysisContext context, Source source, | 59 factory CompletionManager.create(AnalysisContext context, Source source, |
| 50 int offset, SearchEngine searchEngine, CompletionCache cache, | 60 SearchEngine searchEngine, CompletionCache cache) { |
| 51 CompletionPerformance performance) { | |
| 52 if (context != null) { | 61 if (context != null) { |
| 53 if (AnalysisEngine.isDartFileName(source.shortName)) { | 62 if (AnalysisEngine.isDartFileName(source.shortName)) { |
| 54 return new DartCompletionManager.create( | 63 return new DartCompletionManager.create( |
| 55 context, | 64 context, |
| 56 searchEngine, | 65 searchEngine, |
| 57 source, | 66 source, |
| 58 offset, | 67 cache); |
| 59 cache, | |
| 60 performance); | |
| 61 } | 68 } |
| 62 if (AnalysisEngine.isHtmlFileName(source.shortName)) { | 69 if (AnalysisEngine.isHtmlFileName(source.shortName)) { |
| 63 //TODO (danrubel) implement | 70 //TODO (danrubel) implement |
| 64 // return new HtmlCompletionManager(context, searchEngine, source, offset
); | 71 // return new HtmlCompletionManager(context, searchEngine, source, offset
); |
| 65 } | 72 } |
| 66 } | 73 } |
| 67 return new NoOpCompletionManager(source, offset); | 74 return new NoOpCompletionManager(source); |
| 68 } | 75 } |
| 69 | 76 |
| 70 /** | 77 /** |
| 71 * Return cached information from the current completion operation | 78 * Compute completion results for the given reqeust and append them to the str
eam. |
| 72 * if there is any. Subclasses may override this method. | |
| 73 */ | |
| 74 CompletionCache get completionCache => null; | |
| 75 | |
| 76 /** | |
| 77 * Compute completion results and append them to the stream. | |
| 78 * Clients should not call this method directly as it is automatically called | 79 * Clients should not call this method directly as it is automatically called |
| 79 * when a client listens to the stream returned by [results]. | 80 * when a client listens to the stream returned by [results]. |
| 80 * Subclasses should override this method, append at least one result | 81 * Subclasses should override this method, append at least one result |
| 81 * to the [controller], and close the controller stream once complete. | 82 * to the [controller], and close the controller stream once complete. |
| 82 */ | 83 */ |
| 83 void compute(); | 84 void compute(CompletionRequest request); |
| 84 | 85 |
| 85 /** | 86 /** |
| 86 * Generate a stream of code completion results. | 87 * Generate a stream of code completion results. |
| 87 */ | 88 */ |
| 88 Stream<CompletionResult> results() { | 89 Stream<CompletionResult> results(CompletionRequest request) { |
| 89 controller = new StreamController<CompletionResult>(onListen: () { | 90 controller = new StreamController<CompletionResult>(onListen: () { |
| 90 scheduleMicrotask(compute); | 91 scheduleMicrotask(() { |
| 92 compute(request); |
| 93 }); |
| 91 }); | 94 }); |
| 92 return controller.stream; | 95 return controller.stream; |
| 93 } | 96 } |
| 94 } | 97 } |
| 95 | 98 |
| 96 /** | 99 /** |
| 97 * Overall performance of a code completion operation. | 100 * Overall performance of a code completion operation. |
| 98 */ | 101 */ |
| 99 class CompletionPerformance { | 102 class CompletionPerformance { |
| 100 final Map<String, Duration> _startTimes = new Map<String, Duration>(); | 103 final Map<String, Duration> _startTimes = new Map<String, Duration>(); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 void logStartTime(String tag) { | 135 void logStartTime(String tag) { |
| 133 _startTimes[tag] = _stopwatch.elapsed; | 136 _startTimes[tag] = _stopwatch.elapsed; |
| 134 } | 137 } |
| 135 | 138 |
| 136 void _logDuration(String tag, Duration elapsed) { | 139 void _logDuration(String tag, Duration elapsed) { |
| 137 operations.add(new OperationPerformance(tag, elapsed)); | 140 operations.add(new OperationPerformance(tag, elapsed)); |
| 138 } | 141 } |
| 139 } | 142 } |
| 140 | 143 |
| 141 /** | 144 /** |
| 145 * Encapsulates information specific to a particular completion request. |
| 146 */ |
| 147 class CompletionRequest { |
| 148 /** |
| 149 * The offset within the source at which the completion is requested. |
| 150 */ |
| 151 final int offset; |
| 152 |
| 153 /** |
| 154 * Performance measurements for this particular request. |
| 155 */ |
| 156 final CompletionPerformance performance; |
| 157 |
| 158 CompletionRequest(this.offset, this.performance); |
| 159 } |
| 160 |
| 161 /** |
| 142 * Code completion result generated by an [CompletionManager]. | 162 * Code completion result generated by an [CompletionManager]. |
| 143 */ | 163 */ |
| 144 class CompletionResult { | 164 class CompletionResult { |
| 145 | 165 |
| 146 /** | 166 /** |
| 147 * The length of the text to be replaced if the remainder of the identifier | 167 * The length of the text to be replaced if the remainder of the identifier |
| 148 * containing the cursor is to be replaced when the suggestion is applied | 168 * containing the cursor is to be replaced when the suggestion is applied |
| 149 * (that is, the number of characters in the existing identifier). | 169 * (that is, the number of characters in the existing identifier). |
| 150 */ | 170 */ |
| 151 final int replacementLength; | 171 final int replacementLength; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 167 * `true` if this is that last set of results that will be returned | 187 * `true` if this is that last set of results that will be returned |
| 168 * for the indicated completion. | 188 * for the indicated completion. |
| 169 */ | 189 */ |
| 170 final bool last; | 190 final bool last; |
| 171 | 191 |
| 172 CompletionResult(this.replacementOffset, this.replacementLength, | 192 CompletionResult(this.replacementOffset, this.replacementLength, |
| 173 this.suggestions, this.last); | 193 this.suggestions, this.last); |
| 174 } | 194 } |
| 175 | 195 |
| 176 class NoOpCompletionManager extends CompletionManager { | 196 class NoOpCompletionManager extends CompletionManager { |
| 177 final Source source; | |
| 178 final int offset; | |
| 179 | 197 |
| 180 NoOpCompletionManager(this.source, this.offset); | 198 NoOpCompletionManager(Source source) : super(null, source); |
| 181 | 199 |
| 182 @override | 200 @override |
| 183 void compute() { | 201 void compute(CompletionRequest request) { |
| 184 controller.add(new CompletionResult(offset, 0, [], true)); | 202 controller.add(new CompletionResult(request.offset, 0, [], true)); |
| 185 } | 203 } |
| 186 } | 204 } |
| 187 | 205 |
| 188 /** | 206 /** |
| 189 * The performance of an operation when computing code completion. | 207 * The performance of an operation when computing code completion. |
| 190 */ | 208 */ |
| 191 class OperationPerformance { | 209 class OperationPerformance { |
| 192 | 210 |
| 193 /** | 211 /** |
| 194 * The name of the operation | 212 * The name of the operation |
| 195 */ | 213 */ |
| 196 final String name; | 214 final String name; |
| 197 | 215 |
| 198 /** | 216 /** |
| 199 * The elapse time or `null` if undefined. | 217 * The elapse time or `null` if undefined. |
| 200 */ | 218 */ |
| 201 final Duration elapsed; | 219 final Duration elapsed; |
| 202 | 220 |
| 203 OperationPerformance(this.name, this.elapsed); | 221 OperationPerformance(this.name, this.elapsed); |
| 204 } | 222 } |
| OLD | NEW |