From 5a1b1cfc69232cab7e7198422f6e42ae6ace806b Mon Sep 17 00:00:00 2001 From: David Thompson Date: Tue, 31 Jan 2023 15:59:20 -0500 Subject: [PATCH] Context-aware Java snippets Closes #782 Signed-off-by: David Thompson --- .../com/redhat/quarkus/snippets/quarkus-java.json | 9 ++++++--- .../quarkus/ls/JavaTextDocumentSnippetRegistryTest.java | 8 ++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/quarkus.ls.ext/com.redhat.quarkus.ls/src/main/resources/com/redhat/quarkus/snippets/quarkus-java.json b/quarkus.ls.ext/com.redhat.quarkus.ls/src/main/resources/com/redhat/quarkus/snippets/quarkus-java.json index 35d7a669f..30e36df3e 100644 --- a/quarkus.ls.ext/com.redhat.quarkus.ls/src/main/resources/com/redhat/quarkus/snippets/quarkus-java.json +++ b/quarkus.ls.ext/com.redhat.quarkus.ls/src/main/resources/com/redhat/quarkus/snippets/quarkus-java.json @@ -26,7 +26,8 @@ ], "description": "Quarkus test resource class", "context": { - "type": "io.quarkus.test.junit.QuarkusTest" + "type": "io.quarkus.test.junit.QuarkusTest", + "contentType": "class" } }, "Quarkus - new native test resource class": { @@ -44,7 +45,8 @@ ], "description": "Quarkus native test resource class", "context": { - "type": "io.quarkus.test.junit.SubstrateTest" + "type": "io.quarkus.test.junit.SubstrateTest", + "contentType": "class" } }, "Quarkus - new integration test resource class": { @@ -62,7 +64,8 @@ ], "description": "Quarkus integration test resource class", "context": { - "type": "io.quarkus.test.junit.QuarkusIntegrationTest" + "type": "io.quarkus.test.junit.QuarkusIntegrationTest", + "contentType": "class" } } } diff --git a/quarkus.ls.ext/com.redhat.quarkus.ls/src/test/java/com/redhat/quarkus/ls/JavaTextDocumentSnippetRegistryTest.java b/quarkus.ls.ext/com.redhat.quarkus.ls/src/test/java/com/redhat/quarkus/ls/JavaTextDocumentSnippetRegistryTest.java index 215b66e29..f6eee97db 100644 --- a/quarkus.ls.ext/com.redhat.quarkus.ls/src/test/java/com/redhat/quarkus/ls/JavaTextDocumentSnippetRegistryTest.java +++ b/quarkus.ls.ext/com.redhat.quarkus.ls/src/test/java/com/redhat/quarkus/ls/JavaTextDocumentSnippetRegistryTest.java @@ -19,6 +19,8 @@ import org.junit.Test; import org.eclipse.lsp4mp.ls.java.JavaTextDocumentSnippetRegistry; +import org.eclipse.lsp4mp.commons.JavaCursorContextKind; +import org.eclipse.lsp4mp.commons.JavaCursorContextResult; import org.eclipse.lsp4mp.commons.ProjectLabelInfoEntry; import org.eclipse.lsp4mp.ls.commons.snippets.ISnippetContext; import org.eclipse.lsp4mp.ls.commons.snippets.Snippet; @@ -60,6 +62,12 @@ public void javaSnippets() { Arrays.asList("io.quarkus.test.junit.QuarkusTest")); boolean match2 = ((SnippetContextForJava) context).isMatch(new JavaSnippetCompletionContext(projectInfo2, null)); Assert.assertTrue("Project has io.quarkus.test.junit.QuarkusTest type", match2); + + boolean match3 = ((SnippetContextForJava) context).isMatch(new JavaSnippetCompletionContext(projectInfo2, new JavaCursorContextResult(JavaCursorContextKind.BEFORE_CLASS, "aaaa"))); + Assert.assertFalse("Cursor is not in an empty file", match3); + + boolean match4 = ((SnippetContextForJava) context).isMatch(new JavaSnippetCompletionContext(projectInfo2, new JavaCursorContextResult(JavaCursorContextKind.IN_EMPTY_FILE, "aaaa"))); + Assert.assertTrue("Cursor is in an empty file", match4); } private static Optional findByPrefix(String prefix, SnippetRegistry registry) {