OLD | NEW |
| (Empty) |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 library services.search.element_visitors; | |
6 | |
7 import 'package:analyzer/src/generated/element.dart'; | |
8 | |
9 | |
10 /** | |
11 * Uses [processor] to visit all of the children of [element]. | |
12 * If [processor] returns `true`, then children of a child are visited too. | |
13 */ | |
14 void visitChildren(Element element, ElementProcessor processor) { | |
15 element.visitChildren(new _ElementVisitorAdapter(processor)); | |
16 } | |
17 | |
18 | |
19 /** | |
20 * Uses [processor] to visit all of the top-level elements of [library]. | |
21 */ | |
22 void visitLibraryTopLevelElements(LibraryElement library, | |
23 ElementProcessor processor) { | |
24 library.visitChildren(new _TopLevelElementsVisitor(processor)); | |
25 } | |
26 | |
27 | |
28 /** | |
29 * An [Element] processor function type. | |
30 * If `true` is returned, children of [element] will be visited. | |
31 */ | |
32 typedef bool ElementProcessor(Element element); | |
33 | |
34 | |
35 /** | |
36 * A [GeneralizingElementVisitor] adapter for [ElementProcessor]. | |
37 */ | |
38 class _ElementVisitorAdapter extends GeneralizingElementVisitor { | |
39 final ElementProcessor processor; | |
40 | |
41 _ElementVisitorAdapter(this.processor); | |
42 | |
43 @override | |
44 void visitElement(Element element) { | |
45 bool visitChildren = processor(element); | |
46 if (visitChildren == true) { | |
47 element.visitChildren(this); | |
48 } | |
49 } | |
50 } | |
51 | |
52 | |
53 /** | |
54 * A [GeneralizingElementVisitor] for visiting top-level elements. | |
55 */ | |
56 class _TopLevelElementsVisitor extends GeneralizingElementVisitor { | |
57 final ElementProcessor processor; | |
58 | |
59 _TopLevelElementsVisitor(this.processor); | |
60 | |
61 @override | |
62 void visitElement(Element element) { | |
63 if (element is CompilationUnitElement) { | |
64 element.visitChildren(this); | |
65 } else { | |
66 processor(element); | |
67 } | |
68 } | |
69 } | |
OLD | NEW |