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

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

Issue 925723002: update code completion suggestion relevance based on common usage (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: rework common usage computer to inject test relevance map Created 5 years, 10 months 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/common_usage_computer.dart
diff --git a/pkg/analysis_server/lib/src/services/completion/common_usage_computer.dart b/pkg/analysis_server/lib/src/services/completion/common_usage_computer.dart
new file mode 100644
index 0000000000000000000000000000000000000000..36a94a59f1dd589fe06bd2fed616a2023a117fd5
--- /dev/null
+++ b/pkg/analysis_server/lib/src/services/completion/common_usage_computer.dart
@@ -0,0 +1,152 @@
+// 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.computer.dart.relevance;
+
+import 'package:analysis_server/src/protocol_server.dart' as protocol;
+import 'package:analysis_server/src/protocol_server.dart' show
+ CompletionSuggestion, CompletionSuggestionKind;
+import 'package:analysis_server/src/services/completion/dart_completion_manager.dart';
+import 'package:analyzer/src/generated/ast.dart';
+import 'package:analyzer/src/generated/element.dart';
+
+/**
+ * A map of <library>.<classname> to an ordered list of method names,
+ * field names, getter names, and named constructors.
+ * The names are ordered from most relevant to least relevant.
+ * Names not listed are considered equally less relevant than those listed.
+ */
+const Map<String, List<String>> defaultSelectorRelevance = const
+ {//
+// Sample implementation which updates the relevance of the following
+// new Random().nextInt(...)
+// new Random().nextDouble(...)
+// new Random().nextBool() - not commonly used thus omitted from list
+// Entries should look something like this
+// 'dart.math.Random': const ['nextInt', 'nextDouble'],
+// 'dart.async.Future': const ['value', 'wait'],
+};
+
+/**
+ * A computer for adjusting the relevance of completions computed by others
+ * based upon common Dart usage patterns.
+ */
+class CommonUsageComputer {
+ /**
+ * A map of <library>.<classname> to an ordered list of method names,
+ * field names, getter names, and named constructors.
+ * The names are ordered from most relevant to least relevant.
+ * Names not listed are considered equally less relevant than those listed.
+ */
+ Map<String, List<String>> selectorRelevance;
+
+ CommonUsageComputer([this.selectorRelevance = defaultSelectorRelevance]);
+
+ /**
+ * Adjusts the relevance based on the given completion context.
+ * The compilation unit and completion node
+ * in the given completion context may not be resolved.
+ * This method should execute quickly and not block waiting for any analysis.
+ */
+ void computeFast(DartCompletionRequest request) {
+ _update(request);
+ }
+
+ /**
+ * Adjusts the relevance based on the given completion context.
+ * The compilation unit and completion node
+ * in the given completion context are resolved.
+ */
+ void computeFull(DartCompletionRequest request) {
+ _update(request);
+ }
+
+ /**
+ * Adjusts the relevance based on the given completion context.
+ * The compilation unit and completion node
+ * in the given completion context may not be resolved.
+ */
+ void _update(DartCompletionRequest request) {
+ var visitor = new _BestTypeVisitor(request.target.entity);
+ DartType type = request.target.containingNode.accept(visitor);
+ if (type != null) {
+ Element typeElem = type.element;
+ if (typeElem != null) {
+ LibraryElement libElem =
+ typeElem.getAncestor((p) => p is LibraryElement);
scheglov 2015/02/15 19:06:29 typeElem.library
danrubel 2015/02/15 21:17:16 Done.
+ if (libElem != null) {
+ _updateMethodRelevance(request, type, libElem);
+ }
+ }
+ }
+ }
+
+ /**
+ * Adjusts the relevance of all method suggestions based upon the given
+ * target type and library.
+ */
+ void _updateMethodRelevance(DartCompletionRequest request, DartType type,
+ LibraryElement libElem) {
+ String typeName = type.name;
+ List<String> selectors = selectorRelevance['${libElem.name}.${typeName}'];
+ if (selectors != null) {
+ for (CompletionSuggestion suggestion in request.suggestions) {
+ protocol.Element element = suggestion.element;
+ if (element != null &&
+ (element.kind == protocol.ElementKind.CONSTRUCTOR ||
+ element.kind == protocol.ElementKind.METHOD) &&
+ suggestion.kind == CompletionSuggestionKind.INVOCATION &&
scheglov 2015/02/15 19:06:28 Field accessors?
danrubel 2015/02/15 21:17:16 Great point. I have added support and tests for fi
+ suggestion.declaringType == typeName) {
+ int index = selectors.indexOf(suggestion.completion);
+ if (index != -1) {
+ suggestion.relevance = DART_RELEVANCE_COMMON_USAGE - index;
+ }
+ }
+ }
+ }
+ }
+}
+
+/**
+ * An [AstVisitor] used to determine the best [DartType] of a node.
scheglov 2015/02/15 19:06:29 From implementation it does not look that it retur
danrubel 2015/02/15 21:17:16 Correct. I updated the comment per your suggestion
+ */
+class _BestTypeVisitor extends GeneralizingAstVisitor {
+ final entity;
scheglov 2015/02/15 19:06:28 type?
danrubel 2015/02/15 21:17:16 Done.
+
+ _BestTypeVisitor(this.entity);
+
+ DartType visitConstructorName(ConstructorName node) {
+ if (node.period != null && node.name == entity) {
+ TypeName typeName = node.type;
+ if (typeName != null) {
+ return typeName.type;
+ }
+ }
+ return null;
+ }
+
+ DartType visitNode(AstNode node) {
+ return null;
+ }
+
+ DartType visitPrefixedIdentifier(PrefixedIdentifier node) {
+ if (node.identifier == entity) {
+ SimpleIdentifier prefix = node.prefix;
+ if (prefix != null) {
+ return prefix.bestType;
+ }
+ }
+ return null;
+ }
+
+ DartType visitPropertyAccess(PropertyAccess node) {
+ if (node.propertyName == entity) {
+ Expression target = node.realTarget;
+ if (target != null) {
+ return target.bestType;
+ }
+ }
+ return null;
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698