### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core Index: model/org/eclipse/jdt/internal/core/CopyResourceElementsOperation.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/CopyResourceElementsOperation.java,v retrieving revision 1.110 diff -u -r1.110 CopyResourceElementsOperation.java --- model/org/eclipse/jdt/internal/core/CopyResourceElementsOperation.java 10 Sep 2008 07:57:21 -0000 1.110 +++ model/org/eclipse/jdt/internal/core/CopyResourceElementsOperation.java 24 Sep 2008 16:40:32 -0000 @@ -20,9 +20,11 @@ import org.eclipse.core.runtime.jobs.MultiRule; import org.eclipse.jdt.core.*; import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.ASTParser; import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.Javadoc; import org.eclipse.jdt.core.dom.MethodDeclaration; import org.eclipse.jdt.core.dom.Name; import org.eclipse.jdt.core.dom.PackageDeclaration; @@ -524,7 +526,7 @@ CompilationUnit astCU = (CompilationUnit) this.parser.createAST(this.progressMonitor); AST ast = astCU.getAST(); ASTRewrite rewrite = ASTRewrite.create(ast); - updatePackageStatement(astCU, newFragName, rewrite); + updatePackageStatement(astCU, newFragName, rewrite, cu); TextEdit edits = rewrite.rewriteAST(); applyTextEdit(cu, edits); cu.save(null, false); @@ -618,17 +620,35 @@ AST ast = astCU.getAST(); ASTRewrite rewrite = ASTRewrite.create(ast); updateTypeName(cu, astCU, cu.getElementName(), newName, rewrite); - updatePackageStatement(astCU, destPackageName, rewrite); + updatePackageStatement(astCU, destPackageName, rewrite, cu); return rewrite.rewriteAST(); } } - private void updatePackageStatement(CompilationUnit astCU, String[] pkgName, ASTRewrite rewriter) throws JavaModelException { + private void updatePackageStatement(CompilationUnit astCU, String[] pkgName, ASTRewrite rewriter, ICompilationUnit cu) throws JavaModelException { boolean defaultPackage = pkgName.length == 0; AST ast = astCU.getAST(); if (defaultPackage) { // remove existing package statement - if (astCU.getPackage() != null) - rewriter.set(astCU, CompilationUnit.PACKAGE_PROPERTY, null, null); + PackageDeclaration pkg = astCU.getPackage(); + if (pkg != null) { + int pkgStart; + Javadoc javadoc = pkg.getJavadoc(); + if (javadoc != null) { + pkgStart = javadoc.getStartPosition() + javadoc.getLength() + 1; + } else { + pkgStart = pkg.getStartPosition(); + } + int extendedStart = astCU.getExtendedStartPosition(pkg); + if (pkgStart != extendedStart) { + // keep the comments associated with package declaration + // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=247757 + String commentSource = cu.getSource().substring(extendedStart, pkgStart); + ASTNode comment = rewriter.createStringPlaceholder(commentSource, ASTNode.PACKAGE_DECLARATION); + rewriter.set(astCU, CompilationUnit.PACKAGE_PROPERTY, comment, null); + } else { + rewriter.set(astCU, CompilationUnit.PACKAGE_PROPERTY, null, null); + } + } } else { org.eclipse.jdt.core.dom.PackageDeclaration pkg = astCU.getPackage(); if (pkg != null) { Index: dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java,v retrieving revision 1.57 diff -u -r1.57 ASTRewriteAnalyzer.java --- dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java 1 Jul 2008 13:51:37 -0000 1.57 +++ dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java 24 Sep 2008 16:40:32 -0000 @@ -609,10 +609,19 @@ case RewriteEvent.REMOVED: { ASTNode node= (ASTNode) event.getOriginalValue(); TextEditGroup editGroup= getEditGroup(event); - - int nodeEnd= getExtendedEnd(node); + // if there is a prefix, remove the prefix as well - int len= nodeEnd - offset; + int nodeEnd; + int len; + if (offset == 0) { + SourceRange range= getExtendedRange(node); + offset= range.getStartPosition(); + len= range.getLength(); + nodeEnd= offset+len; + } else { + nodeEnd= getExtendedEnd(node); + len= nodeEnd-offset; + } doTextRemoveAndVisit(offset, len, node, editGroup); return nodeEnd; } #P org.eclipse.jdt.core.tests.model Index: src/org/eclipse/jdt/core/tests/model/CopyMoveResourcesTests.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/CopyMoveResourcesTests.java,v retrieving revision 1.26 diff -u -r1.26 CopyMoveResourcesTests.java --- src/org/eclipse/jdt/core/tests/model/CopyMoveResourcesTests.java 10 Sep 2008 07:57:20 -0000 1.26 +++ src/org/eclipse/jdt/core/tests/model/CopyMoveResourcesTests.java 24 Sep 2008 16:40:34 -0000 @@ -952,6 +952,114 @@ }; getWorkspace().run(runnable, getFolder("/P/src"), IResource.NONE, null); } +/* + * Ensures that the first block comment is not lost if moving a cu to the default package + * (regression test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=247757 ) + */ +public void testMoveCU10() throws CoreException { + createFolder("/P/src/p1"); + createFile( + "/P/src/p1/X.java", + "/* some comment */\n" + + "package p1;\n" + + "public class X {\n" + + "}" + ); + ICompilationUnit cuSource = getCompilationUnit("/P/src/p1/X.java"); + IPackageFragment pkgDest = getPackage("/P/src"); + cuSource.move(pkgDest, null/*no sibling*/, null/*no rename*/, false/*don't replace*/, null/*no progress*/); + + ICompilationUnit cuDest = getCompilationUnit("/P/src/X.java"); + assertSourceEquals( + "Unexpected source", + "/* some comment */\n" + + "\n" + + "public class X {\n" + + "}", + cuDest.getSource()); +} +/* + * Ensures that the first block comments are not lost if moving a cu to the default package + * (regression test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=247757 ) + */ +public void testMoveCU11() throws CoreException { + createFolder("/P/src/p1"); + createFile( + "/P/src/p1/X.java", + "/* some comment */\n" + + "/* other comment */\n" + + "package p1;\n" + + "public class X {\n" + + "}" + ); + ICompilationUnit cuSource = getCompilationUnit("/P/src/p1/X.java"); + IPackageFragment pkgDest = getPackage("/P/src"); + cuSource.move(pkgDest, null/*no sibling*/, null/*no rename*/, false/*don't replace*/, null/*no progress*/); + + ICompilationUnit cuDest = getCompilationUnit("/P/src/X.java"); + assertSourceEquals( + "Unexpected source", + "/* some comment */\n" + + "/* other comment */\n" + + "\n" + + "public class X {\n" + + "}", + cuDest.getSource()); +} +/* + * Ensures that the Javadoc comment is not lost if moving a cu to the default package + * (regression test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=247757 ) + */ +public void testMoveCU12() throws CoreException { + createFolder("/P/src/p1"); + createFile( + "/P/src/p1/X.java", + "/** some Javadoc */\n" + + "package p1;\n" + + "public class X {\n" + + "}" + ); + ICompilationUnit cuSource = getCompilationUnit("/P/src/p1/X.java"); + IPackageFragment pkgDest = getPackage("/P/src"); + cuSource.move(pkgDest, null/*no sibling*/, null/*no rename*/, false/*don't replace*/, null/*no progress*/); + + ICompilationUnit cuDest = getCompilationUnit("/P/src/X.java"); + assertSourceEquals( + "Unexpected source", + "/** some Javadoc */\n" + + "\n" + + "public class X {\n" + + "}", + cuDest.getSource()); +} +/* + * Ensures that the Javadoc comment is not lost if moving a cu to the default package + * (regression test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=247757 ) + */ +public void testMoveCU13() throws CoreException { + createFolder("/P/src/p1"); + createFile( + "/P/src/p1/X.java", + "/** some Javadoc */\n" + + "// some line comment\n" + + "package p1;\n" + + "public class X {\n" + + "}" + ); + ICompilationUnit cuSource = getCompilationUnit("/P/src/p1/X.java"); + IPackageFragment pkgDest = getPackage("/P/src"); + cuSource.move(pkgDest, null/*no sibling*/, null/*no rename*/, false/*don't replace*/, null/*no progress*/); + + ICompilationUnit cuDest = getCompilationUnit("/P/src/X.java"); + assertSourceEquals( + "Unexpected source", + "/** some Javadoc */\n" + + "// some line comment\n" + + "\n" + + "public class X {\n" + + "}", + cuDest.getSource()); +} /** * Ensures that a package fragment can be moved to a different package fragment root. */