Skip to content

Commit c2ceaa2

Browse files
authored
Modify string concat to text block cleanup to handle empty strings (#1717)
- add logic to StringConcatToTextBlockFixCore to handle the case where empty strings are appended - modify tests in CleanUpTest15 to add some empty strings to existing tests - for #1703
1 parent 29f2f2d commit c2ceaa2

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/StringConcatToTextBlockFixCore.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public boolean visit(final InfixExpression visited) {
156156
endPosition= getLineOffset(cUnit, lineNo + 1) == -1 ? cUnit.getLength() : getLineOffset(cUnit, lineNo + 1);
157157
hasComments= hasComments || hasNLS(ASTNodes.getCommentsForRegion(cUnit, stringLiteral.getStartPosition(), endPosition - stringLiteral.getStartPosition()), cu);
158158
String string= stringLiteral.getLiteralValue();
159-
if (!string.isEmpty() && (fAllConcats || string.endsWith("\n") || i == extendedOperands.size() - 1)) { //$NON-NLS-1$
159+
if (string.isEmpty() || fAllConcats || string.endsWith("\n") || i == extendedOperands.size() - 1) { //$NON-NLS-1$
160160
continue;
161161
}
162162
}
@@ -315,8 +315,11 @@ public SourceRange computeSourceRange(final ASTNode nodeWithComment) {
315315
expressions.forEach(new Consumer<Expression>() {
316316
@Override
317317
public void accept(Expression t) {
318-
String value= ((StringLiteral) t).getEscapedValue();
319-
parts.addAll(unescapeBlock(value.substring(1, value.length() - 1)));
318+
StringLiteral literal= (StringLiteral)t;
319+
if (!literal.getLiteralValue().equals("\"\"")) { //$NON-NLS-1$
320+
String value= literal.getEscapedValue();
321+
parts.addAll(unescapeBlock(value.substring(1, value.length() - 1)));
322+
}
320323
}
321324
});
322325

org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest15.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,15 @@ public void testParameter() {
117117
Integer k = foo("" +\s
118118
"abcdef\\n" +\s
119119
"123456\\n" +\s
120-
"klm");
120+
"klm" +\s
121+
"");
121122
}
122123
public void testAssignment() {
123124
Integer k = null;
124125
k = foo("" +\s
125126
"abcdef\\n" +\s
126127
"123456\\n" +\s
127-
"klm");
128+
"" + "klm");
128129
}
129130
public void testConcatInConstructor() {
130131
new StringBuffer("abc\\n" + "def\\n" + "ghi");

0 commit comments

Comments
 (0)