| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'package:analysis_server/protocol/protocol_generated.dart'; | 5 import 'package:analysis_server/protocol/protocol_generated.dart'; |
| 6 import 'package:analyzer/dart/ast/ast.dart'; | 6 import 'package:analyzer/dart/ast/ast.dart'; |
| 7 import 'package:analyzer/dart/ast/visitor.dart'; |
| 8 import 'package:analyzer/dart/element/element.dart'; |
| 7 | 9 |
| 8 /** | 10 /** |
| 9 * An object used to compute the list of elements referenced within a given | 11 * An object used to compute the list of elements referenced within a given |
| 10 * region of a compilation unit that are imported into the compilation unit's | 12 * region of a compilation unit that are imported into the compilation unit's |
| 11 * library. | 13 * library. |
| 12 */ | 14 */ |
| 13 class ImportedElementsComputer { | 15 class ImportedElementsComputer { |
| 14 /** | 16 /** |
| 15 * The compilation unit in which the elements are referenced. | 17 * The compilation unit in which the elements are referenced. |
| 16 */ | 18 */ |
| (...skipping 13 matching lines...) Expand all Loading... |
| 30 * Initialize a newly created computer to compute the list of imported | 32 * Initialize a newly created computer to compute the list of imported |
| 31 * elements referenced in the given [unit] within the region with the given | 33 * elements referenced in the given [unit] within the region with the given |
| 32 * [offset] and [length]. | 34 * [offset] and [length]. |
| 33 */ | 35 */ |
| 34 ImportedElementsComputer(this.unit, this.offset, this.length); | 36 ImportedElementsComputer(this.unit, this.offset, this.length); |
| 35 | 37 |
| 36 /** | 38 /** |
| 37 * Compute and return the list of imported elements. | 39 * Compute and return the list of imported elements. |
| 38 */ | 40 */ |
| 39 List<ImportedElements> compute() { | 41 List<ImportedElements> compute() { |
| 40 // TODO(brianwilkerson) Implement this. | 42 _Visitor visitor = |
| 41 return <ImportedElements>[]; | 43 new _Visitor(unit.element.library, offset, offset + length); |
| 44 unit.accept(visitor); |
| 45 return visitor.importedElements.values.toList(); |
| 42 } | 46 } |
| 43 } | 47 } |
| 48 |
| 49 /** |
| 50 * The visitor used by an [ImportedElementsComputer] to record the names of all |
| 51 * imported elements. |
| 52 */ |
| 53 class _Visitor extends UnifyingAstVisitor<Object> { |
| 54 /** |
| 55 * The element representing the library containing the code being visited. |
| 56 */ |
| 57 final LibraryElement containingLibrary; |
| 58 |
| 59 /** |
| 60 * The offset of the start of the region of text being copied. |
| 61 */ |
| 62 final int startOffset; |
| 63 |
| 64 /** |
| 65 * The offset of the end of the region of text being copied. |
| 66 */ |
| 67 final int endOffset; |
| 68 |
| 69 /** |
| 70 * A table mapping library path and prefix keys to the imported elements from |
| 71 * that library. |
| 72 */ |
| 73 Map<String, ImportedElements> importedElements = <String, ImportedElements>{}; |
| 74 |
| 75 /** |
| 76 * Initialize a newly created visitor to visit nodes within a specified |
| 77 * region. |
| 78 */ |
| 79 _Visitor(this.containingLibrary, this.startOffset, this.endOffset); |
| 80 |
| 81 @override |
| 82 Object visitNode(AstNode node) { |
| 83 if (node.offset <= endOffset && node.end >= startOffset) { |
| 84 node.visitChildren(this); |
| 85 } |
| 86 return null; |
| 87 } |
| 88 |
| 89 @override |
| 90 Object visitSimpleIdentifier(SimpleIdentifier node) { |
| 91 if (!node.inDeclarationContext() && |
| 92 node.offset <= endOffset && |
| 93 node.end >= startOffset) { |
| 94 Element nodeElement = node.staticElement; |
| 95 if (nodeElement != null && |
| 96 nodeElement.enclosingElement is CompilationUnitElement) { |
| 97 LibraryElement nodeLibrary = nodeElement.library; |
| 98 String path = nodeLibrary.definingCompilationUnit.source.fullName; |
| 99 String prefix = ''; |
| 100 AstNode parent = node.parent; |
| 101 if (parent is PrefixedIdentifier && parent.identifier == node) { |
| 102 SimpleIdentifier prefixIdentifier = parent.prefix; |
| 103 if (prefixIdentifier.offset <= endOffset && |
| 104 prefixIdentifier.end >= startOffset) { |
| 105 Element prefixElement = prefixIdentifier.staticElement; |
| 106 if (prefixElement is PrefixElement) { |
| 107 prefix = prefixElement.name; |
| 108 } |
| 109 } |
| 110 } |
| 111 String key = '$prefix;$path'; |
| 112 ImportedElements elements = importedElements.putIfAbsent( |
| 113 key, () => new ImportedElements(path, prefix, <String>[])); |
| 114 List<String> elementNames = elements.elements; |
| 115 String elementName = nodeElement.name; |
| 116 if (!elementNames.contains(elementName)) { |
| 117 elementNames.add(elementName); |
| 118 } |
| 119 } |
| 120 } |
| 121 return null; |
| 122 } |
| 123 } |
| OLD | NEW |