| 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_services/completion/completion_suggestion.dart'; | 9 import 'package:analysis_services/completion/completion_suggestion.dart'; |
| 10 import 'package:analysis_services/search/search_engine.dart'; | 10 import 'package:analysis_services/search/search_engine.dart'; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 */ | 22 */ |
| 23 Future<List<CompletionSuggestion>> compute(); | 23 Future<List<CompletionSuggestion>> compute(); |
| 24 } | 24 } |
| 25 | 25 |
| 26 /** | 26 /** |
| 27 * Manages `CompletionComputer`s for a given completion request. | 27 * Manages `CompletionComputer`s for a given completion request. |
| 28 */ | 28 */ |
| 29 abstract class CompletionManager { | 29 abstract class CompletionManager { |
| 30 | 30 |
| 31 /** | 31 /** |
| 32 * Create a manager for the given request. |
| 33 */ |
| 34 static CompletionManager create(Source source, int offset, |
| 35 SearchEngine searchEngine) { |
| 36 if (AnalysisEngine.isDartFileName(source.shortName)) { |
| 37 return new DartCompletionManager(source, offset, searchEngine); |
| 38 } |
| 39 return new NoOpCompletionManager(source, offset); |
| 40 } |
| 41 |
| 42 StreamController<CompletionResult> controller; |
| 43 |
| 44 /** |
| 45 * Generate a stream of code completion results. |
| 46 */ |
| 47 Stream<CompletionResult> results() { |
| 48 controller = new StreamController<CompletionResult>(onListen: () { |
| 49 scheduleMicrotask(compute); |
| 50 }); |
| 51 return controller.stream; |
| 52 } |
| 53 |
| 54 /** |
| 55 * Compute completion results and append them to the stream. |
| 56 */ |
| 57 void compute(); |
| 58 } |
| 59 |
| 60 /** |
| 61 * Code completion result generated by an [CompletionManager]. |
| 62 */ |
| 63 class CompletionResult { |
| 64 |
| 65 /** |
| 32 * The length of the text to be replaced if the remainder of the identifier | 66 * The length of the text to be replaced if the remainder of the identifier |
| 33 * containing the cursor is to be replaced when the suggestion is applied | 67 * containing the cursor is to be replaced when the suggestion is applied |
| 34 * (that is, the number of characters in the existing identifier). | 68 * (that is, the number of characters in the existing identifier). |
| 35 */ | 69 */ |
| 36 int get replacementLength; | 70 final int replacementLength; |
| 37 | 71 |
| 38 /** | 72 /** |
| 39 * The offset of the start of the text to be replaced. This will be different | 73 * The offset of the start of the text to be replaced. This will be different |
| 40 * than the offset used to request the completion suggestions if there was a | 74 * than the offset used to request the completion suggestions if there was a |
| 41 * portion of an identifier before the original offset. In particular, the | 75 * portion of an identifier before the original offset. In particular, the |
| 42 * replacementOffset will be the offset of the beginning of said identifier. | 76 * replacementOffset will be the offset of the beginning of said identifier. |
| 43 */ | 77 */ |
| 44 int get replacementOffset; | 78 final int replacementOffset; |
| 45 | 79 |
| 46 /** | 80 /** |
| 47 * Generate code completion computers for the given situation. | 81 * The suggested completions. |
| 48 */ | 82 */ |
| 49 Future<List<CompletionComputer>> generate(); | 83 final List<CompletionSuggestion> suggestions; |
| 50 | 84 |
| 51 /** | 85 /** |
| 52 * Create a manager for the given request. | 86 * `true` if this is that last set of results that will be returned |
| 87 * for the indicated completion. |
| 53 */ | 88 */ |
| 54 static CompletionManager create(Source source, int offset, | 89 final bool last; |
| 55 SearchEngine searchEngine) { | 90 |
| 56 if (AnalysisEngine.isDartFileName(source.shortName)) { | 91 CompletionResult(this.replacementOffset, this.replacementLength, |
| 57 return new DartCompletionManager(source, offset, searchEngine); | 92 this.suggestions, this.last); |
| 58 } | |
| 59 return new NoOpCompletionManager(source, offset); | |
| 60 } | |
| 61 } | 93 } |
| 62 | 94 |
| 63 /** | 95 /** |
| 64 * Manages code completion for a given Dart file completion request. | 96 * Manages code completion for a given Dart file completion request. |
| 65 */ | 97 */ |
| 66 class DartCompletionManager extends CompletionManager { | 98 class DartCompletionManager extends CompletionManager { |
| 67 final Source source; | 99 final Source source; |
| 68 final int offset; | 100 final int offset; |
| 69 final SearchEngine searchEngine; | 101 final SearchEngine searchEngine; |
| 70 | 102 |
| 71 DartCompletionManager(this.source, this.offset, this.searchEngine); | 103 DartCompletionManager(this.source, this.offset, this.searchEngine); |
| 72 | 104 |
| 73 @override | 105 @override |
| 74 int get replacementLength => 0; | 106 void compute() { |
| 75 | 107 var computer = new TopLevelComputer(searchEngine); |
| 76 @override | 108 computer.compute().then((List<CompletionSuggestion> suggestions) { |
| 77 int get replacementOffset => offset; | 109 controller.add(new CompletionResult(offset, 0, suggestions, true)); |
| 78 | 110 }); |
| 79 @override | |
| 80 Future<List<CompletionComputer>> generate() { | |
| 81 List<CompletionComputer> computers = []; | |
| 82 computers.add(new TopLevelComputer(searchEngine)); | |
| 83 return new Future.value(computers); | |
| 84 } | 111 } |
| 85 } | 112 } |
| 86 | 113 |
| 87 class NoOpCompletionComputer extends CompletionComputer { | |
| 88 @override | |
| 89 Future<List<CompletionSuggestion>> compute() { | |
| 90 return new Future.value([]); | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 class NoOpCompletionManager extends CompletionManager { | 114 class NoOpCompletionManager extends CompletionManager { |
| 95 final Source source; | 115 final Source source; |
| 96 final int offset; | 116 final int offset; |
| 97 | 117 |
| 98 NoOpCompletionManager(this.source, this.offset); | 118 NoOpCompletionManager(this.source, this.offset); |
| 99 | 119 |
| 100 @override | 120 @override |
| 101 int get replacementLength => 0; | 121 void compute() { |
| 102 | 122 controller.add(new CompletionResult(offset, 0, [], true)); |
| 103 @override | |
| 104 int get replacementOffset => offset; | |
| 105 | |
| 106 @override | |
| 107 Future<List<CompletionComputer>> generate() { | |
| 108 return new Future.value([new NoOpCompletionComputer()]); | |
| 109 } | 123 } |
| 110 } | 124 } |
| OLD | NEW |