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 20, 2023
1 parent 906a34f commit 4159b14
Show file tree
Hide file tree
Showing 33 changed files with 1,219 additions and 288 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.acme.qute;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import io.quarkus.qute.CheckedTemplate;
import io.quarkus.qute.TemplateExtension;
import io.quarkus.qute.TemplateInstance;

@Path("items2")
public class ItemResourceWithFragment {

@CheckedTemplate
static class Templates {
static native TemplateInstance items(List<Item> items);
static native TemplateInstance items$id1(List<Item> items);
static native TemplateInstance items3$id2(List<Item> items);
}

@CheckedTemplate(ignoreFragments = true)
static class Templates2 {
static native TemplateInstance items2(List<Item> items);
static native TemplateInstance items2$id1(List<Item> items);
static native TemplateInstance items2$id2(List<Item> items);
}

@GET
@Produces(MediaType.TEXT_HTML)
public TemplateInstance get() {
List<Item> items = new ArrayList<>();
items.add(new Item(new BigDecimal(10), "Apple"));
items.add(new Item(new BigDecimal(16), "Pear"));
items.add(new Item(new BigDecimal(30), "Orange"));
return Templates.items(items);
}

/**
* This template extension method implements the "discountedPrice" computed
* property.
*/
@TemplateExtension
static BigDecimal discountedPrice(Item item) {
return item.price.multiply(new BigDecimal("0.9"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,55 @@ public void checkedTemplateInInnerClass() throws CoreException, Exception {
"qute.command.generate.template.file", Arrays.asList(items2Uri)));
}

@Test
public void checkedTemplateWithFragment() throws CoreException, Exception {

IJavaProject javaProject = loadMavenProject(QuteMavenProjectName.qute_quickstart);

QuteJavaCodeLensParams params = new QuteJavaCodeLensParams();
IFile javaFile = javaProject.getProject().getFile(new Path("src/main/java/org/acme/qute/ItemResourceWithFragment.java"));
params.setUri(javaFile.getLocation().toFile().toURI().toString());

List<? extends CodeLens> lenses = QuteSupportForJava.getInstance().codeLens(params, getJDTUtils(),
new NullProgressMonitor());
assertEquals(6, lenses.size());

String itemsUri = javaProject.getProject().getFile("src/main/resources/templates/ItemResourceWithFragment/items.html")
.getLocationURI().toString();
String items3Uri = javaProject.getProject()
.getFile("src/main/resources/templates/ItemResourceWithFragment/items3.html").getLocationURI()
.toString();
String items2Uri = javaProject.getProject()
.getFile("src/main/resources/templates/ItemResourceWithFragment/items2.html").getLocationURI()
.toString();
String items2Uri_id1 = javaProject.getProject()
.getFile("src/main/resources/templates/ItemResourceWithFragment/items2$id1.html").getLocationURI()
.toString();
String items2Uri_id2 = javaProject.getProject()
.getFile("src/main/resources/templates/ItemResourceWithFragment/items2$id2.html").getLocationURI()
.toString();

assertCodeLens(lenses, //
cl(r(21, 2, 21, 57), //
"Open `src/main/resources/templates/ItemResourceWithFragment/items.html`", //
"qute.command.open.uri", Arrays.asList(itemsUri)), //
cl(r(22, 2, 22, 61), //
"Open `id1` fragment of `src/main/resources/templates/ItemResourceWithFragment/items.html`", //
"qute.command.open.uri", Arrays.asList(itemsUri, "id1")), //
cl(r(23, 2, 23, 62), //
"Create `src/main/resources/templates/ItemResourceWithFragment/items3.html`", //
"qute.command.generate.template.file", Arrays.asList(items3Uri)), //

cl(r(28, 2, 28, 58), //
"Open `src/main/resources/templates/ItemResourceWithFragment/items2.html`", //
"qute.command.open.uri", Arrays.asList(items2Uri)), //
cl(r(29, 2, 29, 62), //
"Open `src/main/resources/templates/ItemResourceWithFragment/items2$id1.html`", //
"qute.command.open.uri", Arrays.asList(items2Uri_id1)), //
cl(r(30, 2, 30, 62), //
"Create `src/main/resources/templates/ItemResourceWithFragment/items2$id2.html`", //
"qute.command.generate.template.file", Arrays.asList(items2Uri_id2)));
}
public static Range r(int line, int startChar, int endChar) {
return r(line, startChar, line, endChar);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,9 @@ public void checkedTemplateInInnerClass() throws CoreException, Exception {
// static class Templates {
// [Open `src/main/resources/templates/ItemResource/items.qute.html`]
// static native TemplateInstance items(List<Item> items);
// [Create `src/main/resources/templates/ItemResource/map.qute.html`]
// static native TemplateInstance map(Map<String, List<Item>> items, Map.Entry<String, Integer> entry);
// [Create `src/main/resources/templates/ItemResource/map.qute.html`]
// static native TemplateInstance map(Map<String, List<Item>> items,
// Map.Entry<String, Integer> entry);

IJavaProject javaProject = loadMavenProject(QuteMavenProjectName.qute_quickstart);

Expand All @@ -165,6 +166,40 @@ public void checkedTemplateInInnerClass() throws CoreException, Exception {
DiagnosticSeverity.Error, "qute", QuteErrorCode.NoMatchingTemplate.name()));
}

@Test
public void checkedTemplateInWithFragment() throws CoreException, Exception {
// public class ItemResource {
// @CheckedTemplate
// static class Templates {
// [Open `src/main/resources/templates/ItemResource/items.qute.html`]
// static native TemplateInstance items(List<Item> items);
// [Create `src/main/resources/templates/ItemResource/map.qute.html`]
// static native TemplateInstance map(Map<String, List<Item>> items,
// Map.Entry<String, Integer> entry);

IJavaProject javaProject = loadMavenProject(QuteMavenProjectName.qute_quickstart);

QuteJavaDiagnosticsParams params = new QuteJavaDiagnosticsParams();
IFile javaFile = javaProject.getProject()
.getFile(new Path("src/main/java/org/acme/qute/ItemResourceWithFragment.java"));
params.setUris(Arrays.asList(javaFile.getLocation().toFile().toURI().toString()));

List<PublishDiagnosticsParams> publishDiagnostics = QuteSupportForJava.getInstance().diagnostics(params,
getJDTUtils(), new NullProgressMonitor());
assertEquals(1, publishDiagnostics.size());

List<Diagnostic> diagnostics = publishDiagnostics.get(0).getDiagnostics();
assertEquals(2, diagnostics.size());

assertDiagnostic(diagnostics, //
new Diagnostic(r(23, 33, 23, 43),
"No template matching the path ItemResourceWithFragment/items3 could be found for: org.acme.qute.ItemResourceWithFragment$Templates",
DiagnosticSeverity.Error, "qute", QuteErrorCode.NoMatchingTemplate.name()), //
new Diagnostic(r(30, 33, 30, 43),
"No template matching the path ItemResourceWithFragment/items2$id2 could be found for: org.acme.qute.ItemResourceWithFragment$Templates2",
DiagnosticSeverity.Error, "qute", QuteErrorCode.NoMatchingTemplate.name()));
}

public static Range r(int line, int startChar, int endChar) {
return r(line, startChar, line, endChar);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*******************************************************************************
* 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;

/**
* Base class for data model fragment / template.
*
* @param <T> data model parameter.
*
* @see <a href=
* "https://quarkus.io/guides/qute-reference#fragments">Fragments</a>
* @see <a href=
* "https://quarkus.io/guides/qute-reference#type_safe_fragments">Type-safe
* Fragments</a>
*
* @author Angelo ZERR
*/

public class DataModelBaseTemplate<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) {
if (parameters == null) {
return null;
}
return getParametersMap().get(key);
}

/**
* Add the given parameter.
*
* @param parameter the parameter to add.
*/
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
@@ -0,0 +1,59 @@
/*******************************************************************************
* 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;

/**
* Data model fragment hosts informations about the expected data model
* (parameters) for a given fragment.
*
* @param <T> data model parameter.
*
* @see <a href=
* "https://quarkus.io/guides/qute-reference#fragments">Fragments</a>
* @see <a href=
* "https://quarkus.io/guides/qute-reference#type_safe_fragments">Type-safe
* Fragments</a>
*
* @author Angelo ZERR
*/
public class DataModelFragment<T extends DataModelParameter> extends DataModelBaseTemplate<T> {

private String id;

/**
* Returns the fragment id.
*
* @return the fragment id.
*/
public String getId() {
return id;
}

/**
* Set the fragment id.
*
* @param id the fragment 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();
}
}
Loading

0 comments on commit 4159b14

Please sign in to comment.