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