Skip to content

Commit

Permalink
Fragment support between Java / Template file
Browse files Browse the repository at this point in the history
  • Loading branch information
angelozerr committed Jan 18, 2023
1 parent 906a34f commit 6df6159
Show file tree
Hide file tree
Showing 22 changed files with 654 additions and 272 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*******************************************************************************
* Copyright (c) 2023 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.commons.datamodel;

import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;

public class DataModelFragment<T extends DataModelParameter> extends DataModelParameters<T> {

private String id;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

@Override
public String toString() {
ToStringBuilder b = new ToStringBuilder(this);
b.add("id", this.id);
b.add("parameters", this.getParameters());
return b.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*******************************************************************************
* Copyright (c) 2023 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.commons.datamodel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class DataModelParameters<T extends DataModelParameter> {

private String sourceType;

private String sourceMethod;

private List<T> parameters;

private transient Map<String, T> parametersMap;

/**
* Returns the Java source type where this data model template is defined.
*
* @return the Java source type where this data model template is defined.
*/
public String getSourceType() {
return sourceType;
}

/**
* Set the Java source type where this data model template is defined.
*
* @param sourceType the Java source type where this data model template is
* defined.
*/
public void setSourceType(String sourceType) {
this.sourceType = sourceType;
}

/**
* Returns the Java source method where this data model template is defined and
* null otherwise.
*
* @return the Java source method where this data model template is defined and
* null otherwise.
*/
public String getSourceMethod() {
return sourceMethod;
}

/**
* Set the Java source method where this data model template is defined and null
* otherwise.
*
* @param sourceMethod the Java source method where this data model template is
* defined and null otherwise.
*/
public void setSourceMethod(String sourceMethod) {
this.sourceMethod = sourceMethod;
}

/**
* Returns the list of data model parameters.
*
* @return the list of data model parameters.
*/
public List<T> getParameters() {
return parameters;
}

/**
* Set the list of data model parameters.
*
* @param parameters the list of data model parameters.
*/
public void setParameters(List<T> parameters) {
this.parameters = parameters;
}

/**
* Returns the parameter from the given key and null otherwise.
*
* @param key the parameter key.
*
* @return the parameter from the given key and null otherwise.
*/
public T getParameter(String key) {
List<T> parameters = getParameters();
if (parameters == null) {
return null;
}
return getParametersMap().get(key);
}

public void addParameter(T parameter) {
if (parameters == null) {
parameters = new ArrayList<>();
}
parameters.add(parameter);
getParametersMap().put(parameter.getKey(), parameter);
}

private Map<String, T> getParametersMap() {
if (parametersMap == null) {
parametersMap = parameters.stream()
.collect(Collectors.toMap(DataModelParameter::getKey, Function.identity()));
}
return parametersMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;

Expand All @@ -27,19 +24,13 @@
*
* @param <T> data model parameter.
*/
public class DataModelTemplate<T extends DataModelParameter> {
public class DataModelTemplate<T extends DataModelParameter> extends DataModelParameters<T> {

private String templateUri;

private String sourceType;

private String sourceMethod;

private String sourceField;

private List<T> parameters;

private transient Map<String, T> parametersMap;
private List<DataModelFragment<T>> fragments;

/**
* Returns the template Uri.
Expand All @@ -59,47 +50,6 @@ public void setTemplateUri(String templateUri) {
this.templateUri = templateUri;
}

/**
* Returns the Java source type where this data model template is defined.
*
* @return the Java source type where this data model template is defined.
*/
public String getSourceType() {
return sourceType;
}

/**
* Set the Java source type where this data model template is defined.
*
* @param sourceType the Java source type where this data model template is
* defined.
*/
public void setSourceType(String sourceType) {
this.sourceType = sourceType;
}

/**
* Returns the Java source method where this data model template is defined and
* null otherwise.
*
* @return the Java source method where this data model template is defined and
* null otherwise.
*/
public String getSourceMethod() {
return sourceMethod;
}

/**
* Set the Java source method where this data model template is defined and null
* otherwise.
*
* @param sourceMethod the Java source method where this data model template is
* defined and null otherwise.
*/
public void setSourceMethod(String sourceMethod) {
this.sourceMethod = sourceMethod;
}

/**
* Returns the Java source field where this data model template is defined and
* null otherwise.
Expand All @@ -122,63 +72,46 @@ public void setSourceField(String sourceField) {
this.sourceField = sourceField;
}

/**
* Returns the list of data model parameters.
*
* @return the list of data model parameters.
*/
public List<T> getParameters() {
return parameters;
public void addFragment(DataModelFragment<T> fragment) {
if (fragments == null) {
fragments = new ArrayList<>();
}
fragments.add(fragment);
}

/**
* Set the list of data model parameters.
*
* @param parameters the list of data model parameters.
*/
public void setParameters(List<T> parameters) {
this.parameters = parameters;
public List<DataModelFragment<T>> getFragments() {
return fragments;
}

/**
* Returns the parameter from the given key and null otherwise.
*
* @param key the parameter key.
*
* @return the parameter from the given key and null otherwise.
*/
public T getParameter(String key) {
List<T> parameters = getParameters();
if (parameters == null) {
return null;
}
return getParametersMap().get(key);
public void setFragments(List<DataModelFragment<T>> fragments) {
this.fragments = fragments;
}

public void addParameter(T parameter) {
if (parameters == null) {
parameters = new ArrayList<>();
public DataModelFragment<T> getFragment(String fragmentId) {
if (fragmentId == null) {
return null;
}
parameters.add(parameter);
getParametersMap().put(parameter.getKey(), parameter);
}

private Map<String, T> getParametersMap() {
if (parametersMap == null) {
parametersMap = parameters.stream()
.collect(Collectors.toMap(DataModelParameter::getKey, Function.identity()));
List<DataModelFragment<T>> fragments = getFragments();
if (fragments == null || fragments.isEmpty()) {
return null;
}
for (DataModelFragment<T> fragment : fragments) {
if (fragmentId.equals(fragment.getId())) {
return fragment;
}
}
return parametersMap;
return null;
}

@Override
public String toString() {
ToStringBuilder b = new ToStringBuilder(this);
b.add("templateUri", this.templateUri);
b.add("sourceType", this.sourceType);
b.add("sourceMethod", this.sourceMethod);
b.add("sourceType", this.getSourceType());
b.add("sourceMethod", this.getSourceMethod());
b.add("sourceField", this.sourceField);
b.add("parameters", this.parameters);
b.add("parameters", this.getParameters());
b.add("fragments", this.getFragments());
return b.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,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.TemplatePathInfo;

/**
* Abstract class which collects {@link MethodDeclaration} or
Expand All @@ -65,6 +66,7 @@ public abstract class AbstractQuteTemplateLinkCollector extends ASTVisitor {
".txt", ".yaml" };

private static final String PREFERRED_SUFFIX = ".html"; // TODO make it configurable
private static final boolean FRAGMENT_SUPPORT = true; // TODO make it configurable

protected final ITypeRoot typeRoot;
protected final IJDTUtils utils;
Expand Down Expand Up @@ -192,17 +194,19 @@ private void collectTemplateLink(ASTNode fieldOrMethod, StringLiteral locationAn
try {
String location = locationAnnotation != null ? locationAnnotation.getLiteralValue() : null;
IProject project = typeRoot.getJavaProject().getProject();
String templateFilePath = location != null ? JDTQuteProjectUtils.getTemplatePath(null, location)
TemplatePathInfo templatePathInfo = location != null ? JDTQuteProjectUtils.getTemplatePath(null, location)
: JDTQuteProjectUtils.getTemplatePath(className, fieldOrMethodName);
IFile templateFile = null;
if (location == null) {
templateFile = getTemplateFile(project, templateFilePath);
templateFilePath = templateFile.getLocation().makeRelativeTo(project.getLocation()).toString();
templateFile = getTemplateFile(project, templatePathInfo.getTemplateUri());
templatePathInfo = new TemplatePathInfo(
templateFile.getLocation().makeRelativeTo(project.getLocation()).toString(),
templatePathInfo.getFragmentId());
} else {
templateFile = project.getFile(templateFilePath);
templateFile = project.getFile(templatePathInfo.getTemplateUri());
}
collectTemplateLink(fieldOrMethod, locationAnnotation, type, className, fieldOrMethodName, location,
templateFile, templateFilePath);
templateFile, templatePathInfo);
} catch (JavaModelException e) {
LOGGER.log(Level.SEVERE, "Error while creating Qute CodeLens for Java file.", e);
}
Expand All @@ -229,8 +233,8 @@ protected Range createRange(ASTNode fieldOrMethod) throws JavaModelException {
}

protected abstract void collectTemplateLink(ASTNode node, ASTNode locationAnnotation, TypeDeclaration type,
String className, String fieldOrMethodName, String location, IFile templateFile, String templateFilePath)
throws JavaModelException;
String className, String fieldOrMethodName, String location, IFile templateFile,
TemplatePathInfo templatePathInfo) throws JavaModelException;

private static IFile getTemplateFile(IProject project, String templateFilePathWithoutExtension) {
for (String suffix : suffixes) {
Expand Down
Loading

0 comments on commit 6df6159

Please sign in to comment.