Skip to content

Commit 9b54cbc

Browse files
committed
Add reference searching
1 parent 2ca4a37 commit 9b54cbc

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

java/src/processing/mode/java/lsp/PdeSymbolFinder.java

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
import processing.mode.java.PreprocSketch;
1919
import processing.mode.java.SketchInterval;
2020

21-
import static processing.mode.java.ASTUtils.getSimpleNameAt;
22-
import static processing.mode.java.ASTUtils.resolveBinding;
21+
import static processing.mode.java.ASTUtils.*;
2322

2423

2524
public class PdeSymbolFinder {
@@ -85,6 +84,51 @@ static public List<? extends Location> searchDeclaration(PreprocSketch ps, int j
8584
}
8685

8786

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+
88132
/**
89133
* Looks for a location(range) for a given sketchInterval
90134
*

java/src/processing/mode/java/lsp/PdeTextDocumentService.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.eclipse.lsp4j.TextEdit;
1818
import org.eclipse.lsp4j.Location;
1919
import org.eclipse.lsp4j.LocationLink;
20+
import org.eclipse.lsp4j.ReferenceParams;
2021

2122
import java.util.Collections;
2223
import java.net.URI;
@@ -137,4 +138,36 @@ public CompletableFuture<Either<List<? extends Location>, List<? extends Locatio
137138
);
138139
}
139140

141+
142+
@Override
143+
public CompletableFuture<List<? extends Location>> references(
144+
ReferenceParams params
145+
) {
146+
147+
System.out.println("searching for references");
148+
URI uri = URI.create(params.getTextDocument().getUri());
149+
int lineNumber = params.getPosition().getLine();
150+
int colNumber = params.getPosition().getCharacter();
151+
152+
Optional<PdeAdapter> adapterOptional = pls.getAdapter(uri);
153+
if (adapterOptional.isEmpty()) {
154+
System.out.println("pde adapter not found");
155+
return CompletableFutures.computeAsync(_x -> Collections.emptyList());
156+
}
157+
PdeAdapter adapter = adapterOptional.get();
158+
PreprocSketch preprocSketch = adapter.ps;
159+
160+
Optional<Integer> optionalJavaOffset =
161+
adapter.findJavaOffset(uri, lineNumber, colNumber);
162+
if (optionalJavaOffset.isEmpty()) {
163+
System.out.println("javaOffset not found");
164+
return CompletableFutures.computeAsync(_x -> (Collections.emptyList()));
165+
}
166+
167+
int javaOffset = optionalJavaOffset.get();
168+
List<? extends Location> locations;
169+
locations = PdeSymbolFinder.searchReference(preprocSketch, javaOffset);
170+
171+
return CompletableFutures.computeAsync(_x -> locations);
172+
}
140173
}

0 commit comments

Comments
 (0)