From 2d6ad465a829351cc63b9f9f57ce1ccc7cde5d71 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Fri, 9 Dec 2022 10:30:36 -0500 Subject: [PATCH] Use template to determine jakarta namespace in snippets - Use `${jakarta}` to point to either `javax` or `jakarta`, depending on which is available, and prefering `jakarta` - Rename `jaxrc` to `rest_class` and `jaxrm` to `rest_get` Closes #320, closes #229 Signed-off-by: David Thompson --- .../java/JavaTextDocumentSnippetRegistry.java | 25 +++++++++- ...MicroProfileJavaSnippetRegistryLoader.java | 2 +- .../eclipse/lsp4mp/snippets/jakarta-rest.json | 47 +++++++++++++++++++ .../org/eclipse/lsp4mp/snippets/jax-rs.json | 41 ---------------- .../eclipse/lsp4mp/snippets/mp-health.json | 4 +- .../lsp4mp/snippets/mp-restclient.json | 8 ++-- .../JavaTextDocumentSnippetRegistryTest.java | 29 ++++++++---- 7 files changed, 98 insertions(+), 58 deletions(-) create mode 100644 microprofile.ls/org.eclipse.lsp4mp.ls/src/main/resources/org/eclipse/lsp4mp/snippets/jakarta-rest.json delete mode 100644 microprofile.ls/org.eclipse.lsp4mp.ls/src/main/resources/org/eclipse/lsp4mp/snippets/jax-rs.json diff --git a/microprofile.ls/org.eclipse.lsp4mp.ls/src/main/java/org/eclipse/lsp4mp/ls/java/JavaTextDocumentSnippetRegistry.java b/microprofile.ls/org.eclipse.lsp4mp.ls/src/main/java/org/eclipse/lsp4mp/ls/java/JavaTextDocumentSnippetRegistry.java index 9d7dc156c..001873fb0 100644 --- a/microprofile.ls/org.eclipse.lsp4mp.ls/src/main/java/org/eclipse/lsp4mp/ls/java/JavaTextDocumentSnippetRegistry.java +++ b/microprofile.ls/org.eclipse.lsp4mp.ls/src/main/java/org/eclipse/lsp4mp/ls/java/JavaTextDocumentSnippetRegistry.java @@ -17,6 +17,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.function.BiFunction; import java.util.function.BiPredicate; import org.eclipse.lsp4j.CompletionItem; @@ -41,6 +42,17 @@ public class JavaTextDocumentSnippetRegistry extends TextDocumentSnippetRegistry { private static final String PACKAGENAME_KEY = "packagename"; + private static final String JAVAX_OR_JAKARTA_KEY = "jakarta"; + private static final String JAVAX_VALUE = "javax"; + private static final String JAKARTA_VALUE = JAVAX_OR_JAKARTA_KEY; + + private Boolean hasJakarta = null; + + /** + * The type whose presence indicates that the jakarta namespace should be used. + */ + private static final String JAKARTA_FLAG_TYPE = "jakarta.ws.rs.GET"; + private List types; public JavaTextDocumentSnippetRegistry() { @@ -69,6 +81,7 @@ private synchronized List collectTypes() { return types; } List types = new ArrayList<>(); + types.add(JAKARTA_FLAG_TYPE); for (Snippet snippet : getSnippets()) { if (snippet.getContext() != null && snippet.getContext() instanceof SnippetContextForJava) { List snippetTypes = ((SnippetContextForJava) snippet.getContext()).getTypes(); @@ -84,6 +97,15 @@ private synchronized List collectTypes() { return types; } + private boolean hasJakarta() { + if (hasJakarta != null) { + return hasJakarta; + } + hasJakarta = getTypes().stream() // + .reduce(false, (jakartaPresent, type) -> jakartaPresent || JAKARTA_FLAG_TYPE.equals(type), (a, b) -> a || b); + return hasJakarta; + } + @Override public void registerSnippet(Snippet snippet) { preprocessSnippetBody(snippet); @@ -92,7 +114,7 @@ public void registerSnippet(Snippet snippet) { /** * Preprocess Snippet body for managing package name. - * + * * @param snippet */ private void preprocessSnippetBody(Snippet snippet) { @@ -154,6 +176,7 @@ public List getCompletionItems(JavaTextDocument document, int co } } model.put(PACKAGENAME_KEY, packageStatement); + model.put(JAVAX_OR_JAKARTA_KEY, hasJakarta() ? JAKARTA_VALUE : JAVAX_VALUE); return super.getCompletionItems(document, completionOffset, canSupportMarkdown, snippetsSupported, contextFilter, model); } diff --git a/microprofile.ls/org.eclipse.lsp4mp.ls/src/main/java/org/eclipse/lsp4mp/snippets/MicroProfileJavaSnippetRegistryLoader.java b/microprofile.ls/org.eclipse.lsp4mp.ls/src/main/java/org/eclipse/lsp4mp/snippets/MicroProfileJavaSnippetRegistryLoader.java index 8a6b9e69c..cdd493805 100644 --- a/microprofile.ls/org.eclipse.lsp4mp.ls/src/main/java/org/eclipse/lsp4mp/snippets/MicroProfileJavaSnippetRegistryLoader.java +++ b/microprofile.ls/org.eclipse.lsp4mp.ls/src/main/java/org/eclipse/lsp4mp/snippets/MicroProfileJavaSnippetRegistryLoader.java @@ -38,7 +38,7 @@ public void load(SnippetRegistry registry) throws IOException { SnippetContextForJava.TYPE_ADAPTER); registry.registerSnippets(MicroProfileJavaSnippetRegistryLoader.class.getResourceAsStream("mp-health.json"), SnippetContextForJava.TYPE_ADAPTER); - registry.registerSnippets(MicroProfileJavaSnippetRegistryLoader.class.getResourceAsStream("jax-rs.json"), + registry.registerSnippets(MicroProfileJavaSnippetRegistryLoader.class.getResourceAsStream("jakarta-rest.json"), SnippetContextForJava.TYPE_ADAPTER); } diff --git a/microprofile.ls/org.eclipse.lsp4mp.ls/src/main/resources/org/eclipse/lsp4mp/snippets/jakarta-rest.json b/microprofile.ls/org.eclipse.lsp4mp.ls/src/main/resources/org/eclipse/lsp4mp/snippets/jakarta-rest.json new file mode 100644 index 000000000..94bf13612 --- /dev/null +++ b/microprofile.ls/org.eclipse.lsp4mp.ls/src/main/resources/org/eclipse/lsp4mp/snippets/jakarta-rest.json @@ -0,0 +1,47 @@ +{ + "New REST resource class": { + "prefix": "rest_class", + "body": [ + "package ${1:packagename};", + "", + "import ${jakarta}.ws.rs.GET;", + "import ${jakarta}.ws.rs.Path;", + "import ${jakarta}.ws.rs.Produces;", + "import ${jakarta}.ws.rs.core.MediaType;", + "", + "@Path(\"${2:/path}\")", + "public class ${TM_FILENAME_BASE} {", + "", + "\t@GET", + "\t@Produces(MediaType.TEXT_PLAIN)", + "\tpublic String ${3:methodname}() {", + "\t\treturn \"hello\";", + "\t}", + "}" + ], + "description": "REST resource class", + "context": { + "type": [ + "javax.ws.rs.GET", + "jakarta.ws.rs.GET" + ] + } + }, + "New REST resource GET method": { + "prefix": "rest_get", + "body": [ + "@GET", + "@Produces(MediaType.TEXT_PLAIN)", + "public String ${1:methodname}() {", + "\treturn \"hello\";", + "}" + ], + "description": "REST resource GET method", + "context": { + "type": [ + "javax.ws.rs.GET", + "jakarta.ws.rs.GET" + ] + } + } +} diff --git a/microprofile.ls/org.eclipse.lsp4mp.ls/src/main/resources/org/eclipse/lsp4mp/snippets/jax-rs.json b/microprofile.ls/org.eclipse.lsp4mp.ls/src/main/resources/org/eclipse/lsp4mp/snippets/jax-rs.json deleted file mode 100644 index 452c0338d..000000000 --- a/microprofile.ls/org.eclipse.lsp4mp.ls/src/main/resources/org/eclipse/lsp4mp/snippets/jax-rs.json +++ /dev/null @@ -1,41 +0,0 @@ -{ - "JAX-RS - new resource class": { - "prefix": "jaxrc", - "body": [ - "package ${1:packagename};", - "", - "import javax.ws.rs.GET;", - "import javax.ws.rs.Path;", - "import javax.ws.rs.Produces;", - "import javax.ws.rs.core.MediaType;", - "", - "@Path(\"${2:/path}\")", - "public class ${TM_FILENAME_BASE} {", - "", - "\t@GET", - "\t@Produces(MediaType.TEXT_PLAIN)", - "\tpublic String ${3:methodname}() {", - "\t\treturn \"hello\";", - "\t}", - "}" - ], - "description": "JAX-RS REST resource class", - "context": { - "type": "javax.ws.rs.GET" - } - }, - "JAX-RS - new resource method": { - "prefix": "jaxrm", - "body": [ - "@GET", - "@Produces(MediaType.TEXT_PLAIN)", - "public String ${1:methodname}() {", - "\treturn \"hello\";", - "}" - ], - "description": "JAX-RS REST resource method", - "context": { - "type": "javax.ws.rs.GET" - } - } -} diff --git a/microprofile.ls/org.eclipse.lsp4mp.ls/src/main/resources/org/eclipse/lsp4mp/snippets/mp-health.json b/microprofile.ls/org.eclipse.lsp4mp.ls/src/main/resources/org/eclipse/lsp4mp/snippets/mp-health.json index b468ceabf..e88f7158c 100644 --- a/microprofile.ls/org.eclipse.lsp4mp.ls/src/main/resources/org/eclipse/lsp4mp/snippets/mp-health.json +++ b/microprofile.ls/org.eclipse.lsp4mp.ls/src/main/resources/org/eclipse/lsp4mp/snippets/mp-health.json @@ -8,7 +8,7 @@ "import org.eclipse.microprofile.health.HealthCheckResponse;", "import org.eclipse.microprofile.health.Readiness;", "", - "import javax.enterprise.context.ApplicationScoped;", + "import ${jakarta}.enterprise.context.ApplicationScoped;", "", "@Readiness", "@ApplicationScoped", @@ -34,7 +34,7 @@ "import org.eclipse.microprofile.health.HealthCheckResponse;", "import org.eclipse.microprofile.health.Liveness;", "", - "import javax.enterprise.context.ApplicationScoped;", + "import ${jakarta}.enterprise.context.ApplicationScoped;", "", "@Liveness", "@ApplicationScoped", diff --git a/microprofile.ls/org.eclipse.lsp4mp.ls/src/main/resources/org/eclipse/lsp4mp/snippets/mp-restclient.json b/microprofile.ls/org.eclipse.lsp4mp.ls/src/main/resources/org/eclipse/lsp4mp/snippets/mp-restclient.json index 6ea02ea03..a0701b38d 100644 --- a/microprofile.ls/org.eclipse.lsp4mp.ls/src/main/resources/org/eclipse/lsp4mp/snippets/mp-restclient.json +++ b/microprofile.ls/org.eclipse.lsp4mp.ls/src/main/resources/org/eclipse/lsp4mp/snippets/mp-restclient.json @@ -6,10 +6,10 @@ "", "import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;", "", - "import javax.enterprise.context.ApplicationScoped;", - "import javax.ws.rs.GET;", - "import javax.ws.rs.Path;", - "import javax.ws.rs.PathParam;", + "import ${jakarta}.enterprise.context.ApplicationScoped;", + "import ${jakarta}.ws.rs.GET;", + "import ${jakarta}.ws.rs.Path;", + "import ${jakarta}.ws.rs.PathParam;", "", "@RegisterRestClient", "@ApplicationScoped", diff --git a/microprofile.ls/org.eclipse.lsp4mp.ls/src/test/java/org/eclipse/lsp4mp/ls/java/JavaTextDocumentSnippetRegistryTest.java b/microprofile.ls/org.eclipse.lsp4mp.ls/src/test/java/org/eclipse/lsp4mp/ls/java/JavaTextDocumentSnippetRegistryTest.java index 3e8ddac74..4f9af47c6 100644 --- a/microprofile.ls/org.eclipse.lsp4mp.ls/src/test/java/org/eclipse/lsp4mp/ls/java/JavaTextDocumentSnippetRegistryTest.java +++ b/microprofile.ls/org.eclipse.lsp4mp.ls/src/test/java/org/eclipse/lsp4mp/ls/java/JavaTextDocumentSnippetRegistryTest.java @@ -5,7 +5,7 @@ * http://www.eclipse.org/legal/epl-v20.html * * SPDX-License-Identifier: EPL-2.0 -* +* * Contributors: * Red Hat Inc. - initial API and implementation *******************************************************************************/ @@ -33,7 +33,7 @@ /** * Test for Java snippet registry. - * + * * @author Angelo ZERR * */ @@ -47,21 +47,32 @@ public void haveJavaSnippets() { } @Test - public void jaxrsSnippets() { - Optional jaxrsSnippet = findByPrefix("jaxrc", registry); - assertTrue("Tests has jaxrc Java snippets", jaxrsSnippet.isPresent()); + public void restSnippets() { + Optional restClassSnippet = findByPrefix("rest_class", registry); + assertTrue("Tests has rest_class Java snippet", restClassSnippet.isPresent()); + + Optional restGetSnippet = findByPrefix("rest_get", registry); + assertTrue("Tests has rest_get Java snippet", restGetSnippet.isPresent()); - ISnippetContext context = jaxrsSnippet.get().getContext(); - assertNotNull("jaxrc snippet has context", context); - assertTrue("jaxrc snippet context is Java context", context instanceof SnippetContextForJava); + ISnippetContext context = restClassSnippet.get().getContext(); + assertNotNull("rest_class snippet has context", context); + assertTrue("rest_class snippet context is Java context", context instanceof SnippetContextForJava); ProjectLabelInfoEntry projectInfo = new ProjectLabelInfoEntry("", "", new ArrayList<>()); boolean match = ((SnippetContextForJava) context).isMatch(projectInfo); - assertFalse("Project has no javax.ws.rs.GET type", match); + assertFalse("Project has no javax.ws.rs.GET or jakarta.ws.rs.GET type", match); ProjectLabelInfoEntry projectInfo2 = new ProjectLabelInfoEntry("", "", Arrays.asList("javax.ws.rs.GET")); boolean match2 = ((SnippetContextForJava) context).isMatch(projectInfo2); assertTrue("Project has javax.ws.rs.GET type", match2); + + ProjectLabelInfoEntry projectInfo3 = new ProjectLabelInfoEntry("", "", Arrays.asList("jakarta.ws.rs.GET")); + boolean match3 = ((SnippetContextForJava) context).isMatch(projectInfo3); + assertTrue("Project has jakarta.ws.rs.GET type", match3); + + ProjectLabelInfoEntry projectInfo4 = new ProjectLabelInfoEntry("", "", Arrays.asList("javax.ws.rs.GET", "jakarta.ws.rs.GET")); + boolean match4= ((SnippetContextForJava) context).isMatch(projectInfo4); + assertTrue("Project has javax.ws.rs.GET and jakarta.ws.rs.GET types", match4); } @Test