mirror of
https://github.com/processing/processing4.git
synced 2026-01-27 10:21:26 +01:00
Add reference searching
This commit is contained in:
@@ -18,8 +18,7 @@ import processing.app.SketchCode;
|
||||
import processing.mode.java.PreprocSketch;
|
||||
import processing.mode.java.SketchInterval;
|
||||
|
||||
import static processing.mode.java.ASTUtils.getSimpleNameAt;
|
||||
import static processing.mode.java.ASTUtils.resolveBinding;
|
||||
import static processing.mode.java.ASTUtils.*;
|
||||
|
||||
|
||||
public class PdeSymbolFinder {
|
||||
@@ -85,6 +84,51 @@ public class PdeSymbolFinder {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* searches all reference nodes for a provided character offset
|
||||
*
|
||||
* @param ps processed sketch, for AST-nodes and sketch
|
||||
* @param javaOffset character offset for the node we want to look up
|
||||
*
|
||||
* @return Location list of all references found, else an empty list.
|
||||
*/
|
||||
static public List<? extends Location> searchReference(PreprocSketch ps,
|
||||
int javaOffset
|
||||
) {
|
||||
ASTNode root = ps.compilationUnit;
|
||||
|
||||
SimpleName simpleName = getSimpleNameAt(root, javaOffset, javaOffset);
|
||||
if (simpleName == null) {
|
||||
System.out.println("no simple name found at location");
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
IBinding binding = resolveBinding(simpleName);
|
||||
if (binding == null) {
|
||||
System.out.println("binding not resolved");
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
// Find usages
|
||||
String bindingKey = binding.getKey();
|
||||
List<SketchInterval> referenceIntervals =
|
||||
findAllOccurrences(ps.compilationUnit, bindingKey).stream()
|
||||
.map(ps::mapJavaToSketch)
|
||||
// remove occurrences which fall into generated header
|
||||
.filter(ps::inRange)
|
||||
// remove empty intervals (happens when occurence was inserted)
|
||||
.filter(in -> in.startPdeOffset < in.stopPdeOffset)
|
||||
.collect(java.util.stream.Collectors.toList());
|
||||
|
||||
List<Location> referenceList = new ArrayList<>();
|
||||
for (SketchInterval referenceInterval: referenceIntervals) {
|
||||
referenceList.add(findLocation(ps, referenceInterval));
|
||||
}
|
||||
|
||||
return referenceList;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Looks for a location(range) for a given sketchInterval
|
||||
*
|
||||
|
||||
@@ -17,6 +17,7 @@ import org.eclipse.lsp4j.DocumentFormattingParams;
|
||||
import org.eclipse.lsp4j.TextEdit;
|
||||
import org.eclipse.lsp4j.Location;
|
||||
import org.eclipse.lsp4j.LocationLink;
|
||||
import org.eclipse.lsp4j.ReferenceParams;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.net.URI;
|
||||
@@ -137,4 +138,36 @@ class PdeTextDocumentService implements TextDocumentService {
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public CompletableFuture<List<? extends Location>> references(
|
||||
ReferenceParams params
|
||||
) {
|
||||
|
||||
System.out.println("searching for references");
|
||||
URI uri = URI.create(params.getTextDocument().getUri());
|
||||
int lineNumber = params.getPosition().getLine();
|
||||
int colNumber = params.getPosition().getCharacter();
|
||||
|
||||
Optional<PdeAdapter> adapterOptional = pls.getAdapter(uri);
|
||||
if (adapterOptional.isEmpty()) {
|
||||
System.out.println("pde adapter not found");
|
||||
return CompletableFutures.computeAsync(_x -> Collections.emptyList());
|
||||
}
|
||||
PdeAdapter adapter = adapterOptional.get();
|
||||
PreprocSketch preprocSketch = adapter.ps;
|
||||
|
||||
Optional<Integer> optionalJavaOffset =
|
||||
adapter.findJavaOffset(uri, lineNumber, colNumber);
|
||||
if (optionalJavaOffset.isEmpty()) {
|
||||
System.out.println("javaOffset not found");
|
||||
return CompletableFutures.computeAsync(_x -> (Collections.emptyList()));
|
||||
}
|
||||
|
||||
int javaOffset = optionalJavaOffset.get();
|
||||
List<? extends Location> locations;
|
||||
locations = PdeSymbolFinder.searchReference(preprocSketch, javaOffset);
|
||||
|
||||
return CompletableFutures.computeAsync(_x -> locations);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user