| 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.suggestion.builder; | 5 library services.completion.suggestion.builder; |
| 6 | 6 |
| 7 import 'dart:async'; |
| 8 |
| 7 import 'package:analysis_server/src/protocol_server.dart' as protocol; | 9 import 'package:analysis_server/src/protocol_server.dart' as protocol; |
| 8 import 'package:analysis_server/src/protocol_server.dart' hide Element, | 10 import 'package:analysis_server/src/protocol_server.dart' hide Element, |
| 9 ElementKind; | 11 ElementKind; |
| 10 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; | 12 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; |
| 11 import 'package:analyzer/src/generated/ast.dart'; | 13 import 'package:analyzer/src/generated/ast.dart'; |
| 12 import 'package:analyzer/src/generated/element.dart'; | 14 import 'package:analyzer/src/generated/element.dart'; |
| 13 | 15 |
| 14 /** | 16 /** |
| 15 * Call the given function with each non-null non-empty inherited type name | 17 * Call the given function with each non-null non-empty inherited type name |
| 16 * that is defined in the given class. | 18 * that is defined in the given class. |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 static void suggestionsFor(DartCompletionRequest request, | 214 static void suggestionsFor(DartCompletionRequest request, |
| 213 CompletionSuggestionKind kind, LibraryElement library) { | 215 CompletionSuggestionKind kind, LibraryElement library) { |
| 214 if (library != null) { | 216 if (library != null) { |
| 215 library.visitChildren(new LibraryElementSuggestionBuilder(request, kind)); | 217 library.visitChildren(new LibraryElementSuggestionBuilder(request, kind)); |
| 216 } | 218 } |
| 217 } | 219 } |
| 218 } | 220 } |
| 219 | 221 |
| 220 /** | 222 /** |
| 221 * This class visits elements in a class and provides suggestions based upon | 223 * This class visits elements in a class and provides suggestions based upon |
| 222 * the visible named constructors in that class. Clients should call | 224 * the visible named constructors in that class. |
| 223 * [NamedConstructorSuggestionBuilder.suggestionsFor]. | |
| 224 */ | 225 */ |
| 225 class NamedConstructorSuggestionBuilder extends _AbstractSuggestionBuilder { | 226 class NamedConstructorSuggestionBuilder extends _AbstractSuggestionBuilder |
| 227 implements SuggestionBuilder { |
| 226 | 228 |
| 227 NamedConstructorSuggestionBuilder(DartCompletionRequest request) | 229 NamedConstructorSuggestionBuilder(DartCompletionRequest request) |
| 228 : super(request, CompletionSuggestionKind.INVOCATION); | 230 : super(request, CompletionSuggestionKind.INVOCATION); |
| 229 | 231 |
| 230 @override | 232 @override |
| 233 bool computeFast(AstNode node) { |
| 234 return false; |
| 235 } |
| 236 |
| 237 @override |
| 238 Future<bool> computeFull(AstNode node) { |
| 239 if (node is SimpleIdentifier) { |
| 240 node = node.parent; |
| 241 } |
| 242 if (node is ConstructorName) { |
| 243 TypeName typeName = node.type; |
| 244 if (typeName != null) { |
| 245 DartType type = typeName.type; |
| 246 if (type != null) { |
| 247 if (type.element is ClassElement) { |
| 248 type.element.accept(this); |
| 249 } |
| 250 return new Future.value(true); |
| 251 } |
| 252 } |
| 253 } |
| 254 return new Future.value(false); |
| 255 } |
| 256 |
| 257 @override |
| 231 visitClassElement(ClassElement element) { | 258 visitClassElement(ClassElement element) { |
| 232 element.visitChildren(this); | 259 element.visitChildren(this); |
| 233 } | 260 } |
| 234 | 261 |
| 235 @override | 262 @override |
| 236 visitConstructorElement(ConstructorElement element) { | 263 visitConstructorElement(ConstructorElement element) { |
| 237 _addElementSuggestion( | 264 _addElementSuggestion( |
| 238 element, | 265 element, |
| 239 element.returnType, | 266 element.returnType, |
| 240 element.enclosingElement); | 267 element.enclosingElement); |
| 241 } | 268 } |
| 242 | 269 |
| 243 @override | 270 @override |
| 244 visitElement(Element element) { | 271 visitElement(Element element) { |
| 245 // ignored | 272 // ignored |
| 246 } | 273 } |
| 247 | |
| 248 /** | |
| 249 * Add suggestions for the visible members in the given class | |
| 250 */ | |
| 251 static void suggestionsFor(DartCompletionRequest request, Element element) { | |
| 252 if (element is ClassElement) { | |
| 253 element.accept(new NamedConstructorSuggestionBuilder(request)); | |
| 254 } | |
| 255 } | |
| 256 } | 274 } |
| 257 | 275 |
| 258 /** | 276 /** |
| 277 * Common interface implemented by suggestion builders. |
| 278 */ |
| 279 abstract class SuggestionBuilder { |
| 280 /** |
| 281 * Compute suggestions and return `true` if building is complete, |
| 282 * or `false` if [computeFull] should be called. |
| 283 */ |
| 284 bool computeFast(AstNode node); |
| 285 |
| 286 /** |
| 287 * Return a future that computes the suggestions given a fully resolved AST. |
| 288 * The future returns `true` if suggestions were added, else `false`. |
| 289 */ |
| 290 Future<bool> computeFull(AstNode node); |
| 291 } |
| 292 |
| 293 /** |
| 259 * Common superclass for sharing behavior | 294 * Common superclass for sharing behavior |
| 260 */ | 295 */ |
| 261 class _AbstractSuggestionBuilder extends GeneralizingElementVisitor { | 296 class _AbstractSuggestionBuilder extends GeneralizingElementVisitor { |
| 262 final DartCompletionRequest request; | 297 final DartCompletionRequest request; |
| 263 final CompletionSuggestionKind kind; | 298 final CompletionSuggestionKind kind; |
| 264 final Set<String> _completions = new Set<String>(); | 299 final Set<String> _completions = new Set<String>(); |
| 265 | 300 |
| 266 _AbstractSuggestionBuilder(this.request, this.kind); | 301 _AbstractSuggestionBuilder(this.request, this.kind); |
| 267 | 302 |
| 268 void _addElementSuggestion(Element element, DartType type, | 303 void _addElementSuggestion(Element element, DartType type, |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 } | 340 } |
| 306 if (type != null) { | 341 if (type != null) { |
| 307 String typeName = type.displayName; | 342 String typeName = type.displayName; |
| 308 if (typeName != null && typeName.length > 0 && typeName != 'dynamic') { | 343 if (typeName != null && typeName.length > 0 && typeName != 'dynamic') { |
| 309 suggestion.returnType = typeName; | 344 suggestion.returnType = typeName; |
| 310 } | 345 } |
| 311 } | 346 } |
| 312 request.suggestions.add(suggestion); | 347 request.suggestions.add(suggestion); |
| 313 } | 348 } |
| 314 } | 349 } |
| OLD | NEW |