forked from redhat-developer/quarkus-ls
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:Qute: Cannot locate hyphenated template name
Fixes redhat-developer#975 Signed-off-by: azerr <[email protected]>
- Loading branch information
1 parent
cf93107
commit 4ffe2b9
Showing
8 changed files
with
388 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,9 @@ | |
|
||
import static com.redhat.qute.jdt.internal.QuteJavaConstants.CHECKED_TEMPLATE_ANNOTATION; | ||
import static com.redhat.qute.jdt.internal.QuteJavaConstants.CHECKED_TEMPLATE_ANNOTATION_BASE_PATH; | ||
import static com.redhat.qute.jdt.internal.QuteJavaConstants.CHECKED_TEMPLATE_ANNOTATION_DEFAULT_NAME; | ||
import static com.redhat.qute.jdt.internal.QuteJavaConstants.CHECKED_TEMPLATE_ANNOTATION_DEFAULT_NAME_HYPHENATED_ELEMENT_NAME; | ||
import static com.redhat.qute.jdt.internal.QuteJavaConstants.CHECKED_TEMPLATE_ANNOTATION_DEFAULT_NAME_UNDERSCORED_ELEMENT_NAME; | ||
import static com.redhat.qute.jdt.internal.QuteJavaConstants.CHECKED_TEMPLATE_ANNOTATION_IGNORE_FRAGMENTS; | ||
import static com.redhat.qute.jdt.internal.QuteJavaConstants.OLD_CHECKED_TEMPLATE_ANNOTATION; | ||
import static com.redhat.qute.jdt.internal.QuteJavaConstants.TEMPLATE_CLASS; | ||
|
@@ -51,6 +54,7 @@ | |
import com.redhat.qute.jdt.utils.IJDTUtils; | ||
import com.redhat.qute.jdt.utils.JDTQuteProjectUtils; | ||
import com.redhat.qute.jdt.utils.JDTTypeUtils; | ||
import com.redhat.qute.jdt.utils.TemplateNameStrategy; | ||
import com.redhat.qute.jdt.utils.TemplatePathInfo; | ||
|
||
/** | ||
|
@@ -138,7 +142,9 @@ public boolean visit(FieldDeclaration node) { | |
.getLocationExpressionFromConstructorParameter(variable.getName().getIdentifier()); | ||
} | ||
String fieldName = variable.getName().getIdentifier(); | ||
collectTemplateLink(null, node, locationExpression, getTypeDeclaration(node), null, fieldName, false); | ||
TemplateNameStrategy templateNameStrategy = TemplateNameStrategy.ELEMENT_NAME; | ||
collectTemplateLink(null, node, locationExpression, getTypeDeclaration(node), null, fieldName, false, | ||
templateNameStrategy); | ||
} | ||
} | ||
return super.visit(node); | ||
|
@@ -186,10 +192,12 @@ public boolean visit(TypeDeclaration node) { | |
// public static native TemplateInstance book(Book book); | ||
boolean ignoreFragments = isIgnoreFragments(annotation); | ||
String basePath = getBasePath(annotation); | ||
TemplateNameStrategy templateNameStrategy = getDefaultName(annotation); | ||
List body = node.bodyDeclarations(); | ||
for (Object declaration : body) { | ||
if (declaration instanceof MethodDeclaration) { | ||
collectTemplateLink(basePath, (MethodDeclaration) declaration, node, ignoreFragments); | ||
collectTemplateLink(basePath, (MethodDeclaration) declaration, node, ignoreFragments, | ||
templateNameStrategy); | ||
} | ||
} | ||
} | ||
|
@@ -208,8 +216,9 @@ public boolean visit(TypeDeclaration node) { | |
@Override | ||
public boolean visit(RecordDeclaration node) { | ||
if (isImplementTemplateInstance(node)) { | ||
TemplateNameStrategy templateNameStrategy = TemplateNameStrategy.ELEMENT_NAME; | ||
String recordName = node.getName().getIdentifier(); | ||
collectTemplateLink(null, node, null, node, null, recordName, false); | ||
collectTemplateLink(null, node, null, node, null, recordName, false, templateNameStrategy); | ||
} | ||
return super.visit(node); | ||
} | ||
|
@@ -275,18 +284,52 @@ private static boolean isIgnoreFragments(Annotation checkedTemplateAnnotation) { | |
* @return the <code>basePath</code> value declared in the @CheckedTemplate | ||
* annotation | ||
*/ | ||
public static String getBasePath(Annotation checkedTemplateAnnotation) { | ||
private static String getBasePath(Annotation checkedTemplateAnnotation) { | ||
String basePath = null; | ||
try { | ||
Expression ignoreFragmentExpr = AnnotationUtils.getAnnotationMemberValueExpression( | ||
checkedTemplateAnnotation, CHECKED_TEMPLATE_ANNOTATION_BASE_PATH); | ||
basePath = AnnotationUtils.getString(ignoreFragmentExpr); | ||
Expression basePathExpr = AnnotationUtils.getAnnotationMemberValueExpression(checkedTemplateAnnotation, | ||
CHECKED_TEMPLATE_ANNOTATION_BASE_PATH); | ||
basePath = AnnotationUtils.getString(basePathExpr); | ||
} catch (Exception e) { | ||
// Do nothing | ||
} | ||
return basePath; | ||
} | ||
|
||
/** | ||
* Returns the <code>defaultName</code> value declared in the @CheckedTemplate | ||
* annotation, relative to the templates root, to search the templates from. | ||
* <code> | ||
* @CheckedTemplate([email protected]_ELEMENT_NAME) | ||
*</code> | ||
* | ||
* @param checkedTemplateAnnotation the CheckedTemplate annotation. | ||
* @return the <code>defaultName</code> value declared in the @CheckedTemplate | ||
* annotation | ||
*/ | ||
private static TemplateNameStrategy getDefaultName(Annotation checkedTemplateAnnotation) { | ||
TemplateNameStrategy defaultName = TemplateNameStrategy.ELEMENT_NAME; | ||
try { | ||
Expression defaultNameExpr = AnnotationUtils.getAnnotationMemberValueExpression(checkedTemplateAnnotation, | ||
CHECKED_TEMPLATE_ANNOTATION_DEFAULT_NAME); | ||
defaultName = getDefaultName(AnnotationUtils.getString(defaultNameExpr)); | ||
} catch (Exception e) { | ||
// Do nothing | ||
} | ||
return defaultName; | ||
} | ||
|
||
private static TemplateNameStrategy getDefaultName(String defaultName) { | ||
switch (defaultName) { | ||
case CHECKED_TEMPLATE_ANNOTATION_DEFAULT_NAME_HYPHENATED_ELEMENT_NAME: | ||
return TemplateNameStrategy.HYPHENATED_ELEMENT_NAME; | ||
|
||
case CHECKED_TEMPLATE_ANNOTATION_DEFAULT_NAME_UNDERSCORED_ELEMENT_NAME: | ||
return TemplateNameStrategy.UNDERSCORED_ELEMENT_NAME; | ||
} | ||
return TemplateNameStrategy.ELEMENT_NAME; | ||
} | ||
|
||
@Override | ||
public void endVisit(TypeDeclaration node) { | ||
levelTypeDecl--; | ||
|
@@ -302,24 +345,28 @@ private static TypeDeclaration getTypeDeclaration(ASTNode node) { | |
} | ||
|
||
private void collectTemplateLink(String basePath, MethodDeclaration methodDeclaration, TypeDeclaration type, | ||
boolean ignoreFragment) { | ||
boolean ignoreFragment, TemplateNameStrategy templateNameStrategy) { | ||
String className = null; | ||
boolean innerClass = levelTypeDecl > 1; | ||
if (innerClass) { | ||
className = JDTTypeUtils.getSimpleClassName(typeRoot.getElementName()); | ||
} | ||
String methodName = methodDeclaration.getName().getIdentifier(); | ||
collectTemplateLink(basePath, methodDeclaration, null, type, className, methodName, ignoreFragment); | ||
collectTemplateLink(basePath, methodDeclaration, null, type, className, methodName, ignoreFragment, | ||
templateNameStrategy); | ||
} | ||
|
||
private void collectTemplateLink(String basePath, ASTNode fieldOrMethod, StringLiteral locationAnnotation, | ||
AbstractTypeDeclaration type, String className, String fieldOrMethodName, boolean ignoreFragment) { | ||
AbstractTypeDeclaration type, String className, String fieldOrMethodName, boolean ignoreFragment, | ||
TemplateNameStrategy templateNameStrategy) { | ||
try { | ||
String location = locationAnnotation != null ? locationAnnotation.getLiteralValue() : null; | ||
IProject project = typeRoot.getJavaProject().getProject(); | ||
TemplatePathInfo templatePathInfo = location != null | ||
? JDTQuteProjectUtils.getTemplatePath(basePath, null, location, ignoreFragment) | ||
: JDTQuteProjectUtils.getTemplatePath(basePath, className, fieldOrMethodName, ignoreFragment); | ||
? JDTQuteProjectUtils.getTemplatePath(basePath, null, location, ignoreFragment, | ||
templateNameStrategy) | ||
: JDTQuteProjectUtils.getTemplatePath(basePath, className, fieldOrMethodName, ignoreFragment, | ||
templateNameStrategy); | ||
IFile templateFile = null; | ||
if (location == null) { | ||
templateFile = getTemplateFile(project, templatePathInfo.getTemplateUri()); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
|
||
import static com.redhat.qute.jdt.internal.QuteJavaConstants.CHECKED_TEMPLATE_ANNOTATION; | ||
import static com.redhat.qute.jdt.internal.QuteJavaConstants.CHECKED_TEMPLATE_ANNOTATION_BASE_PATH; | ||
import static com.redhat.qute.jdt.internal.QuteJavaConstants.CHECKED_TEMPLATE_ANNOTATION_DEFAULT_NAME; | ||
import static com.redhat.qute.jdt.internal.QuteJavaConstants.CHECKED_TEMPLATE_ANNOTATION_IGNORE_FRAGMENTS; | ||
import static com.redhat.qute.jdt.internal.QuteJavaConstants.OLD_CHECKED_TEMPLATE_ANNOTATION; | ||
import static com.redhat.qute.jdt.utils.JDTQuteProjectUtils.getTemplatePath; | ||
|
@@ -43,6 +44,7 @@ | |
import com.redhat.qute.jdt.template.datamodel.SearchContext; | ||
import com.redhat.qute.jdt.utils.AnnotationUtils; | ||
import com.redhat.qute.jdt.utils.JDTTypeUtils; | ||
import com.redhat.qute.jdt.utils.TemplateNameStrategy; | ||
import com.redhat.qute.jdt.utils.TemplatePathInfo; | ||
|
||
/** | ||
|
@@ -91,8 +93,9 @@ protected void processAnnotation(IJavaElement javaElement, IAnnotation checkedTe | |
IType type = (IType) javaElement; | ||
boolean ignoreFragments = isIgnoreFragments(checkedTemplateAnnotation); | ||
String basePath = getBasePath(checkedTemplateAnnotation); | ||
collectDataModelTemplateForCheckedTemplate(type, basePath, ignoreFragments, context.getTypeResolver(type), | ||
context.getDataModelProject().getTemplates(), monitor); | ||
TemplateNameStrategy templateNameStrategy = getDefaultName(checkedTemplateAnnotation); | ||
collectDataModelTemplateForCheckedTemplate(type, basePath, ignoreFragments, templateNameStrategy, | ||
context.getTypeResolver(type), context.getDataModelProject().getTemplates(), monitor); | ||
} | ||
} | ||
|
||
|
@@ -145,6 +148,32 @@ private static String getBasePath(IAnnotation checkedTemplateAnnotation) { | |
return basePath; | ||
} | ||
|
||
/** | ||
* Returns the <code>defaultName</code> value declared in the @CheckedTemplate | ||
* annotation, relative to the templates root, to search the templates from. | ||
* <code> | ||
* @CheckedTemplate([email protected]_ELEMENT_NAME) | ||
*</code> | ||
* | ||
* @param checkedTemplateAnnotation the CheckedTemplate annotation. | ||
* @return the <code>defaultName</code> value declared in the @CheckedTemplate | ||
* annotation | ||
*/ | ||
private static TemplateNameStrategy getDefaultName(IAnnotation checkedTemplateAnnotation) { | ||
TemplateNameStrategy defaultName = TemplateNameStrategy.ELEMENT_NAME; | ||
try { | ||
for (IMemberValuePair pair : checkedTemplateAnnotation.getMemberValuePairs()) { | ||
if (CHECKED_TEMPLATE_ANNOTATION_DEFAULT_NAME.equalsIgnoreCase(pair.getMemberName())) { | ||
String value = AnnotationUtils.getValueAsString(pair); | ||
System.err.println(value); | ||
} | ||
} | ||
} catch (Exception e) { | ||
// Do nothing | ||
} | ||
return defaultName; | ||
} | ||
|
||
/** | ||
* Collect data model template from @CheckedTemplate. | ||
* | ||
|
@@ -158,8 +187,8 @@ private static String getBasePath(IAnnotation checkedTemplateAnnotation) { | |
* @throws JavaModelException | ||
*/ | ||
private static void collectDataModelTemplateForCheckedTemplate(IType type, String basePath, boolean ignoreFragments, | ||
ITypeResolver typeResolver, List<DataModelTemplate<DataModelParameter>> templates, IProgressMonitor monitor) | ||
throws JavaModelException { | ||
TemplateNameStrategy templateNameStrategy, ITypeResolver typeResolver, | ||
List<DataModelTemplate<DataModelParameter>> templates, IProgressMonitor monitor) throws JavaModelException { | ||
boolean innerClass = type.getParent() != null && type.getParent().getElementType() == IJavaElement.TYPE; | ||
String className = !innerClass ? null | ||
: JDTTypeUtils.getSimpleClassName( | ||
|
@@ -172,7 +201,8 @@ private static void collectDataModelTemplateForCheckedTemplate(IType type, Strin | |
for (IMethod method : methods) { | ||
|
||
// src/main/resources/templates/${className}/${methodName}.qute.html | ||
TemplatePathInfo templatePathInfo = getTemplatePath(basePath, className, method.getElementName(), ignoreFragments); | ||
TemplatePathInfo templatePathInfo = getTemplatePath(basePath, className, method.getElementName(), | ||
ignoreFragments, templateNameStrategy); | ||
|
||
// Get or create template | ||
String templateUri = templatePathInfo.getTemplateUri(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...jdt/com.redhat.qute.jdt/src/main/java/com/redhat/qute/jdt/utils/TemplateNameStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.redhat.qute.jdt.utils; | ||
|
||
public enum TemplateNameStrategy { | ||
|
||
ELEMENT_NAME, | ||
HYPHENATED_ELEMENT_NAME, | ||
UNDERSCORED_ELEMENT_NAME, | ||
OTHER | ||
} |
Oops, something went wrong.