Skip to content

Commit ba985f6

Browse files
committed
Fragment support between Java / Template file
See redhat-developer/vscode-quarkus#563 Signed-off-by: azerr <[email protected]>
1 parent 906a34f commit ba985f6

24 files changed

+743
-289
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2023 Red Hat Inc. and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* which accompanies this distribution, and is available at
5+
* http://www.eclipse.org/legal/epl-v20.html
6+
*
7+
* SPDX-License-Identifier: EPL-2.0
8+
*
9+
* Contributors:
10+
* Red Hat Inc. - initial API and implementation
11+
*******************************************************************************/
12+
package com.redhat.qute.commons.datamodel;
13+
14+
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;
15+
16+
public class DataModelFragment<T extends DataModelParameter> extends DataModelParameters<T> {
17+
18+
private String id;
19+
20+
public String getId() {
21+
return id;
22+
}
23+
24+
public void setId(String id) {
25+
this.id = id;
26+
}
27+
28+
@Override
29+
public String toString() {
30+
ToStringBuilder b = new ToStringBuilder(this);
31+
b.add("id", this.id);
32+
b.add("parameters", this.getParameters());
33+
return b.toString();
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2023 Red Hat Inc. and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* which accompanies this distribution, and is available at
5+
* http://www.eclipse.org/legal/epl-v20.html
6+
*
7+
* SPDX-License-Identifier: EPL-2.0
8+
*
9+
* Contributors:
10+
* Red Hat Inc. - initial API and implementation
11+
*******************************************************************************/
12+
package com.redhat.qute.commons.datamodel;
13+
14+
import java.util.ArrayList;
15+
import java.util.List;
16+
import java.util.Map;
17+
import java.util.function.Function;
18+
import java.util.stream.Collectors;
19+
20+
public class DataModelParameters<T extends DataModelParameter> {
21+
22+
private String sourceType;
23+
24+
private String sourceMethod;
25+
26+
private List<T> parameters;
27+
28+
private transient Map<String, T> parametersMap;
29+
30+
/**
31+
* Returns the Java source type where this data model template is defined.
32+
*
33+
* @return the Java source type where this data model template is defined.
34+
*/
35+
public String getSourceType() {
36+
return sourceType;
37+
}
38+
39+
/**
40+
* Set the Java source type where this data model template is defined.
41+
*
42+
* @param sourceType the Java source type where this data model template is
43+
* defined.
44+
*/
45+
public void setSourceType(String sourceType) {
46+
this.sourceType = sourceType;
47+
}
48+
49+
/**
50+
* Returns the Java source method where this data model template is defined and
51+
* null otherwise.
52+
*
53+
* @return the Java source method where this data model template is defined and
54+
* null otherwise.
55+
*/
56+
public String getSourceMethod() {
57+
return sourceMethod;
58+
}
59+
60+
/**
61+
* Set the Java source method where this data model template is defined and null
62+
* otherwise.
63+
*
64+
* @param sourceMethod the Java source method where this data model template is
65+
* defined and null otherwise.
66+
*/
67+
public void setSourceMethod(String sourceMethod) {
68+
this.sourceMethod = sourceMethod;
69+
}
70+
71+
/**
72+
* Returns the list of data model parameters.
73+
*
74+
* @return the list of data model parameters.
75+
*/
76+
public List<T> getParameters() {
77+
return parameters;
78+
}
79+
80+
/**
81+
* Set the list of data model parameters.
82+
*
83+
* @param parameters the list of data model parameters.
84+
*/
85+
public void setParameters(List<T> parameters) {
86+
this.parameters = parameters;
87+
}
88+
89+
/**
90+
* Returns the parameter from the given key and null otherwise.
91+
*
92+
* @param key the parameter key.
93+
*
94+
* @return the parameter from the given key and null otherwise.
95+
*/
96+
public T getParameter(String key) {
97+
List<T> parameters = getParameters();
98+
if (parameters == null) {
99+
return null;
100+
}
101+
return getParametersMap().get(key);
102+
}
103+
104+
public void addParameter(T parameter) {
105+
if (parameters == null) {
106+
parameters = new ArrayList<>();
107+
}
108+
parameters.add(parameter);
109+
getParametersMap().put(parameter.getKey(), parameter);
110+
}
111+
112+
private Map<String, T> getParametersMap() {
113+
if (parametersMap == null) {
114+
parametersMap = parameters.stream()
115+
.collect(Collectors.toMap(DataModelParameter::getKey, Function.identity()));
116+
}
117+
return parametersMap;
118+
}
119+
}

qute.jdt/com.redhat.qute.jdt/src/main/java/com/redhat/qute/commons/datamodel/DataModelTemplate.java

+27-94
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313

1414
import java.util.ArrayList;
1515
import java.util.List;
16-
import java.util.Map;
17-
import java.util.function.Function;
18-
import java.util.stream.Collectors;
1916

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

@@ -27,19 +24,13 @@
2724
*
2825
* @param <T> data model parameter.
2926
*/
30-
public class DataModelTemplate<T extends DataModelParameter> {
27+
public class DataModelTemplate<T extends DataModelParameter> extends DataModelParameters<T> {
3128

3229
private String templateUri;
3330

34-
private String sourceType;
35-
36-
private String sourceMethod;
37-
3831
private String sourceField;
39-
40-
private List<T> parameters;
4132

42-
private transient Map<String, T> parametersMap;
33+
private List<DataModelFragment<T>> fragments;
4334

4435
/**
4536
* Returns the template Uri.
@@ -59,47 +50,6 @@ public void setTemplateUri(String templateUri) {
5950
this.templateUri = templateUri;
6051
}
6152

62-
/**
63-
* Returns the Java source type where this data model template is defined.
64-
*
65-
* @return the Java source type where this data model template is defined.
66-
*/
67-
public String getSourceType() {
68-
return sourceType;
69-
}
70-
71-
/**
72-
* Set the Java source type where this data model template is defined.
73-
*
74-
* @param sourceType the Java source type where this data model template is
75-
* defined.
76-
*/
77-
public void setSourceType(String sourceType) {
78-
this.sourceType = sourceType;
79-
}
80-
81-
/**
82-
* Returns the Java source method where this data model template is defined and
83-
* null otherwise.
84-
*
85-
* @return the Java source method where this data model template is defined and
86-
* null otherwise.
87-
*/
88-
public String getSourceMethod() {
89-
return sourceMethod;
90-
}
91-
92-
/**
93-
* Set the Java source method where this data model template is defined and null
94-
* otherwise.
95-
*
96-
* @param sourceMethod the Java source method where this data model template is
97-
* defined and null otherwise.
98-
*/
99-
public void setSourceMethod(String sourceMethod) {
100-
this.sourceMethod = sourceMethod;
101-
}
102-
10353
/**
10454
* Returns the Java source field where this data model template is defined and
10555
* null otherwise.
@@ -122,63 +72,46 @@ public void setSourceField(String sourceField) {
12272
this.sourceField = sourceField;
12373
}
12474

125-
/**
126-
* Returns the list of data model parameters.
127-
*
128-
* @return the list of data model parameters.
129-
*/
130-
public List<T> getParameters() {
131-
return parameters;
75+
public void addFragment(DataModelFragment<T> fragment) {
76+
if (fragments == null) {
77+
fragments = new ArrayList<>();
78+
}
79+
fragments.add(fragment);
13280
}
13381

134-
/**
135-
* Set the list of data model parameters.
136-
*
137-
* @param parameters the list of data model parameters.
138-
*/
139-
public void setParameters(List<T> parameters) {
140-
this.parameters = parameters;
82+
public List<DataModelFragment<T>> getFragments() {
83+
return fragments;
14184
}
14285

143-
/**
144-
* Returns the parameter from the given key and null otherwise.
145-
*
146-
* @param key the parameter key.
147-
*
148-
* @return the parameter from the given key and null otherwise.
149-
*/
150-
public T getParameter(String key) {
151-
List<T> parameters = getParameters();
152-
if (parameters == null) {
153-
return null;
154-
}
155-
return getParametersMap().get(key);
86+
public void setFragments(List<DataModelFragment<T>> fragments) {
87+
this.fragments = fragments;
15688
}
15789

158-
public void addParameter(T parameter) {
159-
if (parameters == null) {
160-
parameters = new ArrayList<>();
90+
public DataModelFragment<T> getFragment(String fragmentId) {
91+
if (fragmentId == null) {
92+
return null;
16193
}
162-
parameters.add(parameter);
163-
getParametersMap().put(parameter.getKey(), parameter);
164-
}
165-
166-
private Map<String, T> getParametersMap() {
167-
if (parametersMap == null) {
168-
parametersMap = parameters.stream()
169-
.collect(Collectors.toMap(DataModelParameter::getKey, Function.identity()));
94+
List<DataModelFragment<T>> fragments = getFragments();
95+
if (fragments == null || fragments.isEmpty()) {
96+
return null;
97+
}
98+
for (DataModelFragment<T> fragment : fragments) {
99+
if (fragmentId.equals(fragment.getId())) {
100+
return fragment;
101+
}
170102
}
171-
return parametersMap;
103+
return null;
172104
}
173105

174106
@Override
175107
public String toString() {
176108
ToStringBuilder b = new ToStringBuilder(this);
177109
b.add("templateUri", this.templateUri);
178-
b.add("sourceType", this.sourceType);
179-
b.add("sourceMethod", this.sourceMethod);
110+
b.add("sourceType", this.getSourceType());
111+
b.add("sourceMethod", this.getSourceMethod());
180112
b.add("sourceField", this.sourceField);
181-
b.add("parameters", this.parameters);
113+
b.add("parameters", this.getParameters());
114+
b.add("fragments", this.getFragments());
182115
return b.toString();
183116
}
184117
}

qute.jdt/com.redhat.qute.jdt/src/main/java/com/redhat/qute/jdt/internal/QuteJavaConstants.java

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public class QuteJavaConstants {
4141

4242
public static final String OLD_CHECKED_TEMPLATE_ANNOTATION = "io.quarkus.qute.api.CheckedTemplate";
4343

44+
public static final String CHECKED_TEMPLATE_ANNOTATION_IGNORE_FRAGMENTS = "ignoreFragments";
45+
4446
// @TemplateExtension
4547

4648
public static final String TEMPLATE_EXTENSION_ANNOTATION = "io.quarkus.qute.TemplateExtension";

0 commit comments

Comments
 (0)