View | Details | Raw Unified | Return to bug 306524 | Differences between
and this patch

Collapse All | Expand All

(-)dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java (+40 lines)
Lines 585-590 Link Here
585
					ASTNode node= (ASTNode) currEvent.getOriginalValue();
585
					ASTNode node= (ASTNode) currEvent.getOriginalValue();
586
					TextEditGroup editGroup= getEditGroup(currEvent);
586
					TextEditGroup editGroup= getEditGroup(currEvent);
587
					int currEnd= getEndOfNode(node);
587
					int currEnd= getEndOfNode(node);
588
					// https://bugs.eclipse.org/bugs/show_bug.cgi?id=306524
589
					// Check for leading comments that are not part of extended range, and prevent them
590
					// from getting removed.
591
					try {
592
						TokenScanner scanner = getScanner();
593
						int nextToken= scanner.readNext(prevEnd, false);
594
						if (TokenScanner.isComment(nextToken)) {
595
							int newOffset = scanner.getNextEndOffset(prevEnd, false);
596
							if (currPos < newOffset) {
597
								currPos = getExtendedOffset(node);
598
								// If currPos is still less than the newly found offset, it means
599
								// the comment was part of extended source range, and we don't need
600
								// to reset the prevEnd to the end of comment
601
								prevEnd = (currPos < newOffset) ? prevEnd : newOffset;
602
							} else {
603
								prevEnd = newOffset;
604
							}
605
						}
606
					} catch (CoreException e) {
607
						// ignore
608
					}
588
					if (i > lastNonDelete && separatorState == EXISTING) {
609
					if (i > lastNonDelete && separatorState == EXISTING) {
589
						// is last, remove previous separator: split delete to allow range copies
610
						// is last, remove previous separator: split delete to allow range copies
590
						doTextRemove(prevEnd, currPos - prevEnd, editGroup); // remove separator
611
						doTextRemove(prevEnd, currPos - prevEnd, editGroup); // remove separator
Lines 598-603 Link Here
598
						
619
						
599
						// remove element and next separator
620
						// remove element and next separator
600
						int end= getStartOfNextNode(nextIndex, currEnd); // start of next
621
						int end= getStartOfNextNode(nextIndex, currEnd); // start of next
622
						// https://bugs.eclipse.org/bugs/show_bug.cgi?id=306524
623
						// Check for trailing comments that are not part of extended range, and prevent them
624
						// from getting removed.
625
						try {
626
							TokenScanner scanner = getScanner();
627
							int nextToken= scanner.readNext(currEnd, false);
628
							if (TokenScanner.isComment(nextToken)) {
629
								// the separator also has comments that are not part of extended
630
								// source range of this node or the next node. So dont remove the separator
631
								if (end != scanner.getNextStartOffset(currEnd, false)) {
632
									// If this condition were true, comments just found as part of the separator would've basically been
633
									// part of the extended source range of the next node. So 'end' wud've safely been set to the correct position
634
									// and no change is needed.
635
									end = currEnd;
636
								}
637
							}
638
						} catch (CoreException e) {
639
							// ignore
640
						}
601
						doTextRemoveAndVisit(currPos, currEnd - currPos, node, getEditGroup(currEvent)); // remove node
641
						doTextRemoveAndVisit(currPos, currEnd - currPos, node, getEditGroup(currEvent)); // remove node
602
						if (mustRemoveSeparator(currPos, i)) {
642
						if (mustRemoveSeparator(currPos, i)) {
603
							doTextRemove(currEnd, end - currEnd, editGroup); // remove separator
643
							doTextRemove(currEnd, end - currEnd, editGroup); // remove separator
(-)src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingInsertBoundTest.java (-3 / +106 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2009 IBM Corporation and others.
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 19-25 Link Here
19
19
20
import org.eclipse.jdt.core.ICompilationUnit;
20
import org.eclipse.jdt.core.ICompilationUnit;
21
import org.eclipse.jdt.core.IPackageFragment;
21
import org.eclipse.jdt.core.IPackageFragment;
22
23
import org.eclipse.jdt.core.dom.AST;
22
import org.eclipse.jdt.core.dom.AST;
24
import org.eclipse.jdt.core.dom.ASTNode;
23
import org.eclipse.jdt.core.dom.ASTNode;
25
import org.eclipse.jdt.core.dom.CompilationUnit;
24
import org.eclipse.jdt.core.dom.CompilationUnit;
Lines 522-529 Link Here
522
		buf.append("public class C {\n");
521
		buf.append("public class C {\n");
523
		buf.append("//c1\n");
522
		buf.append("//c1\n");
524
		buf.append("\n");
523
		buf.append("\n");
524
		buf.append("    \n");
525
		buf.append("\n");
526
		buf.append("//c2\n");
527
		buf.append("\n");
525
		buf.append("    public void foo2();\n");
528
		buf.append("    public void foo2();\n");
526
		buf.append("\n");
529
		buf.append("\n");
530
		buf.append("//c3\n");
531
		buf.append("\n");
532
		buf.append("\n");
527
		buf.append("//c4\n");
533
		buf.append("//c4\n");
528
		buf.append("}\n");
534
		buf.append("}\n");
529
535
Lines 573-578 Link Here
573
		buf.append("\n");
579
		buf.append("\n");
574
		buf.append("//c2\n");
580
		buf.append("//c2\n");
575
		buf.append("\n");
581
		buf.append("\n");
582
		buf.append("    \n");
583
		buf.append("\n");
584
		buf.append("//c3\n");
585
		buf.append("\n");
576
		buf.append("    public void foo3();\n");
586
		buf.append("    public void foo3();\n");
577
		buf.append("\n");
587
		buf.append("\n");
578
		buf.append("//c4\n");
588
		buf.append("//c4\n");
Lines 623-628 Link Here
623
		buf.append("\n");
633
		buf.append("\n");
624
		buf.append("    public void foo1();\n");
634
		buf.append("    public void foo1();\n");
625
		buf.append("\n");
635
		buf.append("\n");
636
		buf.append("//c2\n");
637
		buf.append("\n");
638
		buf.append("\n");
639
		buf.append("//c3\n");
640
		buf.append("\n");
641
		buf.append("\n");
626
		buf.append("//c4\n");
642
		buf.append("//c4\n");
627
		buf.append("}\n");
643
		buf.append("}\n");
628
644
Lines 668-673 Link Here
668
		buf= new StringBuffer();
684
		buf= new StringBuffer();
669
		buf.append("package test1;\n");
685
		buf.append("package test1;\n");
670
		buf.append("public class C {\n");
686
		buf.append("public class C {\n");
687
		buf.append("//c1\n");
688
		buf.append("\n");
689
		buf.append("    \n");
690
		buf.append("\n");
691
		buf.append("//c2\n");
692
		buf.append("\n");
693
		buf.append("    \n");
694
		buf.append("\n");
695
		buf.append("//c3\n");
696
		buf.append("\n");
697
		buf.append("    \n");
671
		buf.append("\n");
698
		buf.append("\n");
672
		buf.append("//c4\n");
699
		buf.append("//c4\n");
673
		buf.append("}\n");
700
		buf.append("}\n");
Lines 726-731 Link Here
726
		buf.append("\n");
753
		buf.append("\n");
727
		buf.append("    void new1();\n");
754
		buf.append("    void new1();\n");
728
		buf.append("\n");
755
		buf.append("\n");
756
		buf.append("    \n");
757
		buf.append("\n");
758
		buf.append("//c2\n");
759
		buf.append("\n");
729
		buf.append("    public void foo2();\n");
760
		buf.append("    public void foo2();\n");
730
		buf.append("\n");
761
		buf.append("\n");
731
		buf.append("//c3\n");
762
		buf.append("//c3\n");
Lines 797-802 Link Here
797
		buf.append("\n");
828
		buf.append("\n");
798
		buf.append("    void new2();\n");
829
		buf.append("    void new2();\n");
799
		buf.append("\n");
830
		buf.append("\n");
831
		buf.append("    \n");
832
		buf.append("\n");
833
		buf.append("//c2\n");
834
		buf.append("\n");
800
		buf.append("    public void foo2();\n");
835
		buf.append("    public void foo2();\n");
801
		buf.append("\n");
836
		buf.append("\n");
802
		buf.append("//c3\n");
837
		buf.append("//c3\n");
Lines 868-873 Link Here
868
		buf.append("\n");
903
		buf.append("\n");
869
		buf.append("    void new2();\n");
904
		buf.append("    void new2();\n");
870
		buf.append("\n");
905
		buf.append("\n");
906
		buf.append("    \n");
907
		buf.append("\n");
908
		buf.append("//c3\n");
909
		buf.append("\n");
871
		buf.append("    public void foo3();\n");
910
		buf.append("    public void foo3();\n");
872
		buf.append("\n");
911
		buf.append("\n");
873
		buf.append("//c4\n");
912
		buf.append("//c4\n");
Lines 928-933 Link Here
928
		buf.append("\n");
967
		buf.append("\n");
929
		buf.append("    int new1;\n");
968
		buf.append("    int new1;\n");
930
		buf.append("\n");
969
		buf.append("\n");
970
		buf.append("    \n");
971
		buf.append("\n");
972
		buf.append("//c2\n");
973
		buf.append("\n");
931
		buf.append("    public int x2;\n");
974
		buf.append("    public int x2;\n");
932
		buf.append("\n");
975
		buf.append("\n");
933
		buf.append("//c3\n");
976
		buf.append("//c3\n");
Lines 998-1003 Link Here
998
		buf.append("\n");
1041
		buf.append("\n");
999
		buf.append("    int new2;\n");
1042
		buf.append("    int new2;\n");
1000
		buf.append("\n");
1043
		buf.append("\n");
1044
		buf.append("    \n");
1045
		buf.append("\n");
1046
		buf.append("//c2\n");
1047
		buf.append("\n");
1001
		buf.append("    public int x2;\n");
1048
		buf.append("    public int x2;\n");
1002
		buf.append("\n");
1049
		buf.append("\n");
1003
		buf.append("//c3\n");
1050
		buf.append("//c3\n");
Lines 1069-1074 Link Here
1069
		buf.append("\n");
1116
		buf.append("\n");
1070
		buf.append("    int new2;\n");
1117
		buf.append("    int new2;\n");
1071
		buf.append("\n");
1118
		buf.append("\n");
1119
		buf.append("    \n");
1120
		buf.append("\n");
1121
		buf.append("//c3\n");
1122
		buf.append("\n");
1072
		buf.append("    public int x3;\n");
1123
		buf.append("    public int x3;\n");
1073
		buf.append("\n");
1124
		buf.append("\n");
1074
		buf.append("//c4\n");
1125
		buf.append("//c4\n");
Lines 1131-1136 Link Here
1131
		buf.append("\n");
1182
		buf.append("\n");
1132
		buf.append("    void new1();\n");
1183
		buf.append("    void new1();\n");
1133
		buf.append("\n");
1184
		buf.append("\n");
1185
		buf.append("    \n");
1186
		buf.append("\n");
1187
		buf.append("//c2\n");
1188
		buf.append("\n");
1134
		buf.append("    public void foo2();\n");
1189
		buf.append("    public void foo2();\n");
1135
		buf.append("\n");
1190
		buf.append("\n");
1136
		buf.append("//c3\n");
1191
		buf.append("//c3\n");
Lines 1196-1201 Link Here
1196
		buf.append("\n");
1251
		buf.append("\n");
1197
		buf.append("    int new1;\n");
1252
		buf.append("    int new1;\n");
1198
		buf.append("\n");
1253
		buf.append("\n");
1254
		buf.append("    \n");
1255
		buf.append("\n");
1256
		buf.append("//c2\n");
1257
		buf.append("\n");
1199
		buf.append("    public int x2;\n");
1258
		buf.append("    public int x2;\n");
1200
		buf.append("\n");
1259
		buf.append("\n");
1201
		buf.append("//c3\n");
1260
		buf.append("//c3\n");
Lines 1269-1274 Link Here
1269
		buf.append("\n");
1328
		buf.append("\n");
1270
		buf.append("    void new2();\n");
1329
		buf.append("    void new2();\n");
1271
		buf.append("\n");
1330
		buf.append("\n");
1331
		buf.append("    \n");
1332
		buf.append("\n");
1333
		buf.append("//c2\n");
1334
		buf.append("\n");
1272
		buf.append("    public void foo2();\n");
1335
		buf.append("    public void foo2();\n");
1273
		buf.append("\n");
1336
		buf.append("\n");
1274
		buf.append("//c3\n");
1337
		buf.append("//c3\n");
Lines 1343-1348 Link Here
1343
		buf.append("\n");
1406
		buf.append("\n");
1344
		buf.append("    int new2;\n");
1407
		buf.append("    int new2;\n");
1345
		buf.append("\n");
1408
		buf.append("\n");
1409
		buf.append("    \n");
1410
		buf.append("\n");
1411
		buf.append("//c2\n");
1412
		buf.append("\n");
1346
		buf.append("    public int x2;\n");
1413
		buf.append("    public int x2;\n");
1347
		buf.append("\n");
1414
		buf.append("\n");
1348
		buf.append("//c3\n");
1415
		buf.append("//c3\n");
Lines 1409-1414 Link Here
1409
		buf.append("\n");
1476
		buf.append("\n");
1410
		buf.append("    void new1();\n");
1477
		buf.append("    void new1();\n");
1411
		buf.append("\n");
1478
		buf.append("\n");
1479
		buf.append("    \n");
1480
		buf.append("\n");
1481
		buf.append("//c2\n");
1482
		buf.append("\n");
1483
		buf.append("    \n");
1484
		buf.append("\n");
1485
		buf.append("//c3\n");
1486
		buf.append("\n");
1412
		buf.append("    void new2();\n");
1487
		buf.append("    void new2();\n");
1413
		buf.append("\n");
1488
		buf.append("\n");
1414
		buf.append("//c4\n");
1489
		buf.append("//c4\n");
Lines 1468-1473 Link Here
1468
		buf.append("\n");
1543
		buf.append("\n");
1469
		buf.append("    int new1;\n");
1544
		buf.append("    int new1;\n");
1470
		buf.append("\n");
1545
		buf.append("\n");
1546
		buf.append("    \n");
1547
		buf.append("\n");
1548
		buf.append("//c2\n");
1549
		buf.append("\n");
1550
		buf.append("    \n");
1551
		buf.append("\n");
1552
		buf.append("//c3\n");
1553
		buf.append("\n");
1471
		buf.append("    int new2;\n");
1554
		buf.append("    int new2;\n");
1472
		buf.append("\n");
1555
		buf.append("\n");
1473
		buf.append("//c4\n");
1556
		buf.append("//c4\n");
Lines 1530-1537 Link Here
1530
		buf.append("\n");
1613
		buf.append("\n");
1531
		buf.append("    void new1();\n");
1614
		buf.append("    void new1();\n");
1532
		buf.append("\n");
1615
		buf.append("\n");
1616
		buf.append("    \n");
1617
		buf.append("\n");
1618
		buf.append("//c2\n");
1619
		buf.append("\n");
1533
		buf.append("    void new2();\n");
1620
		buf.append("    void new2();\n");
1534
		buf.append("\n");
1621
		buf.append("\n");
1622
		buf.append("//c3\n");
1623
		buf.append("\n");
1624
		buf.append("    \n");
1625
		buf.append("\n");
1535
		buf.append("//c4\n");
1626
		buf.append("//c4\n");
1536
		buf.append("}\n");
1627
		buf.append("}\n");
1537
1628
Lines 1591-1598 Link Here
1591
		buf.append("\n");
1682
		buf.append("\n");
1592
		buf.append("    int new1;\n");
1683
		buf.append("    int new1;\n");
1593
		buf.append("\n");
1684
		buf.append("\n");
1685
		buf.append("    \n");
1686
		buf.append("\n");
1687
		buf.append("//c2\n");
1688
		buf.append("\n");
1594
		buf.append("    int new2;\n");
1689
		buf.append("    int new2;\n");
1595
		buf.append("\n");
1690
		buf.append("\n");
1691
		buf.append("//c3\n");
1692
		buf.append("\n");
1693
		buf.append("    \n");
1694
		buf.append("\n");
1596
		buf.append("//c4\n");
1695
		buf.append("//c4\n");
1597
		buf.append("}\n");
1696
		buf.append("}\n");
1598
1697
Lines 1650-1659 Link Here
1650
		rewrite.remove(f1, null);
1749
		rewrite.remove(f1, null);
1651
1750
1652
		String preview= evaluateRewrite(cu, rewrite);
1751
		String preview= evaluateRewrite(cu, rewrite);
1653
1752
		// Note that c1 is absent because source range has been calculated from c1
1654
		buf= new StringBuffer();
1753
		buf= new StringBuffer();
1655
		buf.append("package test1;\n");
1754
		buf.append("package test1;\n");
1656
		buf.append("public class C {\n");
1755
		buf.append("public class C {\n");
1756
		buf.append("    \n");
1757
		buf.append("\n");
1758
		buf.append("    //c2\n");
1759
		buf.append("\n");
1657
		buf.append("    public int x2;\n");
1760
		buf.append("    public int x2;\n");
1658
		buf.append("\n");
1761
		buf.append("\n");
1659
		buf.append("    //c3\n");
1762
		buf.append("    //c3\n");
(-)src/org/eclipse/jdt/core/tests/rewrite/modifying/ASTRewritingModifyingRemoveTest.java (-3 / +99 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2009 IBM Corporation and others.
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 16-23 Link Here
16
16
17
import org.eclipse.jdt.core.ICompilationUnit;
17
import org.eclipse.jdt.core.ICompilationUnit;
18
import org.eclipse.jdt.core.IPackageFragment;
18
import org.eclipse.jdt.core.IPackageFragment;
19
19
import org.eclipse.jdt.core.dom.AST;
20
import org.eclipse.jdt.core.dom.*;
20
import org.eclipse.jdt.core.dom.Block;
21
import org.eclipse.jdt.core.dom.CompilationUnit;
22
import org.eclipse.jdt.core.dom.MethodDeclaration;
23
import org.eclipse.jdt.core.dom.TypeDeclaration;
21
24
22
public class ASTRewritingModifyingRemoveTest extends ASTRewritingModifyingTest {
25
public class ASTRewritingModifyingRemoveTest extends ASTRewritingModifyingTest {
23
	private static final Class THIS = ASTRewritingModifyingRemoveTest.class;
26
	private static final Class THIS = ASTRewritingModifyingRemoveTest.class;
Lines 407-412 Link Here
407
		buf.append("package test0010;\n");
410
		buf.append("package test0010;\n");
408
		buf.append("\n");
411
		buf.append("\n");
409
		buf.append("public class X {\n");
412
		buf.append("public class X {\n");
413
		buf.append("    // comment1\n");
414
		buf.append("\n");
415
		buf.append("    \n");
410
		buf.append("\n");
416
		buf.append("\n");
411
		buf.append("    // comment5\n");
417
		buf.append("    // comment5\n");
412
		buf.append("}\n");
418
		buf.append("}\n");
Lines 458-461 Link Here
458
		buf.append("}\n");
464
		buf.append("}\n");
459
		assertEqualString(preview, buf.toString());
465
		assertEqualString(preview, buf.toString());
460
	}
466
	}
467
	/*
468
	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=306524
469
	 * To test that when types are removed, only the comments in the extended source range are
470
	 * removed, and the rest are left untouched.
471
	 */
472
	public void test0012() throws Exception {
473
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test0012", false, null);
474
		StringBuffer buf= new StringBuffer();
475
		buf.append("package test0012;\n");
476
		buf.append("public class X {\n");
477
		buf.append("\n");
478
		buf.append("    // one line comment1\n");
479
		buf.append("\n");
480
		buf.append("    /*\n");
481
		buf.append("     * comment2\n");
482
		buf.append("     */\n");
483
		buf.append("\n");
484
		buf.append("    // one line comment3\n");
485
		buf.append("    class X1{\n");
486
		buf.append("    }\n");
487
		buf.append("    // one line comment4\n");
488
		buf.append("\n");
489
		buf.append("\n");
490
		buf.append("    // one line comment5\n");
491
		buf.append("\n");
492
		buf.append("    /*\n");
493
		buf.append("     * comment6\n");
494
		buf.append("     */\n");
495
		buf.append("\n");
496
		buf.append("\n");
497
		buf.append("    /*\n");
498
		buf.append("     * comment7\n");
499
		buf.append("     */\n");
500
		buf.append("\n");
501
		buf.append("    // one line comment8\n");
502
		buf.append("    // one line comment9\n");
503
		buf.append("    class X2{\n");
504
		buf.append("    }\n");
505
		buf.append("    /*\n");
506
		buf.append("     * comment10\n");
507
		buf.append("     */\n");
508
		buf.append("\n");
509
		buf.append("    // one line comment11\n");
510
		buf.append("    // one line comment12\n");
511
		buf.append("}\n");
512
		ICompilationUnit cu= pack1.createCompilationUnit("X.java", buf.toString(), false, null);
513
514
		CompilationUnit astRoot= createCU(cu, false);
515
516
		astRoot.recordModifications();
517
518
		List types = astRoot.types();
519
		TypeDeclaration typeDeclaration = (TypeDeclaration)types.get(0);
520
		TypeDeclaration [] members = typeDeclaration.getTypes();
521
		typeDeclaration.bodyDeclarations().remove(members[0]);
522
		typeDeclaration.bodyDeclarations().remove(members[1]);
523
524
		String preview = evaluateRewrite(cu, astRoot);
525
526
		buf= new StringBuffer();
527
		buf.append("package test0012;\n");
528
		buf.append("public class X {\n");
529
		buf.append("\n");
530
		buf.append("    // one line comment1\n");
531
		buf.append("\n");
532
		buf.append("    /*\n");
533
		buf.append("     * comment2\n");
534
		buf.append("     */\n");
535
		buf.append("\n");
536
		buf.append("    \n");
537
		buf.append("\n");
538
		buf.append("\n");
539
		buf.append("    // one line comment5\n");
540
		buf.append("\n");
541
		buf.append("    /*\n");
542
		buf.append("     * comment6\n");
543
		buf.append("     */\n");
544
		buf.append("\n");
545
		buf.append("\n");
546
		buf.append("    /*\n");
547
		buf.append("     * comment7\n");
548
		buf.append("     */\n");
549
		buf.append("\n");
550
		buf.append("    \n");
551
		buf.append("\n");
552
		buf.append("    // one line comment11\n");
553
		buf.append("    // one line comment12\n");
554
		buf.append("}\n");
555
		assertEqualString(preview, buf.toString());
556
	}
461
}
557
}

Return to bug 306524