Skip to content

Commit

Permalink
Add reference searching
Browse files Browse the repository at this point in the history
  • Loading branch information
Efratror committed Mar 19, 2023
1 parent 2ca4a37 commit 9b54cbc
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
48 changes: 46 additions & 2 deletions java/src/processing/mode/java/lsp/PdeSymbolFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
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 {
Expand Down Expand Up @@ -85,6 +84,51 @@ static public List<? extends Location> searchDeclaration(PreprocSketch ps, int j
}


/**
* 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
*
Expand Down
33 changes: 33 additions & 0 deletions java/src/processing/mode/java/lsp/PdeTextDocumentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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;
Expand Down Expand Up @@ -137,4 +138,36 @@ public CompletableFuture<Either<List<? extends Location>, List<? extends Locatio
);
}


@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);
}
}

0 comments on commit 9b54cbc

Please sign in to comment.