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 authored and datho7561 committed Jan 23, 2023
1 parent 4232de9 commit 820e5a9
Show file tree
Hide file tree
Showing 35 changed files with 1,326 additions and 298 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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);
static native TemplateInstance items3$(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,58 @@ 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(29, 2, 29, 58), //
"Open `src/main/resources/templates/ItemResourceWithFragment/items2.html`", //
"qute.command.open.uri", Arrays.asList(items2Uri)), //
cl(r(30, 2, 30, 62), //
"Open `src/main/resources/templates/ItemResourceWithFragment/items2$id1.html`", //
"qute.command.open.uri", Arrays.asList(items2Uri_id1)), //
cl(r(31, 2, 31, 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,35 @@ public void checkedTemplateInInnerClass() throws CoreException, Exception {
DiagnosticSeverity.Error, "qute", QuteErrorCode.NoMatchingTemplate.name()));
}

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

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(3, 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(24, 33, 24, 40),
"Fragment [] not defined in template ItemResourceWithFragment/items3$",
DiagnosticSeverity.Error, "qute", QuteErrorCode.FragmentNotDefined.name()), //
new Diagnostic(r(31, 33, 31, 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
Expand Up @@ -165,6 +165,41 @@ public void checkedTemplateInInnerClass() throws CoreException, Exception {
templateFileUri, "Create `src/main/resources/templates/ItemResource/items2.qute.html`"));
}

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

IJavaProject javaProject = loadMavenProject(QuteMavenProjectName.qute_quickstart);

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

List<DocumentLink> links = QuteSupportForJava.getInstance().documentLink(params, getJDTUtils(),
new NullProgressMonitor());
assertEquals(6, links.size());

String templateFileUri = javaProject.getProject()
.getFile("src/main/resources/templates/ItemResourceWithFragment/items.html").getLocationURI()
.toString();

assertDocumentLink(links, //
dl(r(21, 33, 21, 38), //
templateFileUri, "Open `src/main/resources/templates/ItemResourceWithFragment/items.html`"), //
dl(r(22, 33, 22, 42), //
templateFileUri, "Open `src/main/resources/templates/ItemResourceWithFragment/items.html`"), //
dl(r(23, 33, 23, 43), //
templateFileUri, "Create `src/main/resources/templates/ItemResourceWithFragment/items3.html`"), //
dl(r(29, 33, 29, 39), //
templateFileUri, "Open `src/main/resources/templates/ItemResourceWithFragment/items2.html`"), //
dl(r(30, 33, 30, 43), //
templateFileUri,
"Open `src/main/resources/templates/ItemResourceWithFragment/items2$id1.html`"), //
dl(r(31, 33, 31, 43), //
templateFileUri,
"Open `src/main/resources/templates/ItemResourceWithFragment/items2$id2.html`"));
}

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;
}
}
Loading

0 comments on commit 820e5a9

Please sign in to comment.