|
2 | 2 | // for details. All rights reserved. Use of this source code is governed by a |
3 | 3 | // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
|
5 | | -// ignore_for_file: analyzer_use_new_elements |
6 | | - |
7 | | -import 'package:analyzer/dart/element/element.dart'; |
8 | 5 | import 'package:analyzer/dart/element/element2.dart'; |
9 | 6 | import 'package:analyzer/dart/element/visitor.dart'; |
10 | 7 | import 'package:analyzer/dart/element/visitor2.dart'; |
11 | 8 |
|
12 | | -/// Return the [Element] that is either [root], or one of its direct or |
13 | | -/// indirect children, and has the given [nameOffset]. |
14 | | -Element? findElementByNameOffset(Element? root, int nameOffset) { |
15 | | - if (root == null) { |
16 | | - return null; |
17 | | - } |
18 | | - try { |
19 | | - var visitor = _ElementByNameOffsetVisitor(nameOffset); |
20 | | - root.accept(visitor); |
21 | | - } on Element catch (result) { |
22 | | - return result; |
23 | | - } |
24 | | - return null; |
25 | | -} |
26 | | - |
27 | 9 | /// Returns the fragment that is either [fragment], or one of its direct or |
28 | 10 | /// indirect children, and has the given [nameOffset]. |
29 | 11 | Fragment? findFragmentByNameOffset(LibraryFragment fragment, int nameOffset) { |
30 | 12 | return _FragmentByNameOffsetVisitor(nameOffset).search(fragment); |
31 | 13 | } |
32 | 14 |
|
33 | | -/// Uses [processor] to visit all of the children of [element]. |
34 | | -/// If [processor] returns `true`, then children of a child are visited too. |
35 | | -void visitChildren(Element element, BoolElementProcessor processor) { |
36 | | - element.visitChildren(_ElementVisitorAdapter(processor)); |
37 | | -} |
38 | | - |
39 | 15 | /// Uses [processor] to visit all of the children of [element]. |
40 | 16 | /// If [processor] returns `true`, then children of a child are visited too. |
41 | 17 | void visitChildren2(Element2 element, BoolElementProcessor2 processor) { |
42 | 18 | element.visitChildren2(_ElementVisitorAdapter2(processor)); |
43 | 19 | } |
44 | 20 |
|
45 | | -/// Uses [processor] to visit all of the top-level elements of [library]. |
46 | | -void visitLibraryTopLevelElements( |
47 | | - LibraryElement library, |
48 | | - VoidElementProcessor processor, |
49 | | -) { |
50 | | - library.visitChildren(_TopLevelElementsVisitor(processor)); |
51 | | -} |
52 | | - |
53 | | -/// An [Element] processor function type. |
54 | | -/// If `true` is returned, children of [element] will be visited. |
55 | | -typedef BoolElementProcessor = bool Function(Element element); |
56 | | - |
57 | 21 | /// An [Element2] processor function type. |
58 | 22 | /// If `true` is returned, children of [element] will be visited. |
59 | 23 | typedef BoolElementProcessor2 = bool Function(Element2 element); |
60 | 24 |
|
61 | | -/// An [Element] processor function type. |
62 | | -typedef VoidElementProcessor = void Function(Element element); |
63 | | - |
64 | | -/// A visitor that finds the deep-most [Element] that contains the [nameOffset]. |
65 | | -class _ElementByNameOffsetVisitor extends GeneralizingElementVisitor<void> { |
66 | | - final int nameOffset; |
67 | | - |
68 | | - _ElementByNameOffsetVisitor(this.nameOffset); |
69 | | - |
70 | | - @override |
71 | | - void visitElement(Element element) { |
72 | | - if (element.nameOffset != -1 && |
73 | | - !element.isSynthetic && |
74 | | - element.nameOffset == nameOffset) { |
75 | | - throw element; |
76 | | - } |
77 | | - super.visitElement(element); |
78 | | - } |
79 | | -} |
80 | | - |
81 | | -/// A [GeneralizingElementVisitor] adapter for [BoolElementProcessor]. |
82 | | -class _ElementVisitorAdapter extends GeneralizingElementVisitor<void> { |
83 | | - final BoolElementProcessor processor; |
84 | | - |
85 | | - _ElementVisitorAdapter(this.processor); |
86 | | - |
87 | | - @override |
88 | | - void visitElement(Element element) { |
89 | | - var visitChildren = processor(element); |
90 | | - if (visitChildren == true) { |
91 | | - element.visitChildren(this); |
92 | | - } |
93 | | - } |
94 | | -} |
95 | | - |
96 | 25 | /// A [GeneralizingElementVisitor] adapter for [BoolElementProcessor2]. |
97 | 26 | class _ElementVisitorAdapter2 extends GeneralizingElementVisitor2<void> { |
98 | 27 | final BoolElementProcessor2 processor; |
@@ -129,19 +58,3 @@ class _FragmentByNameOffsetVisitor { |
129 | 58 | return null; |
130 | 59 | } |
131 | 60 | } |
132 | | - |
133 | | -/// A [GeneralizingElementVisitor] for visiting top-level elements. |
134 | | -class _TopLevelElementsVisitor extends GeneralizingElementVisitor<void> { |
135 | | - final VoidElementProcessor processor; |
136 | | - |
137 | | - _TopLevelElementsVisitor(this.processor); |
138 | | - |
139 | | - @override |
140 | | - void visitElement(Element element) { |
141 | | - if (element is CompilationUnitElement) { |
142 | | - element.visitChildren(this); |
143 | | - } else { |
144 | | - processor(element); |
145 | | - } |
146 | | - } |
147 | | -} |
0 commit comments