Skip to content

Commit 99dc03d

Browse files
committed
Add symbol finder
1 parent 2ff786a commit 99dc03d

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package processing.mode.java.lsp;
2+
3+
import java.net.URI;
4+
import java.util.ArrayList;
5+
import java.util.Collections;
6+
import java.util.List;
7+
8+
import org.eclipse.jdt.core.dom.ASTNode;
9+
import org.eclipse.jdt.core.dom.IBinding;
10+
import org.eclipse.jdt.core.dom.MethodDeclaration;
11+
import org.eclipse.jdt.core.dom.SimpleName;
12+
import org.eclipse.jdt.core.dom.TypeDeclaration;
13+
import org.eclipse.jdt.core.dom.VariableDeclaration;
14+
15+
import org.eclipse.lsp4j.Location;
16+
17+
import processing.app.SketchCode;
18+
import processing.mode.java.PreprocSketch;
19+
import processing.mode.java.SketchInterval;
20+
21+
import static processing.mode.java.ASTUtils.getSimpleNameAt;
22+
import static processing.mode.java.ASTUtils.resolveBinding;
23+
24+
25+
public class PdeSymbolFinder {
26+
27+
/**
28+
* searches a declaration node for a provided character offset
29+
*
30+
* @param ps processed sketch, for AST-nodes and sketch
31+
* @param javaOffset character offset for the node we want to look up
32+
* @return Location list if a declaration is found, else an empty list.
33+
*/
34+
static public List<? extends Location> searchDeclaration(PreprocSketch ps, int javaOffset) {
35+
ASTNode root = ps.compilationUnit;
36+
37+
SimpleName simpleName = getSimpleNameAt(root, javaOffset, javaOffset);
38+
if (simpleName == null) {
39+
System.out.println("no simple name found at location");
40+
return Collections.emptyList();
41+
}
42+
43+
IBinding binding = resolveBinding(simpleName);
44+
if (binding == null) {
45+
System.out.println("binding not resolved");
46+
return Collections.emptyList();
47+
}
48+
49+
String key = binding.getKey();
50+
ASTNode declarationNode = ps.compilationUnit.findDeclaringNode(key);
51+
if (declarationNode == null) {
52+
System.out.println("declaration not found");
53+
return Collections.emptyList();
54+
}
55+
56+
SimpleName declarationName = switch (binding.getKind()) {
57+
case IBinding.TYPE -> ((TypeDeclaration) declarationNode).getName();
58+
case IBinding.METHOD -> ((MethodDeclaration) declarationNode).getName();
59+
case IBinding.VARIABLE ->
60+
((VariableDeclaration) declarationNode).getName();
61+
default -> null;
62+
};
63+
64+
if (declarationName == null) {
65+
System.out.println("declaration name not found " + declarationNode);
66+
return Collections.emptyList();
67+
}
68+
69+
if (!declarationName.equals(simpleName)) {
70+
System.out.println("found declaration, name: " + declarationName);
71+
} else {
72+
System.out.println("already at declaration");
73+
}
74+
75+
SketchInterval si = ps.mapJavaToSketch(declarationName);
76+
if (si == SketchInterval.BEFORE_START) {
77+
System.out.println("declaration is outside of the sketch");
78+
return Collections.emptyList();
79+
}
80+
81+
//Create a location for the found declaration
82+
SketchCode code = ps.sketch.getCode(si.tabIndex);
83+
String program = code.getProgram();
84+
URI uri = PdeAdapter.pathToUri(code.getFile());
85+
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;
93+
}
94+
}

0 commit comments

Comments
 (0)