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

Unified Diff: pkg/analysis_server/lib/src/computer/imported_elements_computer.dart

Issue 2988183002: Initial implementation of the import-aware copy support (Closed)
Patch Set: Created 3 years, 5 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
« no previous file with comments | « no previous file | pkg/analysis_server/test/src/computer/imported_elements_computer_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analysis_server/lib/src/computer/imported_elements_computer.dart
diff --git a/pkg/analysis_server/lib/src/computer/imported_elements_computer.dart b/pkg/analysis_server/lib/src/computer/imported_elements_computer.dart
index 058a6ec1dac3f42e1f9956fee223684c1bf3b930..5103c930b264352808c297d56e9af2c00a4f7000 100644
--- a/pkg/analysis_server/lib/src/computer/imported_elements_computer.dart
+++ b/pkg/analysis_server/lib/src/computer/imported_elements_computer.dart
@@ -4,6 +4,8 @@
import 'package:analysis_server/protocol/protocol_generated.dart';
import 'package:analyzer/dart/ast/ast.dart';
+import 'package:analyzer/dart/ast/visitor.dart';
+import 'package:analyzer/dart/element/element.dart';
/**
* An object used to compute the list of elements referenced within a given
@@ -37,7 +39,85 @@ class ImportedElementsComputer {
* Compute and return the list of imported elements.
*/
List<ImportedElements> compute() {
- // TODO(brianwilkerson) Implement this.
- return <ImportedElements>[];
+ _Visitor visitor =
+ new _Visitor(unit.element.library, offset, offset + length);
+ unit.accept(visitor);
+ return visitor.importedElements.values.toList();
+ }
+}
+
+/**
+ * The visitor used by an [ImportedElementsComputer] to record the names of all
+ * imported elements.
+ */
+class _Visitor extends UnifyingAstVisitor<Object> {
+ /**
+ * The element representing the library containing the code being visited.
+ */
+ LibraryElement containingLibrary;
scheglov 2017/07/31 22:08:04 Make all these fields final?
Brian Wilkerson 2017/08/01 14:44:15 Not sure I see the point, given that they're in a
+
+ /**
+ * The offset of the start of the region of text being copied.
+ */
+ int startOffset;
+
+ /**
+ * The offset of the end of the region of text being copied.
+ */
+ int endOffset;
+
+ /**
+ * A table mapping library path and prefix keys to the imported elements from
+ * that library.
+ */
+ Map<String, ImportedElements> importedElements = <String, ImportedElements>{};
+
+ /**
+ * Initialize a newly created visitor to visit nodes within a specified
+ * region.
+ */
+ _Visitor(this.containingLibrary, this.startOffset, this.endOffset);
+
+ @override
+ Object visitNode(AstNode node) {
+ if (node.offset <= endOffset && node.end >= startOffset) {
+ node.visitChildren(this);
+ }
+ return null;
+ }
+
+ @override
+ Object visitSimpleIdentifier(SimpleIdentifier node) {
+ if (node.offset <= endOffset && node.end >= startOffset) {
+ Element nodeElement = node.bestElement;
scheglov 2017/07/31 22:08:04 Hm... I thought that we are interested in names of
Brian Wilkerson 2017/08/01 14:44:15 Done
+ if (nodeElement != null &&
+ nodeElement.enclosingElement is CompilationUnitElement) {
+ LibraryElement nodeLibrary = nodeElement.library;
+ if (nodeLibrary != containingLibrary) {
scheglov 2017/07/31 22:08:04 Why not? If we copy a use of a class defined in t
Brian Wilkerson 2017/08/01 14:44:15 Good point. Done.
+ String path = nodeLibrary.definingCompilationUnit.source.fullName;
+ String prefix = '';
+ AstNode parent = node.parent;
+ if (parent is PrefixedIdentifier && parent.identifier == node) {
+ SimpleIdentifier prefixIdentifier = parent.prefix;
+ if (prefixIdentifier.offset <= endOffset &&
+ prefixIdentifier.end >= startOffset) {
+ Element prefixElement = prefixIdentifier.bestElement;
scheglov 2017/07/31 22:08:04 Same here - prefixes are resolved statically.
Brian Wilkerson 2017/08/01 14:44:15 Done
+ if (prefixElement is PrefixElement) {
+ prefix = prefixElement.name;
+ }
+ }
+ }
+ String key = '$prefix;$path';
+ ImportedElements elements = importedElements.putIfAbsent(
+ key, () => new ImportedElements(path, prefix, <String>[]));
+ List<String> elementNames = elements.elements;
+ String elementName = nodeElement.name;
+ if (!elementNames.contains(elementName)) {
+ elementNames.add(elementName);
+ }
+ }
+ }
+ }
+ return null;
}
}
« no previous file with comments | « no previous file | pkg/analysis_server/test/src/computer/imported_elements_computer_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698