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

Side by Side 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, 4 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 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
58
59 /**
60 * The offset of the start of the region of text being copied.
61 */
62 int startOffset;
63
64 /**
65 * The offset of the end of the region of text being copied.
66 */
67 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.offset <= endOffset && node.end >= startOffset) {
92 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
93 if (nodeElement != null &&
94 nodeElement.enclosingElement is CompilationUnitElement) {
95 LibraryElement nodeLibrary = nodeElement.library;
96 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.
97 String path = nodeLibrary.definingCompilationUnit.source.fullName;
98 String prefix = '';
99 AstNode parent = node.parent;
100 if (parent is PrefixedIdentifier && parent.identifier == node) {
101 SimpleIdentifier prefixIdentifier = parent.prefix;
102 if (prefixIdentifier.offset <= endOffset &&
103 prefixIdentifier.end >= startOffset) {
104 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
105 if (prefixElement is PrefixElement) {
106 prefix = prefixElement.name;
107 }
108 }
109 }
110 String key = '$prefix;$path';
111 ImportedElements elements = importedElements.putIfAbsent(
112 key, () => new ImportedElements(path, prefix, <String>[]));
113 List<String> elementNames = elements.elements;
114 String elementName = nodeElement.name;
115 if (!elementNames.contains(elementName)) {
116 elementNames.add(elementName);
117 }
118 }
119 }
120 }
121 return null;
122 }
123 }
OLDNEW
« 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