| 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.dart.local; | 5 library services.completion.computer.dart.local; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/src/protocol.dart' as protocol | 9 import 'package:analysis_server/src/protocol.dart' as protocol |
| 10 show Element, ElementKind; | 10 show Element, ElementKind; |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 bool computeFast(DartCompletionRequest request) { | 85 bool computeFast(DartCompletionRequest request) { |
| 86 OpType optype = request.optype; | 86 OpType optype = request.optype; |
| 87 | 87 |
| 88 // Collect suggestions from the specific child [AstNode] that contains | 88 // Collect suggestions from the specific child [AstNode] that contains |
| 89 // the completion offset and all of its parents recursively. | 89 // the completion offset and all of its parents recursively. |
| 90 if (optype.includeReturnValueSuggestions || | 90 if (optype.includeReturnValueSuggestions || |
| 91 optype.includeTypeNameSuggestions || | 91 optype.includeTypeNameSuggestions || |
| 92 optype.includeVoidReturnSuggestions) { | 92 optype.includeVoidReturnSuggestions) { |
| 93 _LocalVisitor localVisitor = | 93 _LocalVisitor localVisitor = |
| 94 new _LocalVisitor(request, request.offset, optype); | 94 new _LocalVisitor(request, request.offset, optype); |
| 95 localVisitor.visit(request.node); | 95 localVisitor.visit(request.target.containingNode); |
| 96 } | 96 } |
| 97 if (optype.includeStatementLabelSuggestions || | 97 if (optype.includeStatementLabelSuggestions || |
| 98 optype.includeCaseLabelSuggestions) { | 98 optype.includeCaseLabelSuggestions) { |
| 99 _LabelVisitor labelVisitor = new _LabelVisitor(request, | 99 _LabelVisitor labelVisitor = new _LabelVisitor(request, |
| 100 optype.includeStatementLabelSuggestions, | 100 optype.includeStatementLabelSuggestions, |
| 101 optype.includeCaseLabelSuggestions); | 101 optype.includeCaseLabelSuggestions); |
| 102 labelVisitor.visit(request.node); | 102 labelVisitor.visit(request.target.containingNode); |
| 103 } | 103 } |
| 104 if (optype.includeConstructorSuggestions) { | 104 if (optype.includeConstructorSuggestions) { |
| 105 new _ConstructorVisitor(request).visit(request.node); | 105 new _ConstructorVisitor(request).visit(request.target.containingNode); |
| 106 } | 106 } |
| 107 | 107 |
| 108 // If target is an argument in an argument list | 108 // If target is an argument in an argument list |
| 109 // then suggestions may need to be adjusted | 109 // then suggestions may need to be adjusted |
| 110 return request.target.argIndex == null; | 110 return request.target.argIndex == null; |
| 111 } | 111 } |
| 112 | 112 |
| 113 @override | 113 @override |
| 114 Future<bool> computeFull(DartCompletionRequest request) { | 114 Future<bool> computeFull(DartCompletionRequest request) { |
| 115 _updateSuggestions(request); | 115 _updateSuggestions(request); |
| (...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 } | 422 } |
| 423 | 423 |
| 424 /** | 424 /** |
| 425 * A visitor for collecting suggestions from the most specific child [AstNode] | 425 * A visitor for collecting suggestions from the most specific child [AstNode] |
| 426 * that contains the completion offset to the [CompilationUnit]. | 426 * that contains the completion offset to the [CompilationUnit]. |
| 427 */ | 427 */ |
| 428 class _LocalVisitor extends LocalDeclarationVisitor { | 428 class _LocalVisitor extends LocalDeclarationVisitor { |
| 429 final DartCompletionRequest request; | 429 final DartCompletionRequest request; |
| 430 final OpType optype; | 430 final OpType optype; |
| 431 | 431 |
| 432 /** | |
| 433 * The simple identifier that is being completed, or `null` if none. | |
| 434 */ | |
| 435 SimpleIdentifier targetId; | |
| 436 | |
| 437 _LocalVisitor(this.request, int offset, this.optype) : super(offset); | 432 _LocalVisitor(this.request, int offset, this.optype) : super(offset); |
| 438 | 433 |
| 439 @override | 434 @override |
| 440 void declaredClass(ClassDeclaration declaration) { | 435 void declaredClass(ClassDeclaration declaration) { |
| 441 if (optype.includeTypeNameSuggestions) { | 436 if (optype.includeTypeNameSuggestions) { |
| 442 bool isDeprecated = _isDeprecated(declaration); | 437 bool isDeprecated = _isDeprecated(declaration); |
| 443 CompletionSuggestion suggestion = _addSuggestion(declaration.name, | 438 CompletionSuggestion suggestion = _addSuggestion(declaration.name, |
| 444 _NO_RETURN_TYPE, isDeprecated, DART_RELEVANCE_DEFAULT); | 439 _NO_RETURN_TYPE, isDeprecated, DART_RELEVANCE_DEFAULT); |
| 445 if (suggestion != null) { | 440 if (suggestion != null) { |
| 446 suggestion.element = _createElement( | 441 suggestion.element = _createElement( |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 624 CompletionSuggestion suggestion = _addSuggestion(varDecl.name, | 619 CompletionSuggestion suggestion = _addSuggestion(varDecl.name, |
| 625 varList.type, isDeprecated, DART_RELEVANCE_LOCAL_TOP_LEVEL_VARIABLE); | 620 varList.type, isDeprecated, DART_RELEVANCE_LOCAL_TOP_LEVEL_VARIABLE); |
| 626 if (suggestion != null) { | 621 if (suggestion != null) { |
| 627 suggestion.element = _createElement( | 622 suggestion.element = _createElement( |
| 628 protocol.ElementKind.TOP_LEVEL_VARIABLE, varDecl.name, | 623 protocol.ElementKind.TOP_LEVEL_VARIABLE, varDecl.name, |
| 629 returnType: varList.type, isDeprecated: isDeprecated); | 624 returnType: varList.type, isDeprecated: isDeprecated); |
| 630 } | 625 } |
| 631 } | 626 } |
| 632 } | 627 } |
| 633 | 628 |
| 634 @override | |
| 635 void visitSimpleIdentifier(SimpleIdentifier node) { | |
| 636 // Record the visited identifier so as not to suggest it | |
| 637 targetId = node; | |
| 638 return super.visitSimpleIdentifier(node); | |
| 639 } | |
| 640 | |
| 641 void _addParameterInfo( | 629 void _addParameterInfo( |
| 642 CompletionSuggestion suggestion, FormalParameterList parameters) { | 630 CompletionSuggestion suggestion, FormalParameterList parameters) { |
| 643 var paramList = parameters.parameters; | 631 var paramList = parameters.parameters; |
| 644 suggestion.parameterNames = paramList | 632 suggestion.parameterNames = paramList |
| 645 .map((FormalParameter param) => param.identifier.name) | 633 .map((FormalParameter param) => param.identifier.name) |
| 646 .toList(); | 634 .toList(); |
| 647 suggestion.parameterTypes = paramList.map((FormalParameter param) { | 635 suggestion.parameterTypes = paramList.map((FormalParameter param) { |
| 648 TypeName type = null; | 636 TypeName type = null; |
| 649 if (param is DefaultFormalParameter) { | 637 if (param is DefaultFormalParameter) { |
| 650 NormalFormalParameter child = param.parameter; | 638 NormalFormalParameter child = param.parameter; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 669 return typeId.name; | 657 return typeId.name; |
| 670 }).toList(); | 658 }).toList(); |
| 671 suggestion.requiredParameterCount = paramList.where( | 659 suggestion.requiredParameterCount = paramList.where( |
| 672 (FormalParameter param) => param is! DefaultFormalParameter).length; | 660 (FormalParameter param) => param is! DefaultFormalParameter).length; |
| 673 suggestion.hasNamedParameters = paramList | 661 suggestion.hasNamedParameters = paramList |
| 674 .any((FormalParameter param) => param.kind == ParameterKind.NAMED); | 662 .any((FormalParameter param) => param.kind == ParameterKind.NAMED); |
| 675 } | 663 } |
| 676 | 664 |
| 677 CompletionSuggestion _addSuggestion(SimpleIdentifier id, TypeName returnType, | 665 CompletionSuggestion _addSuggestion(SimpleIdentifier id, TypeName returnType, |
| 678 bool isDeprecated, int defaultRelevance, {ClassDeclaration classDecl}) { | 666 bool isDeprecated, int defaultRelevance, {ClassDeclaration classDecl}) { |
| 679 if (id != null && !identical(id, targetId)) { | 667 if (id != null) { |
| 680 String completion = id.name; | 668 String completion = id.name; |
| 681 if (completion != null && completion.length > 0 && completion != '_') { | 669 if (completion != null && completion.length > 0 && completion != '_') { |
| 682 CompletionSuggestion suggestion = new CompletionSuggestion( | 670 CompletionSuggestion suggestion = new CompletionSuggestion( |
| 683 CompletionSuggestionKind.INVOCATION, | 671 CompletionSuggestionKind.INVOCATION, |
| 684 isDeprecated ? DART_RELEVANCE_LOW : defaultRelevance, completion, | 672 isDeprecated ? DART_RELEVANCE_LOW : defaultRelevance, completion, |
| 685 completion.length, 0, isDeprecated, false, | 673 completion.length, 0, isDeprecated, false, |
| 686 returnType: _nameForType(returnType)); | 674 returnType: _nameForType(returnType)); |
| 687 if (classDecl != null) { | 675 if (classDecl != null) { |
| 688 SimpleIdentifier identifier = classDecl.name; | 676 SimpleIdentifier identifier = classDecl.name; |
| 689 if (identifier != null) { | 677 if (identifier != null) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 703 bool _isVoid(TypeName returnType) { | 691 bool _isVoid(TypeName returnType) { |
| 704 if (returnType != null) { | 692 if (returnType != null) { |
| 705 Identifier id = returnType.name; | 693 Identifier id = returnType.name; |
| 706 if (id != null && id.name == 'void') { | 694 if (id != null && id.name == 'void') { |
| 707 return true; | 695 return true; |
| 708 } | 696 } |
| 709 } | 697 } |
| 710 return false; | 698 return false; |
| 711 } | 699 } |
| 712 } | 700 } |
| OLD | NEW |