From c2ceaa25f34608aeb856c1e90eca4c672e79a123 Mon Sep 17 00:00:00 2001 From: Jeff Johnston Date: Tue, 15 Oct 2024 16:49:09 -0400 Subject: [PATCH] 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 --- .../corext/fix/StringConcatToTextBlockFixCore.java | 9 ++++++--- .../org/eclipse/jdt/ui/tests/quickfix/CleanUpTest15.java | 5 +++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/StringConcatToTextBlockFixCore.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/StringConcatToTextBlockFixCore.java index a5a41b4fc46..52aee735b21 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/StringConcatToTextBlockFixCore.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/StringConcatToTextBlockFixCore.java @@ -156,7 +156,7 @@ public boolean visit(final InfixExpression visited) { endPosition= getLineOffset(cUnit, lineNo + 1) == -1 ? cUnit.getLength() : getLineOffset(cUnit, lineNo + 1); hasComments= hasComments || hasNLS(ASTNodes.getCommentsForRegion(cUnit, stringLiteral.getStartPosition(), endPosition - stringLiteral.getStartPosition()), cu); String string= stringLiteral.getLiteralValue(); - if (!string.isEmpty() && (fAllConcats || string.endsWith("\n") || i == extendedOperands.size() - 1)) { //$NON-NLS-1$ + if (string.isEmpty() || fAllConcats || string.endsWith("\n") || i == extendedOperands.size() - 1) { //$NON-NLS-1$ continue; } } @@ -315,8 +315,11 @@ public SourceRange computeSourceRange(final ASTNode nodeWithComment) { expressions.forEach(new Consumer() { @Override public void accept(Expression t) { - String value= ((StringLiteral) t).getEscapedValue(); - parts.addAll(unescapeBlock(value.substring(1, value.length() - 1))); + StringLiteral literal= (StringLiteral)t; + if (!literal.getLiteralValue().equals("\"\"")) { //$NON-NLS-1$ + String value= literal.getEscapedValue(); + parts.addAll(unescapeBlock(value.substring(1, value.length() - 1))); + } } }); diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest15.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest15.java index 6f2a70fed4b..49e7cf98faa 100644 --- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest15.java +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest15.java @@ -117,14 +117,15 @@ public void testParameter() { Integer k = foo("" +\s "abcdef\\n" +\s "123456\\n" +\s - "klm"); + "klm" +\s + ""); } public void testAssignment() { Integer k = null; k = foo("" +\s "abcdef\\n" +\s "123456\\n" +\s - "klm"); + "" + "klm"); } public void testConcatInConstructor() { new StringBuffer("abc\\n" + "def\\n" + "ghi");