summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Fraser <george@fivetran.com>2019-01-08 20:50:25 -0800
committerGeorge Fraser <george@fivetran.com>2019-01-08 20:50:25 -0800
commit04e4f359cb80c7b8e450103dc7331f6deb363872 (patch)
treeb164426b6d85ba9bdcca10205eb395105efb2c47
parent938fce4f8d2630d456caea03b0c178508680e945 (diff)
downloadjava-language-server-04e4f359cb80c7b8e450103dc7331f6deb363872.zip
Fixes
-rw-r--r--src/main/java/org/javacs/Docs.java2
-rw-r--r--src/main/java/org/javacs/JavaCompilerService.java40
-rw-r--r--src/test/java/org/javacs/GotoTest.java7
-rw-r--r--src/test/test-project/workspace/src/org/javacs/example/Goto.java4
-rw-r--r--src/test/test-project/workspace/src/org/javacs/example/GotoPackagePrivate.java5
5 files changed, 30 insertions, 28 deletions
diff --git a/src/main/java/org/javacs/Docs.java b/src/main/java/org/javacs/Docs.java
index 29d4c28..07c81e2 100644
--- a/src/main/java/org/javacs/Docs.java
+++ b/src/main/java/org/javacs/Docs.java
@@ -42,7 +42,6 @@ public class Docs {
// Find the file el was declared in
var className = ptr.qualifiedClassName();
try {
- // TODO should get current contents of open files from FileStore
var fromSourcePath =
fileManager.getJavaFileForInput(
StandardLocation.SOURCE_PATH, className, JavaFileObject.Kind.SOURCE);
@@ -53,7 +52,6 @@ public class Docs {
for (var module : Classes.JDK_MODULES) {
var moduleLocation = fileManager.getLocationForModule(StandardLocation.MODULE_SOURCE_PATH, module);
if (moduleLocation == null) continue;
- // TODO should get current contents of open files from FileStore
var fromModuleSourcePath =
fileManager.getJavaFileForInput(moduleLocation, className, JavaFileObject.Kind.SOURCE);
if (fromModuleSourcePath != null) {
diff --git a/src/main/java/org/javacs/JavaCompilerService.java b/src/main/java/org/javacs/JavaCompilerService.java
index 1087e5f..80fbd2b 100644
--- a/src/main/java/org/javacs/JavaCompilerService.java
+++ b/src/main/java/org/javacs/JavaCompilerService.java
@@ -8,7 +8,6 @@ import java.net.URI;
import java.nio.file.*;
import java.util.*;
import java.util.logging.Logger;
-import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.lang.model.element.*;
import javax.tools.*;
@@ -234,7 +233,13 @@ public class JavaCompilerService {
}
boolean method() {
- return getCurrentPath().getParentPath().getLeaf() instanceof MethodInvocationTree;
+ var leaf = getCurrentPath().getLeaf();
+ var parent = getCurrentPath().getParentPath().getLeaf();
+ if (parent instanceof MethodInvocationTree) {
+ var method = (MethodInvocationTree) parent;
+ return method.getMethodSelect() == leaf;
+ }
+ return false;
}
@Override
@@ -386,31 +391,14 @@ public class JavaCompilerService {
/** Find the file `e` was declared in */
private Optional<URI> findDeclaringFile(TypeElement e) {
var name = e.getQualifiedName().toString();
- var lastDot = name.lastIndexOf('.');
- var packageName = lastDot == -1 ? "" : name.substring(0, lastDot);
- var className = name.substring(lastDot + 1);
- var isPublic = e.getModifiers().contains(Modifier.PUBLIC);
- for (var file : FileStore.list(packageName)) {
- // Public classes have to be declared in files named ClassName.java
- if (isPublic && !file.getFileName().toString().equals(className + ".java")) continue;
- if (containsTopLevelDeclaration(file, className)) {
- return Optional.of(file.toUri());
- }
- }
- return Optional.empty();
- }
-
- private boolean containsTopLevelDeclaration(Path file, String simpleClassName) {
- var find = Pattern.compile("\\b(class|interface|enum) +" + simpleClassName + "\\b");
- try (var lines = FileStore.lines(file)) {
- for (var line = lines.readLine(); line != null; line = lines.readLine()) {
- if (find.matcher(line).find()) return true;
- line = lines.readLine();
- }
- } catch (IOException e) {
- throw new RuntimeException(e);
+ JavaFileObject file;
+ try {
+ file = fileManager.getJavaFileForInput(StandardLocation.SOURCE_PATH, name, JavaFileObject.Kind.SOURCE);
+ } catch (IOException err) {
+ throw new RuntimeException(err);
}
- return false;
+ if (file == null) return Optional.empty();
+ return Optional.of(file.toUri());
}
private Collection<Path> possibleFiles(Element to) {
diff --git a/src/test/java/org/javacs/GotoTest.java b/src/test/java/org/javacs/GotoTest.java
index 6587eca..5eb0a52 100644
--- a/src/test/java/org/javacs/GotoTest.java
+++ b/src/test/java/org/javacs/GotoTest.java
@@ -168,6 +168,13 @@ public class GotoTest {
assertThat(doGoto(file, 5, 22), empty());
}
+ @Test
+ public void packagePrivate() {
+ var suggestions = doGoto(file, 50, 42);
+
+ assertThat(suggestions, hasItem("GotoPackagePrivate.java:4"));
+ }
+
private static final JavaLanguageServer server = LanguageServerFixture.getJavaLanguageServer();
private List<String> doGoto(String file, int row, int column) {
diff --git a/src/test/test-project/workspace/src/org/javacs/example/Goto.java b/src/test/test-project/workspace/src/org/javacs/example/Goto.java
index 3f4cb2f..6d1b698 100644
--- a/src/test/test-project/workspace/src/org/javacs/example/Goto.java
+++ b/src/test/test-project/workspace/src/org/javacs/example/Goto.java
@@ -45,4 +45,8 @@ public class Goto<Param> {
}
public Param getParam() {
}
+
+ void testGotoPackagePrivate() {
+ var x = GotoPackagePrivate.PUBLIC_FIELD;
+ }
} \ No newline at end of file
diff --git a/src/test/test-project/workspace/src/org/javacs/example/GotoPackagePrivate.java b/src/test/test-project/workspace/src/org/javacs/example/GotoPackagePrivate.java
new file mode 100644
index 0000000..0c5ba11
--- /dev/null
+++ b/src/test/test-project/workspace/src/org/javacs/example/GotoPackagePrivate.java
@@ -0,0 +1,5 @@
+package org.javacs.example;
+
+class GotoPackagePrivate {
+ public static int PUBLIC_FIELD = 1;
+} \ No newline at end of file