|
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 { |
@@ -77,18 +76,74 @@ static public List<? extends Location> searchDeclaration(PreprocSketch ps, int j |
77 | 76 | System.out.println("declaration is outside of the sketch"); |
78 | 77 | return Collections.emptyList(); |
79 | 78 | } |
| 79 | + |
| 80 | + List<Location> declarationList = new ArrayList<>(); |
| 81 | + declarationList.add(findLocation(ps, si)); |
| 82 | + |
| 83 | + return declarationList; |
| 84 | + } |
| 85 | + |
| 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 | + } |
80 | 105 |
|
81 | | - //Create a location for the found declaration |
| 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 | + |
| 132 | + /** |
| 133 | + * Looks for a location(range) for a given sketchInterval |
| 134 | + * |
| 135 | + * @param ps processed sketch, for finding the uri and code |
| 136 | + * @param si The interval to find the location for |
| 137 | + * |
| 138 | + * @return Location(range) inside a file from the workspace |
| 139 | + */ |
| 140 | + static private Location findLocation(PreprocSketch ps, SketchInterval si) { |
82 | 141 | SketchCode code = ps.sketch.getCode(si.tabIndex); |
83 | 142 | String program = code.getProgram(); |
84 | 143 | URI uri = PdeAdapter.pathToUri(code.getFile()); |
85 | 144 |
|
86 | | - Location location = |
87 | | - PdeAdapter.toLocation(program, si.startTabOffset, si.stopTabOffset, uri); |
88 | | - |
89 | | - List<Location> declarationList = new ArrayList<>(); |
90 | | - declarationList.add(location); |
91 | | - |
92 | | - return declarationList; |
| 145 | + return PdeAdapter.toLocation(program, si.startTabOffset, si.stopTabOffset, |
| 146 | + uri |
| 147 | + ); |
93 | 148 | } |
94 | 149 | } |
0 commit comments