View | Details | Raw Unified | Return to bug 247757
Collapse All | Expand All

(-)model/org/eclipse/jdt/internal/core/CopyResourceElementsOperation.java (-5 / +25 lines)
Lines 20-28 Link Here
20
import org.eclipse.core.runtime.jobs.MultiRule;
20
import org.eclipse.core.runtime.jobs.MultiRule;
21
import org.eclipse.jdt.core.*;
21
import org.eclipse.jdt.core.*;
22
import org.eclipse.jdt.core.dom.AST;
22
import org.eclipse.jdt.core.dom.AST;
23
import org.eclipse.jdt.core.dom.ASTNode;
23
import org.eclipse.jdt.core.dom.ASTParser;
24
import org.eclipse.jdt.core.dom.ASTParser;
24
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
25
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
25
import org.eclipse.jdt.core.dom.CompilationUnit;
26
import org.eclipse.jdt.core.dom.CompilationUnit;
27
import org.eclipse.jdt.core.dom.Javadoc;
26
import org.eclipse.jdt.core.dom.MethodDeclaration;
28
import org.eclipse.jdt.core.dom.MethodDeclaration;
27
import org.eclipse.jdt.core.dom.Name;
29
import org.eclipse.jdt.core.dom.Name;
28
import org.eclipse.jdt.core.dom.PackageDeclaration;
30
import org.eclipse.jdt.core.dom.PackageDeclaration;
Lines 524-530 Link Here
524
						CompilationUnit astCU = (CompilationUnit) this.parser.createAST(this.progressMonitor);
526
						CompilationUnit astCU = (CompilationUnit) this.parser.createAST(this.progressMonitor);
525
						AST ast = astCU.getAST();
527
						AST ast = astCU.getAST();
526
						ASTRewrite rewrite = ASTRewrite.create(ast);
528
						ASTRewrite rewrite = ASTRewrite.create(ast);
527
						updatePackageStatement(astCU, newFragName, rewrite);
529
						updatePackageStatement(astCU, newFragName, rewrite, cu);
528
						TextEdit edits = rewrite.rewriteAST();
530
						TextEdit edits = rewrite.rewriteAST();
529
						applyTextEdit(cu, edits);
531
						applyTextEdit(cu, edits);
530
						cu.save(null, false);
532
						cu.save(null, false);
Lines 618-634 Link Here
618
			AST ast = astCU.getAST();
620
			AST ast = astCU.getAST();
619
			ASTRewrite rewrite = ASTRewrite.create(ast);
621
			ASTRewrite rewrite = ASTRewrite.create(ast);
620
			updateTypeName(cu, astCU, cu.getElementName(), newName, rewrite);
622
			updateTypeName(cu, astCU, cu.getElementName(), newName, rewrite);
621
			updatePackageStatement(astCU, destPackageName, rewrite);
623
			updatePackageStatement(astCU, destPackageName, rewrite, cu);
622
			return rewrite.rewriteAST();
624
			return rewrite.rewriteAST();
623
		}
625
		}
624
	}
626
	}
625
	private void updatePackageStatement(CompilationUnit astCU, String[] pkgName, ASTRewrite rewriter) throws JavaModelException {
627
	private void updatePackageStatement(CompilationUnit astCU, String[] pkgName, ASTRewrite rewriter, ICompilationUnit cu) throws JavaModelException {
626
		boolean defaultPackage = pkgName.length == 0;
628
		boolean defaultPackage = pkgName.length == 0;
627
		AST ast = astCU.getAST();
629
		AST ast = astCU.getAST();
628
		if (defaultPackage) {
630
		if (defaultPackage) {
629
			// remove existing package statement
631
			// remove existing package statement
630
			if (astCU.getPackage() != null)
632
			PackageDeclaration pkg = astCU.getPackage();
631
				rewriter.set(astCU, CompilationUnit.PACKAGE_PROPERTY, null, null);
633
			if (pkg != null) {
634
				int pkgStart;
635
				Javadoc javadoc = pkg.getJavadoc();
636
				if (javadoc != null) {
637
					pkgStart = javadoc.getStartPosition() + javadoc.getLength() + 1;
638
				} else {
639
					pkgStart = pkg.getStartPosition();
640
				}
641
				int extendedStart = astCU.getExtendedStartPosition(pkg);
642
				if (pkgStart != extendedStart) {
643
					// keep the comments associated with package declaration
644
					// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=247757
645
					String commentSource = cu.getSource().substring(extendedStart, pkgStart);
646
					ASTNode comment = rewriter.createStringPlaceholder(commentSource, ASTNode.PACKAGE_DECLARATION);
647
					rewriter.set(astCU, CompilationUnit.PACKAGE_PROPERTY, comment, null);
648
				} else {
649
					rewriter.set(astCU, CompilationUnit.PACKAGE_PROPERTY, null, null);
650
				}
651
			}
632
		} else {
652
		} else {
633
			org.eclipse.jdt.core.dom.PackageDeclaration pkg = astCU.getPackage();
653
			org.eclipse.jdt.core.dom.PackageDeclaration pkg = astCU.getPackage();
634
			if (pkg != null) {
654
			if (pkg != null) {
(-)dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java (-3 / +12 lines)
Lines 609-618 Link Here
609
				case RewriteEvent.REMOVED: {
609
				case RewriteEvent.REMOVED: {
610
					ASTNode node= (ASTNode) event.getOriginalValue();
610
					ASTNode node= (ASTNode) event.getOriginalValue();
611
					TextEditGroup editGroup= getEditGroup(event);
611
					TextEditGroup editGroup= getEditGroup(event);
612
612
					
613
					int nodeEnd= getExtendedEnd(node);
614
					// if there is a prefix, remove the prefix as well
613
					// if there is a prefix, remove the prefix as well
615
					int len= nodeEnd - offset;
614
					int nodeEnd;
615
					int len;
616
					if (offset == 0) {
617
						SourceRange range= getExtendedRange(node);
618
						offset= range.getStartPosition();
619
						len= range.getLength();
620
						nodeEnd= offset+len;
621
					} else {
622
						nodeEnd= getExtendedEnd(node);
623
						len= nodeEnd-offset;
624
					}
616
					doTextRemoveAndVisit(offset, len, node, editGroup);
625
					doTextRemoveAndVisit(offset, len, node, editGroup);
617
					return nodeEnd;
626
					return nodeEnd;
618
				}
627
				}
(-)src/org/eclipse/jdt/core/tests/model/CopyMoveResourcesTests.java (+108 lines)
Lines 952-957 Link Here
952
	};
952
	};
953
	getWorkspace().run(runnable, getFolder("/P/src"), IResource.NONE, null);
953
	getWorkspace().run(runnable, getFolder("/P/src"), IResource.NONE, null);
954
}
954
}
955
/*
956
 * Ensures that the first block comment is not lost if moving a cu to the default package
957
 * (regression test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=247757 )
958
 */
959
public void testMoveCU10() throws CoreException {
960
	createFolder("/P/src/p1");
961
	createFile(
962
		"/P/src/p1/X.java",
963
		"/* some comment */\n" +
964
		"package p1;\n" +
965
		"public class X {\n" +
966
		"}"
967
	);
968
	ICompilationUnit cuSource = getCompilationUnit("/P/src/p1/X.java");
969
	IPackageFragment pkgDest = getPackage("/P/src");
970
	cuSource.move(pkgDest, null/*no sibling*/, null/*no rename*/, false/*don't replace*/, null/*no progress*/);
971
	
972
	ICompilationUnit cuDest = getCompilationUnit("/P/src/X.java");
973
	assertSourceEquals(
974
		"Unexpected source",
975
		"/* some comment */\n" +
976
		"\n" +
977
		"public class X {\n" +
978
		"}",
979
		cuDest.getSource());
980
}
981
/*
982
 * Ensures that the first block comments are not lost if moving a cu to the default package
983
 * (regression test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=247757 )
984
 */
985
public void testMoveCU11() throws CoreException {
986
	createFolder("/P/src/p1");
987
	createFile(
988
		"/P/src/p1/X.java",
989
		"/* some comment */\n" +
990
		"/* other comment */\n" +
991
		"package p1;\n" +
992
		"public class X {\n" +
993
		"}"
994
	);
995
	ICompilationUnit cuSource = getCompilationUnit("/P/src/p1/X.java");
996
	IPackageFragment pkgDest = getPackage("/P/src");
997
	cuSource.move(pkgDest, null/*no sibling*/, null/*no rename*/, false/*don't replace*/, null/*no progress*/);
998
	
999
	ICompilationUnit cuDest = getCompilationUnit("/P/src/X.java");
1000
	assertSourceEquals(
1001
		"Unexpected source",
1002
		"/* some comment */\n" +
1003
		"/* other comment */\n" +
1004
		"\n" +
1005
		"public class X {\n" +
1006
		"}",
1007
		cuDest.getSource());
1008
}
1009
/*
1010
 * Ensures that the Javadoc comment is not lost if moving a cu to the default package
1011
 * (regression test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=247757 )
1012
 */
1013
public void testMoveCU12() throws CoreException {
1014
	createFolder("/P/src/p1");
1015
	createFile(
1016
		"/P/src/p1/X.java",
1017
		"/** some Javadoc */\n" +
1018
		"package p1;\n" +
1019
		"public class X {\n" +
1020
		"}"
1021
	);
1022
	ICompilationUnit cuSource = getCompilationUnit("/P/src/p1/X.java");
1023
	IPackageFragment pkgDest = getPackage("/P/src");
1024
	cuSource.move(pkgDest, null/*no sibling*/, null/*no rename*/, false/*don't replace*/, null/*no progress*/);
1025
	
1026
	ICompilationUnit cuDest = getCompilationUnit("/P/src/X.java");
1027
	assertSourceEquals(
1028
		"Unexpected source",
1029
		"/** some Javadoc */\n" + 
1030
		"\n" + 
1031
		"public class X {\n" + 
1032
		"}",
1033
		cuDest.getSource());
1034
}
1035
/*
1036
 * Ensures that the Javadoc comment is not lost if moving a cu to the default package
1037
 * (regression test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=247757 )
1038
 */
1039
public void testMoveCU13() throws CoreException {
1040
	createFolder("/P/src/p1");
1041
	createFile(
1042
		"/P/src/p1/X.java",
1043
		"/** some Javadoc */\n" +
1044
		"// some line comment\n" +
1045
		"package p1;\n" +
1046
		"public class X {\n" +
1047
		"}"
1048
	);
1049
	ICompilationUnit cuSource = getCompilationUnit("/P/src/p1/X.java");
1050
	IPackageFragment pkgDest = getPackage("/P/src");
1051
	cuSource.move(pkgDest, null/*no sibling*/, null/*no rename*/, false/*don't replace*/, null/*no progress*/);
1052
	
1053
	ICompilationUnit cuDest = getCompilationUnit("/P/src/X.java");
1054
	assertSourceEquals(
1055
		"Unexpected source",
1056
		"/** some Javadoc */\n" + 
1057
		"// some line comment\n" +
1058
		"\n" + 
1059
		"public class X {\n" + 
1060
		"}",
1061
		cuDest.getSource());
1062
}
955
/**
1063
/**
956
 * Ensures that a package fragment can be moved to a different package fragment root.
1064
 * Ensures that a package fragment can be moved to a different package fragment root.
957
 */
1065
 */

Return to bug 247757