-
Notifications
You must be signed in to change notification settings - Fork 466
ChopIfTooLong for methodInvocation chains #6112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Jenson3210
wants to merge
10
commits into
main
Choose a base branch
from
chop-methodinvocations-if-too-long
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0e4652f
ChopIfTooLong for methodInvocation chains
Jenson3210 e4af05b
license header
Jenson3210 2efcaad
best practices
Jenson3210 c9611c5
Merge branch 'main' into chop-methodinvocations-if-too-long
Jenson3210 cf8de40
Update rewrite-java/src/main/java/org/openrewrite/java/format/WrapMet…
Jenson3210 37c8640
Removed unneeded comment
Jenson3210 29fb5ec
Use line length calculator
Jenson3210 300100a
license + eol
Jenson3210 f8fb518
comments
Jenson3210 65269cd
Merge branch 'main' into chop-methodinvocations-if-too-long
timtebeek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
79 changes: 79 additions & 0 deletions
79
rewrite-java/src/test/java/org/openrewrite/java/format/MinimumViableSpacingVisitorTest.java
This file contains hidden or 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,79 @@ | ||
package org.openrewrite.java.format; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import static org.openrewrite.java.Assertions.java; | ||
import static org.openrewrite.test.RewriteTest.toRecipe; | ||
|
||
class MinimumViableSpacingVisitorTest implements RewriteTest { | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec | ||
.parser(JavaParser.fromJavaVersion().dependsOn(""" | ||
package com.example; | ||
|
||
public class MyObject { | ||
public static Builder builder() { return new Builder(); } | ||
public static Builder newBuilder() { return new Builder(); } | ||
public static class Builder { | ||
Builder name(String n) { return this; } | ||
Builder age(int a) { return this; } | ||
Builder items(java.util.List<String> items) { return this; } | ||
Builder nested(MyObject nested) { return this; } | ||
MyObject build() { return new MyObject(); } | ||
} | ||
} | ||
""")) | ||
.recipe(toRecipe(() -> new MinimumViableSpacingVisitor<>(null))); | ||
} | ||
|
||
@Test | ||
void reformatChainedMethodInvocationToSingleLine() { | ||
rewriteRun( | ||
java( | ||
""" | ||
package com.example; | ||
class Test { | ||
void test() { | ||
MyObject myObject = MyObject.builder() | ||
. | ||
|
||
name("John"); | ||
} | ||
} | ||
""", | ||
""" | ||
package com.example; | ||
class Test { | ||
void test() { | ||
MyObject myObject = MyObject.builder() | ||
.name("John"); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void doNotReformatChainedMethodInvocationToSingleLineWhenCommentInPrefixOfName() { | ||
rewriteRun( | ||
java( | ||
""" | ||
package com.example; | ||
class Test { | ||
void test() { | ||
MyObject myObject = MyObject.builder() | ||
. //Some comment | ||
name("John"); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} | ||
jkschneider marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
This file contains hidden or 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 |
---|---|---|
|
@@ -16,6 +16,8 @@ | |
package org.openrewrite.java.format; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.java.style.WrappingAndBracesStyle; | ||
import org.openrewrite.test.RecipeSpec; | ||
|
@@ -24,8 +26,7 @@ | |
import java.util.Arrays; | ||
|
||
import static org.openrewrite.java.Assertions.java; | ||
import static org.openrewrite.style.LineWrapSetting.DoNotWrap; | ||
import static org.openrewrite.style.LineWrapSetting.WrapAlways; | ||
import static org.openrewrite.style.LineWrapSetting.*; | ||
import static org.openrewrite.test.RewriteTest.toRecipe; | ||
|
||
class WrapMethodChainsTest implements RewriteTest { | ||
|
@@ -50,6 +51,7 @@ public static class Builder { | |
""")) | ||
.recipe(toRecipe(() -> new WrappingAndBracesVisitor<>( | ||
new WrappingAndBracesStyle( | ||
120, | ||
new WrappingAndBracesStyle.IfStatement(false), | ||
new WrappingAndBracesStyle.ChainedMethodCalls(WrapAlways, Arrays.asList("builder", "newBuilder")), | ||
new WrappingAndBracesStyle.Annotations(WrapAlways), | ||
|
@@ -473,6 +475,7 @@ void test() { | |
void formatStreamChain() { | ||
rewriteRun( | ||
spec -> spec.recipe(toRecipe(() -> new WrappingAndBracesVisitor<>(new WrappingAndBracesStyle( | ||
120, | ||
new WrappingAndBracesStyle.IfStatement(false), | ||
new WrappingAndBracesStyle.ChainedMethodCalls(WrapAlways, Arrays.asList("stream")), | ||
null, | ||
|
@@ -569,6 +572,7 @@ void test() { | |
void formatStreamWithMultilineFilterLambda() { | ||
rewriteRun( | ||
spec -> spec.recipe(toRecipe(() -> new WrappingAndBracesVisitor<>(new WrappingAndBracesStyle( | ||
120, | ||
new WrappingAndBracesStyle.IfStatement(false), | ||
new WrappingAndBracesStyle.ChainedMethodCalls(WrapAlways, Arrays.asList("stream")), | ||
null, | ||
|
@@ -641,6 +645,7 @@ static class Item { | |
void formatStreamWithMultipleMultilineLambdas() { | ||
rewriteRun( | ||
spec -> spec.recipe(toRecipe(() -> new WrappingAndBracesVisitor<>(new WrappingAndBracesStyle( | ||
120, | ||
new WrappingAndBracesStyle.IfStatement(false), | ||
new WrappingAndBracesStyle.ChainedMethodCalls(WrapAlways, Arrays.asList("stream")), | ||
null, | ||
|
@@ -715,6 +720,7 @@ boolean isValid() { | |
void formatStreamWithMixedLambdaStyles() { | ||
rewriteRun( | ||
spec -> spec.recipe(toRecipe(() -> new WrappingAndBracesVisitor<>(new WrappingAndBracesStyle( | ||
120, | ||
new WrappingAndBracesStyle.IfStatement(false), | ||
new WrappingAndBracesStyle.ChainedMethodCalls(WrapAlways, Arrays.asList("stream")), | ||
null, | ||
|
@@ -773,6 +779,7 @@ List<Integer> process(List<String> items) { | |
void formatStreamWithComplexNestedLambda() { | ||
rewriteRun( | ||
spec -> spec.recipe(toRecipe(() -> new WrappingAndBracesVisitor<>(new WrappingAndBracesStyle( | ||
120, | ||
new WrappingAndBracesStyle.IfStatement(false), | ||
new WrappingAndBracesStyle.ChainedMethodCalls(WrapAlways, Arrays.asList("stream")), | ||
null, | ||
|
@@ -848,6 +855,7 @@ static class Employee { | |
void formatStreamWithMethodReferencesAndLambdas() { | ||
rewriteRun( | ||
spec -> spec.recipe(toRecipe(() -> new WrappingAndBracesVisitor<>(new WrappingAndBracesStyle( | ||
120, | ||
new WrappingAndBracesStyle.IfStatement(false), | ||
new WrappingAndBracesStyle.ChainedMethodCalls(WrapAlways, Arrays.asList("stream")), | ||
null, | ||
|
@@ -909,6 +917,7 @@ String preprocess(String s) { | |
void formatStreamWithPeekAndMultilineLambda() { | ||
rewriteRun( | ||
spec -> spec.recipe(toRecipe(() -> new WrappingAndBracesVisitor<>(new WrappingAndBracesStyle( | ||
120, | ||
new WrappingAndBracesStyle.IfStatement(false), | ||
new WrappingAndBracesStyle.ChainedMethodCalls(WrapAlways, Arrays.asList("stream")), | ||
null, | ||
|
@@ -964,6 +973,7 @@ List<String> process(List<String> items) { | |
void preserveAlreadyFormattedStreamWithMultilineLambda() { | ||
rewriteRun( | ||
spec -> spec.recipe(toRecipe(() -> new WrappingAndBracesVisitor<>(new WrappingAndBracesStyle( | ||
120, | ||
new WrappingAndBracesStyle.IfStatement(false), | ||
new WrappingAndBracesStyle.ChainedMethodCalls(WrapAlways, Arrays.asList("stream")), | ||
null, | ||
|
@@ -1038,6 +1048,7 @@ static class Item { | |
void formatStreamWithReduceMultilineLambda() { | ||
rewriteRun( | ||
spec -> spec.recipe(toRecipe(() -> new WrappingAndBracesVisitor<>(new WrappingAndBracesStyle( | ||
120, | ||
new WrappingAndBracesStyle.IfStatement(false), | ||
new WrappingAndBracesStyle.ChainedMethodCalls(WrapAlways, Arrays.asList("stream")), | ||
null, | ||
|
@@ -1087,6 +1098,7 @@ Integer sum(List<Integer> numbers) { | |
void formatStreamInBuilderArgument() { | ||
rewriteRun( | ||
spec -> spec.recipe(toRecipe(() -> new WrappingAndBracesVisitor<>(new WrappingAndBracesStyle( | ||
120, | ||
new WrappingAndBracesStyle.IfStatement(false), | ||
new WrappingAndBracesStyle.ChainedMethodCalls(WrapAlways, Arrays.asList("builder", "stream")), | ||
null, | ||
|
@@ -1129,4 +1141,71 @@ MyObject process(List<String> items) { | |
) | ||
); | ||
} | ||
|
||
@Test | ||
void chopIfTooLong() { | ||
rewriteRun( | ||
spec -> spec.recipe(toRecipe(() -> new WrappingAndBracesVisitor<>(new WrappingAndBracesStyle( | ||
70, | ||
new WrappingAndBracesStyle.IfStatement(false), | ||
new WrappingAndBracesStyle.ChainedMethodCalls(ChopIfTooLong, Arrays.asList("builder", "stream")), | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null)))), | ||
java( | ||
""" | ||
package com.example; | ||
|
||
class Test { | ||
void test() { | ||
MyObject obj = MyObject.builder().name("test").age(25).build(); | ||
} | ||
} | ||
""", | ||
""" | ||
package com.example; | ||
|
||
class Test { | ||
void test() { | ||
MyObject obj = MyObject.builder() | ||
.name("test") | ||
.age(25) | ||
.build(); | ||
Comment on lines
+1173
to
+1176
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Somewhat surprised by the left aligned new lines here; is the expectation that a formatter is run afterwards? |
||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(ints = {71, 72}) | ||
void doNotChopIfNotTooLong(int length) { | ||
rewriteRun( | ||
spec -> spec.recipe(toRecipe(() -> new WrappingAndBracesVisitor<>(new WrappingAndBracesStyle( | ||
length, | ||
new WrappingAndBracesStyle.IfStatement(false), | ||
new WrappingAndBracesStyle.ChainedMethodCalls(ChopIfTooLong, Arrays.asList("builder", "stream")), | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null)))), | ||
java( | ||
""" | ||
package com.example; | ||
|
||
class Test { | ||
void test() { | ||
MyObject obj = MyObject.builder().name("test").age(25).build(); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A method invocation meeting this condition could be contained within another method invocation which meets this condition. In that circumstance the inner method would not be reformatted as no traversal into sub-elements happens when this condition is matched.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch, thanks!