| 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 * Clients should not call this method directly as it is automatically called |
| 57 * when a client listens to the stream returned by [results]. |
| 58 */ |
| 59 void compute(); |
| 60 } |
| 61 |
| 62 /** |
| 63 * Code completion result generated by an [CompletionManager]. |
| 64 */ |
| 65 class CompletionResult { |
| 66 |
| 67 /** |
| 32 * The length of the text to be replaced if the remainder of the identifier | 68 * 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 | 69 * containing the cursor is to be replaced when the suggestion is applied |
| 34 * (that is, the number of characters in the existing identifier). | 70 * (that is, the number of characters in the existing identifier). |
| 35 */ | 71 */ |
| 36 int get replacementLength; | 72 final int replacementLength; |
| 37 | 73 |
| 38 /** | 74 /** |
| 39 * The offset of the start of the text to be replaced. This will be different | 75 * 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 | 76 * 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 | 77 * portion of an identifier before the original offset. In particular, the |
| 42 * replacementOffset will be the offset of the beginning of said identifier. | 78 * replacementOffset will be the offset of the beginning of said identifier. |
| 43 */ | 79 */ |
| 44 int get replacementOffset; | 80 final int replacementOffset; |
| 45 | 81 |
| 46 /** | 82 /** |
| 47 * Generate code completion computers for the given situation. | 83 * The suggested completions. |
| 48 */ | 84 */ |
| 49 Future<List<CompletionComputer>> generate(); | 85 final List<CompletionSuggestion> suggestions; |
| 50 | 86 |
| 51 /** | 87 /** |
| 52 * Create a manager for the given request. | 88 * `true` if this is that last set of results that will be returned |
| 89 * for the indicated completion. |
| 53 */ | 90 */ |
| 54 static CompletionManager create(Source source, int offset, | 91 final bool last; |
| 55 SearchEngine searchEngine) { | 92 |
| 56 if (AnalysisEngine.isDartFileName(source.shortName)) { | 93 CompletionResult(this.replacementOffset, this.replacementLength, |
| 57 return new DartCompletionManager(source, offset, searchEngine); | 94 this.suggestions, this.last); |
| 58 } | |
| 59 return new NoOpCompletionManager(source, offset); | |
| 60 } | |
| 61 } | 95 } |
| 62 | 96 |
| 63 /** | 97 /** |
| 64 * Manages code completion for a given Dart file completion request. | 98 * Manages code completion for a given Dart file completion request. |
| 65 */ | 99 */ |
| 66 class DartCompletionManager extends CompletionManager { | 100 class DartCompletionManager extends CompletionManager { |
| 67 final Source source; | 101 final Source source; |
| 68 final int offset; | 102 final int offset; |
| 69 final SearchEngine searchEngine; | 103 final SearchEngine searchEngine; |
| 70 | 104 |
| 71 DartCompletionManager(this.source, this.offset, this.searchEngine); | 105 DartCompletionManager(this.source, this.offset, this.searchEngine); |
| 72 | 106 |
| 73 @override | 107 @override |
| 74 int get replacementLength => 0; | 108 void compute() { |
| 75 | 109 var computer = new TopLevelComputer(searchEngine); |
| 76 @override | 110 computer.compute().then((List<CompletionSuggestion> suggestions) { |
| 77 int get replacementOffset => offset; | 111 controller.add(new CompletionResult(offset, 0, suggestions, true)); |
| 78 | 112 }); |
| 79 @override | |
| 80 Future<List<CompletionComputer>> generate() { | |
| 81 List<CompletionComputer> computers = []; | |
| 82 computers.add(new TopLevelComputer(searchEngine)); | |
| 83 return new Future.value(computers); | |
| 84 } | 113 } |
| 85 } | 114 } |
| 86 | 115 |
| 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 { | 116 class NoOpCompletionManager extends CompletionManager { |
| 95 final Source source; | 117 final Source source; |
| 96 final int offset; | 118 final int offset; |
| 97 | 119 |
| 98 NoOpCompletionManager(this.source, this.offset); | 120 NoOpCompletionManager(this.source, this.offset); |
| 99 | 121 |
| 100 @override | 122 @override |
| 101 int get replacementLength => 0; | 123 void compute() { |
| 102 | 124 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 } | 125 } |
| 110 } | 126 } |
| OLD | NEW |