Index: pkg/analysis_server/lib/src/services/completion/dart/local_constructor_contributor.dart |
diff --git a/pkg/analysis_server/lib/src/services/completion/dart/label_contributor.dart b/pkg/analysis_server/lib/src/services/completion/dart/local_constructor_contributor.dart |
similarity index 65% |
copy from pkg/analysis_server/lib/src/services/completion/dart/label_contributor.dart |
copy to pkg/analysis_server/lib/src/services/completion/dart/local_constructor_contributor.dart |
index 64ed3f89eeb359de63066fae1f20c25bb9b9ffa7..3e5b753d4e5d8b1bd8ba4c1222594faf2e451697 100644 |
--- a/pkg/analysis_server/lib/src/services/completion/dart/label_contributor.dart |
+++ b/pkg/analysis_server/lib/src/services/completion/dart/local_constructor_contributor.dart |
@@ -2,7 +2,7 @@ |
// for details. All rights reserved. Use of this source code is governed by a |
// BSD-style license that can be found in the LICENSE file. |
-library services.completion.contributor.dart.local_ref; |
+library services.completion.contributor.dart.constructor; |
import 'dart:async'; |
@@ -11,10 +11,12 @@ import 'package:analysis_server/plugin/protocol/protocol.dart' as protocol |
import 'package:analysis_server/src/provisional/completion/dart/completion_dart.dart'; |
import 'package:analysis_server/src/services/completion/dart/completion_manager.dart' |
show DartCompletionRequestImpl; |
+import 'package:analysis_server/src/services/completion/dart/suggestion_builder.dart'; |
import 'package:analysis_server/src/services/completion/local_declaration_visitor.dart' |
show LocalDeclarationVisitor; |
import 'package:analysis_server/src/services/completion/optype.dart'; |
import 'package:analyzer/src/generated/ast.dart'; |
+import 'package:analyzer/src/generated/element.dart'; |
import 'package:analyzer/src/generated/scanner.dart'; |
import 'package:analyzer/src/generated/source.dart'; |
@@ -27,9 +29,10 @@ final TypeName NO_RETURN_TYPE = new TypeName( |
new SimpleIdentifier(new StringToken(TokenType.IDENTIFIER, '', 0)), null); |
/** |
- * A contributor for calculating label suggestions. |
+ * A contributor for calculating constructor suggestions |
+ * for declarations in the local file. |
*/ |
-class LabelContributor extends DartCompletionContributor { |
+class LocalConstructorContributor extends DartCompletionContributor { |
@override |
Future<List<CompletionSuggestion>> computeSuggestions( |
DartCompletionRequest request) async { |
@@ -39,11 +42,9 @@ class LabelContributor extends DartCompletionContributor { |
// the completion offset and all of its parents recursively. |
List<CompletionSuggestion> suggestions = <CompletionSuggestion>[]; |
if (!optype.isPrefixed) { |
- if (optype.includeStatementLabelSuggestions || |
- optype.includeCaseLabelSuggestions) { |
- new _LabelVisitor(request, optype.includeStatementLabelSuggestions, |
- optype.includeCaseLabelSuggestions, suggestions) |
- .visit(request.target.containingNode); |
+ if (optype.includeConstructorSuggestions) { |
+ _Visitor visitor = new _Visitor(request, suggestions); |
+ visitor.visit(request.target.containingNode); |
} |
} |
return suggestions; |
@@ -51,116 +52,110 @@ class LabelContributor extends DartCompletionContributor { |
} |
/** |
- * A visitor for collecting suggestions for break and continue labels. |
+ * A visitor for collecting constructor suggestions. |
*/ |
-class _LabelVisitor extends LocalDeclarationVisitor { |
+class _Visitor extends LocalDeclarationVisitor { |
final DartCompletionRequest request; |
final List<CompletionSuggestion> suggestions; |
- /** |
- * True if statement labels should be included as suggestions. |
- */ |
- final bool includeStatementLabels; |
- |
- /** |
- * True if case labels should be included as suggestions. |
- */ |
- final bool includeCaseLabels; |
- |
- _LabelVisitor(DartCompletionRequest request, this.includeStatementLabels, |
- this.includeCaseLabels, this.suggestions) |
+ _Visitor(DartCompletionRequest request, this.suggestions) |
: super(request.offset), |
request = request; |
@override |
void declaredClass(ClassDeclaration declaration) { |
- // ignored |
+ bool found = false; |
+ for (ClassMember member in declaration.members) { |
+ if (member is ConstructorDeclaration) { |
+ found = true; |
+ _addSuggestion(declaration, member); |
+ } |
+ } |
+ if (!found) { |
+ _addSuggestion(declaration, null); |
+ } |
} |
@override |
- void declaredClassTypeAlias(ClassTypeAlias declaration) { |
- // ignored |
- } |
+ void declaredClassTypeAlias(ClassTypeAlias declaration) {} |
@override |
- void declaredField(FieldDeclaration fieldDecl, VariableDeclaration varDecl) { |
- // ignored |
- } |
+ void declaredField(FieldDeclaration fieldDecl, VariableDeclaration varDecl) {} |
@override |
- void declaredFunction(FunctionDeclaration declaration) { |
- // ignored |
- } |
+ void declaredFunction(FunctionDeclaration declaration) {} |
@override |
- void declaredFunctionTypeAlias(FunctionTypeAlias declaration) { |
- // ignored |
- } |
+ void declaredFunctionTypeAlias(FunctionTypeAlias declaration) {} |
@override |
- void declaredLabel(Label label, bool isCaseLabel) { |
- if (isCaseLabel ? includeCaseLabels : includeStatementLabels) { |
- CompletionSuggestion suggestion = _addSuggestion(label.label); |
- if (suggestion != null) { |
- suggestion.element = createLocalElement( |
- request.source, protocol.ElementKind.LABEL, label.label, |
- returnType: NO_RETURN_TYPE); |
- } |
- } |
- } |
+ void declaredLabel(Label label, bool isCaseLabel) {} |
@override |
- void declaredLocalVar(SimpleIdentifier name, TypeName type) { |
- // ignored |
- } |
+ void declaredLocalVar(SimpleIdentifier name, TypeName type) {} |
@override |
- void declaredMethod(MethodDeclaration declaration) { |
- // ignored |
- } |
+ void declaredMethod(MethodDeclaration declaration) {} |
@override |
- void declaredParam(SimpleIdentifier name, TypeName type) { |
- // ignored |
- } |
+ void declaredParam(SimpleIdentifier name, TypeName type) {} |
@override |
void declaredTopLevelVar( |
- VariableDeclarationList varList, VariableDeclaration varDecl) { |
- // ignored |
- } |
+ VariableDeclarationList varList, VariableDeclaration varDecl) {} |
- @override |
- void visitFunctionExpression(FunctionExpression node) { |
- // Labels are only accessible within the local function, so stop visiting |
- // once we reach a function boundary. |
- finished(); |
- } |
- |
- @override |
- void visitMethodDeclaration(MethodDeclaration node) { |
- // Labels are only accessible within the local function, so stop visiting |
- // once we reach a function boundary. |
- finished(); |
- } |
+ /** |
+ * For the given class and constructor, |
+ * add a suggestion of the form B(...) or B.name(...). |
+ * If the given constructor is `null` |
+ * then add a default constructor suggestion. |
+ */ |
+ void _addSuggestion( |
+ ClassDeclaration classDecl, ConstructorDeclaration constructorDecl) { |
+ String completion = classDecl.name.name; |
+ SimpleIdentifier elemId; |
- CompletionSuggestion _addSuggestion(SimpleIdentifier id) { |
- if (id != null) { |
- String completion = id.name; |
- if (completion != null && completion.length > 0 && completion != '_') { |
- CompletionSuggestion suggestion = new CompletionSuggestion( |
- CompletionSuggestionKind.IDENTIFIER, |
- DART_RELEVANCE_DEFAULT, |
- completion, |
- completion.length, |
- 0, |
- false, |
- false); |
- suggestions.add(suggestion); |
- return suggestion; |
+ // Build a suggestion for explicitly declared constructor |
+ if (constructorDecl != null) { |
+ elemId = constructorDecl.name; |
+ ConstructorElement elem = constructorDecl.element; |
+ if (elemId != null) { |
+ String name = elemId.name; |
+ if (name != null && name.length > 0) { |
+ completion = '$completion.$name'; |
+ } |
+ } |
+ if (elem != null) { |
+ CompletionSuggestion suggestion = |
+ createSuggestion(elem, completion: completion); |
+ if (suggestion != null) { |
+ suggestions.add(suggestion); |
+ } |
} |
} |
- return null; |
+ |
+ // Build a suggestion for an implicit constructor |
+ else { |
+ protocol.Element element = createLocalElement( |
+ request.source, protocol.ElementKind.CONSTRUCTOR, elemId, |
+ parameters: '()'); |
+ element.returnType = classDecl.name.name; |
+ CompletionSuggestion suggestion = new CompletionSuggestion( |
+ CompletionSuggestionKind.INVOCATION, |
+ DART_RELEVANCE_DEFAULT, |
+ completion, |
+ completion.length, |
+ 0, |
+ false, |
+ false, |
+ declaringType: classDecl.name.name, |
+ element: element, |
+ parameterNames: [], |
+ parameterTypes: [], |
+ requiredParameterCount: 0, |
+ hasNamedParameters: false); |
+ suggestions.add(suggestion); |
+ } |
} |
} |