| 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'; |
| 11 import 'package:analysis_server/src/services/search/search_engine.dart'; | 11 import 'package:analysis_server/src/services/search/search_engine.dart'; |
| 12 import 'package:analyzer/src/generated/engine.dart'; | 12 import 'package:analyzer/src/generated/engine.dart'; |
| 13 import 'package:analyzer/src/generated/source.dart'; | 13 import 'package:analyzer/src/generated/source.dart'; |
| 14 | 14 |
| 15 /** | 15 /** |
| 16 * [CompletionCache] contains information about the prior code completion |
| 17 * for use in the next code completion. |
| 18 */ |
| 19 abstract class CompletionCache { |
| 20 |
| 21 /** |
| 22 * The context in which the completion was computed. |
| 23 */ |
| 24 final AnalysisContext context; |
| 25 |
| 26 /** |
| 27 * The source in which the completion was computed. |
| 28 */ |
| 29 final Source source; |
| 30 |
| 31 CompletionCache(this.context, this.source); |
| 32 } |
| 33 |
| 34 /** |
| 16 * Manages `CompletionComputer`s for a given completion request. | 35 * Manages `CompletionComputer`s for a given completion request. |
| 17 */ | 36 */ |
| 18 abstract class CompletionManager { | 37 abstract class CompletionManager { |
| 19 | 38 |
| 39 /** |
| 40 * The controller used for returning completion results. |
| 41 */ |
| 20 StreamController<CompletionResult> controller; | 42 StreamController<CompletionResult> controller; |
| 21 | 43 |
| 22 /** | 44 CompletionManager(); |
| 23 * Compute completion results and append them to the stream. | |
| 24 * Subclasses should override this method, append at least one result | |
| 25 * to the [controller], and close the controller stream once complete. | |
| 26 * Clients should not call this method directly as it is automatically called | |
| 27 * when a client listens to the stream returned by [results]. | |
| 28 */ | |
| 29 void compute(); | |
| 30 | |
| 31 /** | |
| 32 * Generate a stream of code completion results. | |
| 33 */ | |
| 34 Stream<CompletionResult> results() { | |
| 35 controller = new StreamController<CompletionResult>(onListen: () { | |
| 36 scheduleMicrotask(compute); | |
| 37 }); | |
| 38 return controller.stream; | |
| 39 } | |
| 40 | 45 |
| 41 /** | 46 /** |
| 42 * Create a manager for the given request. | 47 * Create a manager for the given request. |
| 43 */ | 48 */ |
| 44 static CompletionManager create(AnalysisContext context, Source source, | 49 factory CompletionManager.create(AnalysisContext context, Source source, |
| 45 int offset, SearchEngine searchEngine, CompletionPerformance performance)
{ | 50 int offset, SearchEngine searchEngine, CompletionCache cache, |
| 51 CompletionPerformance performance) { |
| 46 if (context != null) { | 52 if (context != null) { |
| 47 if (AnalysisEngine.isDartFileName(source.shortName)) { | 53 if (AnalysisEngine.isDartFileName(source.shortName)) { |
| 48 return new DartCompletionManager( | 54 return new DartCompletionManager( |
| 49 context, | 55 context, |
| 50 searchEngine, | 56 searchEngine, |
| 51 source, | 57 source, |
| 52 offset, | 58 offset, |
| 53 performance); | 59 performance); |
| 54 } | 60 } |
| 55 if (AnalysisEngine.isHtmlFileName(source.shortName)) { | 61 if (AnalysisEngine.isHtmlFileName(source.shortName)) { |
| 56 //TODO (danrubel) implement | 62 //TODO (danrubel) implement |
| 57 // return new HtmlCompletionManager(context, searchEngine, source, offset
); | 63 // return new HtmlCompletionManager(context, searchEngine, source, offset
); |
| 58 } | 64 } |
| 59 } | 65 } |
| 60 return new NoOpCompletionManager(source, offset); | 66 return new NoOpCompletionManager(source, offset); |
| 61 } | 67 } |
| 68 |
| 69 /** |
| 70 * Return cached information from the current completion operation |
| 71 * if there is any. Subclasses may override this method. |
| 72 */ |
| 73 CompletionCache get completionCache => null; |
| 74 |
| 75 /** |
| 76 * Compute completion results and append them to the stream. |
| 77 * Clients should not call this method directly as it is automatically called |
| 78 * when a client listens to the stream returned by [results]. |
| 79 * Subclasses should override this method, append at least one result |
| 80 * to the [controller], and close the controller stream once complete. |
| 81 */ |
| 82 void compute(); |
| 83 |
| 84 /** |
| 85 * Generate a stream of code completion results. |
| 86 */ |
| 87 Stream<CompletionResult> results() { |
| 88 controller = new StreamController<CompletionResult>(onListen: () { |
| 89 scheduleMicrotask(compute); |
| 90 }); |
| 91 return controller.stream; |
| 92 } |
| 62 } | 93 } |
| 63 | 94 |
| 64 /** | 95 /** |
| 65 * Overall performance of a code completion operation. | 96 * Overall performance of a code completion operation. |
| 66 */ | 97 */ |
| 67 class CompletionPerformance { | 98 class CompletionPerformance { |
| 68 final Map<String, Duration> _startTimes = new Map<String, Duration>(); | 99 final Map<String, Duration> _startTimes = new Map<String, Duration>(); |
| 69 final Stopwatch _stopwatch = new Stopwatch(); | 100 final Stopwatch _stopwatch = new Stopwatch(); |
| 70 final List<OperationPerformance> operations = []; | 101 final List<OperationPerformance> operations = <OperationPerformance>[]; |
| 71 | 102 |
| 72 CompletionPerformance() { | 103 CompletionPerformance() { |
| 73 _stopwatch.start(); | 104 _stopwatch.start(); |
| 74 } | 105 } |
| 75 | 106 |
| 76 void complete() { | 107 void complete() { |
| 77 _stopwatch.stop(); | 108 _stopwatch.stop(); |
| 78 _logDuration('total time', _stopwatch.elapsed); | 109 _logDuration('total time', _stopwatch.elapsed); |
| 79 } | 110 } |
| 80 | 111 |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 */ | 194 */ |
| 164 final String name; | 195 final String name; |
| 165 | 196 |
| 166 /** | 197 /** |
| 167 * The elapse time or `null` if undefined. | 198 * The elapse time or `null` if undefined. |
| 168 */ | 199 */ |
| 169 final Duration elapsed; | 200 final Duration elapsed; |
| 170 | 201 |
| 171 OperationPerformance(this.name, this.elapsed); | 202 OperationPerformance(this.name, this.elapsed); |
| 172 } | 203 } |
| OLD | NEW |