Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(20)

Unified Diff: pkg/analysis_server/lib/src/services/completion/dart/named_constructor_contributor.dart

Issue 1507633002: extract named constructor suggestions from prefixed element contributor (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: merge Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..f9dcd4c17897ee1553818c9c77fc5705c5ca0854
--- /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 named constructor suggestions
+ * such as suggesting `bar` in `new Foo.bar()`.
+ */
+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.libraryElement;
+ 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;
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698