Skip to content

Commit 83ded43

Browse files
authored
Allow routes containing Java DSL at start to be formatted (#1208)
1 parent 010c629 commit 83ded43

File tree

6 files changed

+178
-10
lines changed

6 files changed

+178
-10
lines changed

src/main/java/com/github/cameltooling/idea/service/extension/camel/JavaCamelIdeaUtils.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public class JavaCamelIdeaUtils extends CamelIdeaUtils implements CamelIdeaUtils
9797
private static final Set<String> ADD_INDENT = Set.of("choice", "doTry", "pipeline", "multicast", "split",
9898
"circuitBreaker", "intercept", "interceptFrom", "interceptSendToEndpoint", "aggregate", "loadBalance", "loop",
9999
"kamelet", "step", "transacted", "saga", "route", "resequence", "policy", "onException", "onCompletion",
100-
"from", "rest", "restConfiguration");
100+
"from", "rest", "restConfiguration", "routeTemplate");
101101
/**
102102
* Name of the methods corresponding to the root element of sub DSL.
103103
*/
@@ -482,13 +482,20 @@ private List<PsiElement> findEndpoints(Project project, SearchScope scope, Predi
482482
Collection<PsiClass> allClasses = AllClassesSearch.search(scope, project).findAll();
483483
for (PsiClass aClass : allClasses) {
484484
boolean insideRouteBuilder = aClass.isInheritorDeep(routeBuilderClass, null);
485-
Collection<PsiLiteralExpression> literals = PsiTreeUtil.findChildrenOfType(aClass, PsiLiteralExpression.class);
486-
for (PsiLiteralExpression literal : literals) {
487-
if (insideRouteBuilder || isConsumerEndpoint(literal) || isProducerEndpoint(literal)) {
488-
Object val = literal.getValue();
489-
if (val instanceof String endpointUri) {
490-
if (uriCondition.test(endpointUri) && elementCondition.test(literal)) {
491-
results.add(literal);
485+
Collection<PsiExpression> expressions = PsiTreeUtil.findChildrenOfType(aClass, PsiExpression.class);
486+
for (PsiExpression expression : expressions) {
487+
if (insideRouteBuilder || isConsumerEndpoint(expression) || isProducerEndpoint(expression)) {
488+
if (expression instanceof PsiLiteralExpression literalExpression) {
489+
Object val = literalExpression.getValue();
490+
if (val instanceof String endpointUri) {
491+
if (uriCondition.test(endpointUri) && elementCondition.test(literalExpression)) {
492+
results.add(expression);
493+
}
494+
}
495+
} else if (expression instanceof PsiMethodCallExpression methodCallExpression) {
496+
if (isCamelRouteStartExpression(methodCallExpression)
497+
&& !List.of("kamelet", "templateParameter").contains(methodCallExpression.getMethodExpression().getReferenceName())) {
498+
results.add(methodCallExpression);
492499
}
493500
}
494501
}
@@ -846,7 +853,9 @@ private static boolean isReturnTypeOfMethodToFormat(String methodName, String re
846853
// By default, it is accepted
847854
return true;
848855
}
849-
return !"org.apache.camel.builder.ValueBuilder".equals(returnType) || "expression".equals(methodName);
856+
return (!"org.apache.camel.builder.ValueBuilder".equals(returnType)
857+
&& !returnType.startsWith("org.apache.camel.builder.endpoint.dsl"))
858+
|| "expression".equals(methodName);
850859
}
851860

852861
/**

src/test/java/com/github/cameltooling/idea/formatter/CamelPostFormatProcessorIT.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.intellij.openapi.command.WriteCommandAction;
2323
import com.intellij.openapi.project.Project;
2424
import com.intellij.openapi.util.TextRange;
25-
import com.intellij.psi.PsiFile;
2625
import com.intellij.psi.codeStyle.CodeStyleManager;
2726
import com.intellij.testFramework.DumbModeTestUtils;
2827
import com.intellij.testFramework.PlatformTestUtil;
@@ -51,6 +50,20 @@ public void testRouteWithDataFormatDSL() {
5150
doTest("RouteWithDataFormatDSL", null);
5251
}
5352

53+
/**
54+
* Ensures that a route using only the Java DSL is supported.
55+
*/
56+
public void testRouteWithOnlyJavaDSL() {
57+
doTest("RouteWithOnlyJavaDSL", null);
58+
}
59+
60+
/**
61+
* Ensures that a route template is supported.
62+
*/
63+
public void testRouteTemplate() {
64+
doTest("RouteTemplate", null);
65+
}
66+
5467
/**
5568
* Ensures that a malformed route won't make the formatter fail.
5669
*/
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import org.apache.camel.builder.RouteBuilder;
19+
20+
import static org.apache.camel.builder.endpoint.StaticEndpointBuilders.kamelet;
21+
22+
public class RouteTemplate extends RouteBuilder {
23+
24+
@Override
25+
public void configure() throws Exception {
26+
routeTemplate("template")
27+
.templateParameter("v1")
28+
.from(kamelet("source"))
29+
.setProperty("version", constant("{{version}}"));
30+
}
31+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import java.util.Map;
19+
20+
import org.apache.camel.builder.RouteBuilder;
21+
import org.apache.camel.model.dataformat.CsvDataFormat;
22+
23+
import static org.apache.camel.builder.endpoint.StaticEndpointBuilders.direct;
24+
25+
public class RouteWithOnlyJavaDSL extends RouteBuilder {
26+
27+
@Override
28+
public void configure() throws Exception {
29+
CsvDataFormat dataFormat = new CsvDataFormat();
30+
dataFormat.setDelimiter("|");
31+
32+
from(direct("format"))
33+
.setBody(constant(Map.of("foo", "abc", "bar", 123)))
34+
.marshal(dataFormat);
35+
36+
from(direct("format"))
37+
.setBody(constant(Map.of("foo", "abc", "bar", 123)))
38+
.marshal()
39+
.csv();
40+
41+
from(direct("format"))
42+
.setBody(constant(Map.of("foo", "abc", "bar", 123)))
43+
.marshal(
44+
dataFormat()
45+
.csv()
46+
.delimiter(",")
47+
.end());
48+
}
49+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import org.apache.camel.builder.RouteBuilder;
19+
20+
import static org.apache.camel.builder.endpoint.StaticEndpointBuilders.kamelet;
21+
22+
public class RouteTemplate extends RouteBuilder {
23+
24+
@Override
25+
public void configure() throws Exception {
26+
routeTemplate("template").templateParameter("v1").from(kamelet("source")).setProperty("version", constant("{{version}}"));
27+
}
28+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import java.util.Map;
19+
20+
import org.apache.camel.builder.RouteBuilder;
21+
import org.apache.camel.model.dataformat.CsvDataFormat;
22+
23+
import static org.apache.camel.builder.endpoint.StaticEndpointBuilders.direct;
24+
25+
public class RouteWithOnlyJavaDSL extends RouteBuilder {
26+
27+
@Override
28+
public void configure() throws Exception {
29+
CsvDataFormat dataFormat = new CsvDataFormat();
30+
dataFormat.setDelimiter("|");
31+
32+
from(direct("format")).setBody(constant(Map.of("foo", "abc", "bar", 123))).marshal(dataFormat);
33+
34+
from(direct("format")).setBody(constant(Map.of("foo", "abc", "bar", 123))).marshal().csv();
35+
36+
from(direct("format")).setBody(constant(Map.of("foo", "abc", "bar", 123))).marshal(dataFormat().csv().delimiter(",").end());
37+
}
38+
}

0 commit comments

Comments
 (0)