diff options
author | George Fraser <george@fivetran.com> | 2018-12-15 14:37:00 -0800 |
---|---|---|
committer | George Fraser <george@fivetran.com> | 2018-12-15 14:37:13 -0800 |
commit | 4533aaf75a75bc64ae67e685cdff95a96a6cf5cc (patch) | |
tree | 09d52b7c67e0f8002a0a0bfeb749ab70b16adbe2 /src | |
parent | 74f850eb6e283c68c9338da8da887acaea975f1f (diff) | |
download | java-language-server-4533aaf75a75bc64ae67e685cdff95a96a6cf5cc.zip |
Sort already-imported classes first
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/org/javacs/Completion.java | 21 | ||||
-rw-r--r-- | src/main/java/org/javacs/JavaCompilerService.java | 21 | ||||
-rw-r--r-- | src/main/java/org/javacs/JavaTextDocumentService.java | 13 | ||||
-rw-r--r-- | src/test/java/org/javacs/JavaCompilerServiceTest.java | 2 |
4 files changed, 41 insertions, 16 deletions
diff --git a/src/main/java/org/javacs/Completion.java b/src/main/java/org/javacs/Completion.java index 3a8976d..8d6f93c 100644 --- a/src/main/java/org/javacs/Completion.java +++ b/src/main/java/org/javacs/Completion.java @@ -10,15 +10,14 @@ public class Completion { public final Element element; public final PackagePart packagePart; public final String keyword; - public final String notImportedClass; // TODO this is a misnomer, all classes go down this path now + public final ClassName className; public final Snippet snippet; // TODO separate label and insertText - private Completion( - Element element, PackagePart packagePart, String keyword, String notImportedClass, Snippet snippet) { + private Completion(Element element, PackagePart packagePart, String keyword, ClassName className, Snippet snippet) { this.element = element; this.packagePart = packagePart; this.keyword = keyword; - this.notImportedClass = notImportedClass; + this.className = className; this.snippet = snippet; } @@ -34,14 +33,24 @@ public class Completion { return new Completion(null, null, keyword, null, null); } - public static Completion ofNotImportedClass(String className) { - return new Completion(null, null, null, className, null); + public static Completion ofClassName(String className, boolean isImported) { + return new Completion(null, null, null, new ClassName(className, isImported), null); } public static Completion ofSnippet(String label, String snippet) { return new Completion(null, null, null, null, new Snippet(label, snippet)); } + public static class ClassName { + public final String name; + public final boolean isImported; + + public ClassName(String name, boolean isImported) { + this.name = name; + this.isImported = isImported; + } + } + public static class PackagePart { public final String fullName, name; diff --git a/src/main/java/org/javacs/JavaCompilerService.java b/src/main/java/org/javacs/JavaCompilerService.java index 5b998da..28e0de8 100644 --- a/src/main/java/org/javacs/JavaCompilerService.java +++ b/src/main/java/org/javacs/JavaCompilerService.java @@ -517,6 +517,21 @@ public class JavaCompilerService { return result; } + private boolean isImported(String qualifiedName) { + var packageName = Parser.mostName(qualifiedName); + var className = Parser.lastName(qualifiedName); + for (var i : cache.root.getImports()) { + var importName = i.getQualifiedIdentifier().toString(); + var importPackage = Parser.mostName(importName); + var importClass = Parser.lastName(importName); + if (importClass.equals("*") && importPackage.equals(packageName)) + return true; + if (importClass.equals(className) && importPackage.equals(packageName)) + return true; + } + return false; + } + private List<TypeMirror> supersWithSelf(TypeMirror t) { var elements = cache.task.getElements(); var types = cache.task.getTypes(); @@ -965,13 +980,13 @@ public class JavaCompilerService { for (var c : jdkClasses.classes()) { if (full.getAsBoolean()) return; if (matchesPartialName.test(c) && jdkClasses.isAccessibleFromPackage(c, packageName)) { - result.add(Completion.ofNotImportedClass(c)); + result.add(Completion.ofClassName(c, isImported(c))); } } for (var c : classPathClasses.classes()) { if (full.getAsBoolean()) return; if (matchesPartialName.test(c) && classPathClasses.isAccessibleFromPackage(c, packageName)) { - result.add(Completion.ofNotImportedClass(c)); + result.add(Completion.ofClassName(c, isImported(c))); } } Predicate<Path> matchesFileName = @@ -1003,7 +1018,7 @@ public class JavaCompilerService { var c = qualifiedName.apply(file); // Slow check, open file if (matchesPartialName.test(c) && isPublic.test(file)) { - result.add(Completion.ofNotImportedClass(c)); + result.add(Completion.ofClassName(c, isImported(c))); } } } diff --git a/src/main/java/org/javacs/JavaTextDocumentService.java b/src/main/java/org/javacs/JavaTextDocumentService.java index d24d959..767e5a0 100644 --- a/src/main/java/org/javacs/JavaTextDocumentService.java +++ b/src/main/java/org/javacs/JavaTextDocumentService.java @@ -103,11 +103,12 @@ class JavaTextDocumentService implements TextDocumentService { i.setKind(CompletionItemKind.Keyword); i.setDetail("keyword"); i.setSortText(3 + i.getLabel()); - } else if (c.notImportedClass != null) { - i.setLabel(Parser.lastName(c.notImportedClass)); + } else if (c.className != null) { + i.setLabel(Parser.lastName(c.className.name)); i.setKind(CompletionItemKind.Class); - i.setDetail(c.notImportedClass); - i.setSortText(4 + i.getLabel()); + i.setDetail(c.className.name); + if (c.className.isImported) i.setSortText(2 + i.getLabel()); + else i.setSortText(4 + i.getLabel()); } else if (c.snippet != null) { i.setLabel(c.snippet.label); i.setKind(CompletionItemKind.Snippet); @@ -188,8 +189,8 @@ class JavaTextDocumentService implements TextDocumentService { LOG.info("Don't know how to look up docs for element " + cached.element); } // TODO constructors, fields - } else if (cached.notImportedClass != null) { - var doc = server.compiler.classDoc(cached.notImportedClass); + } else if (cached.className != null) { + var doc = server.compiler.classDoc(cached.className.name); var markdown = doc.map(this::asMarkupContent); markdown.ifPresent(unresolved::setDocumentation); } diff --git a/src/test/java/org/javacs/JavaCompilerServiceTest.java b/src/test/java/org/javacs/JavaCompilerServiceTest.java index d5cf119..0c04930 100644 --- a/src/test/java/org/javacs/JavaCompilerServiceTest.java +++ b/src/test/java/org/javacs/JavaCompilerServiceTest.java @@ -71,7 +71,7 @@ public class JavaCompilerServiceTest { if (c.element != null) result.add(c.element.getSimpleName().toString()); else if (c.packagePart != null) result.add(c.packagePart.name); else if (c.keyword != null) result.add(c.keyword); - else if (c.notImportedClass != null) result.add(Parser.lastName(c.notImportedClass)); + else if (c.className != null) result.add(Parser.lastName(c.className.name)); else if (c.snippet != null) result.add(c.snippet.snippet); } return result; |