-
Notifications
You must be signed in to change notification settings - Fork 79
ReplaceStringBuilderWithString should keep comments #511
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
ReplaceStringBuilderWithString should keep comments #511
Conversation
@@ -126,8 +125,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu | |||
*/ | |||
public static J.Binary concatAdditionBinary(Expression left, Expression right) { | |||
J.Binary b = getAdditiveBinaryTemplate(); | |||
String rightWhiteSpace = right.getPrefix().getWhitespace(); | |||
Space rightPrefix = rightWhiteSpace.isEmpty() ? Space.SINGLE_SPACE : Space.build(rightWhiteSpace, emptyList()); | |||
Space rightPrefix = right.getPrefix().isEmpty() ? Space.SINGLE_SPACE : right.getPrefix(); |
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.
avoid building Space
so we can preserve Comments
Expression additive = ChainStringBuilderAppendCalls.additiveExpression(arguments); | ||
|
||
if (additive == null) { | ||
return m; | ||
} | ||
|
||
if (arguments.get(0).getComments().isEmpty() || !arguments.get(0).getPrefix().getWhitespace().startsWith("\n")) { | ||
additive = additive.withPrefix(method.getPrefix()); | ||
} |
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.
this is to ensure no additional white space is added if there are comments and the first argument has a prefix that begins with a new line.
this accounts for cases such as.
@Test
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/88")
void retainComments() {
rewriteRun(
//language=java
java(
"""
class A {
void foo() {
String scenarioOne = new StringBuilder()
// A
.append("A")
// B
.append("B")
// C
.append("C")
.toString();
}
}
""",
"""
class A {
void foo() {
String scenarioOne =
// A
"A" +
// B
"B" +
// C
"C";
}
"""
)
);
}
@@ -111,7 +120,7 @@ private List<Expression> adjustExpressions(J.MethodInvocation method, List<Expre | |||
} | |||
} | |||
} else if (!(arg instanceof J.Identifier || arg instanceof J.Literal || arg instanceof J.MethodInvocation)) { | |||
return new J.Parentheses<>(randomId(), Space.EMPTY, Markers.EMPTY, JRightPadded.build(arg)); | |||
return new J.Parentheses<>(randomId(), arg.getPrefix(), Markers.EMPTY, JRightPadded.build(arg.withPrefix(Space.EMPTY))); |
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.
preserve the prefix on the Parentheses
so if the comments would look like
// comments
(1 + 1)
instead of
(
// comments
1 + 1)
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.
Thanks for the fix!
What's changed?
This retains comments that may appear such that the following scenario can be supported.
What's your motivation?
-related to ReplaceStringBuilderWithString should keep newlines #88
Anything in particular you'd like reviewers to focus on?
Anyone you would like to review specifically?
Have you considered any alternatives or workarounds?
Any additional context
Checklist