Bug 567007 - fix trailing whitespace bug
- modify StringFix to remove trailing whitespace after fixing
nls comments
- add new test to CleanUpTest and modify existing ones which
were expecting trailing white-space after nls cleanups
Change-Id: Ia9baefa06c4e940f89761eaba7631c3713ac8537
Signed-off-by: Carsten Hammer <carsten.hammer@t-online.de>
Signed-off-by: Jeff Johnston <jjohnstn@redhat.com>
diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest.java
index 49de3d3..082fea1 100644
--- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest.java
+++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest.java
@@ -242,42 +242,68 @@
enable(CleanUpConstants.REMOVE_UNNECESSARY_NLS_TAGS);
- sample= "" //
+ String expected1= "" //
+ "package test1;\n" //
+ "public class E1 {\n" //
+ " public void foo() {\n" //
- + " String s= null; \n" //
+ + " String s= null;\n" //
+ " }\n" //
+ "}\n";
- String expected1= sample;
- sample= "" //
+ String expected2= "" //
+ "package test1;\n" //
+ "public class E2 {\n" //
- + " public String s1 = null; \n" //
+ + " public String s1 = null;\n" //
+ " public void foo() {\n" //
- + " String s2 = null; \n" //
- + " String s3 = s2 + s2; \n" //
+ + " String s2 = null;\n" //
+ + " String s3 = s2 + s2;\n" //
+ " }\n" //
+ "}\n";
- String expected2= sample;
- sample= "" //
+ String expected3= "" //
+ "package test2;\n" //
+ "import test1.E2;\n" //
+ "public class E3 extends E2 {\n" //
- + " public static final String s= null; \n" //
+ + " public static final String s= null;\n" //
+ " public static String bar(String s1, String s2) {\n" //
- + " bar(s2, s1); \n" //
- + " return s1; \n" //
+ + " bar(s2, s1);\n" //
+ + " return s1;\n" //
+ " }\n" //
+ "}\n";
- String expected3= sample;
assertRefactoringResultAsExpected(new ICompilationUnit[] {cu1, cu2, cu3}, new String[] {expected1, expected2, expected3});
}
@Test
+ public void testRemoveNLSTagWhitespace() throws Exception {
+ IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
+ String sample= "" //
+ + "package test2;\n" //
+ + "import test1.E2;\n" //
+ + "public class E3 extends E2 {\n" //
+ + " public static String bar(String s1, String s2) {\n" //
+ + " bar(s2, s1); //$NON-NLS-1$ //$NON-NLS-2$\n" //
+ + " return s1;\n" //
+ + " }\n" //
+ + "}\n";
+ ICompilationUnit cu1= pack1.createCompilationUnit("E1.java", sample, false, null);
+
+ enable(CleanUpConstants.REMOVE_UNNECESSARY_NLS_TAGS);
+
+ String expected1= "" //
+ + "package test2;\n" //
+ + "import test1.E2;\n" //
+ + "public class E3 extends E2 {\n" //
+ + " public static String bar(String s1, String s2) {\n" //
+ + " bar(s2, s1);\n" //
+ + " return s1;\n" //
+ + " }\n" //
+ + "}\n";
+
+ assertRefactoringResultAsExpected(new ICompilationUnit[] {cu1}, new String[] {expected1});
+ }
+
+ @Test
public void testUnusedCode01() throws Exception {
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
String sample= "" //
@@ -2665,7 +2691,7 @@
+ " if (true) {\n" //
+ " this.fNb++;\n" //
+ " }\n" //
- + " String s; \n" //
+ + " String s;\n" //
+ " }\n" //
+ "}\n";
String expected1= sample;
diff --git a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/StringFix.java b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/StringFix.java
index 3413339..58450aa 100644
--- a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/StringFix.java
+++ b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/fix/StringFix.java
@@ -119,7 +119,7 @@
if (removeNLSTag && problem.getProblemId() == IProblem.UnnecessaryNLSTag) {
IBuffer buffer= cu.getBuffer();
if (buffer != null) {
- TextEdit edit= StringFix.getReplace(problem.getOffset(), problem.getLength(), buffer, false);
+ TextEdit edit= StringFix.getReplace(problem.getOffset(), problem.getLength(), buffer, true);
if (edit != null) {
String label= FixMessages.StringFix_RemoveNonNls_description;
result.add(new CategorizedTextEditGroup(label, edit, new GroupCategorySet(new GroupCategory(label, label, label))));
@@ -159,6 +159,9 @@
char ch= buffer.getChar(next);
if (IndentManipulation.isIndentChar(ch)) {
next++; // remove all whitespace
+ if (buffer.getContents().substring(next).startsWith("//$NON-NLS-")){ //$NON-NLS-1$
+ break;
+ }
} else if (IndentManipulation.isLineDelimiterChar(ch)) {
length= next - offset;
break;