Skip to content

Commit

Permalink
Use template to determine jakarta namespace in snippets
Browse files Browse the repository at this point in the history
- 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 eclipse#320, closes eclipse#229

Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
datho7561 committed Dec 9, 2022
1 parent 2d6a264 commit 0acc431
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> types;

public JavaTextDocumentSnippetRegistry() {
Expand Down Expand Up @@ -69,6 +81,7 @@ private synchronized List<String> collectTypes() {
return types;
}
List<String> types = new ArrayList<>();
types.add(JAKARTA_FLAG_TYPE);
for (Snippet snippet : getSnippets()) {
if (snippet.getContext() != null && snippet.getContext() instanceof SnippetContextForJava) {
List<String> snippetTypes = ((SnippetContextForJava) snippet.getContext()).getTypes();
Expand All @@ -84,6 +97,15 @@ private synchronized List<String> 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);
Expand All @@ -92,7 +114,7 @@ public void registerSnippet(Snippet snippet) {

/**
* Preprocess Snippet body for managing package name.
*
*
* @param snippet
*/
private void preprocessSnippetBody(Snippet snippet) {
Expand Down Expand Up @@ -124,6 +146,13 @@ private void preprocessSnippetBody(Snippet snippet) {
// org.eclipse.microprofile.health.HealthCheck;
body.set(0, "${" + PACKAGENAME_KEY + "}" + line);
}
/*for (int i = 0; i < snippet.getBody().size(); i++) {
String line = snippet.getBody().get(i);
if (line.contains(JAVAX_OR_JAKARTA_KEY)) {
String newLine = line.replace("${" + JAVAX_OR_JAKARTA_KEY + "}", hasJakarta() ? JAKARTA_VALUE : JAVAX_VALUE);
snippet.getBody().set(i, newLine);
}
}*/
}

public List<CompletionItem> getCompletionItems(JavaTextDocument document, int completionOffset,
Expand Down Expand Up @@ -154,6 +183,7 @@ public List<CompletionItem> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"JAX-RS - new resource class": {
"prefix": "jaxrc",
"New resource class": {
"prefix": "rest_class",
"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;",
"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} {",
Expand All @@ -19,23 +19,29 @@
"\t}",
"}"
],
"description": "JAX-RS REST resource class",
"description": "REST resource class",
"context": {
"type": "javax.ws.rs.GET"
"type": [
"javax.ws.rs.GET",
"jakarta.ws.rs.GET"
]
}
},
"JAX-RS - new resource method": {
"prefix": "jaxrm",
"New resource GET method": {
"prefix": "rest_get",
"body": [
"@GET",
"@Produces(MediaType.TEXT_PLAIN)",
"public String ${1:methodname}() {",
"\treturn \"hello\";",
"}"
],
"description": "JAX-RS REST resource method",
"description": "REST resource GET method",
"context": {
"type": "javax.ws.rs.GET"
"type": [
"javax.ws.rs.GET",
"jakarta.ws.rs.GET"
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*******************************************************************************/
Expand Down Expand Up @@ -33,7 +33,7 @@

/**
* Test for Java snippet registry.
*
*
* @author Angelo ZERR
*
*/
Expand All @@ -47,21 +47,32 @@ public void haveJavaSnippets() {
}

@Test
public void jaxrsSnippets() {
Optional<Snippet> jaxrsSnippet = findByPrefix("jaxrc", registry);
assertTrue("Tests has jaxrc Java snippets", jaxrsSnippet.isPresent());
public void restSnippets() {
Optional<Snippet> restClassSnippet = findByPrefix("rest_class", registry);
assertTrue("Tests has rest_class Java snippet", restClassSnippet.isPresent());

Optional<Snippet> 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
Expand Down

0 comments on commit 0acc431

Please sign in to comment.