diff options
Diffstat (limited to 'src')
4 files changed, 18 insertions, 9 deletions
diff --git a/src/main/java/org/javacs/Completion.java b/src/main/java/org/javacs/Completion.java index 48e57ee..48a43db 100644 --- a/src/main/java/org/javacs/Completion.java +++ b/src/main/java/org/javacs/Completion.java @@ -11,10 +11,10 @@ public class Completion { public final PackagePart packagePart; public final String keyword; public final String notImportedClass; - public final String snippet; // TODO separate label and insertText + public final Snippet snippet; // TODO separate label and insertText private Completion( - Element element, PackagePart packagePart, String keyword, String notImportedClass, String snippet) { + Element element, PackagePart packagePart, String keyword, String notImportedClass, Snippet snippet) { this.element = element; this.packagePart = packagePart; this.keyword = keyword; @@ -38,8 +38,8 @@ public class Completion { return new Completion(null, null, null, className, null); } - public static Completion ofSnippet(String snippet) { - return new Completion(null, null, null, null, snippet); + public static Completion ofSnippet(String label, String snippet) { + return new Completion(null, null, null, null, new Snippet(label, snippet)); } public static class PackagePart { @@ -50,4 +50,13 @@ public class Completion { this.name = name; } } + + public static class Snippet { + public final String label, snippet; + + public Snippet(String label, String snippet) { + this.label = label; + this.snippet = snippet; + } + } } diff --git a/src/main/java/org/javacs/JavaCompilerService.java b/src/main/java/org/javacs/JavaCompilerService.java index f1587dc..94c0641 100644 --- a/src/main/java/org/javacs/JavaCompilerService.java +++ b/src/main/java/org/javacs/JavaCompilerService.java @@ -703,7 +703,7 @@ public class JavaCompilerService { if (parse.getPackage() == null) { relativeToSourcePath(file).ifPresent(relative -> { var name = relative.toString().replace(File.separatorChar, '.'); - result.add(Completion.ofSnippet("package " + name + ";\n\n")); + result.add(Completion.ofSnippet("package " + name, "package " + name + ";\n\n")); }); } // If no class declaration is present, suggest class [file name] @@ -716,7 +716,7 @@ public class JavaCompilerService { if (!hasClassDeclaration) { var name = Paths.get(file).getFileName().toString(); name = name.substring(0, name.length() - ".java".length()); - result.add(Completion.ofSnippet("class " + name + " {\n $0\n}")); + result.add(Completion.ofSnippet("class " + name, "class " + name + " {\n $0\n}")); } // Add keywords for (var k : TOP_LEVEL_KEYWORDS) { diff --git a/src/main/java/org/javacs/JavaTextDocumentService.java b/src/main/java/org/javacs/JavaTextDocumentService.java index 7c93a85..a5e44e6 100644 --- a/src/main/java/org/javacs/JavaTextDocumentService.java +++ b/src/main/java/org/javacs/JavaTextDocumentService.java @@ -109,9 +109,9 @@ class JavaTextDocumentService implements TextDocumentService { i.setDetail(c.notImportedClass); i.setSortText(1 + i.getLabel()); } else if (c.snippet != null) { - i.setLabel(c.snippet); + i.setLabel(c.snippet.label); i.setKind(CompletionItemKind.Snippet); - i.setInsertText(c.snippet); + i.setInsertText(c.snippet.snippet); i.setInsertTextFormat(InsertTextFormat.Snippet); i.setSortText(0 + i.getLabel()); } else throw new RuntimeException(c + " is not valid"); diff --git a/src/test/java/org/javacs/JavaCompilerServiceTest.java b/src/test/java/org/javacs/JavaCompilerServiceTest.java index e1f43fd..764da38 100644 --- a/src/test/java/org/javacs/JavaCompilerServiceTest.java +++ b/src/test/java/org/javacs/JavaCompilerServiceTest.java @@ -72,7 +72,7 @@ public class JavaCompilerServiceTest { 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.snippet != null) result.add(c.snippet); + else if (c.snippet != null) result.add(c.snippet.snippet); } return result; } |