|
18 | 18 | import processing.mode.java.PreprocSketch;
|
19 | 19 | import processing.mode.java.SketchInterval;
|
20 | 20 |
|
21 |
| -import static processing.mode.java.ASTUtils.getSimpleNameAt; |
22 |
| -import static processing.mode.java.ASTUtils.resolveBinding; |
| 21 | +import static processing.mode.java.ASTUtils.*; |
23 | 22 |
|
24 | 23 |
|
25 | 24 | public class PdeSymbolFinder {
|
@@ -85,6 +84,51 @@ static public List<? extends Location> searchDeclaration(PreprocSketch ps, int j
|
85 | 84 | }
|
86 | 85 |
|
87 | 86 |
|
| 87 | + /** |
| 88 | + * searches all reference nodes for a provided character offset |
| 89 | + * |
| 90 | + * @param ps processed sketch, for AST-nodes and sketch |
| 91 | + * @param javaOffset character offset for the node we want to look up |
| 92 | + * |
| 93 | + * @return Location list of all references found, else an empty list. |
| 94 | + */ |
| 95 | + static public List<? extends Location> searchReference(PreprocSketch ps, |
| 96 | + int javaOffset |
| 97 | + ) { |
| 98 | + ASTNode root = ps.compilationUnit; |
| 99 | + |
| 100 | + SimpleName simpleName = getSimpleNameAt(root, javaOffset, javaOffset); |
| 101 | + if (simpleName == null) { |
| 102 | + System.out.println("no simple name found at location"); |
| 103 | + return Collections.emptyList(); |
| 104 | + } |
| 105 | + |
| 106 | + IBinding binding = resolveBinding(simpleName); |
| 107 | + if (binding == null) { |
| 108 | + System.out.println("binding not resolved"); |
| 109 | + return Collections.emptyList(); |
| 110 | + } |
| 111 | + |
| 112 | + // Find usages |
| 113 | + String bindingKey = binding.getKey(); |
| 114 | + List<SketchInterval> referenceIntervals = |
| 115 | + findAllOccurrences(ps.compilationUnit, bindingKey).stream() |
| 116 | + .map(ps::mapJavaToSketch) |
| 117 | + // remove occurrences which fall into generated header |
| 118 | + .filter(ps::inRange) |
| 119 | + // remove empty intervals (happens when occurence was inserted) |
| 120 | + .filter(in -> in.startPdeOffset < in.stopPdeOffset) |
| 121 | + .collect(java.util.stream.Collectors.toList()); |
| 122 | + |
| 123 | + List<Location> referenceList = new ArrayList<>(); |
| 124 | + for (SketchInterval referenceInterval: referenceIntervals) { |
| 125 | + referenceList.add(findLocation(ps, referenceInterval)); |
| 126 | + } |
| 127 | + |
| 128 | + return referenceList; |
| 129 | + } |
| 130 | + |
| 131 | + |
88 | 132 | /**
|
89 | 133 | * Looks for a location(range) for a given sketchInterval
|
90 | 134 | *
|
|
0 commit comments