OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library services.completion.computer; | |
6 | |
7 import 'dart:async'; | |
8 | |
9 import 'package:analysis_services/completion/completion_suggestion.dart'; | |
10 import 'package:analysis_services/search/search_engine.dart'; | |
11 import 'package:analysis_services/src/completion/dart_completion_manager.dart'; | |
12 import 'package:analyzer/src/generated/engine.dart'; | |
13 import 'package:analyzer/src/generated/source.dart'; | |
14 | |
15 /** | |
16 * Manages `CompletionComputer`s for a given completion request. | |
17 */ | |
18 abstract class CompletionManager { | |
19 | |
20 StreamController<CompletionResult> controller; | |
21 | |
22 /** | |
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 | |
41 /** | |
42 * Create a manager for the given request. | |
43 */ | |
44 static CompletionManager create(AnalysisContext context, Source source, | |
45 int offset, SearchEngine searchEngine) { | |
46 if (context != null) { | |
47 if (AnalysisEngine.isDartFileName(source.shortName)) { | |
48 return new DartCompletionManager(context, searchEngine, source, offset); | |
49 } | |
50 if (AnalysisEngine.isHtmlFileName(source.shortName)) { | |
51 //TODO (danrubel) implement | |
52 // return new HtmlCompletionManager(context, searchEngine, source, offset
); | |
53 } | |
54 } | |
55 return new NoOpCompletionManager(source, offset); | |
56 } | |
57 } | |
58 | |
59 /** | |
60 * Code completion result generated by an [CompletionManager]. | |
61 */ | |
62 class CompletionResult { | |
63 | |
64 /** | |
65 * The length of the text to be replaced if the remainder of the identifier | |
66 * containing the cursor is to be replaced when the suggestion is applied | |
67 * (that is, the number of characters in the existing identifier). | |
68 */ | |
69 final int replacementLength; | |
70 | |
71 /** | |
72 * The offset of the start of the text to be replaced. This will be different | |
73 * than the offset used to request the completion suggestions if there was a | |
74 * portion of an identifier before the original offset. In particular, the | |
75 * replacementOffset will be the offset of the beginning of said identifier. | |
76 */ | |
77 final int replacementOffset; | |
78 | |
79 /** | |
80 * The suggested completions. | |
81 */ | |
82 final List<CompletionSuggestion> suggestions; | |
83 | |
84 /** | |
85 * `true` if this is that last set of results that will be returned | |
86 * for the indicated completion. | |
87 */ | |
88 final bool last; | |
89 | |
90 CompletionResult(this.replacementOffset, this.replacementLength, | |
91 this.suggestions, this.last); | |
92 } | |
93 | |
94 class NoOpCompletionManager extends CompletionManager { | |
95 final Source source; | |
96 final int offset; | |
97 | |
98 NoOpCompletionManager(this.source, this.offset); | |
99 | |
100 @override | |
101 void compute() { | |
102 controller.add(new CompletionResult(offset, 0, [], true)); | |
103 } | |
104 } | |
OLD | NEW |