--- a/java.source/src/org/netbeans/modules/java/source/save/CasualDiff.java +++ a/java.source/src/org/netbeans/modules/java/source/save/CasualDiff.java @@ -2926,6 +2926,7 @@ if (!matcher.match()) { return localPointer; } + Queue deletedItems = new LinkedList(); // deleted items JCTree lastdel = null; // last deleted element ResultItem[] result = matcher.getResult(); @@ -3019,6 +3020,10 @@ } } } + JCTree ld = null; + if (deletedItems != null && !deletedItems.isEmpty()) { + ld = deletedItems.poll(); + } if (!found) { if (lastdel != null) { boolean wasInFieldGroup = false; @@ -3045,7 +3050,9 @@ printer.print(this.printer.toString()); this.printer = oldPrinter; this.printer.undent(old); - lastdel = null; + if (treesMatch(ld, lastdel)) { + lastdel = null; + } break; } } @@ -3061,9 +3068,10 @@ copyTo(localPointer, pos[0], printer); } lastdel = oldList.get(i); + deletedItems.add( lastdel ); + CommentSet ch = comments.getComments(oldList.get(i)); + localPointer = Math.max(pos[1], Math.max(commentEnd(ch, CommentSet.RelativePosition.INLINE), commentEnd(ch, CommentSet.RelativePosition.TRAILING))); ++i; - CommentSet ch = comments.getComments(lastdel); - localPointer = Math.max(pos[1], Math.max(commentEnd(ch, CommentSet.RelativePosition.INLINE), commentEnd(ch, CommentSet.RelativePosition.TRAILING))); break; } case NOCHANGE: { --- a/java.source/test/unit/src/org/netbeans/api/java/source/gen/IfTest.java +++ a/java.source/test/unit/src/org/netbeans/api/java/source/gen/IfTest.java @@ -85,6 +85,8 @@ // suite.addTest(new IfTest("testModifyingIf")); // suite.addTest(new IfTest("test158463a")); // suite.addTest(new IfTest("test158463b")); +// suite.addTest(new IfTest("test158154OneIf")); +// suite.addTest(new IfTest("test158154TwoIfs")); return suite; } @@ -433,6 +435,93 @@ assertEquals(golden, res); } + public void test158154OneIf() throws Exception { + String source = "class Test {\n" + + " void m1(boolean b) {\n" + + " if (b) ; else System.out.println(\"hi\");\n" + + " }\n" + + "}"; + String golden = "class Test {\n" + + " void m1(boolean b) {\n" + + " if (!(b)) System.out.println(\"hi\");\n" + + " }\n" + + "}"; + testFile = new File(getWorkDir(), "Test.java"); + + TestUtilities.copyStringToFile(testFile, source); + JavaSource src = getJavaSource(testFile); + Task task = new Task() { + public void run(WorkingCopy copy) throws Exception { + if (copy.toPhase(Phase.RESOLVED).compareTo(Phase.RESOLVED) < 0) { + return; + } + + TreeMaker make = copy.getTreeMaker(); + ClassTree clazz = (ClassTree) copy.getCompilationUnit().getTypeDecls().get(0); + MethodTree method = (MethodTree) clazz.getMembers().get(1); + BlockTree block = method.getBody(); + IfTree original = (IfTree) block.getStatements().get(0); + + IfTree modified = make.If( + make.Parenthesized( + make.Unary(Kind.LOGICAL_COMPLEMENT, original.getCondition())), + original.getElseStatement(), null); + copy.rewrite(original, modified); + } + }; + + src.runModificationTask(task).commit(); + String res = TestUtilities.copyFileToString(testFile); + System.out.println(res); + assertEquals(golden, res); + } + + public void test158154TwoIfs() throws Exception { + String source = "class Test {\n" + + " void m1(boolean b) {\n" + + " if (b) ; else System.out.println(\"first hi\");\n" + + " if (b) ; else System.out.println(\"second hi\");\n" + + " }\n" + + "}"; + String golden = "class Test {\n" + + " void m1(boolean b) {\n" + + " if (!(b)) System.out.println(\"first hi\");\n" + + " if (!(b)) System.out.println(\"second hi\");\n" + + " }\n" + + "}"; + testFile = new File(getWorkDir(), "Test.java"); + TestUtilities.copyStringToFile(testFile, source); + JavaSource src = getJavaSource(testFile); + Task task = new Task() { + public void run(WorkingCopy copy) throws Exception { + if (copy.toPhase(Phase.RESOLVED).compareTo(Phase.RESOLVED) < 0) { + return; + } + + TreeMaker make = copy.getTreeMaker(); + ClassTree clazz = (ClassTree) copy.getCompilationUnit().getTypeDecls().get(0); + MethodTree method = (MethodTree) clazz.getMembers().get(1); + BlockTree block = method.getBody(); + IfTree originalA = (IfTree) block.getStatements().get(0); + IfTree originalB = (IfTree) block.getStatements().get(1); + IfTree modifiedA = make.If( + make.Parenthesized( + make.Unary(Kind.LOGICAL_COMPLEMENT, originalA.getCondition())), + originalA.getElseStatement(), null); + copy.rewrite(originalA, modifiedA); + IfTree modifiedB = make.If( + make.Parenthesized( + make.Unary(Kind.LOGICAL_COMPLEMENT, originalB.getCondition())), + originalB.getElseStatement(), null); + copy.rewrite(originalB, modifiedB); + } + }; + src.runModificationTask(task).commit(); + String res = TestUtilities.copyFileToString(testFile); + System.out.println(res); + assertEquals(golden, res); + } + String getGoldenPckg() { return ""; }