Chromium Code Reviews| Index: pkg/analysis_server/lib/src/services/completion/dart/named_constructor_contributor.dart |
| diff --git a/pkg/analysis_server/lib/src/services/completion/dart/named_constructor_contributor.dart b/pkg/analysis_server/lib/src/services/completion/dart/named_constructor_contributor.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ab263cecc9133830af187e87176f1a346f1109e1 |
| --- /dev/null |
| +++ b/pkg/analysis_server/lib/src/services/completion/dart/named_constructor_contributor.dart |
| @@ -0,0 +1,85 @@ |
| +// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| +// 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.named_constructor; |
| + |
| +import 'dart:async'; |
| + |
| +import 'package:analysis_server/plugin/protocol/protocol.dart' hide Element; |
| +import 'package:analysis_server/src/provisional/completion/dart/completion_dart.dart'; |
| +import 'package:analyzer/src/generated/ast.dart'; |
| +import 'package:analyzer/src/generated/element.dart'; |
| +import 'package:analysis_server/src/services/completion/dart/suggestion_builder.dart'; |
| + |
| +/** |
| + * A contributor for calculating invocation / access suggestions |
| + * `completion.getSuggestions` request results. |
|
scheglov
2015/12/07 16:16:15
Is the comment valid?
danrubel
2015/12/07 16:59:38
Fixed.
|
| + */ |
| +class NamedConstructorContributor extends DartCompletionContributor { |
| + @override |
| + Future<List<CompletionSuggestion>> computeSuggestions( |
| + DartCompletionRequest request) async { |
| + // Determine if the target looks like a named constructor. |
| + AstNode parsedNode = request.target.containingNode; |
| + SimpleIdentifier targetId; |
| + if (parsedNode is ConstructorName) { |
| + TypeName type = parsedNode.type; |
| + if (type != null) { |
| + targetId = type.name; |
| + } |
| + } else if (parsedNode is PrefixedIdentifier) { |
| + // Some PrefixedIdentifier nodes are transformed into |
| + // ConstructorName nodes during the resolution process. |
| + targetId = parsedNode.prefix; |
| + } |
| + if (targetId == null) { |
| + return EMPTY_LIST; |
| + } |
| + |
| + // Resolve the containing library element |
| + LibraryElement libElem = await request.resolveContainingLibrary(); |
| + if (libElem == null) { |
| + return EMPTY_LIST; |
| + } |
| + |
| + // Resolve the target to determine the type |
| + await request.resolveIdentifier(targetId); |
| + |
| + // Recompute the target since resolution may have changed it |
| + AstNode node = request.target.containingNode; |
| + |
| + // Build the list of suggestions |
| + if (node is ConstructorName) { |
| + TypeName typeName = node.type; |
| + if (typeName != null) { |
| + DartType type = typeName.type; |
| + if (type != null) { |
| + Element classElem = type.element; |
| + if (classElem is ClassElement) { |
| + return _buildSuggestions(libElem, classElem); |
| + } |
| + } |
| + } |
| + } |
| + return EMPTY_LIST; |
| + } |
| + |
| + List<CompletionSuggestion> _buildSuggestions( |
| + LibraryElement libElem, ClassElement classElem) { |
| + bool isLocalClassDecl = classElem.library == libElem; |
| + List<CompletionSuggestion> suggestions = <CompletionSuggestion>[]; |
| + for (ConstructorElement elem in classElem.constructors) { |
| + if (isLocalClassDecl || !elem.isPrivate) { |
| + String name = elem.name; |
| + if (name != null) { |
| + CompletionSuggestion s = createSuggestion(elem, completion: name); |
| + if (s != null) { |
| + suggestions.add(s); |
| + } |
| + } |
| + } |
| + } |
| + return suggestions; |
| + } |
| +} |