| 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_server/src/protocol.dart'; | 9 import 'package:analysis_server/src/protocol.dart'; |
| 10 import 'package:analysis_server/src/services/completion/completion_manager.dart'
; | 10 import 'package:analysis_server/src/services/completion/completion_manager.dart'
; |
| 11 import 'package:analysis_server/src/services/search/search_engine.dart'; | |
| 12 import 'package:analysis_server/src/services/completion/imported_type_computer.d
art'; | 11 import 'package:analysis_server/src/services/completion/imported_type_computer.d
art'; |
| 13 import 'package:analysis_server/src/services/completion/invocation_computer.dart
'; | 12 import 'package:analysis_server/src/services/completion/invocation_computer.dart
'; |
| 14 import 'package:analysis_server/src/services/completion/keyword_computer.dart'; | 13 import 'package:analysis_server/src/services/completion/keyword_computer.dart'; |
| 15 import 'package:analysis_server/src/services/completion/local_computer.dart'; | 14 import 'package:analysis_server/src/services/completion/local_computer.dart'; |
| 15 import 'package:analysis_server/src/services/search/search_engine.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. | 22 * The base class for computing code completion suggestions. |
| 23 */ | 23 */ |
| 24 abstract class DartCompletionComputer { | 24 abstract class DartCompletionComputer { |
| 25 /** | 25 /** |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 } | 66 } |
| 67 | 67 |
| 68 /** | 68 /** |
| 69 * Compute suggestions based upon cached information only | 69 * Compute suggestions based upon cached information only |
| 70 * then send an initial response to the client. | 70 * then send an initial response to the client. |
| 71 */ | 71 */ |
| 72 void computeFast() { | 72 void computeFast() { |
| 73 CompilationUnit unit = context.parseCompilationUnit(source); | 73 CompilationUnit unit = context.parseCompilationUnit(source); |
| 74 request.unit = unit; | 74 request.unit = unit; |
| 75 request.node = new NodeLocator.con1(offset).searchWithin(unit); | 75 request.node = new NodeLocator.con1(offset).searchWithin(unit); |
| 76 if (request.node != null) { |
| 77 request.node.accept(new _ReplacementOffsetBuilder(request)); |
| 78 } |
| 76 computers.removeWhere((DartCompletionComputer c) => c.computeFast(request)); | 79 computers.removeWhere((DartCompletionComputer c) => c.computeFast(request)); |
| 77 sendResults(computers.isEmpty); | 80 sendResults(computers.isEmpty); |
| 78 } | 81 } |
| 79 | 82 |
| 80 /** | 83 /** |
| 81 * If there is remaining work to be done, then wait for the unit to be | 84 * If there is remaining work to be done, then wait for the unit to be |
| 82 * resolved and request that each remaining computer finish their work. | 85 * resolved and request that each remaining computer finish their work. |
| 83 */ | 86 */ |
| 84 void computeFull() { | 87 void computeFull() { |
| 85 waitForAnalysis().then((CompilationUnit unit) { | 88 waitForAnalysis().then((CompilationUnit unit) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 112 new ImportedTypeComputer(), | 115 new ImportedTypeComputer(), |
| 113 new InvocationComputer()]; | 116 new InvocationComputer()]; |
| 114 } | 117 } |
| 115 } | 118 } |
| 116 | 119 |
| 117 /** | 120 /** |
| 118 * Send the current list of suggestions to the client. | 121 * Send the current list of suggestions to the client. |
| 119 */ | 122 */ |
| 120 void sendResults(bool last) { | 123 void sendResults(bool last) { |
| 121 controller.add( | 124 controller.add( |
| 122 new CompletionResult(request.offset, 0, request.suggestions, last)); | 125 new CompletionResult( |
| 126 request.replacementOffset, |
| 127 request.replacementLength, |
| 128 request.suggestions, |
| 129 last)); |
| 123 if (last) { | 130 if (last) { |
| 124 controller.close(); | 131 controller.close(); |
| 125 } | 132 } |
| 126 } | 133 } |
| 127 | 134 |
| 128 /** | 135 /** |
| 129 * Return a future that completes when analysis is complete. | 136 * Return a future that completes when analysis is complete. |
| 130 * Return `true` if the compilation unit is be resolved. | 137 * Return `true` if the compilation unit is be resolved. |
| 131 */ | 138 */ |
| 132 Future<CompilationUnit> waitForAnalysis() { | 139 Future<CompilationUnit> waitForAnalysis() { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 CompilationUnit unit; | 182 CompilationUnit unit; |
| 176 | 183 |
| 177 /** | 184 /** |
| 178 * The node in which the completion occurred. This node | 185 * The node in which the completion occurred. This node |
| 179 * may or may not be resolved when [DartCompletionComputer.computeFast] | 186 * may or may not be resolved when [DartCompletionComputer.computeFast] |
| 180 * is called but is resolved when [DartCompletionComputer.computeFull]. | 187 * is called but is resolved when [DartCompletionComputer.computeFull]. |
| 181 */ | 188 */ |
| 182 AstNode node; | 189 AstNode node; |
| 183 | 190 |
| 184 /** | 191 /** |
| 192 * The offset of the start of the text to be replaced. |
| 193 * This will be different than the offset used to request the completion |
| 194 * suggestions if there was a portion of an identifier before the original |
| 195 * offset. In particular, the replacementOffset will be the offset of the |
| 196 * beginning of said identifier. |
| 197 */ |
| 198 int replacementOffset; |
| 199 |
| 200 /** |
| 201 * The length of the text to be replaced if the remainder of the identifier |
| 202 * containing the cursor is to be replaced when the suggestion is applied |
| 203 * (that is, the number of characters in the existing identifier). |
| 204 */ |
| 205 int replacementLength; |
| 206 |
| 207 /** |
| 185 * The list of suggestions to be sent to the client. | 208 * The list of suggestions to be sent to the client. |
| 186 */ | 209 */ |
| 187 final List<CompletionSuggestion> suggestions = []; | 210 final List<CompletionSuggestion> suggestions = []; |
| 188 | 211 |
| 189 DartCompletionRequest(this.context, this.searchEngine, this.source, | 212 DartCompletionRequest(this.context, this.searchEngine, this.source, |
| 190 this.offset); | 213 this.offset); |
| 191 } | 214 } |
| 215 |
| 216 /** |
| 217 * Visitor used to determine the replacement offset and length |
| 218 * based upon the cursor location. |
| 219 */ |
| 220 class _ReplacementOffsetBuilder extends SimpleAstVisitor { |
| 221 final DartCompletionRequest request; |
| 222 |
| 223 _ReplacementOffsetBuilder(this.request) { |
| 224 request.replacementOffset = request.offset; |
| 225 request.replacementLength = 0; |
| 226 } |
| 227 |
| 228 visitSimpleIdentifier(SimpleIdentifier node) { |
| 229 request.replacementOffset = node.offset; |
| 230 request.replacementLength = node.length; |
| 231 } |
| 232 } |
| OLD | NEW |