33
33
import com .redhat .devtools .intellij .qute .psi .utils .AnnotationUtils ;
34
34
import com .redhat .devtools .intellij .qute .psi .utils .PsiTypeUtils ;
35
35
36
+ import com .redhat .devtools .intellij .qute .psi .utils .TemplateNameStrategy ;
36
37
import com .redhat .devtools .intellij .qute .psi .utils .TemplatePathInfo ;
37
38
import com .redhat .qute .commons .datamodel .DataModelBaseTemplate ;
38
39
import com .redhat .qute .commons .datamodel .DataModelFragment ;
@@ -81,7 +82,8 @@ protected void processAnnotation(PsiElement javaElement, PsiAnnotation checkedTe
81
82
PsiClass type = (PsiClass ) javaElement ;
82
83
boolean ignoreFragments = isIgnoreFragments (checkedTemplateAnnotation );
83
84
String basePath = getBasePath (checkedTemplateAnnotation );
84
- collectDataModelTemplateForCheckedTemplate (type , context .getRelativeTemplateBaseDir (), basePath , ignoreFragments , context .getTypeResolver (type ),
85
+ TemplateNameStrategy templateNameStrategy = getDefaultName (checkedTemplateAnnotation );
86
+ collectDataModelTemplateForCheckedTemplate (type , context .getRelativeTemplateBaseDir (), basePath , ignoreFragments , templateNameStrategy , context .getTypeResolver (type ),
85
87
context .getDataModelProject ().getTemplates (), monitor );
86
88
}
87
89
}
@@ -119,12 +121,11 @@ public static boolean isIgnoreFragments(PsiAnnotation checkedTemplateAnnotation)
119
121
120
122
/**
121
123
* Returns the <code>basePath</code> value declared in the @CheckedTemplate annotation, relative to the templates root, to search the templates from.
122
- *<code>
123
- * @CheckedTemplate(basePath="somewhere")
124
- *</code>
124
+ * <code>
125
125
*
126
126
* @param checkedTemplateAnnotation the CheckedTemplate annotation.
127
127
* @return the <code>basePath</code> value declared in the @CheckedTemplate annotation
128
+ * @CheckedTemplate(basePath="somewhere") </code>
128
129
*/
129
130
public static String getBasePath (PsiAnnotation checkedTemplateAnnotation ) {
130
131
String basePath = null ;
@@ -146,6 +147,53 @@ public static String getBasePath(PsiAnnotation checkedTemplateAnnotation) {
146
147
return basePath ;
147
148
}
148
149
150
+ /**
151
+ * Returns the <code>defaultName</code> value declared in the @CheckedTemplate annotation.
152
+ * <code>
153
+ *
154
+ * @param checkedTemplateAnnotation the CheckedTemplate annotation.
155
+ * @return the <code>basePath</code> value declared in the @CheckedTemplate annotation
156
+ * @CheckedTemplate(defaultName="somewhere") </code>
157
+ */
158
+ public static TemplateNameStrategy getDefaultName (PsiAnnotation checkedTemplateAnnotation ) {
159
+ TemplateNameStrategy templateNameStrategy = TemplateNameStrategy .ELEMENT_NAME ;
160
+ try {
161
+ for (PsiNameValuePair pair : checkedTemplateAnnotation .getParameterList ().getAttributes ()) {
162
+ if (CHECKED_TEMPLATE_ANNOTATION_DEFAULT_NAME .equalsIgnoreCase (pair .getAttributeName ())) {
163
+ if (pair .getValue () != null
164
+ && pair .getValue ().getReference () != null
165
+ && pair .getValue ().getReference ().resolve () != null &&
166
+ pair .getValue ().getReference ().resolve () instanceof PsiField field ) {
167
+ Object value = field .computeConstantValue ();
168
+ if (value != null ) {
169
+ templateNameStrategy = getDefaultName (value .toString ());
170
+ }
171
+ }
172
+ }
173
+ }
174
+ } catch (ProcessCanceledException e ) {
175
+ //Since 2024.2 ProcessCanceledException extends CancellationException so we can't use multicatch to keep backward compatibility
176
+ //TODO delete block when minimum required version is 2024.2
177
+ throw e ;
178
+ } catch (IndexNotReadyException | CancellationException e ) {
179
+ throw e ;
180
+ } catch (Exception e ) {
181
+ // Do nothing
182
+ }
183
+ return templateNameStrategy ;
184
+ }
185
+
186
+ private static TemplateNameStrategy getDefaultName (String defaultName ) {
187
+ switch (defaultName ) {
188
+ case CHECKED_TEMPLATE_ANNOTATION_DEFAULT_NAME_HYPHENATED_ELEMENT_NAME :
189
+ return TemplateNameStrategy .HYPHENATED_ELEMENT_NAME ;
190
+
191
+ case CHECKED_TEMPLATE_ANNOTATION_DEFAULT_NAME_UNDERSCORED_ELEMENT_NAME :
192
+ return TemplateNameStrategy .UNDERSCORED_ELEMENT_NAME ;
193
+ }
194
+ return TemplateNameStrategy .ELEMENT_NAME ;
195
+ }
196
+
149
197
/**
150
198
* Collect data model template from @CheckedTemplate.
151
199
*
@@ -155,8 +203,14 @@ public static String getBasePath(PsiAnnotation checkedTemplateAnnotation) {
155
203
* @param templates the data model templates to update with collect of template.
156
204
* @param monitor the progress monitor.
157
205
*/
158
- private static void collectDataModelTemplateForCheckedTemplate (PsiClass type , String templatesBaseDir , String basePath , boolean ignoreFragments , ITypeResolver typeResolver ,
159
- List <DataModelTemplate <DataModelParameter >> templates , ProgressIndicator monitor ) {
206
+ private static void collectDataModelTemplateForCheckedTemplate (PsiClass type ,
207
+ String templatesBaseDir ,
208
+ String basePath ,
209
+ boolean ignoreFragments ,
210
+ TemplateNameStrategy templateNameStrategy ,
211
+ ITypeResolver typeResolver ,
212
+ List <DataModelTemplate <DataModelParameter >> templates ,
213
+ ProgressIndicator monitor ) {
160
214
boolean innerClass = type .getContainingClass () != null ;
161
215
String className = !innerClass ? null
162
216
: PsiTypeUtils .getSimpleClassName (type .getContainingFile ().getName ());
@@ -166,7 +220,7 @@ private static void collectDataModelTemplateForCheckedTemplate(PsiClass type, St
166
220
PsiMethod [] methods = type .getMethods ();
167
221
for (PsiMethod method : methods ) {
168
222
// src/main/resources/templates/${className}/${methodName}.qute.html
169
- TemplatePathInfo templatePathInfo = getTemplatePath (templatesBaseDir , basePath , className , method .getName (), ignoreFragments );
223
+ TemplatePathInfo templatePathInfo = getTemplatePath (templatesBaseDir , basePath , className , method .getName (), ignoreFragments , templateNameStrategy );
170
224
171
225
// Get or create template
172
226
String templateUri = templatePathInfo .getTemplateUri ();
@@ -234,7 +288,11 @@ public static void collectParameters(PsiMethod method, ITypeResolver typeResolve
234
288
for (int i = 0 ; i < parameters .getParametersCount (); i ++) {
235
289
DataModelParameter parameter = createParameterDataModel (parameters .getParameter (i ),
236
290
varargs && i == parameters .getParametersCount () - 1 , typeResolver );
237
- templateOrFragment .getParameters ().add (parameter );
291
+ if (templateOrFragment .getParameter (parameter .getKey ()) == null ) {
292
+ // Add parameter if it doesn't exist
293
+ // to avoid parameters duplication
294
+ templateOrFragment .getParameters ().add (parameter );
295
+ }
238
296
}
239
297
}
240
298
0 commit comments