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: add support for template records
Fixes redhat-developer#956 Signed-off-by: azerr <[email protected]>
- Loading branch information
1 parent
48ec48e
commit 426e102
Showing
8 changed files
with
216 additions
and
6 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
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
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
83 changes: 83 additions & 0 deletions
83
...src/main/java/com/redhat/qute/jdt/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,83 @@ | ||
/******************************************************************************* | ||
* 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.qute.jdt.internal.template.datamodel; | ||
|
||
import static com.redhat.qute.jdt.internal.QuteJavaConstants.TEMPLATE_INSTANCE_INTERFACE; | ||
import static com.redhat.qute.jdt.utils.JDTQuteProjectUtils.getTemplatePath; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
import org.eclipse.core.runtime.IProgressMonitor; | ||
import org.eclipse.jdt.core.IField; | ||
import org.eclipse.jdt.core.IType; | ||
import org.eclipse.jdt.core.JavaModelException; | ||
|
||
import com.redhat.qute.commons.datamodel.DataModelParameter; | ||
import com.redhat.qute.commons.datamodel.DataModelTemplate; | ||
import com.redhat.qute.jdt.internal.template.TemplateDataSupport; | ||
import com.redhat.qute.jdt.template.datamodel.AbstractTypeWhichImplementsInterfaceDataModelProvider; | ||
import com.redhat.qute.jdt.template.datamodel.SearchContext; | ||
|
||
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(IType type, SearchContext context, IProgressMonitor monitor) throws JavaModelException { | ||
if (!type.isRecord()) { | ||
return; | ||
} | ||
collectDataModelTemplateForTemplateRecord(type, context.getDataModelProject().getTemplates(), monitor); | ||
} | ||
|
||
private static void collectDataModelTemplateForTemplateRecord(IType type, | ||
List<DataModelTemplate<DataModelParameter>> templates, IProgressMonitor monitor) throws JavaModelException { | ||
DataModelTemplate<DataModelParameter> template = createTemplateDataModel(type, monitor); | ||
templates.add(template); | ||
} | ||
|
||
private static DataModelTemplate<DataModelParameter> createTemplateDataModel(IType type, IProgressMonitor monitor) throws JavaModelException { | ||
|
||
String recordName = type.getElementName(); | ||
// src/main/resources/templates/${recordName}.qute.html | ||
String templateUri = getTemplatePath(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.getDeclaringType().getFullyQualifiedName()); | ||
|
||
// Collect data parameters from the record fields | ||
for(IField field : type.getRecordComponents()) { | ||
DataModelParameter parameter = new DataModelParameter(); | ||
parameter.setKey(field.getElementName()); | ||
parameter.setSourceType(type.getDeclaringType().getFullyQualifiedName()); | ||
template.getParameters().add(parameter); | ||
} | ||
|
||
// Collect data parameters for the given template | ||
TemplateDataSupport.collectParametersFromDataMethodInvocation(type, template, monitor); | ||
return template; | ||
} | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
...at/qute/jdt/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,75 @@ | ||
package com.redhat.qute.jdt.template.datamodel; | ||
|
||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import org.eclipse.core.runtime.IProgressMonitor; | ||
import org.eclipse.jdt.core.IType; | ||
import org.eclipse.jdt.core.JavaModelException; | ||
import org.eclipse.jdt.core.search.IJavaSearchConstants; | ||
import org.eclipse.jdt.core.search.SearchMatch; | ||
import org.eclipse.jdt.core.search.SearchPattern; | ||
|
||
import com.redhat.qute.jdt.internal.resolver.ITypeResolver; | ||
|
||
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 | ||
public void collectDataModel(SearchMatch match, SearchContext context, IProgressMonitor monitor) { | ||
Object element = match.getElement(); | ||
if (element instanceof IType 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.getElementName() + "'.", e); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private boolean isApplicable(IType type) throws JavaModelException { | ||
String[] superInterfaceNames = type.getSuperInterfaceNames(); | ||
if (superInterfaceNames == null || superInterfaceNames.length == 0) { | ||
return false; | ||
} | ||
for (String interfaceName : getInterfaceNames()) { | ||
for (String superInterfaceName : superInterfaceNames) { | ||
if (interfaceName.endsWith(superInterfaceName)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
protected SearchPattern createSearchPattern(String interfaceName) { | ||
return SearchPattern.createPattern(interfaceName, IJavaSearchConstants.CLASS, IJavaSearchConstants.IMPLEMENTORS, | ||
SearchPattern.R_EXACT_MATCH); | ||
} | ||
|
||
protected abstract void processType(IType recordElement, SearchContext context, IProgressMonitor monitor) | ||
throws JavaModelException; | ||
|
||
} |