mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
insert package definition when moving in the same package
testdata fixed
This commit is contained in:
+13
-9
@@ -62,12 +62,13 @@ public class MoveGroovyClassHandler implements MoveClassHandler {
|
||||
|
||||
PsiClass newClass = null;
|
||||
|
||||
final String newPackageName = newPackage.getQualifiedName();
|
||||
if (aClass instanceof GroovyScriptClass) {
|
||||
final PsiClass[] classes = ((GroovyFile)file).getClasses();
|
||||
if (classes.length == 1) {
|
||||
if (!moveDestination.equals(file.getContainingDirectory())) {
|
||||
aClass.getManager().moveFile(file, moveDestination);
|
||||
((PsiClassOwner)file).setPackageName(newPackage.getQualifiedName());
|
||||
((PsiClassOwner)file).setPackageName(newPackageName);
|
||||
}
|
||||
return ((GroovyFile)file).getScriptClass();
|
||||
}
|
||||
@@ -113,18 +114,18 @@ public class MoveGroovyClassHandler implements MoveClassHandler {
|
||||
else if (((GroovyFile)file).getClasses().length > 1) {
|
||||
correctSelfReferences(aClass, newPackage);
|
||||
String modifiersText = null;
|
||||
if (newPackage.getQualifiedName().equals(((GroovyFile)file).getPackageName())) {
|
||||
if (newPackageName.equals(((GroovyFile)file).getPackageName())) {
|
||||
final GrPackageDefinition packageDefinition = ((GroovyFile)file).getPackageDefinition();
|
||||
if (packageDefinition != null) {
|
||||
final PsiModifierList modifierList = packageDefinition.getModifierList();
|
||||
if (modifierList != null) {
|
||||
modifiersText = modifierList.getText();
|
||||
modifiersText = modifierList.getText().trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
final PsiClass created = ((GroovyFile)GroovyTemplatesFactory
|
||||
.createFromTemplate(moveDestination, aClass.getName(), aClass.getName() + NewGroovyActionBase.GROOVY_EXTENSION,
|
||||
"GroovyClass.groovy")).getClasses()[0];
|
||||
final PsiFile fromTemplate =
|
||||
GroovyTemplatesFactory.createFromTemplate(moveDestination, aClass.getName(), aClass.getName() + NewGroovyActionBase.GROOVY_EXTENSION, "GroovyClass.groovy");
|
||||
final PsiClass created = ((GroovyFile)fromTemplate).getClasses()[0];
|
||||
PsiDocComment docComment = aClass.getDocComment();
|
||||
if (docComment != null) {
|
||||
final PsiDocComment createdDocComment = created.getDocComment();
|
||||
@@ -137,11 +138,14 @@ public class MoveGroovyClassHandler implements MoveClassHandler {
|
||||
docComment.delete();
|
||||
}
|
||||
newClass = (PsiClass)created.replace(aClass);
|
||||
if (modifiersText != null) {
|
||||
final GrPackageDefinition newPackageDefinition = (GrPackageDefinition)GroovyPsiElementFactory.getInstance(aClass.getProject())
|
||||
.createTopElementFromText(modifiersText + " package " + newPackage.getQualifiedName());
|
||||
if (modifiersText != null && modifiersText.length() > 0) {
|
||||
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(aClass.getProject());
|
||||
final GrPackageDefinition newPackageDefinition = (GrPackageDefinition)factory.createTopElementFromText(modifiersText + " package " + newPackageName);
|
||||
((GroovyFile)newClass.getContainingFile()).setPackage(newPackageDefinition);
|
||||
}
|
||||
else {
|
||||
((GroovyFile)newClass.getContainingFile()).setPackageName(newPackageName);
|
||||
}
|
||||
correctOldClassReferences(newClass, aClass);
|
||||
aClass.delete();
|
||||
}
|
||||
|
||||
+55
-18
@@ -16,22 +16,27 @@
|
||||
|
||||
package org.jetbrains.plugins.groovy.refactoring.move;
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightTestCase;
|
||||
import com.intellij.ide.fileTemplates.FileTemplate;
|
||||
import com.intellij.ide.fileTemplates.FileTemplateManager;
|
||||
import com.intellij.openapi.application.Application;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.application.PathManager;
|
||||
import com.intellij.openapi.command.CommandProcessor;
|
||||
import com.intellij.openapi.fileEditor.FileDocumentManager;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.projectRoots.impl.JavaSdkImpl;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.LocalFileSystem;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.openapi.vfs.VirtualFileManager;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.source.PostprocessReformattingAspect;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.refactoring.PackageWrapper;
|
||||
import com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesProcessor;
|
||||
import com.intellij.refactoring.move.moveClassesOrPackages.SingleSourceRootMoveDestination;
|
||||
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
|
||||
import com.intellij.testFramework.PlatformTestUtil;
|
||||
import com.intellij.testFramework.PsiTestUtil;
|
||||
import junit.framework.AssertionFailedError;
|
||||
import org.jetbrains.plugins.groovy.util.TestUtils;
|
||||
|
||||
@@ -45,9 +50,8 @@ import java.util.Comparator;
|
||||
/**
|
||||
* @author Maxim.Medvedev
|
||||
*/
|
||||
public class GroovyMoveClassTest extends LightCodeInsightFixtureTestCase {
|
||||
@Override
|
||||
protected String getBasePath() {
|
||||
public class GroovyMoveClassTest extends CodeInsightTestCase {
|
||||
protected static String getBasePath() {
|
||||
return TestUtils.getTestDataPath() + "refactoring/move/moveClass/";
|
||||
}
|
||||
|
||||
@@ -134,7 +138,11 @@ public class GroovyMoveClassTest extends LightCodeInsightFixtureTestCase {
|
||||
doTest("aliasImported", new String[]{"p1.C1"}, "p2");
|
||||
}
|
||||
|
||||
private void performAction(String[] classNames, String newPackageName) {
|
||||
public void _testTwoModules() {
|
||||
doTwoModulesTest("twoModules", new String[]{"p1.C1"}, "p2");
|
||||
}
|
||||
|
||||
private void performAction(String[] classNames, String newPackageName, int dirCount) {
|
||||
final PsiClass[] classes = new PsiClass[classNames.length];
|
||||
for (int i = 0; i < classes.length; i++) {
|
||||
String className = classNames[i];
|
||||
@@ -145,17 +153,19 @@ public class GroovyMoveClassTest extends LightCodeInsightFixtureTestCase {
|
||||
PsiPackage aPackage = JavaPsiFacade.getInstance(getProject()).findPackage(newPackageName);
|
||||
assertNotNull("Package " + newPackageName + " not found", aPackage);
|
||||
final PsiDirectory[] dirs = aPackage.getDirectories();
|
||||
assertEquals(dirs.length, 1);
|
||||
assertEquals(dirs.length, dirCount);
|
||||
|
||||
final Application application = ApplicationManager.getApplication();
|
||||
CommandProcessor.getInstance().executeCommand(myFixture.getProject(), new Runnable() {
|
||||
CommandProcessor.getInstance().executeCommand(myProject, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
application.runWriteAction(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
new MoveClassesOrPackagesProcessor(getProject(), classes, new SingleSourceRootMoveDestination(
|
||||
PackageWrapper.create(JavaDirectoryService.getInstance().getPackage(dirs[0])), dirs[0]), true, true, null).run();
|
||||
final PsiDirectory dir = dirs[dirs.length - 1];
|
||||
final SingleSourceRootMoveDestination moveDestination =
|
||||
new SingleSourceRootMoveDestination(PackageWrapper.create(JavaDirectoryService.getInstance().getPackage(dir)), dir);
|
||||
new MoveClassesOrPackagesProcessor(getProject(), classes, moveDestination, true, true, null).run();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -166,17 +176,44 @@ public class GroovyMoveClassTest extends LightCodeInsightFixtureTestCase {
|
||||
}
|
||||
|
||||
private void doTest(String testName, String[] classNames, String newPackageName) {
|
||||
final VirtualFile actualRoot = myFixture.copyDirectoryToProject(testName + "/before", "");
|
||||
try {
|
||||
String root = PathManager.getHomePath().replace(File.separatorChar, '/') + getBasePath() + testName;
|
||||
|
||||
performAction(classNames, newPackageName);
|
||||
String rootBefore = root + "/before";
|
||||
PsiTestUtil.removeAllRoots(myModule, JavaSdkImpl.getMockJdk17());
|
||||
VirtualFile rootDir = PsiTestUtil.createTestProjectStructure(getProject(), myModule, rootBefore, myFilesToDelete);
|
||||
|
||||
File expectedRoot = new File(getTestDataPath() + testName + "/after");
|
||||
//VirtualFile expectedRoot = LocalFileSystem.getInstance().findFileByPath(getTestDataPath() + testName + "/after");
|
||||
getProject().getComponent(PostprocessReformattingAspect.class).doPostponedFormatting();
|
||||
FileDocumentManager.getInstance().saveAllDocuments();
|
||||
performAction(classNames, newPackageName, 1);
|
||||
|
||||
VirtualFileManager.getInstance().refresh(false);
|
||||
assertDirsEquals(expectedRoot, actualRoot);
|
||||
String rootAfter = root + "/after";
|
||||
VirtualFile rootDir2 = LocalFileSystem.getInstance().findFileByPath(rootAfter.replace(File.separatorChar, '/'));
|
||||
myProject.getComponent(PostprocessReformattingAspect.class).doPostponedFormatting();
|
||||
PlatformTestUtil.assertDirectoriesEqual(rootDir2, rootDir, PlatformTestUtil.CVS_FILE_FILTER);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void doTwoModulesTest(String testName, String[] classNames, String newPackageName) {
|
||||
try {
|
||||
String root = PathManager.getHomePath().replace(File.separatorChar, '/') + getBasePath() + testName;
|
||||
|
||||
String rootBefore = root + "/before";
|
||||
PsiTestUtil.removeAllRoots(myModule, JavaSdkImpl.getMockJdk17());
|
||||
VirtualFile rootDir = PsiTestUtil.createTestProjectStructure(getProject(), myModule, rootBefore, myFilesToDelete);
|
||||
final Module second = createModule("second");
|
||||
rootDir.findChild("second").createChildDirectory(this, "p2");
|
||||
performAction(classNames, newPackageName, 1);
|
||||
|
||||
String rootAfter = root + "/after";
|
||||
VirtualFile rootDir2 = LocalFileSystem.getInstance().findFileByPath(rootAfter.replace(File.separatorChar, '/'));
|
||||
myProject.getComponent(PostprocessReformattingAspect.class).doPostponedFormatting();
|
||||
PlatformTestUtil.assertDirectoriesEqual(rootDir2, rootDir, PlatformTestUtil.CVS_FILE_FILTER);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void assertDirsEquals(File d1, VirtualFile d2) {
|
||||
|
||||
-8
@@ -1,12 +1,4 @@
|
||||
package p2
|
||||
|
||||
import static p2.C1.E.e
|
||||
|
||||
class C1 {
|
||||
def foo() {
|
||||
print e
|
||||
}
|
||||
static enum E {
|
||||
e
|
||||
}
|
||||
}
|
||||
Vendored
+3
-1
@@ -1,6 +1,8 @@
|
||||
package pack2
|
||||
package pack2;
|
||||
|
||||
public class Class1 {
|
||||
}
|
||||
|
||||
class Class2 {
|
||||
|
||||
}
|
||||
-1
@@ -1,4 +1,3 @@
|
||||
package pack1;
|
||||
|
||||
|
||||
class Y {}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package pack2
|
||||
package pack2;
|
||||
|
||||
public class Class1 {
|
||||
Class2 a;
|
||||
|
||||
-1
@@ -3,6 +3,5 @@ package pack1
|
||||
import pack2.Xx
|
||||
|
||||
|
||||
|
||||
Xx x = new Xx(2);
|
||||
print x;
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
package p2
|
||||
|
||||
import static p2.C1.E.e
|
||||
import static C1.E.e
|
||||
|
||||
class C1 {
|
||||
def foo() {
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
package p1
|
||||
|
||||
print new C1()
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
package p2
|
||||
|
||||
class C1 {
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
package p2
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
package p1
|
||||
|
||||
class C1 {
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
package p1
|
||||
|
||||
print new C1()
|
||||
+1
@@ -0,0 +1 @@
|
||||
package p2
|
||||
Reference in New Issue
Block a user