-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Qute: add support for template records
See redhat-developer/quarkus-ls#956 Signed-off-by: azerr <[email protected]>
- Loading branch information
1 parent
5fc6859
commit 6eec921
Showing
6 changed files
with
257 additions
and
1 deletion.
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
107 changes: 107 additions & 0 deletions
107
...redhat/devtools/intellij/qute/psi/internal/template/datamodel/TemplateRecordsSupport.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,107 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package com.redhat.devtools.intellij.qute.psi.internal.template.datamodel; | ||
|
||
import static com.redhat.devtools.intellij.qute.psi.internal.QuteJavaConstants.TEMPLATE_INSTANCE_INTERFACE; | ||
import static com.redhat.devtools.intellij.qute.psi.utils.PsiQuteProjectUtils.getTemplatePath; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.intellij.openapi.progress.ProgressIndicator; | ||
import com.intellij.psi.PsiClass; | ||
import com.intellij.psi.PsiRecordComponent; | ||
import com.redhat.devtools.intellij.qute.psi.internal.template.TemplateDataSupport; | ||
import com.redhat.devtools.intellij.qute.psi.template.datamodel.AbstractTypeWhichImplementsInterfaceDataModelProvider; | ||
import com.redhat.devtools.intellij.qute.psi.template.datamodel.SearchContext; | ||
import com.redhat.devtools.intellij.qute.psi.utils.PsiTypeUtils; | ||
|
||
import com.redhat.qute.commons.datamodel.DataModelParameter; | ||
import com.redhat.qute.commons.datamodel.DataModelTemplate; | ||
|
||
/** | ||
* Template Records support for template files: | ||
* | ||
* <code> | ||
* public class HelloResource { | ||
* <p> | ||
* record Hello(String name) implements TemplateInstance {} | ||
* <p> | ||
* ... | ||
* <p> | ||
* <p> | ||
* @GET | ||
* @Produces(MediaType.TEXT_PLAIN) | ||
* public TemplateInstance get(@QueryParam("name") String name) { | ||
* return new Hello(name).data("bar", 100); | ||
* } | ||
* </code> | ||
* | ||
* @author Angelo ZERR | ||
* @see <a href= | ||
* "https://quarkus.io/guides/qute-reference#template-records">Template | ||
* Records</a> | ||
*/ | ||
public class TemplateRecordsSupport extends AbstractTypeWhichImplementsInterfaceDataModelProvider { | ||
|
||
private static final String[] INTERFACE_NAMES = {TEMPLATE_INSTANCE_INTERFACE}; | ||
|
||
@Override | ||
protected String[] getInterfaceNames() { | ||
return INTERFACE_NAMES; | ||
} | ||
|
||
@Override | ||
protected void processType(PsiClass type, SearchContext context, ProgressIndicator monitor) { | ||
if (!type.isRecord()) { | ||
return; | ||
} | ||
collectDataModelTemplateForTemplateRecord(type, context.getDataModelProject().getTemplates(), | ||
monitor); | ||
} | ||
|
||
private static void collectDataModelTemplateForTemplateRecord(PsiClass type, | ||
List<DataModelTemplate<DataModelParameter>> templates, ProgressIndicator monitor) { | ||
DataModelTemplate<DataModelParameter> template = createTemplateDataModel(type, monitor); | ||
templates.add(template); | ||
} | ||
|
||
private static DataModelTemplate<DataModelParameter> createTemplateDataModel(PsiClass type, | ||
ProgressIndicator monitor) { | ||
|
||
String recordName = type.getName(); | ||
// src/main/resources/templates/${recordName}.qute.html | ||
String templateUri = getTemplatePath(context.getRelativeTemplateBaseDir(), null, null, recordName, true).getTemplateUri(); | ||
|
||
// Create template data model with: | ||
// - template uri : Qute template file which must be bind with data model. | ||
// - source type : the record class which defines Templates | ||
// - | ||
DataModelTemplate<DataModelParameter> template = new DataModelTemplate<DataModelParameter>(); | ||
template.setParameters(new ArrayList<>()); | ||
template.setTemplateUri(templateUri); | ||
template.setSourceType(type.getQualifiedName()); | ||
|
||
// Collect data parameters from the record fields | ||
for (PsiRecordComponent field : type.getRecordComponents()) { | ||
DataModelParameter parameter = new DataModelParameter(); | ||
parameter.setKey(field.getName()); | ||
parameter.setSourceType(PsiTypeUtils.resolveSignature(field.getType(), field.isVarArgs())); | ||
template.getParameters().add(parameter); | ||
} | ||
|
||
// Collect data parameters for the given template | ||
TemplateDataSupport.collectParametersFromDataMethodInvocation(type, template, monitor); | ||
return template; | ||
} | ||
|
||
} |
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
85 changes: 85 additions & 0 deletions
85
...ij/qute/psi/template/datamodel/AbstractTypeWhichImplementsInterfaceDataModelProvider.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,85 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package com.redhat.devtools.intellij.qute.psi.template.datamodel; | ||
|
||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import com.intellij.openapi.progress.ProgressIndicator; | ||
import com.intellij.psi.PsiClass; | ||
import com.intellij.util.Query; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* Abstract class for data model provider based on class type search which | ||
* implements some interfaces. | ||
* | ||
* @author Angelo ZERR | ||
* | ||
*/ | ||
public abstract class AbstractTypeWhichImplementsInterfaceDataModelProvider extends AbstractDataModelProvider { | ||
|
||
private static final Logger LOGGER = Logger | ||
.getLogger(AbstractTypeWhichImplementsInterfaceDataModelProvider.class.getName()); | ||
|
||
@Override | ||
protected String[] getPatterns() { | ||
return getInterfaceNames(); | ||
} | ||
|
||
/** | ||
* Returns the interface names to search. | ||
* | ||
* @return the interface names to search. | ||
*/ | ||
protected abstract String[] getInterfaceNames(); | ||
|
||
@Override | ||
protected Query<? extends Object> createSearchPattern(SearchContext context, String interfaceName) { | ||
return createTypeWhichImplementsInterfaceSearchPattern(context, interfaceName); | ||
} | ||
|
||
@Override | ||
public void collectDataModel(Object match, SearchContext context, ProgressIndicator monitor) { | ||
Object element = match; | ||
if (element instanceof PsiClass type) { | ||
try { | ||
if (isApplicable(type)) { | ||
processType(type, context, monitor); | ||
} | ||
} catch (Exception e) { | ||
if (LOGGER.isLoggable(Level.SEVERE)) { | ||
LOGGER.log(Level.SEVERE, | ||
"Cannot collect Qute data model for the type '" + type.getQualifiedName() + "'.", e); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private boolean isApplicable(PsiClass type) { | ||
PsiClass @NotNull [] superInterfaceNames = type.getInterfaces(); | ||
if (superInterfaceNames == null || superInterfaceNames.length == 0) { | ||
return false; | ||
} | ||
for (String interfaceName : getInterfaceNames()) { | ||
for (PsiClass superInterfaceName : superInterfaceNames) { | ||
if (interfaceName.equals(superInterfaceName.getQualifiedName())) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
protected abstract void processType(PsiClass recordElement, SearchContext context, ProgressIndicator monitor); | ||
|
||
} |
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