Skip to content

Commit 73670aa

Browse files
authored
Merge pull request processing#690 from Efratror/LSP-Feature/Reference_Support
Lsp feature/reference support
2 parents c57069f + b3ee57f commit 73670aa

File tree

5 files changed

+106
-15
lines changed

5 files changed

+106
-15
lines changed

java/src/processing/mode/java/ASTUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ public static boolean isNameOrType(ASTNode node) {
163163
}
164164

165165

166-
protected static List<SimpleName> findAllOccurrences(ASTNode root, String bindingKey) {
166+
public static List<SimpleName> findAllOccurrences(ASTNode root,
167+
String bindingKey
168+
) {
167169
List<SimpleName> occurrences = new ArrayList<>();
168170
root.getRoot().accept(new ASTVisitor() {
169171
@Override

java/src/processing/mode/java/SketchInterval.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ public class SketchInterval {
1818
public final int startTabOffset;
1919
public final int stopTabOffset;
2020

21-
final int startPdeOffset;
22-
final int stopPdeOffset;
21+
public final int startPdeOffset;
22+
public final int stopPdeOffset;
2323
}

java/src/processing/mode/java/lsp/PdeLanguageServer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
7878
capabilities.setCompletionProvider(completionOptions);
7979
capabilities.setDocumentFormattingProvider(true);
8080
capabilities.setDeclarationProvider(true);
81+
capabilities.setReferencesProvider(true);
8182
var result = new InitializeResult(capabilities);
8283
return CompletableFuture.completedFuture(result);
8384
}

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

Lines changed: 65 additions & 10 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 {
@@ -77,18 +76,74 @@ static public List<? extends Location> searchDeclaration(PreprocSketch ps, int j
7776
System.out.println("declaration is outside of the sketch");
7877
return Collections.emptyList();
7978
}
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+
}
80105

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) {
82141
SketchCode code = ps.sketch.getCode(si.tabIndex);
83142
String program = code.getProgram();
84143
URI uri = PdeAdapter.pathToUri(code.getFile());
85144

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+
);
93148
}
94149
}

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

Lines changed: 35 additions & 2 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;
@@ -105,7 +106,7 @@ public CompletableFuture<Either<List<? extends Location>, List<? extends Locatio
105106
Optional<PdeAdapter> adapterOptional =
106107
pls.getAdapter(uri);
107108

108-
if(adapterOptional.isEmpty()){
109+
if (adapterOptional.isEmpty()) {
109110
System.out.println("pde adapter not found");
110111
return CompletableFutures.computeAsync(_x -> Either
111112
.forLeft(Collections.emptyList()));
@@ -116,7 +117,7 @@ public CompletableFuture<Either<List<? extends Location>, List<? extends Locatio
116117
Optional<Integer> optionalJavaOffset = adapter.findJavaOffset(uri,
117118
lineNumber, colNumber);
118119

119-
if(optionalJavaOffset.isEmpty()){
120+
if (optionalJavaOffset.isEmpty()) {
120121
System.out.println("javaOffset not found");
121122
return CompletableFutures.computeAsync(_x -> Either
122123
.forLeft(Collections.emptyList()));
@@ -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)