| 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 test.services.completion.dart; | 5 library test.services.completion.dart; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_services/completion/completion_computer.dart'; | 9 import 'package:analysis_services/completion/completion_manager.dart'; |
| 10 import 'package:analysis_services/completion/completion_suggestion.dart'; | 10 import 'package:analysis_services/completion/completion_suggestion.dart'; |
| 11 import 'package:analysis_services/search/search_engine.dart'; | 11 import 'package:analysis_services/search/search_engine.dart'; |
| 12 import 'package:analysis_services/src/completion/imported_type_computer.dart'; | 12 import 'package:analysis_services/src/completion/imported_type_computer.dart'; |
| 13 import 'package:analysis_services/src/completion/invocation_computer.dart'; |
| 13 import 'package:analysis_services/src/completion/keyword_computer.dart'; | 14 import 'package:analysis_services/src/completion/keyword_computer.dart'; |
| 14 import 'package:analysis_services/src/completion/invocation_computer.dart'; | |
| 15 import 'package:analysis_services/src/completion/local_computer.dart'; | 15 import 'package:analysis_services/src/completion/local_computer.dart'; |
| 16 import 'package:analyzer/src/generated/ast.dart'; | 16 import 'package:analyzer/src/generated/ast.dart'; |
| 17 import 'package:analyzer/src/generated/element.dart'; | 17 import 'package:analyzer/src/generated/element.dart'; |
| 18 import 'package:analyzer/src/generated/engine.dart'; | 18 import 'package:analyzer/src/generated/engine.dart'; |
| 19 import 'package:analyzer/src/generated/source.dart'; | 19 import 'package:analyzer/src/generated/source.dart'; |
| 20 | 20 |
| 21 /** | 21 /** |
| 22 * The base class for computing code completion suggestions. |
| 23 */ |
| 24 abstract class DartCompletionComputer { |
| 25 /** |
| 26 * Computes the initial set of [CompletionSuggestion]s based on |
| 27 * the given completion context. The compilation unit and completion node |
| 28 * in the given completion context may not be resolved. |
| 29 * This method should execute quickly and not block waiting for any analysis. |
| 30 * Returns `true` if the computer's work is complete |
| 31 * or `false` if [computeFull] should be called to complete the work. |
| 32 */ |
| 33 bool computeFast(DartCompletionRequest request); |
| 34 |
| 35 /** |
| 36 * Computes the complete set of [CompletionSuggestion]s based on |
| 37 * the given completion context. The compilation unit and completion node |
| 38 * in the given completion context are resolved. |
| 39 * Returns `true` if the receiver modified the list of suggestions. |
| 40 */ |
| 41 Future<bool> computeFull(DartCompletionRequest request); |
| 42 } |
| 43 |
| 44 /** |
| 22 * Manages code completion for a given Dart file completion request. | 45 * Manages code completion for a given Dart file completion request. |
| 23 */ | 46 */ |
| 24 class DartCompletionManager extends CompletionManager { | 47 class DartCompletionManager extends CompletionManager { |
| 25 final AnalysisContext context; | 48 final AnalysisContext context; |
| 26 final Source source; | 49 final Source source; |
| 27 final int offset; | 50 final int offset; |
| 28 final SearchEngine searchEngine; | 51 DartCompletionRequest request; |
| 29 final List<CompletionSuggestion> suggestions = []; | 52 List<DartCompletionComputer> computers; |
| 30 List<CompletionComputer> computers; | |
| 31 | 53 |
| 32 DartCompletionManager(this.context, this.source, this.offset, | 54 DartCompletionManager(this.context, SearchEngine searchEngine, this.source, |
| 33 this.searchEngine); | 55 this.offset) { |
| 56 request = new DartCompletionRequest(context, searchEngine, source, offset); |
| 57 } |
| 34 | 58 |
| 35 @override | 59 @override |
| 36 void compute() { | 60 void compute() { |
| 37 initComputers(); | 61 initComputers(); |
| 38 computeFast(); | 62 computeFast(); |
| 39 if (!computers.isEmpty) { | 63 if (!computers.isEmpty) { |
| 40 computeFull(); | 64 computeFull(); |
| 41 } | 65 } |
| 42 } | 66 } |
| 43 | 67 |
| 44 /** | 68 /** |
| 45 * Compute suggestions based upon cached information only | 69 * Compute suggestions based upon cached information only |
| 46 * then send an initial response to the client. | 70 * then send an initial response to the client. |
| 47 */ | 71 */ |
| 48 void computeFast() { | 72 void computeFast() { |
| 49 CompilationUnit unit = context.parseCompilationUnit(source); | 73 CompilationUnit unit = context.parseCompilationUnit(source); |
| 50 AstNode node = new NodeLocator.con1(offset).searchWithin(unit); | 74 request.unit = unit; |
| 51 computers.removeWhere((c) => c.computeFast(unit, node, suggestions)); | 75 request.node = new NodeLocator.con1(offset).searchWithin(unit); |
| 76 computers.removeWhere((DartCompletionComputer c) => c.computeFast(request)); |
| 52 sendResults(computers.isEmpty); | 77 sendResults(computers.isEmpty); |
| 53 } | 78 } |
| 54 | 79 |
| 55 /** | 80 /** |
| 56 * If there is remaining work to be done, then wait for the unit to be | 81 * If there is remaining work to be done, then wait for the unit to be |
| 57 * resolved and request that each remaining computer finish their work. | 82 * resolved and request that each remaining computer finish their work. |
| 58 */ | 83 */ |
| 59 void computeFull() { | 84 void computeFull() { |
| 60 waitForAnalysis().then((CompilationUnit unit) { | 85 waitForAnalysis().then((CompilationUnit unit) { |
| 61 if (unit == null) { | 86 if (unit == null) { |
| 62 sendResults(true); | 87 sendResults(true); |
| 63 return; | 88 return; |
| 64 } | 89 } |
| 65 AstNode node = new NodeLocator.con1(offset).searchWithin(unit); | 90 request.unit = unit; |
| 91 request.node = new NodeLocator.con1(offset).searchWithin(unit); |
| 66 int count = computers.length; | 92 int count = computers.length; |
| 67 computers.forEach((c) { | 93 computers.forEach((c) { |
| 68 c.computeFull(unit, node, suggestions).then((bool changed) { | 94 c.computeFull(request).then((bool changed) { |
| 69 var last = --count == 0; | 95 var last = --count == 0; |
| 70 if (changed || last) { | 96 if (changed || last) { |
| 71 sendResults(last); | 97 sendResults(last); |
| 72 } | 98 } |
| 73 }); | 99 }); |
| 74 }); | 100 }); |
| 75 }); | 101 }); |
| 76 } | 102 } |
| 77 | 103 |
| 78 /** | 104 /** |
| 79 * Build and initialize the list of completion computers | 105 * Build and initialize the list of completion computers |
| 80 */ | 106 */ |
| 81 void initComputers() { | 107 void initComputers() { |
| 82 if (computers == null) { | 108 if (computers == null) { |
| 83 computers = [ | 109 computers = [ |
| 84 new KeywordComputer(), | 110 new KeywordComputer(), |
| 85 new LocalComputer(), | 111 new LocalComputer(), |
| 86 new ImportedTypeComputer(), | 112 new ImportedTypeComputer(), |
| 87 new InvocationComputer()]; | 113 new InvocationComputer()]; |
| 88 } | 114 } |
| 89 computers.forEach((CompletionComputer c) { | |
| 90 c.context = context; | |
| 91 c.source = source; | |
| 92 c.offset = offset; | |
| 93 c.searchEngine = searchEngine; | |
| 94 }); | |
| 95 } | 115 } |
| 96 | 116 |
| 97 /** | 117 /** |
| 98 * Send the current list of suggestions to the client. | 118 * Send the current list of suggestions to the client. |
| 99 */ | 119 */ |
| 100 void sendResults(bool last) { | 120 void sendResults(bool last) { |
| 101 controller.add(new CompletionResult(offset, 0, suggestions, last)); | 121 controller.add( |
| 122 new CompletionResult(request.offset, 0, request.suggestions, last)); |
| 102 if (last) { | 123 if (last) { |
| 103 controller.close(); | 124 controller.close(); |
| 104 } | 125 } |
| 105 } | 126 } |
| 106 | 127 |
| 107 /** | 128 /** |
| 108 * Wait for analysis to be complete and return the resolved unit | 129 * Return a future that completes when analysis is complete. |
| 109 * or `null` if the unit could not be resolved. | 130 * Return `true` if the compilation unit is be resolved. |
| 110 */ | 131 */ |
| 111 Future<CompilationUnit> waitForAnalysis() { | 132 Future<CompilationUnit> waitForAnalysis() { |
| 112 LibraryElement library = context.getLibraryElement(source); | 133 LibraryElement library = context.getLibraryElement(source); |
| 113 if (library != null) { | 134 if (library != null) { |
| 114 var unit = context.getResolvedCompilationUnit(source, library); | 135 CompilationUnit unit = |
| 136 context.getResolvedCompilationUnit(source, library); |
| 115 if (unit != null) { | 137 if (unit != null) { |
| 116 return new Future.value(unit); | 138 return new Future.value(unit); |
| 117 } | 139 } |
| 118 } | 140 } |
| 119 //TODO (danrubel) Determine if analysis is complete but unit not resolved | 141 //TODO (danrubel) Determine if analysis is complete but unit not resolved |
| 120 return new Future(waitForAnalysis); | 142 return new Future(waitForAnalysis); |
| 121 } | 143 } |
| 122 } | 144 } |
| 145 |
| 146 /** |
| 147 * The context in which the completion is requested. |
| 148 */ |
| 149 class DartCompletionRequest { |
| 150 /** |
| 151 * The analysis context in which the completion is requested. |
| 152 */ |
| 153 final AnalysisContext context; |
| 154 |
| 155 /** |
| 156 * The search engine for use when building suggestions. |
| 157 */ |
| 158 final SearchEngine searchEngine; |
| 159 |
| 160 /** |
| 161 * The source in which the completion is requested. |
| 162 */ |
| 163 final Source source; |
| 164 |
| 165 /** |
| 166 * The offset within the source at which the completion is requested. |
| 167 */ |
| 168 final int offset; |
| 169 |
| 170 /** |
| 171 * The compilation unit in which the completion was requested. This unit |
| 172 * may or may not be resolved when [DartCompletionComputer.computeFast] |
| 173 * is called but is resolved when [DartCompletionComputer.computeFull]. |
| 174 */ |
| 175 CompilationUnit unit; |
| 176 |
| 177 /** |
| 178 * The node in which the completion occurred. This node |
| 179 * may or may not be resolved when [DartCompletionComputer.computeFast] |
| 180 * is called but is resolved when [DartCompletionComputer.computeFull]. |
| 181 */ |
| 182 AstNode node; |
| 183 |
| 184 /** |
| 185 * The list of suggestions to be sent to the client. |
| 186 */ |
| 187 final List<CompletionSuggestion> suggestions = []; |
| 188 |
| 189 DartCompletionRequest(this.context, this.searchEngine, this.source, |
| 190 this.offset); |
| 191 } |
| OLD | NEW |