From 77f5098f9efb1379ce385ae3da032f91f39807c2 Mon Sep 17 00:00:00 2001 From: Jeff Johnston Date: Fri, 6 Dec 2024 20:45:16 -0500 Subject: [PATCH] Enhance StringBuffer/StringBuilder to text block cleanup/quickfix (#1842) - modify StringConcatToTextBlockFixCore to support the case where the StringBuilder/StringBuffer is passed as an argument and in that case do not replace the StringBuilder/StringBuffer but change it to initialize with a text block - add new tests to AssistQuickFixTest15 and CleanUpTest15 - fixes #1841 --- .../fix/StringConcatToTextBlockFixCore.java | 195 ++- .../tests/quickfix/AssistQuickFixTest15.java | 1177 +++++++++-------- .../jdt/ui/tests/quickfix/CleanUpTest15.java | 16 + 3 files changed, 785 insertions(+), 603 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 52aee735b21..d02dd10adf2 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 @@ -67,6 +67,7 @@ import org.eclipse.jdt.core.dom.VariableDeclarationStatement; import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; +import org.eclipse.jdt.core.dom.rewrite.ListRewrite; import org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer; import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; @@ -509,6 +510,7 @@ private class CheckValidityVisitor extends ASTVisitor { private List toStringList= new ArrayList<>(); private List indexOfList= new ArrayList<>(); private List argList= new ArrayList<>(); + private boolean passedAsArgument= false; public CheckValidityVisitor(int lastStatementEnd, IBinding varBinding) { this.lastStatementEnd= lastStatementEnd; @@ -519,6 +521,10 @@ public boolean isValid() { return valid; } + public boolean isPassedAsArgument() { + return passedAsArgument; + } + public ExpressionStatement getNextAssignment() { return nextAssignment; } @@ -581,8 +587,11 @@ public boolean visit(SimpleName name) { paramType.getQualifiedName().equals("java.lang.CharSequence")) { //$NON-NLS-1$ argList.add(name); valid= true; + return true; } else { - valid= false; + passedAsArgument= true; + valid= true; + return true; } } } else { @@ -739,9 +748,22 @@ public boolean visit(SimpleName simpleName) { } catch (AbortSearchException e) { // do nothing } - if (!checkValidityVisitor.isValid() || (checkValidityVisitor.getToStringList().isEmpty() && checkValidityVisitor.getArgList().isEmpty())) { + + if (!checkValidityVisitor.isValid()) { + return failure(); + } else if (checkValidityVisitor.isPassedAsArgument()) { + List statements= new ArrayList<>(statementList); + List literals= new ArrayList<>(fLiterals); + ModifyStringBufferToUseTextBlock operation= new ModifyStringBufferToUseTextBlock(node, statements, + literals); + fOperations.add(operation); + statementList.clear(); + fLiterals.clear(); + return false; + } else if (checkValidityVisitor.getToStringList().isEmpty() && checkValidityVisitor.getArgList().isEmpty()) { return failure(); } + ExpressionStatement assignmentToConvert= checkValidityVisitor.getNextAssignment(); List statements= new ArrayList<>(statementList); List literals= new ArrayList<>(fLiterals); @@ -884,6 +906,47 @@ private static boolean hasNLSMarker(ASTNode node, ICompilationUnit cu) throws Ab return hasNLS; } + public static class ModifyStringBufferToUseTextBlock extends CompilationUnitRewriteOperation { + + private final List fStatements; + private final List fLiterals; + private final ClassInstanceCreation fConstructor; + + public ModifyStringBufferToUseTextBlock(final ClassInstanceCreation constructor, final List statements, final List literals) { + fStatements= statements; + fLiterals= literals; + fConstructor= constructor; + } + + @Override + public void rewriteAST(final CompilationUnitRewrite cuRewrite, final LinkedProposalModelCore linkedModel) throws CoreException { + ASTRewrite rewrite= cuRewrite.getASTRewrite(); + TextEditGroup group= createTextEditGroup(MultiFixMessages.StringConcatToTextBlockCleanUp_description, cuRewrite); + rewrite.setTargetSourceRangeComputer(new TargetSourceRangeComputer() { + @Override + public SourceRange computeSourceRange(final ASTNode nodeWithComment) { + if (Boolean.TRUE.equals(nodeWithComment.getProperty(ASTNodes.UNTOUCH_COMMENT))) { + return new SourceRange(nodeWithComment.getStartPosition(), nodeWithComment.getLength()); + } + + return super.computeSourceRange(nodeWithComment); + } + }); + + StringBuilder buf= createTextBlockBuffer(fLiterals, cuRewrite); + TextBlock textBlock= (TextBlock) rewrite.createStringPlaceholder(buf.toString(), ASTNode.TEXT_BLOCK); + ListRewrite argList= rewrite.getListRewrite(fConstructor, ClassInstanceCreation.ARGUMENTS_PROPERTY); + List original= argList.getOriginalList(); + for (ASTNode node : original) { + rewrite.remove(node, group); + } + argList.insertFirst(textBlock, group); + for (int i= 1; i < fStatements.size(); ++i) { + rewrite.remove(fStatements.get(i), group); + } + } + } + public static class ChangeStringBufferToTextBlock extends CompilationUnitRewriteOperation { private final List fToStringList; @@ -891,7 +954,6 @@ public static class ChangeStringBufferToTextBlock extends CompilationUnitRewrite private final List fArgList; private final List fStatements; private final List fLiterals; - private String fIndent; private final Set fExcludedNames; private final BodyDeclaration fLastBodyDecl; private final boolean fNonNLS; @@ -905,7 +967,6 @@ public ChangeStringBufferToTextBlock(final List toStringList, this.fStatements= statements; this.fLiterals= literals; this.fAssignmentToConvert= assignmentToConvert; - this.fIndent= "\t"; //$NON-NLS-1$ this.fExcludedNames= excludedNames; this.fLastBodyDecl= lastBodyDecl; this.fNonNLS= nonNLS; @@ -927,19 +988,6 @@ public void rewriteAST(final CompilationUnitRewrite cuRewrite, final LinkedPropo fExcludedNames.clear(); } ASTRewrite rewrite= cuRewrite.getASTRewrite(); - IJavaElement root= cuRewrite.getRoot().getJavaElement(); - if (root != null) { - IJavaProject project= root.getJavaProject(); - if (project != null) { - String tab_option= project.getOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, true); - if (JavaCore.SPACE.equals(tab_option)) { - fIndent= ""; //$NON-NLS-1$ - for (int i= 0; i < CodeFormatterUtil.getTabWidth(project); ++i) { - fIndent += " "; //$NON-NLS-1$ - } - } - } - } TextEditGroup group= createTextEditGroup(MultiFixMessages.StringConcatToTextBlockCleanUp_description, cuRewrite); rewrite.setTargetSourceRangeComputer(new TargetSourceRangeComputer() { @Override @@ -952,53 +1000,7 @@ public SourceRange computeSourceRange(final ASTNode nodeWithComment) { } }); - StringBuilder buf= new StringBuilder(); - - List parts= new ArrayList<>(); - fLiterals.stream().forEach((t) -> { String value= t.getEscapedValue(); parts.addAll(unescapeBlock(value.substring(1, value.length() - 1))); }); - - - buf.append("\"\"\"\n"); //$NON-NLS-1$ - boolean newLine= false; - boolean allWhiteSpaceStart= true; - boolean allEmpty= true; - for (String part : parts) { - if (buf.length() > 4) {// the first part has been added after the text block delimiter and newline - if (!newLine) { - // no line terminator in this part: merge the line by emitting a line continuation escape - buf.append("\\").append(System.lineSeparator()); //$NON-NLS-1$ - } - } - newLine= part.endsWith(System.lineSeparator()); - allWhiteSpaceStart= allWhiteSpaceStart && (part.isEmpty() || Character.isWhitespace(part.charAt(0))); - allEmpty= allEmpty && part.isEmpty(); - buf.append(fIndent).append(part); - } - - if (newLine || allEmpty) { - buf.append(fIndent); - } else if (allWhiteSpaceStart) { - buf.append("\\").append(System.lineSeparator()); //$NON-NLS-1$ - buf.append(fIndent); - } else { - // Replace trailing un-escaped quotes with escaped quotes before adding text block end - int readIndex= buf.length() - 1; - int count= 0; - while (readIndex >= 0 && buf.charAt(readIndex) == '"' && count <= 3) { - --readIndex; - ++count; - } - if (readIndex >= 0 && buf.charAt(readIndex) == '\\') { - --count; - } - for (int i= count; i > 0; --i) { - buf.deleteCharAt(buf.length() - 1); - } - for (int i= count; i > 0; --i) { - buf.append("\\\""); //$NON-NLS-1$ - } - } - buf.append("\"\"\""); //$NON-NLS-1$ + StringBuilder buf= createTextBlockBuffer(fLiterals, cuRewrite); AST ast= fStatements.get(0).getAST(); if (fToStringList.size() == 1 && fIndexOfList.isEmpty() && @@ -1146,6 +1148,71 @@ public static StringConcatToTextBlockFixCore createStringConcatToTextBlockFix(AS return new StringConcatToTextBlockFixCore(FixMessages.StringConcatToTextBlockFix_convert_msg, root, new CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation[] { operations.get(0) }); } + private static StringBuilder createTextBlockBuffer(List literals, CompilationUnitRewrite cuRewrite) { + IJavaElement root= cuRewrite.getRoot().getJavaElement(); + String fIndent= "\t"; //$NON-NLS-1$ + if (root != null) { + IJavaProject project= root.getJavaProject(); + if (project != null) { + String tab_option= project.getOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, true); + if (JavaCore.SPACE.equals(tab_option)) { + fIndent= ""; //$NON-NLS-1$ + for (int i= 0; i < CodeFormatterUtil.getTabWidth(project); ++i) { + fIndent += " "; //$NON-NLS-1$ + } + } + } + } + + StringBuilder buf= new StringBuilder(); + + List parts= new ArrayList<>(); + literals.stream().forEach((t) -> { String value= t.getEscapedValue(); parts.addAll(unescapeBlock(value.substring(1, value.length() - 1))); }); + + buf.append("\"\"\"\n"); //$NON-NLS-1$ + boolean newLine= false; + boolean allWhiteSpaceStart= true; + boolean allEmpty= true; + for (String part : parts) { + if (buf.length() > 4) {// the first part has been added after the text block delimiter and newline + if (!newLine) { + // no line terminator in this part: merge the line by emitting a line continuation escape + buf.append("\\").append(System.lineSeparator()); //$NON-NLS-1$ + } + } + newLine= part.endsWith(System.lineSeparator()); + allWhiteSpaceStart= allWhiteSpaceStart && (part.isEmpty() || Character.isWhitespace(part.charAt(0))); + allEmpty= allEmpty && part.isEmpty(); + buf.append(fIndent).append(part); + } + + if (newLine || allEmpty) { + buf.append(fIndent); + } else if (allWhiteSpaceStart) { + buf.append("\\").append(System.lineSeparator()); //$NON-NLS-1$ + buf.append(fIndent); + } else { + // Replace trailing un-escaped quotes with escaped quotes before adding text block end + int readIndex= buf.length() - 1; + int count= 0; + while (readIndex >= 0 && buf.charAt(readIndex) == '"' && count <= 3) { + --readIndex; + ++count; + } + if (readIndex >= 0 && buf.charAt(readIndex) == '\\') { + --count; + } + for (int i= count; i > 0; --i) { + buf.deleteCharAt(buf.length() - 1); + } + for (int i= count; i > 0; --i) { + buf.append("\\\""); //$NON-NLS-1$ + } + } + buf.append("\"\"\""); //$NON-NLS-1$ + return buf; + } + protected StringConcatToTextBlockFixCore(final String name, final CompilationUnit compilationUnit, final CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation[] fixRewriteOperations) { super(name, compilationUnit, fixRewriteOperations); } diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest15.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest15.java index def0097f865..3c132d97e56 100644 --- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest15.java +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AssistQuickFixTest15.java @@ -76,20 +76,21 @@ public void testConcatToTextBlock1() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void foo() {\n"); - buf.append(" // comment 1\n"); - buf.append(" String x = \"\" + //$NON-NLS-1$\n"); - buf.append(" \"public void foo() {\\n\" + //$NON-NLS-1$\n"); - buf.append(" \" System.out.println(\\\"abc\\\");\\n\" + //$NON-NLS-1$\n"); - buf.append(" \"}\\n\"; //$NON-NLS-1$ // comment 2\n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - - int index= buf.indexOf("x"); + String str1= """ + package test; + public class Cls { + public void foo() { + // comment 1 + String x = "" + //$NON-NLS-1$ + "public void foo() {\\n" + //$NON-NLS-1$ + " System.out.println(\\"abc\\");\\n" + //$NON-NLS-1$ + "}\\n"; //$NON-NLS-1$ // comment 2 + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + + int index= str1.indexOf("x"); IInvocationContext ctx= getCorrectionContext(cu, index, 1); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -127,20 +128,21 @@ public void testConcatToTextBlock2() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void foo() {\n"); - buf.append(" // comment 1\n"); - buf.append(" String x = \"\" +\n"); - buf.append(" \"public void foo() { \\n\" +\n"); - buf.append(" \" System.out.println(\\\"abc\\\");\\n\" +\n"); - buf.append(" \"}\\n\"; // comment 2\n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - - int index= buf.indexOf("System"); + String str1= """ + package test; + public class Cls { + public void foo() { + // comment 1 + String x = "" + + "public void foo() { \\n" + + " System.out.println(\\"abc\\");\\n" + + "}\\n"; // comment 2 + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + + int index= str1.indexOf("System"); IInvocationContext ctx= getCorrectionContext(cu, index, 6); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -178,20 +180,21 @@ public void testConcatToTextBlock3() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void foo() {\n"); - buf.append(" // comment 1\n"); - buf.append(" String x = \"\" +\n"); - buf.append(" \"public void foo() { \\n\" +\n"); - buf.append(" \" System.out.println(\\\"\\\"\\\"abc\\\"\\\"\\\");\\n\" +\n"); - buf.append(" \"}\\n\"; // comment 2\n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - - int index= buf.indexOf("System"); + String str1= """ + package test; + public class Cls { + public void foo() { + // comment 1 + String x = "" + + "public void foo() { \\n" + + " System.out.println(\\"\\"\\"abc\\"\\"\\");\\n" + + "}\\n"; // comment 2 + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + + int index= str1.indexOf("System"); IInvocationContext ctx= getCorrectionContext(cu, index, 6); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -229,20 +232,21 @@ public void testConcatToTextBlock4() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void foo() {\n"); - buf.append(" // comment 1\n"); - buf.append(" String x = \"\" +\n"); - buf.append(" \"abcdef\" +\n"); - buf.append(" \"ghijkl\\\"\\\"\\\"123\\\"\\\"\\\"\" +\n"); - buf.append(" \"mnop\";\n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - - int index= buf.indexOf("abcdef"); + String str1= """ + package test; + public class Cls { + public void foo() { + // comment 1 + String x = "" + + "abcdef" + + "ghijkl\\"\\"\\"123\\"\\"\\"" + + "mnop"; + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + + int index= str1.indexOf("abcdef"); IInvocationContext ctx= getCorrectionContext(cu, index, 6); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -279,26 +283,27 @@ public void testConcatToTextBlock5() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void foo() {\n"); - buf.append(" // comment 1\n"); - buf.append(" StringBuffer buf= new StringBuffer(\"intro string\\n\");\n"); - buf.append(" buf.append(\"public void foo() {\\n\");\n"); - buf.append(" buf.append(\" return null;\\n\");\n"); - buf.append(" buf.append(\"}\\n\");\n"); - buf.append(" buf.append(\"\\n\");\n"); - buf.append(" System.out.println(buf.toString());\n"); - buf.append(" System.out.println(buf.toString() + \"abc\");\n"); - buf.append(" // comment 2\n"); - buf.append(" buf = new StringBuffer(\"intro string 2\\n\");\n"); - buf.append(" buf.append(\"some string\\n\");\n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - - int index= buf.indexOf("buf"); + String str1= """ + package test; + public class Cls { + public void foo() { + // comment 1 + StringBuffer buf= new StringBuffer("intro string\\n"); + buf.append("public void foo() {\\n"); + buf.append(" return null;\\n"); + buf.append("}\\n"); + buf.append("\\n"); + System.out.println(buf.toString()); + System.out.println(buf.toString() + "abc"); + // comment 2 + buf = new StringBuffer("intro string 2\\n"); + buf.append("some string\\n"); + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + + int index= str1.indexOf("buf"); IInvocationContext ctx= getCorrectionContext(cu, index, 6); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -343,23 +348,24 @@ public void testConcatToTextBlock6() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void foo() {\n"); - buf.append(" StringBuilder buf3= new StringBuilder();\n"); - buf.append(" buf3.append(\"public void foo() {\\n\");\n"); - buf.append(" buf3.append(\" return null;\\n\");\n"); - buf.append(" buf3.append(\"}\\n\");\n"); - buf.append(" buf3.append(\"\\n\");\n"); - buf.append(" // comment 1\n"); - buf.append(" String k = buf3.toString();\n"); - buf.append(" \n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - - int index= buf.indexOf("buf"); + String str1= """ + package test; + public class Cls { + public void foo() { + StringBuilder buf3= new StringBuilder(); + buf3.append("public void foo() {\\n"); + buf3.append(" return null;\\n"); + buf3.append("}\\n"); + buf3.append("\\n"); + // comment 1 + String k = buf3.toString(); + \s + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + + int index= str1.indexOf("buf"); IInvocationContext ctx= getCorrectionContext(cu, index, 6); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -399,23 +405,24 @@ public void testConcatToTextBlock7() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void foo() {\n"); - buf.append(" StringBuilder buf3= new StringBuilder();\n"); - buf.append(" buf3.append(\"public void foo() {\\n\"); //$NON-NLS-1$\n"); - buf.append(" buf3.append(\" return null;\\n\"); //$NON-NLS-1$\n"); - buf.append(" buf3.append(\"}\\n\"); //$NON-NLS-1$\n"); - buf.append(" buf3.append(\"\\n\"); //$NON-NLS-1$\n"); - buf.append(" // comment 1\n"); - buf.append(" String k = buf3.toString();\n"); - buf.append(" \n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - - int index= buf.indexOf("buf3"); + String str1= """ + package test; + public class Cls { + public void foo() { + StringBuilder buf3= new StringBuilder(); + buf3.append("public void foo() {\\n"); //$NON-NLS-1$ + buf3.append(" return null;\\n"); //$NON-NLS-1$ + buf3.append("}\\n"); //$NON-NLS-1$ + buf3.append("\\n"); //$NON-NLS-1$ + // comment 1 + String k = buf3.toString(); + \s + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + + int index= str1.indexOf("buf3"); IInvocationContext ctx= getCorrectionContext(cu, index, 4); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -456,23 +463,24 @@ public void testConcatToTextBlock8() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void foo() {\n"); - buf.append(" StringBuilder buf3= new StringBuilder();\n"); - buf.append(" buf3.append(\"public void foo() {\\n\"); //$NON-NLS-1$\n"); - buf.append(" buf3.append(\" return null;\\n\"); //$NON-NLS-1$\n"); - buf.append(" buf3.append(\"}\\n\"); //$NON-NLS-1$\n"); - buf.append(" buf3.append(\"\\n\"); //$NON-NLS-1$\n"); - buf.append(" // comment 1\n"); - buf.append(" String k = buf3.toString();\n"); - buf.append(" \n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - - int index= buf.indexOf("StringBuilder"); + String str1= """ + package test; + public class Cls { + public void foo() { + StringBuilder buf3= new StringBuilder(); + buf3.append("public void foo() {\\n"); //$NON-NLS-1$ + buf3.append(" return null;\\n"); //$NON-NLS-1$ + buf3.append("}\\n"); //$NON-NLS-1$ + buf3.append("\\n"); //$NON-NLS-1$ + // comment 1 + String k = buf3.toString(); + \s + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + + int index= str1.indexOf("StringBuilder"); IInvocationContext ctx= getCorrectionContext(cu, index, 4); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -513,17 +521,18 @@ public void testConcatToTextBlock9() throws Exception { //https://github.com/ecl def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void foo() {\n"); - buf.append(" // comment 1\n"); - buf.append(" String x = \"foo \\n\" + \"bar \" + \"baz\" + \"biz\";\n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - - int index= buf.indexOf("x"); + String str1= """ + package test; + public class Cls { + public void foo() { + // comment 1 + String x = "foo \\n" + "bar " + "baz" + "biz"; + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + + int index= str1.indexOf("x"); IInvocationContext ctx= getCorrectionContext(cu, index, 6); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -564,17 +573,18 @@ public void testConcatToTextBlock10() throws Exception { //https://github.com/ec def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void foo() {\n"); - buf.append(" // comment 1\n"); - buf.append(" String x = \"foo \\n\" + \"bar \" + \"baz\" + \"biz\";\n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - - int index= buf.indexOf("x"); + String str1= """ + package test; + public class Cls { + public void foo() { + // comment 1 + String x = "foo \\n" + "bar " + "baz" + "biz"; + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + + int index= str1.indexOf("x"); IInvocationContext ctx= getCorrectionContext(cu, index, 6); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -615,21 +625,22 @@ public void testConcatToTextBlock11() throws Exception { //https://github.com/ec def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void foo() {\n"); - buf.append(" // comment 1\n"); - buf.append(" String x =\"\\tif (true) {\\n\" +\n"); - buf.append(" \"\\t\\tstuff();\\n\" +\n"); - buf.append(" \"\\t} else\\n\" +\n"); - buf.append(" \"\\t\\tnoStuff\";\n"); - buf.append(" System.out.println(x);\n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - - int index= buf.indexOf("x"); + String str1= """ + package test; + public class Cls { + public void foo() { + // comment 1 + String x ="\\tif (true) {\\n" + + "\\t\\tstuff();\\n" + + "\\t} else\\n" + + "\\t\\tnoStuff"; + System.out.println(x); + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + + int index= str1.indexOf("x"); IInvocationContext ctx= getCorrectionContext(cu, index, 6); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -669,26 +680,27 @@ public void testConcatToTextBlock12() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void foo() {\n"); - buf.append(" StringBuilder buf3= new StringBuilder();\n"); - buf.append(" buf3.append(\"public void foo() {\\n\"); //$NON-NLS-1$\n"); - buf.append(" buf3.append(\" return null;\\n\"); //$NON-NLS-1$\n"); - buf.append(" buf3.append(\"}\\n\"); //$NON-NLS-1$\n"); - buf.append(" buf3.append(\"\\n\"); //$NON-NLS-1$\n"); - buf.append(" // comment 1\n"); - buf.append(" write(buf3);\n"); - buf.append(" \n"); - buf.append(" }\n"); - buf.append(" private void write(CharSequence c) {\n"); - buf.append(" System.out.println(c);\n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - - int index= buf.indexOf("buf3"); + String str1= """ + package test; + public class Cls { + public void foo() { + StringBuilder buf3= new StringBuilder(); + buf3.append("public void foo() {\\n"); //$NON-NLS-1$ + buf3.append(" return null;\\n"); //$NON-NLS-1$ + buf3.append("}\\n"); //$NON-NLS-1$ + buf3.append("\\n"); //$NON-NLS-1$ + // comment 1 + write(buf3); + \s + } + private void write(CharSequence c) { + System.out.println(c); + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + + int index= str1.indexOf("buf3"); IInvocationContext ctx= getCorrectionContext(cu, index, 4); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -732,24 +744,25 @@ public void testConcatToTextBlock13() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void foo() {\n"); - buf.append(" StringBuilder buf3= new StringBuilder();\n"); - buf.append(" buf3.append(\"public void foo() {\\n\");\n"); - buf.append(" buf3.append(\" return null;\\n\");\n"); - buf.append(" buf3.append(\"}\\n\");\n"); - buf.append(" buf3.append(\"\\n\");\n"); - buf.append(" // comment 1\n"); - buf.append(" int index = buf3.indexOf(\"null\");\n"); - buf.append(" String k = buf3.toString();\n"); - buf.append(" \n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - - int index= buf.indexOf("StringBuilder"); + String str1= """ + package test; + public class Cls { + public void foo() { + StringBuilder buf3= new StringBuilder(); + buf3.append("public void foo() {\\n"); + buf3.append(" return null;\\n"); + buf3.append("}\\n"); + buf3.append("\\n"); + // comment 1 + int index = buf3.indexOf("null"); + String k = buf3.toString(); + \s + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + + int index= str1.indexOf("StringBuilder"); IInvocationContext ctx= getCorrectionContext(cu, index, 4); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -776,6 +789,70 @@ public void foo() { assertExpectedExistInProposals(proposals, new String[] { expected }); } + @Test + public void testConcatToTextBlock14() throws Exception { + fJProject1= JavaProjectHelper.createJavaProject("TestProject1", "bin"); + fJProject1.setRawClasspath(projectSetup.getDefaultClasspath(), null); + JavaProjectHelper.set15CompilerOptions(fJProject1, false); + fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src"); + + String str= """ + module test { + } + """; + IPackageFragment def= fSourceFolder.createPackageFragment("", false, null); + def.createCompilationUnit("module-info.java", str, false, null); + + IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); + String str1= """ + package test; + public class Cls { + public void foo() { + StringBuilder buf3= new StringBuilder(); + buf3.append("public void foo() {\\n"); + buf3.append(" return null;\\n"); + buf3.append("}\\n"); + buf3.append("\\n"); + // comment 1 + int index = buf3.indexOf("null"); + bufFunc(buf3); + \s + } + public void bufFunc(StringBuilder x) { + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + + int index= str1.indexOf("StringBuilder"); + IInvocationContext ctx= getCorrectionContext(cu, index, 4); + assertNoErrors(ctx); + ArrayList proposals= collectAssists(ctx, false); + + String expected= """ + package test; + public class Cls { + public void foo() { + StringBuilder buf3= new StringBuilder(\""" + public void foo() { + return null; + } + \t + \"""); + // comment 1 + int index = buf3.indexOf("null"); + bufFunc(buf3); + \s + } + public void bufFunc(StringBuilder x) { + } + } + """; + + assertProposalExists(proposals, FixMessages.StringConcatToTextBlockFix_convert_msg); + assertExpectedExistInProposals(proposals, new String[] { expected }); + } + @Test public void testNoConcatToTextBlock1() throws Exception { fJProject1= JavaProjectHelper.createJavaProject("TestProject1", "bin"); @@ -791,19 +868,20 @@ public void testNoConcatToTextBlock1() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void foo() {\n"); - buf.append(" // comment 1\n"); - buf.append(" String x = \n"); - buf.append(" \"abcdef\" +\n"); - buf.append(" \"ghijkl\";\n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - - int index= buf.indexOf("abcdef"); + String str1= """ + package test; + public class Cls { + public void foo() { + // comment 1 + String x =\s + "abcdef" + + "ghijkl"; + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + + int index= str1.indexOf("abcdef"); IInvocationContext ctx= getCorrectionContext(cu, index, 6); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -826,20 +904,21 @@ public void testNoConcatToTextBlock2() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void foo() {\n"); - buf.append(" // comment 1\n"); - buf.append(" String x = \n"); - buf.append(" \"abcdef\" +\n"); - buf.append(" \"ghijkl\" +\n"); - buf.append(" String.valueOf(true);\n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - - int index= buf.indexOf("abcdef"); + String str1= """ + package test; + public class Cls { + public void foo() { + // comment 1 + String x =\s + "abcdef" + + "ghijkl" + + String.valueOf(true); + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + + int index= str1.indexOf("abcdef"); IInvocationContext ctx= getCorrectionContext(cu, index, 6); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -862,20 +941,21 @@ public void testNoConcatToTextBlock3() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void foo() {\n"); - buf.append(" // comment 1\n"); - buf.append(" String x = \n"); - buf.append(" \"abcdef\" +\n"); - buf.append(" \"ghijkl\" +\n"); - buf.append(" 3;\n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - - int index= buf.indexOf("abcdef"); + String str1= """ + package test; + public class Cls { + public void foo() { + // comment 1 + String x =\s + "abcdef" + + "ghijkl" + + 3; + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + + int index= str1.indexOf("abcdef"); IInvocationContext ctx= getCorrectionContext(cu, index, 6); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -898,20 +978,21 @@ public void testNoConcatToTextBlock4() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void foo(String a) {\n"); - buf.append(" // comment 1\n"); - buf.append(" String x = \n"); - buf.append(" \"abcdef\" +\n"); - buf.append(" \"ghijkl\" +\n"); - buf.append(" a;\n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - - int index= buf.indexOf("abcdef"); + String str1= """ + package test; + public class Cls { + public void foo(String a) { + // comment 1 + String x =\s + "abcdef" + + "ghijkl" + + a; + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + + int index= str1.indexOf("abcdef"); IInvocationContext ctx= getCorrectionContext(cu, index, 6); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -934,21 +1015,22 @@ public void testNoConcatToTextBlock5() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void noToString() {\n"); - buf.append(" StringBuilder buf3= new StringBuilder();\n"); - buf.append(" buf3.append(\"public void foo() {\\n\");\n"); - buf.append(" buf3.append(\" return null;\\n\");\n"); - buf.append(" buf3.append(\"}\\n\");\n"); - buf.append(" buf3.append(\"\\n\");\n"); - buf.append(" \n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - - int index= buf.indexOf("buf3"); + String str1= """ + package test; + public class Cls { + public void noToString() { + StringBuilder buf3= new StringBuilder(); + buf3.append("public void foo() {\\n"); + buf3.append(" return null;\\n"); + buf3.append("}\\n"); + buf3.append("\\n"); + \s + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + + int index= str1.indexOf("buf3"); IInvocationContext ctx= getCorrectionContext(cu, index, 6); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -971,23 +1053,24 @@ public void testNoConcatToTextBlock6() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void extraAppend() {\n"); - buf.append(" StringBuilder buf3= new StringBuilder();\n"); - buf.append(" buf3.append(\"public void foo() {\\n\");\n"); - buf.append(" buf3.append(\" return null;\\n\");\n"); - buf.append(" buf3.append(\"}\\n\");\n"); - buf.append(" buf3.append(\"\\n\");\n"); - buf.append(" String k = buf3.toString();\n"); - buf.append(" buf3.append(\"extra stuff\\n\");\n"); - buf.append(" \n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - - int index= buf.indexOf("buf3"); + String str1= """ + package test; + public class Cls { + public void extraAppend() { + StringBuilder buf3= new StringBuilder(); + buf3.append("public void foo() {\\n"); + buf3.append(" return null;\\n"); + buf3.append("}\\n"); + buf3.append("\\n"); + String k = buf3.toString(); + buf3.append("extra stuff\\n"); + \s + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + + int index= str1.indexOf("buf3"); IInvocationContext ctx= getCorrectionContext(cu, index, 6); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -1010,22 +1093,23 @@ public void testNoConcatToTextBlock7() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void combinedAppendWithNLS() {\n"); - buf.append(" StringBuffer buf3= new StringBuffer();\n"); - buf.append(" buf3.append(\"public void foo() {\\n\"); //$NON-NLS-1$\n"); - buf.append(" buf3.append(\" return null;\\n\"); //$NON-NLS-1$\n"); - buf.append(" buf3.append(\"}\\n\"); //$NON-NLS-1$\n"); - buf.append(" buf3.append(\"\\n\").append(\"extra append\\n\"); //$NON-NLS-1$ //$NON-NLS-2$\n"); - buf.append(" String k = buf3.toString();\n"); - buf.append(" \n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - - int index= buf.indexOf("buf3"); + String str1= """ + package test; + public class Cls { + public void combinedAppendWithNLS() { + StringBuffer buf3= new StringBuffer(); + buf3.append("public void foo() {\\n"); //$NON-NLS-1$ + buf3.append(" return null;\\n"); //$NON-NLS-1$ + buf3.append("}\\n"); //$NON-NLS-1$ + buf3.append("\\n").append("extra append\\n"); //$NON-NLS-1$ //$NON-NLS-2$ + String k = buf3.toString(); + \s + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + + int index= str1.indexOf("buf3"); IInvocationContext ctx= getCorrectionContext(cu, index, 6); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -1048,32 +1132,33 @@ public void testNoConcatToTextBlock8() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void inconsistentNLSMarkers() {\n"); - buf.append(" StringBuffer buf3= new StringBuffer();\n"); - buf.append(" buf3.append(\"public void foo() {\\n\"); //$NON-NLS-1$\n"); - buf.append(" buf3.append(\" return null;\\n\");\n"); - buf.append(" buf3.append(\"}\\n\");\n"); - buf.append(" buf3.append(\"\\n\");\n"); - buf.append(" String k = buf3.toString();\n"); - buf.append(" \n"); - buf.append(" }\n"); - buf.append(" public void indexOfInside() {\n"); - buf.append(" StringBuffer buf3= new StringBuffer();\n"); - buf.append(" buf3.append(\"public void foo() {\\n\");\n"); - buf.append(" buf3.append(\" return null;\\n\");\n"); - buf.append(" buf3.append(\"}\\n\");\n"); - buf.append(" int index = buf3.indexOf(\"foo\");\n"); - buf.append(" buf3.append(\"\\n\");\n"); - buf.append(" String k = buf3.toString();\n"); - buf.append(" \n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - - int index= buf.indexOf("buf3"); + String str1= """ + package test; + public class Cls { + public void inconsistentNLSMarkers() { + StringBuffer buf3= new StringBuffer(); + buf3.append("public void foo() {\\n"); //$NON-NLS-1$ + buf3.append(" return null;\\n"); + buf3.append("}\\n"); + buf3.append("\\n"); + String k = buf3.toString(); + \s + } + public void indexOfInside() { + StringBuffer buf3= new StringBuffer(); + buf3.append("public void foo() {\\n"); + buf3.append(" return null;\\n"); + buf3.append("}\\n"); + int index = buf3.indexOf("foo"); + buf3.append("\\n"); + String k = buf3.toString(); + \s + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + + int index= str1.indexOf("buf3"); IInvocationContext ctx= getCorrectionContext(cu, index, 6); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -1096,20 +1181,21 @@ public void testNoConcatToTextBlock9() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void inconsistentNLS() {\n"); - buf.append(" // comment 1\n"); - buf.append(" String x = \"\" + //$NON-NLS-1$\n"); - buf.append(" \"public void foo() {\\n\" +\n"); - buf.append(" \" System.out.println(\\\"abc\\\");\\n\" + //$NON-NLS-1$\n"); - buf.append(" \"}\\n\"; //$NON-NLS-1$ // comment 2\n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - - int index= buf.indexOf("x"); + String str1= """ + package test; + public class Cls { + public void inconsistentNLS() { + // comment 1 + String x = "" + //$NON-NLS-1$ + "public void foo() {\\n" + + " System.out.println(\\"abc\\");\\n" + //$NON-NLS-1$ + "}\\n"; //$NON-NLS-1$ // comment 2 + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + + int index= str1.indexOf("x"); IInvocationContext ctx= getCorrectionContext(cu, index, 1); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -1132,22 +1218,23 @@ public void testNoConcatToTextBlock10() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void inconsistentNLS() {\n"); - buf.append(" // comment 1\n"); - buf.append(" String y = \"something\";\n"); - buf.append(" String x = \"\" +\n"); - buf.append(" \"public void foo() {\\n\" +\n"); - buf.append(" \" System.out.println(\\\"abc\\\");\\n\" +\n"); - buf.append(" y + \n"); - buf.append(" \"}\\n\"; //$NON-NLS-1$ // comment 2\n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - - int index= buf.indexOf("x"); + String str1= """ + package test; + public class Cls { + public void inconsistentNLS() { + // comment 1 + String y = "something"; + String x = "" + + "public void foo() {\\n" + + " System.out.println(\\"abc\\");\\n" + + y +\s + "}\\n"; //$NON-NLS-1$ // comment 2 + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + + int index= str1.indexOf("x"); IInvocationContext ctx= getCorrectionContext(cu, index, 1); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -1170,23 +1257,24 @@ public void testConcatToMessageFormatTextBlock1() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void foo() {\n"); - buf.append(" String statement= \" * statement\\n\";\n"); - buf.append(" // comment 1\n"); - buf.append(" String copyright=\n"); - buf.append(" \"/***********\\n\" +\n"); - buf.append(" \" * simple {} \\n\" +\n"); - buf.append(" \" * copyright\\n\" +\n"); - buf.append(" statement +\n"); - buf.append(" \" * notice {0}\\n\" +\n"); - buf.append(" \"***********/\\n\"; // comment 2\n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - int index= buf.indexOf("simple"); + String str1= """ + package test; + public class Cls { + public void foo() { + String statement= " * statement\\n"; + // comment 1 + String copyright= + "/***********\\n" + + " * simple {} \\n" + + " * copyright\\n" + + statement + + " * notice {0}\\n" + + "***********/\\n"; // comment 2 + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + int index= str1.indexOf("simple"); IInvocationContext ctx= getCorrectionContext(cu, index, 6); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -1232,23 +1320,24 @@ public void testConcatToMessageFormatTextBlock2() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void foo() {\n"); - buf.append(" String statement= \" * statement\\n\";\n"); - buf.append(" // comment 1\n"); - buf.append(" String copyright=\n"); - buf.append(" \"/******************************\\n\" + //$NON-NLS-1$\n"); - buf.append(" \" * simple \\n\" + //$NON-NLS-1$\n"); - buf.append(" statement +\n"); - buf.append(" \" * copyright\\n\" + //$NON-NLS-1$\n"); - buf.append(" \" ******************************/\\n\"; //$NON-NLS-1$\n"); - buf.append(" System.out.println(copyright);\n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - int index= buf.indexOf("simple"); + String str1= """ + package test; + public class Cls { + public void foo() { + String statement= " * statement\\n"; + // comment 1 + String copyright= + "/******************************\\n" + //$NON-NLS-1$ + " * simple \\n" + //$NON-NLS-1$ + statement + + " * copyright\\n" + //$NON-NLS-1$ + " ******************************/\\n"; //$NON-NLS-1$ + System.out.println(copyright); + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + int index= str1.indexOf("simple"); IInvocationContext ctx= getCorrectionContext(cu, index, 6); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -1294,22 +1383,23 @@ public void testConcatToMessageFormatTextBlock3() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void foo() {\n"); - buf.append(" String statement= \" * statement\\n\";\n"); - buf.append(" // comment 1\n"); - buf.append(" String copyright=\n"); - buf.append(" \"/***************************************************\\n\" + //$NON-NLS-1$\n"); - buf.append(" \" * simple \\n\" + //$NON-NLS-1$\n"); - buf.append(" \" * copyright\\n\" + //$NON-NLS-1$\n"); - buf.append(" statement;\n"); - buf.append(" System.out.println(copyright);\n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - int index= buf.indexOf("simple"); + String str1= """ + package test; + public class Cls { + public void foo() { + String statement= " * statement\\n"; + // comment 1 + String copyright= + "/***************************************************\\n" + //$NON-NLS-1$ + " * simple \\n" + //$NON-NLS-1$ + " * copyright\\n" + //$NON-NLS-1$ + statement; + System.out.println(copyright); + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + int index= str1.indexOf("simple"); IInvocationContext ctx= getCorrectionContext(cu, index, 6); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -1353,22 +1443,23 @@ public void testConcatToMessageFormatTextBlock4() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void foo() {\n"); - buf.append(" String statement= \" * statement\\n\";\n"); - buf.append(" // comment 1\n"); - buf.append(" String copyright=\n"); - buf.append(" \"/*********************\\n\" + //$NON-NLS-1$\n"); - buf.append(" \" * simple \\n\" + //$NON-NLS-1$\n"); - buf.append(" \" * copyright\\n\" + //$NON-NLS-1$\n"); - buf.append(" statement + \" * notice\\n\" + \"*********************/\\n\"; //comment 2 //$NON-NLS-1$ //$NON-NLS-2$\n"); - buf.append(" System.out.println(copyright);\n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - int index= buf.indexOf("simple"); + String str1= """ + package test; + public class Cls { + public void foo() { + String statement= " * statement\\n"; + // comment 1 + String copyright= + "/*********************\\n" + //$NON-NLS-1$ + " * simple \\n" + //$NON-NLS-1$ + " * copyright\\n" + //$NON-NLS-1$ + statement + " * notice\\n" + "*********************/\\n"; //comment 2 //$NON-NLS-1$ //$NON-NLS-2$ + System.out.println(copyright); + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + int index= str1.indexOf("simple"); IInvocationContext ctx= getCorrectionContext(cu, index, 6); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -1415,16 +1506,17 @@ public void testConcatToMessageFormatTextBlock5() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void foo(String name, String id) {\n"); - buf.append(" String title = \"Name: \" + name + \" ID: \" + id;\n"); - buf.append(" System.out.println(title);\n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - int index= buf.indexOf("title"); + String str1= """ + package test; + public class Cls { + public void foo(String name, String id) { + String title = "Name: " + name + " ID: " + id; + System.out.println(title); + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + int index= str1.indexOf("title"); IInvocationContext ctx= getCorrectionContext(cu, index, 6); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -1461,19 +1553,20 @@ public void testConcatToMessageFormatTextBlock6() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void foo(String name, String id) {\n"); - buf.append(" String title = \"Name: \" +\n"); - buf.append(" name + \n"); - buf.append(" \" ID: \" + \n"); - buf.append(" id;\n"); - buf.append(" System.out.println(title);\n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - int index= buf.indexOf("title"); + String str1= """ + package test; + public class Cls { + public void foo(String name, String id) { + String title = "Name: " + + name +\s + " ID: " +\s + id; + System.out.println(title); + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + int index= str1.indexOf("title"); IInvocationContext ctx= getCorrectionContext(cu, index, 6); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -1510,23 +1603,24 @@ public void testConcatToStringFormatTextBlock1() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void foo() {\n"); - buf.append(" String statement= \" * statement\\n\";\n"); - buf.append(" // comment 1\n"); - buf.append(" String copyright=\n"); - buf.append(" \"/*******************************\\n\" +\n"); - buf.append(" \" * simple \\n\" +\n"); - buf.append(" \" * copyright %\\n\" +\n"); - buf.append(" statement +\n"); - buf.append(" \" * notice\\n\" +\n"); - buf.append(" \" *******************************/\\n\"; // comment 2\n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - int index= buf.indexOf("simple"); + String str1= """ + package test; + public class Cls { + public void foo() { + String statement= " * statement\\n"; + // comment 1 + String copyright= + "/*******************************\\n" + + " * simple \\n" + + " * copyright %\\n" + + statement + + " * notice\\n" + + " *******************************/\\n"; // comment 2 + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + int index= str1.indexOf("simple"); IInvocationContext ctx= getCorrectionContext(cu, index, 6); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -1569,23 +1663,24 @@ public void testConcatToStringFormatTextBlock2() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void foo() {\n"); - buf.append(" String statement= \" * statement\\n\";\n"); - buf.append(" // comment 1\n"); - buf.append(" String copyright=\n"); - buf.append(" \"/******************************\\n\" + //$NON-NLS-1$\n"); - buf.append(" \" * simple \\n\" + //$NON-NLS-1$\n"); - buf.append(" statement +\n"); - buf.append(" \" * copyright\\n\" + //$NON-NLS-1$\n"); - buf.append(" \" ******************************/\\n\"; //$NON-NLS-1$\n"); - buf.append(" System.out.println(copyright);\n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - int index= buf.indexOf("simple"); + String str1= """ + package test; + public class Cls { + public void foo() { + String statement= " * statement\\n"; + // comment 1 + String copyright= + "/******************************\\n" + //$NON-NLS-1$ + " * simple \\n" + //$NON-NLS-1$ + statement + + " * copyright\\n" + //$NON-NLS-1$ + " ******************************/\\n"; //$NON-NLS-1$ + System.out.println(copyright); + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + int index= str1.indexOf("simple"); IInvocationContext ctx= getCorrectionContext(cu, index, 6); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -1628,22 +1723,23 @@ public void testConcatToStringFormatTextBlock3() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void foo() {\n"); - buf.append(" String statement= \" * statement\\n\";\n"); - buf.append(" // comment 1\n"); - buf.append(" String copyright=\n"); - buf.append(" \"/***************************************************\\n\" + //$NON-NLS-1$\n"); - buf.append(" \" * simple \\n\" + //$NON-NLS-1$\n"); - buf.append(" \" * copyright\\n\" + //$NON-NLS-1$\n"); - buf.append(" statement;\n"); - buf.append(" System.out.println(copyright);\n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - int index= buf.indexOf("simple"); + String str1= """ + package test; + public class Cls { + public void foo() { + String statement= " * statement\\n"; + // comment 1 + String copyright= + "/***************************************************\\n" + //$NON-NLS-1$ + " * simple \\n" + //$NON-NLS-1$ + " * copyright\\n" + //$NON-NLS-1$ + statement; + System.out.println(copyright); + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + int index= str1.indexOf("simple"); IInvocationContext ctx= getCorrectionContext(cu, index, 6); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -1684,22 +1780,23 @@ public void testConcatToStringFormatTextBlock4() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void foo() {\n"); - buf.append(" String statement= \" * statement\\n\";\n"); - buf.append(" // comment 1\n"); - buf.append(" String copyright=\n"); - buf.append(" \"/*********************\\n\" + //$NON-NLS-1$\n"); - buf.append(" \" * simple \\n\" + //$NON-NLS-1$\n"); - buf.append(" \" * copyright\\n\" + //$NON-NLS-1$\n"); - buf.append(" statement + \" * notice\\n\" + \"*********************/\\n\"; //comment 2 //$NON-NLS-1$ //$NON-NLS-2$\n"); - buf.append(" System.out.println(copyright);\n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - int index= buf.indexOf("simple"); + String str1= """ + package test; + public class Cls { + public void foo() { + String statement= " * statement\\n"; + // comment 1 + String copyright= + "/*********************\\n" + //$NON-NLS-1$ + " * simple \\n" + //$NON-NLS-1$ + " * copyright\\n" + //$NON-NLS-1$ + statement + " * notice\\n" + "*********************/\\n"; //comment 2 //$NON-NLS-1$ //$NON-NLS-2$ + System.out.println(copyright); + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + int index= str1.indexOf("simple"); IInvocationContext ctx= getCorrectionContext(cu, index, 6); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -1743,16 +1840,17 @@ public void testConcatToStringFormatTextBlock5() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void foo(String name, String id) {\n"); - buf.append(" String title = \"Name: \" + name + \" ID: \" + id;\n"); - buf.append(" System.out.println(title);\n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - int index= buf.indexOf("title"); + String str1= """ + package test; + public class Cls { + public void foo(String name, String id) { + String title = "Name: " + name + " ID: " + id; + System.out.println(title); + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + int index= str1.indexOf("title"); IInvocationContext ctx= getCorrectionContext(cu, index, 6); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); @@ -1786,19 +1884,20 @@ public void testConcatToStringFormatTextBlock6() throws Exception { def.createCompilationUnit("module-info.java", str, false, null); IPackageFragment pack= fSourceFolder.createPackageFragment("test", false, null); - StringBuilder buf= new StringBuilder(); - buf.append("package test;\n"); - buf.append("public class Cls {\n"); - buf.append(" public void foo(String name, String id) {\n"); - buf.append(" String title = \"Name: \" +\n"); - buf.append(" name + \n"); - buf.append(" \" ID: \" + \n"); - buf.append(" id;\n"); - buf.append(" System.out.println(title);\n"); - buf.append(" }\n"); - buf.append("}\n"); - ICompilationUnit cu= pack.createCompilationUnit("Cls.java", buf.toString(), false, null); - int index= buf.indexOf("title"); + String str1= """ + package test; + public class Cls { + public void foo(String name, String id) { + String title = "Name: " + + name +\s + " ID: " +\s + id; + System.out.println(title); + } + } + """; + ICompilationUnit cu= pack.createCompilationUnit("Cls.java", str1, false, null); + int index= str1.indexOf("title"); IInvocationContext ctx= getCorrectionContext(cu, index, 6); assertNoErrors(ctx); ArrayList proposals= collectAssists(ctx, false); 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 49e7cf98faa..e2c79bd81d9 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 @@ -409,10 +409,17 @@ public void foo() { buf11.append("mnop\\n"); int index = buf11.indexOf("fg"); System.out.println(buf11.toString()); + StringBuffer buf12 = new StringBuffer("abcd\\n"); + buf12.append("efgh\\n"); + buf12.append("ijkl\\n"); + buf12.append("mnopq\\n"); + bufFunc(buf12); } private void write(CharSequence c) { System.out.println(c); } + private void bufFunc(StringBuffer buf) { + } }"""; ICompilationUnit cu1= pack1.createCompilationUnit("E.java", sample, false, null); @@ -538,10 +545,19 @@ public class C { \"""; int index = str9.indexOf("fg"); System.out.println(str9); + StringBuffer buf12 = new StringBuffer(\""" + abcd + efgh + ijkl + mnopq + \"""); + bufFunc(buf12); } private void write(CharSequence c) { System.out.println(c); } + private void bufFunc(StringBuffer buf) { + } }"""; assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 }, null);