Skip to content

Commit

Permalink
Fix concat to MessageFormat and String.format (#1423)
Browse files Browse the repository at this point in the history
* Fix concat to MessageFormat and String.format

- fix ConvertToMessageFormatFixCore and ConvertToStringFormatFixCore
  to refine the logic for determining when to use a text block vs
  a single line - logic now only uses text block if there are 3 or
  more lines used in the concatenation AND the number of string literal
  characters is greater than 80
- modify existing tests and add new tests to AssistQuickFixTest15
- fixes #1406
  • Loading branch information
jjohnstn committed Jun 7, 2024
1 parent bf11c06 commit 048557e
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModelCore
Expression lastStringLiteral= firstStringLiteral;
Expression firstArgumentExpression= operands.get(0);
Expression lastArgumentExpression= firstArgumentExpression;
int totalSize= 0;
for (Expression operand : operands) {
if (operand instanceof StringLiteral) {
if (isFirstStringLiteral) {
Expand All @@ -262,6 +263,7 @@ public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModelCore
}
}
String value= ((StringLiteral) operand).getEscapedValue();
totalSize += value.length();
value= value.replace("'", "''"); //$NON-NLS-1$ //$NON-NLS-2$
value= value.replace("{", "'{'"); //$NON-NLS-1$ //$NON-NLS-2$
value= value.replace("}", "'}'"); //$NON-NLS-1$ //$NON-NLS-2$
Expand Down Expand Up @@ -304,7 +306,6 @@ public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModelCore
}
}


importRewrite.addImport("java.text.MessageFormat", importContext); //$NON-NLS-1$

StringBuilder buffer= new StringBuilder();
Expand All @@ -314,9 +315,9 @@ public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModelCore
int maxOffset= lastStringLiteral.getStartPosition() > lastArgumentExpression.getStartPosition() ?
lastStringLiteral.getStartPosition() + lastStringLiteral.getLength() : lastArgumentExpression.getStartPosition() + lastArgumentExpression.getLength();

boolean isSingleLine= root.getLineNumber(maxOffset) == root.getLineNumber(minOffset);
boolean isLessThanThreeLines= root.getLineNumber(maxOffset) - root.getLineNumber(minOffset) < 2;

if (is15OrHigher && !isSingleLine) {
if (is15OrHigher && !isLessThanThreeLines && totalSize > 80) {
StringBuilder buf= new StringBuilder();

List<String> parts= new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModelCore
Expression lastStringLiteral= firstStringLiteral;
Expression firstArgumentExpression= operands.get(0);
Expression lastArgumentExpression= firstArgumentExpression;
int totalSize= 0;
for (Expression operand : operands) {
if (operand instanceof StringLiteral) {
if (isFirstStringLiteral) {
Expand All @@ -254,6 +255,7 @@ public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModelCore
}
}
String value= ((StringLiteral) operand).getEscapedValue();
totalSize += value.length();
value= value.replace("%", "%%"); //$NON-NLS-1$ //$NON-NLS-2$
fLiterals.add(value);
value= value.substring(1, value.length() - 1);
Expand Down Expand Up @@ -281,9 +283,9 @@ public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModelCore
int maxOffset= lastStringLiteral.getStartPosition() > lastArgumentExpression.getStartPosition() ?
lastStringLiteral.getStartPosition() + lastStringLiteral.getLength() : lastArgumentExpression.getStartPosition() + lastArgumentExpression.getLength();

boolean isSingleLine= root.getLineNumber(maxOffset) == root.getLineNumber(minOffset);
boolean isLessThanThreeLines= root.getLineNumber(maxOffset) - root.getLineNumber(minOffset) < 2;

if (is15OrHigher && !isSingleLine) {
if (is15OrHigher && !isLessThanThreeLines && totalSize > 80) {
StringBuilder buf= new StringBuilder();

List<String> parts= new ArrayList<>();
Expand Down
2 changes: 1 addition & 1 deletion org.eclipse.jdt.ui.tests/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Automatic-Module-Name: org.eclipse.jdt.ui.tests
Bundle-ManifestVersion: 2
Bundle-Name: %Plugin.name
Bundle-SymbolicName: org.eclipse.jdt.ui.tests; singleton:=true
Bundle-Version: 3.15.400.qualifier
Bundle-Version: 3.15.500.qualifier
Bundle-Activator: org.eclipse.jdt.testplugin.JavaTestPlugin
Bundle-Vendor: %Plugin.providerName
Bundle-Localization: plugin
Expand Down
2 changes: 1 addition & 1 deletion org.eclipse.jdt.ui.tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</parent>
<groupId>org.eclipse.jdt</groupId>
<artifactId>org.eclipse.jdt.ui.tests</artifactId>
<version>3.15.400-SNAPSHOT</version>
<version>3.15.500-SNAPSHOT</version>
<packaging>eclipse-test-plugin</packaging>
<properties>
<defaultSigning-excludeInnerJars>true</defaultSigning-excludeInnerJars>
Expand Down

0 comments on commit 048557e

Please sign in to comment.