diff options
Diffstat (limited to 'src/main/java/org/javacs/CompileFile.java')
-rw-r--r-- | src/main/java/org/javacs/CompileFile.java | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/main/java/org/javacs/CompileFile.java b/src/main/java/org/javacs/CompileFile.java index 297e3f5..826f7e9 100644 --- a/src/main/java/org/javacs/CompileFile.java +++ b/src/main/java/org/javacs/CompileFile.java @@ -6,8 +6,10 @@ import java.io.IOException; import java.net.URI; import java.util.HashSet; import java.util.Objects; +import java.util.Optional; import java.util.logging.Logger; import javax.lang.model.element.*; +import org.eclipse.lsp4j.Range; public class CompileFile { private final JavaCompilerService parent; @@ -37,6 +39,52 @@ public class CompileFile { profiler.print(); } + public Optional<TreePath> find(Ptr target) { + class FindPtr extends TreePathScanner<Void, Void> { + TreePath found = null; + + boolean toStringEquals(Object left, Object right) { + return Objects.equals(Objects.toString(left, ""), Objects.toString(right, "")); + } + + /** Check if the declaration at the current path is the same symbol as `e` */ + boolean sameSymbol() { + return new Ptr(getCurrentPath()).equals(target); + } + + void check() { + if (sameSymbol()) { + found = getCurrentPath(); + } + } + + @Override + public Void visitClass(ClassTree node, Void aVoid) { + check(); + return super.visitClass(node, aVoid); + } + + @Override + public Void visitMethod(MethodTree node, Void aVoid) { + check(); + return super.visitMethod(node, aVoid); + } + + @Override + public Void visitVariable(VariableTree node, Void aVoid) { + check(); + return super.visitVariable(node, aVoid); + } + } + var find = new FindPtr(); + find.scan(root, null); + return Optional.ofNullable(find.found); + } + + public Optional<Range> range(TreePath path) { + return ParseFile.range(task, contents, path); + } + /** * Figure out what imports this file should have. Star-imports like `import java.util.*` are converted to individual * class imports. Missing imports are inferred by looking at imports in other source files. |