From 8d274b02c44f7f63c4cdb956aed73bdfe1c1fdad Mon Sep 17 00:00:00 2001 From: Nicolas Filotto Date: Mon, 23 Oct 2023 16:35:16 +0200 Subject: [PATCH] Ref #945: formatting - Exclude new expressions (#948) ## Motivation When using the keyword new in the middle of a Camel route, the formatting doesn't work as expected so it needs to be improved. ## Modifications: * Ensure that new expressions are not seen as method calls --- .../extension/camel/JavaCamelIdeaUtils.java | 11 +++-- .../formatter/CamelPostFormatProcessorIT.java | 8 ++++ .../formatter/after/Formatting945.txt | 48 +++++++++++++++++++ .../formatter/before/Formatting945.java | 48 +++++++++++++++++++ 4 files changed, 111 insertions(+), 4 deletions(-) create mode 100644 camel-idea-plugin/src/test/resources/testData/formatter/after/Formatting945.txt create mode 100644 camel-idea-plugin/src/test/resources/testData/formatter/before/Formatting945.java diff --git a/camel-idea-plugin/src/main/java/com/github/cameltooling/idea/service/extension/camel/JavaCamelIdeaUtils.java b/camel-idea-plugin/src/main/java/com/github/cameltooling/idea/service/extension/camel/JavaCamelIdeaUtils.java index 1dd52732..501dee90 100644 --- a/camel-idea-plugin/src/main/java/com/github/cameltooling/idea/service/extension/camel/JavaCamelIdeaUtils.java +++ b/camel-idea-plugin/src/main/java/com/github/cameltooling/idea/service/extension/camel/JavaCamelIdeaUtils.java @@ -32,6 +32,7 @@ import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.JavaResolveResult; import com.intellij.psi.PsiAnnotation; +import com.intellij.psi.PsiCallExpression; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiElement; @@ -656,11 +657,11 @@ private static boolean testMethod(PsiFile file, int offset, Predicate#945 + * is properly fixed. + */ + public void testFormatting945() { + doTest("Formatting945", null); + } + /** * Ensures that a partial format not including a route has no effect. */ diff --git a/camel-idea-plugin/src/test/resources/testData/formatter/after/Formatting945.txt b/camel-idea-plugin/src/test/resources/testData/formatter/after/Formatting945.txt new file mode 100644 index 00000000..702644d8 --- /dev/null +++ b/camel-idea-plugin/src/test/resources/testData/formatter/after/Formatting945.txt @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; + +public class Formatting945 extends RouteBuilder { + @Override + public void configure() { + from("direct:start") + .doTry() + .process(new ProcessorFail()) + .to("mock:result") + .doCatch(IOException.class, IllegalStateException.class) + .to("mock:catch") + .doFinally() + .to("mock:finally") + .end(); + + from("direct:start2") + .doTry() + .unmarshal(new JacksonDataFormat(objectMapper, Foo.class)) + .doCatch(JsonProcessingException.class) + .to("direct:deserialization-error-handler") + .end() + } + + public static class ProcessorFail implements Processor { + @Override + public void process(Exchange exchange) throws Exception { + throw new IOException("Forced"); + } + } +} diff --git a/camel-idea-plugin/src/test/resources/testData/formatter/before/Formatting945.java b/camel-idea-plugin/src/test/resources/testData/formatter/before/Formatting945.java new file mode 100644 index 00000000..3cf5936a --- /dev/null +++ b/camel-idea-plugin/src/test/resources/testData/formatter/before/Formatting945.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; + +public class Formatting945 extends RouteBuilder { + @Override + public void configure() { + from("direct:start") + .doTry() + .process(new ProcessorFail()) + .to("mock:result") + .doCatch(IOException.class, IllegalStateException.class) + .to("mock:catch") + .doFinally() + .to("mock:finally") + .end(); + + from("direct:start2") + .doTry() + .unmarshal(new JacksonDataFormat(objectMapper, Foo.class)) + .doCatch(JsonProcessingException.class) + .to("direct:deserialization-error-handler") + .end() + } + + public static class ProcessorFail implements Processor { + @Override + public void process(Exchange exchange) throws Exception { + throw new IOException("Forced"); + } + } +}