diff --git a/projects/qute/projects/maven/qute-record/src/main/java/org/acme/sample/HelloResource.java b/projects/qute/projects/maven/qute-record/src/main/java/org/acme/sample/HelloResource.java index a1fbb5679..02a4c18b6 100644 --- a/projects/qute/projects/maven/qute-record/src/main/java/org/acme/sample/HelloResource.java +++ b/projects/qute/projects/maven/qute-record/src/main/java/org/acme/sample/HelloResource.java @@ -18,6 +18,9 @@ record Bonjour(String name) implements TemplateInstance {} record Status() {} + @CheckedTemplate(basePath="Foo", defaultName=CheckedTemplate.HYPHENATED_ELEMENT_NAME) + record HelloWorld(String name) implements TemplateInstance {} + @GET @Produces(MediaType.TEXT_PLAIN) public TemplateInstance get(@QueryParam("name") String name) { diff --git a/projects/qute/projects/maven/qute-record/src/main/resources/templates/Hello.html b/projects/qute/projects/maven/qute-record/src/main/resources/templates/HelloResource/Hello.html similarity index 100% rename from projects/qute/projects/maven/qute-record/src/main/resources/templates/Hello.html rename to projects/qute/projects/maven/qute-record/src/main/resources/templates/HelloResource/Hello.html diff --git a/src/main/java/com/redhat/devtools/intellij/qute/psi/internal/java/AbstractQuteTemplateLinkCollector.java b/src/main/java/com/redhat/devtools/intellij/qute/psi/internal/java/AbstractQuteTemplateLinkCollector.java index fee4dc31c..f782edbb7 100644 --- a/src/main/java/com/redhat/devtools/intellij/qute/psi/internal/java/AbstractQuteTemplateLinkCollector.java +++ b/src/main/java/com/redhat/devtools/intellij/qute/psi/internal/java/AbstractQuteTemplateLinkCollector.java @@ -160,18 +160,16 @@ public void visitClass(PsiClass node) { */ private void visitClassType(PsiClass node) { levelTypeDecl++; - for (PsiAnnotation annotation : node.getAnnotations()) { - if (AnnotationUtils.isMatchAnnotation(annotation, CHECKED_TEMPLATE_ANNOTATION) - || AnnotationUtils.isMatchAnnotation(annotation, OLD_CHECKED_TEMPLATE_ANNOTATION)) { - // @CheckedTemplate - // public static class Templates { - // public static native TemplateInstance book(Book book); - boolean ignoreFragments = isIgnoreFragments(annotation); - String basePath = getBasePath(annotation); - TemplateNameStrategy templateNameStrategy = getDefaultName(annotation); - for (PsiMethod method : node.getMethods()) { - collectTemplateLink(basePath, method, node, ignoreFragments, templateNameStrategy); - } + PsiAnnotation checkedAnnotation = getCheckedAnnotation(node); + if (checkedAnnotation != null) { + // @CheckedTemplate + // public static class Templates { + // public static native TemplateInstance book(Book book); + boolean ignoreFragments = isIgnoreFragments(checkedAnnotation); + String basePath = getBasePath(checkedAnnotation); + TemplateNameStrategy templateNameStrategy = getDefaultName(checkedAnnotation); + for (PsiMethod method : node.getMethods()) { + collectTemplateLinkForMethodOrRecord(basePath, method, method.getName(), node, ignoreFragments, templateNameStrategy); } } super.visitClass(node); @@ -187,8 +185,17 @@ private void visitClassType(PsiClass node) { */ private void visitRecordType(PsiClass node) { if (isImplementTemplateInstance(node)) { + + // public class HelloResource { + // record Hello(String name) implements TemplateInstance {} String recordName = node.getName(); collectTemplateLink(null, node, null, node, null, recordName, false, TemplateNameStrategy.ELEMENT_NAME); + PsiAnnotation checkedAnnotation = getCheckedAnnotation(node); + boolean ignoreFragments = isIgnoreFragments(checkedAnnotation); + String basePath = getBasePath(checkedAnnotation); + TemplateNameStrategy templateNameStrategy = getDefaultName(checkedAnnotation); + collectTemplateLinkForMethodOrRecord(basePath, node, recordName, node, ignoreFragments, + templateNameStrategy); } } @@ -216,14 +223,18 @@ private static PsiClass getTypeDeclaration(PsiElement node) { return PsiTreeUtil.getParentOfType(node, PsiClass.class); } - private void collectTemplateLink(String basePath, PsiMethod methodDeclaration, PsiClass type, boolean ignoreFragments, TemplateNameStrategy templateNameStrategy) { + private void collectTemplateLinkForMethodOrRecord(String basePath, + PsiElement methodOrRecord, + String methodOrRecordName, + PsiClass type, + boolean ignoreFragments, + TemplateNameStrategy templateNameStrategy) { String className = null; - boolean innerClass = levelTypeDecl > 1; + boolean innerClass = methodOrRecord instanceof PsiRecordComponent ? levelTypeDecl >= 1 : levelTypeDecl > 1; if (innerClass) { className = PsiTypeUtils.getSimpleClassName(typeRoot.getName()); } - String methodName = methodDeclaration.getName(); - collectTemplateLink(basePath, methodDeclaration, null, type, className, methodName, ignoreFragments,templateNameStrategy); + collectTemplateLink(basePath, methodOrRecord, null, type, className, methodOrRecordName, ignoreFragments, templateNameStrategy); } private void collectTemplateLink(String basePath, PsiElement fieldOrMethod, PsiLiteralValue locationAnnotation, PsiClass type, String className, diff --git a/src/main/java/com/redhat/devtools/intellij/qute/psi/internal/template/datamodel/CheckedTemplateSupport.java b/src/main/java/com/redhat/devtools/intellij/qute/psi/internal/template/datamodel/CheckedTemplateSupport.java index fa2a1a4c1..49ceec73f 100644 --- a/src/main/java/com/redhat/devtools/intellij/qute/psi/internal/template/datamodel/CheckedTemplateSupport.java +++ b/src/main/java/com/redhat/devtools/intellij/qute/psi/internal/template/datamodel/CheckedTemplateSupport.java @@ -39,6 +39,7 @@ import com.redhat.qute.commons.datamodel.DataModelFragment; import com.redhat.qute.commons.datamodel.DataModelParameter; import com.redhat.qute.commons.datamodel.DataModelTemplate; +import org.jetbrains.annotations.Nullable; /** * CheckedTemplate support for template files: @@ -88,6 +89,16 @@ protected void processAnnotation(PsiElement javaElement, PsiAnnotation checkedTe } } + public static PsiAnnotation getCheckedAnnotation(PsiJvmModifiersOwner node) { + for (PsiAnnotation annotation : node.getAnnotations()) { + if (AnnotationUtils.isMatchAnnotation(annotation, CHECKED_TEMPLATE_ANNOTATION) + || AnnotationUtils.isMatchAnnotation(annotation, OLD_CHECKED_TEMPLATE_ANNOTATION)) { + return annotation; + } + } + return null; + } + /** * Returns true if @CheckedTemplate annotation declares that fragment must be * ignored and false otherwise. @@ -99,22 +110,24 @@ protected void processAnnotation(PsiElement javaElement, PsiAnnotation checkedTe * ignored and false otherwise. * @CheckedTemplate(ignoreFragments=true) */ - public static boolean isIgnoreFragments(PsiAnnotation checkedTemplateAnnotation) { + public static boolean isIgnoreFragments(@Nullable PsiAnnotation checkedTemplateAnnotation) { Boolean ignoreFragment = null; - try { - for (PsiNameValuePair pair : checkedTemplateAnnotation.getParameterList().getAttributes()) { - if (CHECKED_TEMPLATE_ANNOTATION_IGNORE_FRAGMENTS.equalsIgnoreCase(pair.getAttributeName())) { - ignoreFragment = AnnotationUtils.getValueAsBoolean(pair); + if (checkedTemplateAnnotation != null) { + try { + for (PsiNameValuePair pair : checkedTemplateAnnotation.getParameterList().getAttributes()) { + if (CHECKED_TEMPLATE_ANNOTATION_IGNORE_FRAGMENTS.equalsIgnoreCase(pair.getAttributeName())) { + ignoreFragment = AnnotationUtils.getValueAsBoolean(pair); + } } + } catch (ProcessCanceledException e) { + //Since 2024.2 ProcessCanceledException extends CancellationException so we can't use multicatch to keep backward compatibility + //TODO delete block when minimum required version is 2024.2 + throw e; + } catch (IndexNotReadyException | CancellationException e) { + throw e; + } catch (Exception e) { + // Do nothing } - } catch (ProcessCanceledException e) { - //Since 2024.2 ProcessCanceledException extends CancellationException so we can't use multicatch to keep backward compatibility - //TODO delete block when minimum required version is 2024.2 - throw e; - } catch (IndexNotReadyException | CancellationException e) { - throw e; - } catch (Exception e) { - // Do nothing } return ignoreFragment != null ? ignoreFragment.booleanValue() : false; } @@ -127,22 +140,24 @@ public static boolean isIgnoreFragments(PsiAnnotation checkedTemplateAnnotation) * @return the basePath value declared in the @CheckedTemplate annotation * @CheckedTemplate(basePath="somewhere") */ - public static String getBasePath(PsiAnnotation checkedTemplateAnnotation) { + public static String getBasePath(@Nullable PsiAnnotation checkedTemplateAnnotation) { String basePath = null; - try { - for (PsiNameValuePair pair : checkedTemplateAnnotation.getParameterList().getAttributes()) { - if (CHECKED_TEMPLATE_ANNOTATION_BASE_PATH.equalsIgnoreCase(pair.getAttributeName())) { - basePath = pair.getLiteralValue(); + if (checkedTemplateAnnotation != null) { + try { + for (PsiNameValuePair pair : checkedTemplateAnnotation.getParameterList().getAttributes()) { + if (CHECKED_TEMPLATE_ANNOTATION_BASE_PATH.equalsIgnoreCase(pair.getAttributeName())) { + basePath = pair.getLiteralValue(); + } } + } catch (ProcessCanceledException e) { + //Since 2024.2 ProcessCanceledException extends CancellationException so we can't use multicatch to keep backward compatibility + //TODO delete block when minimum required version is 2024.2 + throw e; + } catch (IndexNotReadyException | CancellationException e) { + throw e; + } catch (Exception e) { + // Do nothing } - } catch (ProcessCanceledException e) { - //Since 2024.2 ProcessCanceledException extends CancellationException so we can't use multicatch to keep backward compatibility - //TODO delete block when minimum required version is 2024.2 - throw e; - } catch (IndexNotReadyException | CancellationException e) { - throw e; - } catch (Exception e) { - // Do nothing } return basePath; } @@ -157,28 +172,30 @@ public static String getBasePath(PsiAnnotation checkedTemplateAnnotation) { */ public static TemplateNameStrategy getDefaultName(PsiAnnotation checkedTemplateAnnotation) { TemplateNameStrategy templateNameStrategy = TemplateNameStrategy.ELEMENT_NAME; - try { - for (PsiNameValuePair pair : checkedTemplateAnnotation.getParameterList().getAttributes()) { - if (CHECKED_TEMPLATE_ANNOTATION_DEFAULT_NAME.equalsIgnoreCase(pair.getAttributeName())) { - if (pair.getValue() != null - && pair.getValue().getReference() != null - && pair.getValue().getReference().resolve() != null && - pair.getValue().getReference().resolve() instanceof PsiField field) { - Object value = field.computeConstantValue(); - if (value != null) { - templateNameStrategy = getDefaultName(value.toString()); + if (checkedTemplateAnnotation != null) { + try { + for (PsiNameValuePair pair : checkedTemplateAnnotation.getParameterList().getAttributes()) { + if (CHECKED_TEMPLATE_ANNOTATION_DEFAULT_NAME.equalsIgnoreCase(pair.getAttributeName())) { + if (pair.getValue() != null + && pair.getValue().getReference() != null + && pair.getValue().getReference().resolve() != null && + pair.getValue().getReference().resolve() instanceof PsiField field) { + Object value = field.computeConstantValue(); + if (value != null) { + templateNameStrategy = getDefaultName(value.toString()); + } } } } + } catch (ProcessCanceledException e) { + //Since 2024.2 ProcessCanceledException extends CancellationException so we can't use multicatch to keep backward compatibility + //TODO delete block when minimum required version is 2024.2 + throw e; + } catch (IndexNotReadyException | CancellationException e) { + throw e; + } catch (Exception e) { + // Do nothing } - } catch (ProcessCanceledException e) { - //Since 2024.2 ProcessCanceledException extends CancellationException so we can't use multicatch to keep backward compatibility - //TODO delete block when minimum required version is 2024.2 - throw e; - } catch (IndexNotReadyException | CancellationException e) { - throw e; - } catch (Exception e) { - // Do nothing } return templateNameStrategy; } @@ -211,9 +228,7 @@ private static void collectDataModelTemplateForCheckedTemplate(PsiClass type, ITypeResolver typeResolver, List> templates, ProgressIndicator monitor) { - boolean innerClass = type.getContainingClass() != null; - String className = !innerClass ? null - : PsiTypeUtils.getSimpleClassName(type.getContainingFile().getName()); + String className = getParentClassName(type); // Loop for each method (book, book) and create a template data model per // method. @@ -254,6 +269,13 @@ private static void collectDataModelTemplateForCheckedTemplate(PsiClass type, } } + public static @Nullable String getParentClassName(PsiClass type) { + boolean innerClass = type.getContainingClass() != null; + String className = !innerClass ? null + : PsiTypeUtils.getSimpleClassName(type.getContainingFile().getName()); + return className; + } + private static DataModelTemplate createTemplateDataModel(String templateUri, PsiMethod method, PsiClass type) { String methodName = method.getName(); diff --git a/src/main/java/com/redhat/devtools/intellij/qute/psi/internal/template/datamodel/TemplateRecordsSupport.java b/src/main/java/com/redhat/devtools/intellij/qute/psi/internal/template/datamodel/TemplateRecordsSupport.java index 9e87ba28e..23b62e6cf 100644 --- a/src/main/java/com/redhat/devtools/intellij/qute/psi/internal/template/datamodel/TemplateRecordsSupport.java +++ b/src/main/java/com/redhat/devtools/intellij/qute/psi/internal/template/datamodel/TemplateRecordsSupport.java @@ -12,6 +12,7 @@ package com.redhat.devtools.intellij.qute.psi.internal.template.datamodel; import com.intellij.openapi.progress.ProgressIndicator; +import com.intellij.psi.PsiAnnotation; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiRecordComponent; import com.redhat.devtools.intellij.qute.psi.internal.template.TemplateDataSupport; @@ -26,6 +27,7 @@ import java.util.List; import static com.redhat.devtools.intellij.qute.psi.internal.QuteJavaConstants.TEMPLATE_INSTANCE_INTERFACE; +import static com.redhat.devtools.intellij.qute.psi.internal.template.datamodel.CheckedTemplateSupport.*; import static com.redhat.devtools.intellij.qute.psi.utils.PsiQuteProjectUtils.getTemplatePath; /** @@ -36,6 +38,9 @@ *

* record Hello(String name) implements TemplateInstance {} *

+ *

+ * @CheckedTemplate(basePath="Foo", defaultName=CheckedTemplate.HYPHENATED_ELEMENT_NAME) + * record HelloWorld(String name) implements TemplateInstance {} * ... *

*

@@ -76,13 +81,18 @@ private static void collectDataModelTemplateForTemplateRecord(PsiClass type, templates.add(template); } - private static DataModelTemplate createTemplateDataModel(PsiClass type, + private static DataModelTemplate createTemplateDataModel(PsiClass recordType, String relativeTemplateBaseDir, ProgressIndicator monitor) { - String recordName = type.getName(); + PsiAnnotation checkedTemplateAnnotation = getCheckedAnnotation(recordType); + boolean ignoreFragments = isIgnoreFragments(checkedTemplateAnnotation); + String basePath = getBasePath(checkedTemplateAnnotation); + TemplateNameStrategy templateNameStrategy = getDefaultName(checkedTemplateAnnotation); + String className = getParentClassName(recordType); + String recordName = recordType.getName(); // src/main/resources/templates/${recordName}.qute.html - String templateUri = getTemplatePath(relativeTemplateBaseDir, null, null, recordName, true, TemplateNameStrategy.ELEMENT_NAME).getTemplateUri(); + String templateUri = getTemplatePath(relativeTemplateBaseDir, basePath, className, recordName, ignoreFragments, templateNameStrategy).getTemplateUri(); // Create template data model with: // - template uri : Qute template file which must be bind with data model. @@ -91,10 +101,10 @@ private static DataModelTemplate createTemplateDataModel(Psi DataModelTemplate template = new DataModelTemplate(); template.setParameters(new ArrayList<>()); template.setTemplateUri(templateUri); - template.setSourceType(type.getQualifiedName()); + template.setSourceType(recordType.getQualifiedName()); // Collect data parameters from the record fields - for (PsiRecordComponent field : type.getRecordComponents()) { + for (PsiRecordComponent field : recordType.getRecordComponents()) { DataModelParameter parameter = new DataModelParameter(); parameter.setKey(field.getName()); parameter.setSourceType(PsiTypeUtils.resolveSignature(field.getType(), field.isVarArgs())); @@ -106,7 +116,7 @@ private static DataModelTemplate createTemplateDataModel(Psi } // Collect data parameters for the given template - TemplateDataSupport.collectParametersFromDataMethodInvocation(type, template, monitor); + TemplateDataSupport.collectParametersFromDataMethodInvocation(recordType, template, monitor); return template; } diff --git a/src/test/java/com/redhat/devtools/intellij/qute/psi/java/MavenJavaCodeLensTest.java b/src/test/java/com/redhat/devtools/intellij/qute/psi/java/MavenJavaCodeLensTest.java index a985e0b54..349b58859 100644 --- a/src/test/java/com/redhat/devtools/intellij/qute/psi/java/MavenJavaCodeLensTest.java +++ b/src/test/java/com/redhat/devtools/intellij/qute/psi/java/MavenJavaCodeLensTest.java @@ -353,6 +353,10 @@ public void testTemplateRecord() throws Exception { // record Bonjour(String name) implements TemplateInstance {} // record Status() {} + + // @CheckedTemplate(basePath="Foo", defaultName=CheckedTemplate.HYPHENATED_ELEMENT_NAME) + // record HelloWorld(String name) implements TemplateInstance {} + var module = loadMavenProject(QuteMavenProjectName.qute_record); QuteJavaCodeLensParams params = new QuteJavaCodeLensParams(); @@ -362,16 +366,20 @@ public void testTemplateRecord() throws Exception { List lenses = QuteSupportForJava.getInstance().codeLens(params, PsiUtilsLSImpl.getInstance(myProject), new EmptyProgressIndicator()); - String helloFileUri = LSPIJUtils.toUri(module).resolve("/src/main/resources/templates/Hello.html").toASCIIString(); - String bonjourFileUri = LSPIJUtils.toUri(module).resolve("/src/main/resources/templates/Bonjour.html").toASCIIString(); + String helloFileUri = LSPIJUtils.toUri(module).resolve("/src/main/resources/templates/HelloResource/Hello.html").toASCIIString(); + String bonjourFileUri = LSPIJUtils.toUri(module).resolve("/src/main/resources/templates/HelloResource/Bonjour.html").toASCIIString(); + String helloWorldFileUri = LSPIJUtils.toUri(module).resolve("/src/main/resources/templates/Foo/hello-world.html").toASCIIString(); assertCodeLens(lenses, // - cl(r(14, 4, 14, 60), // - "Open `src/main/resources/templates/Hello.html`", // + cl(r(15, 4, 15, 60), // + "Open `src/main/resources/templates/HelloResource/Hello.html`", // "qute.command.open.uri", Arrays.asList(helloFileUri)), // - cl(r(16, 4, 16, 62), // - "Create `src/main/resources/templates/Bonjour.html`", // - "qute.command.generate.template.file", Arrays.asList(bonjourFileUri))); + cl(r(17, 4, 17, 62), // + "Create `src/main/resources/templates/HelloResource/Bonjour.html`", // + "qute.command.generate.template.file", Arrays.asList(bonjourFileUri)), // + cl(r(21, 4, 22, 65), // + "Create `src/main/resources/templates/Foo/hello-world.html`", // + "qute.command.generate.template.file", Arrays.asList(helloWorldFileUri))); } @Test diff --git a/src/test/java/com/redhat/devtools/intellij/qute/psi/java/MavenJavaDiagnosticsTest.java b/src/test/java/com/redhat/devtools/intellij/qute/psi/java/MavenJavaDiagnosticsTest.java index 1b6ff7e92..26213a1b7 100644 --- a/src/test/java/com/redhat/devtools/intellij/qute/psi/java/MavenJavaDiagnosticsTest.java +++ b/src/test/java/com/redhat/devtools/intellij/qute/psi/java/MavenJavaDiagnosticsTest.java @@ -265,6 +265,9 @@ public void testTemplateRecord() throws Exception { // record Status() {} + // @CheckedTemplate(basePath="Foo", defaultName=CheckedTemplate.HYPHENATED_ELEMENT_NAME) + // record HelloWorld(String name) implements TemplateInstance {} + var module = loadMavenProject(QuteMavenProjectName.qute_record); QuteJavaDiagnosticsParams params = new QuteJavaDiagnosticsParams(); String javaFileUri = LSPIJUtils.toUri(module).resolve("src/main/java/org/acme/sample/HelloResource.java").toASCIIString(); @@ -272,14 +275,17 @@ public void testTemplateRecord() throws Exception { List publishDiagnostics = QuteSupportForJava.getInstance().diagnostics(params, PsiUtilsLSImpl.getInstance(myProject), new EmptyProgressIndicator()); - assertEquals(1, publishDiagnostics.size()); + assertEquals(2, publishDiagnostics.size()); List diagnostics = publishDiagnostics.get(0).getDiagnostics(); assertEquals(1, diagnostics.size()); assertDiagnostic(diagnostics, // - new Diagnostic(r(16, 11, 16, 18), - "No template matching the path Bonjour could be found for: org.acme.sample.HelloResource$Bonjour", + new Diagnostic(r(17, 11, 17, 18), + "No template matching the path HelloResource/Bonjour could be found for: org.acme.sample.HelloResource$Bonjour", + DiagnosticSeverity.Error, "qute", QuteErrorCode.NoMatchingTemplate.name()), // + new Diagnostic(r(22, 11, 22, 21), + "No template matching the path Foo/HelloWorld could be found for: org.acme.sample.HelloResource$HelloWorld", DiagnosticSeverity.Error, "qute", QuteErrorCode.NoMatchingTemplate.name())); } diff --git a/src/test/java/com/redhat/devtools/intellij/qute/psi/java/MavenJavaDocumentLinkTest.java b/src/test/java/com/redhat/devtools/intellij/qute/psi/java/MavenJavaDocumentLinkTest.java index 4f7ff9b34..3c430e0d3 100644 --- a/src/test/java/com/redhat/devtools/intellij/qute/psi/java/MavenJavaDocumentLinkTest.java +++ b/src/test/java/com/redhat/devtools/intellij/qute/psi/java/MavenJavaDocumentLinkTest.java @@ -290,6 +290,10 @@ public void testTemplateRecord() throws Exception { // record Bonjour(String name) implements TemplateInstance {} // record Status() {} + + // @CheckedTemplate(basePath="Foo", defaultName=CheckedTemplate.HYPHENATED_ELEMENT_NAME) + // record HelloWorld(String name) implements TemplateInstance {} + var module = loadMavenProject(QuteMavenProjectName.qute_record); QuteJavaDocumentLinkParams params = new QuteJavaDocumentLinkParams(); String javaFileUri = LSPIJUtils.toUri(module).resolve("src/main/java/org/acme/sample/HelloResource.java").toASCIIString(); @@ -297,15 +301,19 @@ public void testTemplateRecord() throws Exception { List links = QuteSupportForJava.getInstance().documentLink(params, PsiUtilsLSImpl.getInstance(myProject), new EmptyProgressIndicator()); - assertEquals(2, links.size()); + assertEquals(3, links.size()); - String templateFileUri = LSPIJUtils.toUri(module).resolve("src/main/resources/templates/Hello.html").toASCIIString(); + String helloFileUri = LSPIJUtils.toUri(module).resolve("/src/main/resources/templates/HelloResource/Hello.html").toASCIIString(); + String bonjourFileUri = LSPIJUtils.toUri(module).resolve("/src/main/resources/templates/HelloResource/Bonjour.html").toASCIIString(); + String helloWorldFileUri = LSPIJUtils.toUri(module).resolve("/src/main/resources/templates/Foo/hello-world.html").toASCIIString(); assertDocumentLink(links, // - dl(r(14, 11, 14, 16), // - templateFileUri, "Open `src/main/resources/templates/Hello.html`"), // - dl(r(16, 11, 16, 18), // - templateFileUri, "Create `src/main/resources/templates/Bonjour.html`")); + dl(r(15, 11, 15, 16), // + helloFileUri, "Open `src/main/resources/templates/HelloResource/Hello.html`"), // + dl(r(17, 11, 17, 18), // + bonjourFileUri, "Create `src/main/resources/templates/HelloResource/Bonjour.html`"), + dl(r(22, 11, 22, 21), // + helloWorldFileUri, "Create `src/main/resources/templates/Foo/hello-world.html`")); } @Test diff --git a/src/test/java/com/redhat/devtools/intellij/qute/psi/template/TemplateGetDataModelProjectTest.java b/src/test/java/com/redhat/devtools/intellij/qute/psi/template/TemplateGetDataModelProjectTest.java index 2a60da69d..b92a73c67 100644 --- a/src/test/java/com/redhat/devtools/intellij/qute/psi/template/TemplateGetDataModelProjectTest.java +++ b/src/test/java/com/redhat/devtools/intellij/qute/psi/template/TemplateGetDataModelProjectTest.java @@ -383,13 +383,21 @@ public void testQuteRecord() throws Exception { Assert.assertNotNull(project); // public class HelloResource { + // record Hello(String name) implements TemplateInstance {} - // Hello + // record Bonjour(String name) implements TemplateInstance {} + + // record Status() {} + + // @CheckedTemplate(basePath="Foo", defaultName=CheckedTemplate.HYPHENATED_ELEMENT_NAME) + // record HelloWorld(String name) implements TemplateInstance {} + + // Hello -> DataModelTemplate helloTemplate = project - .findDataModelTemplate("src/main/resources/templates/Hello"); + .findDataModelTemplate("src/main/resources/HelloResource/templates/Hello"); Assert.assertNotNull(helloTemplate); - Assert.assertEquals("src/main/resources/templates/Hello", helloTemplate.getTemplateUri()); + Assert.assertEquals("src/main/resources/templates/HelloResource/Hello", helloTemplate.getTemplateUri()); Assert.assertEquals("org.acme.sample.HelloResource.Hello", helloTemplate.getSourceType()); Assert.assertNull(helloTemplate.getSourceField()); Assert.assertNull(helloTemplate.getSourceMethod()); @@ -405,6 +413,26 @@ public void testQuteRecord() throws Exception { // public TemplateInstance get(@QueryParam("name") String name) { // return new Hello(name).data("foo", 100); assertParameter("foo", "int", true, helloParameters, 1); + + // HelloWorld -> + // @CheckedTemplate(basePath="Foo", + // defaultName=CheckedTemplate.HYPHENATED_ELEMENT_NAME) + // record HelloWorld(String name) implements TemplateInstance {} + DataModelTemplate helloWorldTemplate = project + .findDataModelTemplate("src/main/resources/templates/Foo/hello-world"); + Assert.assertNotNull(helloWorldTemplate); + Assert.assertEquals("src/main/resources/templates/Foo/hello-world", helloWorldTemplate.getTemplateUri()); + Assert.assertEquals("org.acme.sample.HelloResource$HelloWorld", helloWorldTemplate.getSourceType()); + Assert.assertNull(helloWorldTemplate.getSourceField()); + Assert.assertNull(helloWorldTemplate.getSourceMethod()); + + List helloWorldParameters = helloWorldTemplate.getParameters(); + Assert.assertNotNull(helloWorldParameters); + + Assert.assertEquals(1, helloWorldParameters.size()); + + // record HelloWorld(String name) implements TemplateInstance {} + assertParameter("name", "java.lang.String", false, helloWorldParameters, 0); } @Test