Chromium Code Reviews| 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; |
| } |
| } |