diff -r 663611676d2c java.source/src/org/netbeans/modules/java/source/save/CasualDiff.java --- a/java.source/src/org/netbeans/modules/java/source/save/CasualDiff.java Sun May 10 19:08:23 2009 +0200 +++ b/java.source/src/org/netbeans/modules/java/source/save/CasualDiff.java Sat May 16 18:21:21 2009 +0100 @@ -1304,16 +1304,24 @@ } protected int diffUnary(JCUnary oldT, JCUnary newT, int[] bounds) { - int localPointer = bounds[0]; int[] argBounds = getBounds(oldT.arg); - copyTo(localPointer, argBounds[0]); - localPointer = diffTree(oldT.arg, newT.arg, argBounds); - if (oldT.getTag() != newT.getTag()) { - copyTo(localPointer, oldT.pos); - printer.print(operatorName(newT.getTag())); - localPointer = oldT.pos + operatorName(oldT.getTag()).length(); + boolean newOpOnLeft = newT.getKind() != Kind.POSTFIX_DECREMENT && newT.getKind() != Kind.POSTFIX_INCREMENT; + if (newOpOnLeft) { + if (oldT.getTag() != newT.getTag()) { + printer.print(operatorName(newT.getTag())); + } else { + copyTo(bounds[0], argBounds[0]); + } } - copyTo(localPointer, bounds[1]); + int localPointer = diffTree(oldT.arg, newT.arg, argBounds); + copyTo(localPointer, argBounds[1]); + if (!newOpOnLeft) { + if (oldT.getTag() != newT.getTag()) { + printer.print(operatorName(newT.getTag())); + } else { + copyTo(argBounds[1], bounds[1]); + } + } return bounds[1]; } diff -r 663611676d2c java.source/test/unit/src/org/netbeans/api/java/source/gen/OperatorsTest.java --- a/java.source/test/unit/src/org/netbeans/api/java/source/gen/OperatorsTest.java Sun May 10 19:08:23 2009 +0200 +++ b/java.source/test/unit/src/org/netbeans/api/java/source/gen/OperatorsTest.java Sat May 16 18:21:21 2009 +0100 @@ -286,6 +286,66 @@ return make.Unary(Kind.LOGICAL_COMPLEMENT, make.Parenthesized(input)); } } + + public void testChangeUnary2() throws Exception { + String test = "public class Test { void m(int x) { int y = x+|+; } }"; + String golden = "public class Test { void m(int x) { int y = --x; } }"; + testFile = new File(getWorkDir(), "Test.java"); + final int index = test.indexOf("|"); + assertTrue(index != -1); + TestUtilities.copyStringToFile(testFile, test.replace("|", "")); + 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; + } + Tree node = copy.getTreeUtilities().pathFor(index).getLeaf(); + assertEquals(Kind.POSTFIX_INCREMENT, node.getKind()); + UnaryTree node2 = (UnaryTree) node; + IdentifierTree original = (IdentifierTree) node2.getExpression(); + System.out.println("node: " + node); + TreeMaker make = copy.getTreeMaker(); + UnaryTree modified = make.Unary(Kind.PREFIX_DECREMENT, original); + System.out.println("modified: " + modified); + copy.rewrite(node, modified); + } + }; + src.runModificationTask(task).commit(); + String res = TestUtilities.copyFileToString(testFile); + assertEquals(golden, res); + } + + public void testUnary158150() throws Exception { + String test = "public class Test { void m(int x) { int y = -| - x; } }"; + String golden = "public class Test { void m(int x) { int y = +x; } }"; + testFile = new File(getWorkDir(), "Test.java"); + final int index = test.indexOf("|"); + assertTrue(index != -1); + TestUtilities.copyStringToFile(testFile, test.replace("|", "")); + 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; + } + Tree node = copy.getTreeUtilities().pathFor(index).getLeaf(); + assertEquals(Kind.UNARY_MINUS, node.getKind()); + UnaryTree node2 = (UnaryTree) node; + UnaryTree original = (UnaryTree) node2.getExpression(); + System.out.println("node: " + node); + TreeMaker make = copy.getTreeMaker(); + UnaryTree modified = make.Unary(Kind.UNARY_PLUS, original.getExpression()); + System.out.println("modified: " + modified); + copy.rewrite(node, modified); + } + }; + src.runModificationTask(task).commit(); + String res = TestUtilities.copyFileToString(testFile); + assertEquals(golden, res); + } String getGoldenPckg() { return "";