-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance autoformat logic for Groovy files (#4009)
* Enhance autoformat logic for Groovy files * Fix failing Gradle tests
- Loading branch information
1 parent
0727b0a
commit 2055f3f
Showing
10 changed files
with
666 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
rewrite-groovy/src/main/java/org/openrewrite/groovy/format/AutoFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* Licensed 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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. | ||
*/ | ||
package org.openrewrite.groovy.format; | ||
|
||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
|
||
public class AutoFormat extends Recipe { | ||
@Override | ||
public String getDisplayName() { | ||
return "Format Groovy code"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Format Groovy code using a standard comprehensive set of Groovy formatting recipes."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return new AutoFormatVisitor<>(null); | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
rewrite-groovy/src/main/java/org/openrewrite/groovy/format/AutoFormatVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* Licensed 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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. | ||
*/ | ||
package org.openrewrite.groovy.format; | ||
|
||
import org.openrewrite.Cursor; | ||
import org.openrewrite.Tree; | ||
import org.openrewrite.groovy.GroovyIsoVisitor; | ||
import org.openrewrite.internal.lang.Nullable; | ||
import org.openrewrite.java.format.*; | ||
import org.openrewrite.java.style.*; | ||
import org.openrewrite.java.tree.J; | ||
import org.openrewrite.java.tree.JavaSourceFile; | ||
import org.openrewrite.style.GeneralFormatStyle; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.openrewrite.java.format.AutodetectGeneralFormatStyle.autodetectGeneralFormatStyle; | ||
|
||
public class AutoFormatVisitor<P> extends GroovyIsoVisitor<P> { | ||
@Nullable | ||
private final Tree stopAfter; | ||
|
||
@SuppressWarnings("unused") | ||
public AutoFormatVisitor() { | ||
this(null); | ||
} | ||
|
||
public AutoFormatVisitor(@Nullable Tree stopAfter) { | ||
this.stopAfter = stopAfter; | ||
} | ||
|
||
@Override | ||
public J visit(@Nullable Tree tree, P p, Cursor cursor) { | ||
JavaSourceFile cu = (tree instanceof JavaSourceFile) ? | ||
(JavaSourceFile) tree : | ||
cursor.firstEnclosingOrThrow(JavaSourceFile.class); | ||
|
||
J t = new NormalizeFormatVisitor<>(stopAfter).visit(tree, p, cursor.fork()); | ||
|
||
t = new BlankLinesVisitor<>(Optional.ofNullable(cu.getStyle(BlankLinesStyle.class)) | ||
.orElse(IntelliJ.blankLines()), stopAfter) | ||
.visit(t, p, cursor.fork()); | ||
|
||
t = new WrappingAndBracesVisitor<>(Optional.ofNullable(cu.getStyle(WrappingAndBracesStyle.class)) | ||
.orElse(IntelliJ.wrappingAndBraces()), stopAfter) | ||
.visit(t, p, cursor.fork()); | ||
|
||
t = new SpacesVisitor<>( | ||
Optional.ofNullable(cu.getStyle(SpacesStyle.class)).orElse(IntelliJ.spaces()), | ||
cu.getStyle(EmptyForInitializerPadStyle.class), | ||
cu.getStyle(EmptyForIteratorPadStyle.class), | ||
stopAfter | ||
).visit(t, p, cursor.fork()); | ||
|
||
t = new NormalizeTabsOrSpacesVisitor<>(Optional.ofNullable(cu.getStyle(TabsAndIndentsStyle.class)) | ||
.orElse(IntelliJ.tabsAndIndents()), stopAfter) | ||
.visit(t, p, cursor.fork()); | ||
|
||
t = new TabsAndIndentsVisitor<>(Optional.ofNullable(cu.getStyle(TabsAndIndentsStyle.class)) | ||
.orElse(IntelliJ.tabsAndIndents()), stopAfter) | ||
.visit(t, p, cursor.fork()); | ||
|
||
t = new NormalizeLineBreaksVisitor<>(Optional.ofNullable(cu.getStyle(GeneralFormatStyle.class)) | ||
.orElse(autodetectGeneralFormatStyle(cu)), stopAfter) | ||
.visit(t, p, cursor.fork()); | ||
|
||
t = new RemoveTrailingWhitespaceVisitor<>(stopAfter).visit(t, p, cursor.fork()); | ||
|
||
t = new OmitParenthesesForLastArgumentLambdaVisitor<>(stopAfter).visitNonNull(t, p, cursor.fork()); | ||
|
||
t = new MinimumViableSpacingVisitor<>(stopAfter).visitNonNull(t, p, cursor.fork()); | ||
|
||
return t; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
rewrite-groovy/src/main/java/org/openrewrite/groovy/service/GroovyAutoFormatService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* Licensed 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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. | ||
*/ | ||
package org.openrewrite.groovy.service; | ||
|
||
import org.openrewrite.Tree; | ||
import org.openrewrite.groovy.format.AutoFormatVisitor; | ||
import org.openrewrite.internal.lang.Nullable; | ||
import org.openrewrite.java.JavaVisitor; | ||
import org.openrewrite.java.service.AutoFormatService; | ||
|
||
public class GroovyAutoFormatService extends AutoFormatService { | ||
@Override | ||
public <P> JavaVisitor<P> autoFormatVisitor(@Nullable Tree stopAfter) { | ||
return new AutoFormatVisitor<>(stopAfter); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
rewrite-groovy/src/test/java/org/openrewrite/groovy/format/AutoFormatVisitorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* Licensed 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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. | ||
*/ | ||
package org.openrewrite.groovy.format; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.DocumentExample; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import static org.openrewrite.groovy.Assertions.groovy; | ||
|
||
class AutoFormatVisitorTest implements RewriteTest { | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new AutoFormat()); | ||
} | ||
|
||
@DocumentExample | ||
@Test | ||
void keepMaximumBetweenHeaderAndPackage() { | ||
rewriteRun( | ||
groovy( | ||
""" | ||
/* | ||
* This is a sample file. | ||
*/ | ||
package com.intellij.samples | ||
class Test { | ||
} | ||
""", | ||
""" | ||
/* | ||
* This is a sample file. | ||
*/ | ||
package com.intellij.samples | ||
class Test { | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |
Oops, something went wrong.