From 78ee8593f595c77edb7d2d0342a5e1e4fa19c49b Mon Sep 17 00:00:00 2001 From: "Maxim.Medvedev" Date: Sat, 17 Dec 2011 22:49:31 +0400 Subject: [PATCH] IDEA-78924 Groovy: Copy+Paste a groovy script file doubles the file extension --- .../refactoring/copy/CopyClassesHandler.java | 4 +- .../copy/CopyFilesOrDirectoriesHandler.java | 26 ++++++-- .../copy/GroovyCopyClassTest.groovy | 65 ++++++++++++++++++ .../refactoring/copy/GroovyCopyClassTest.java | 66 ------------------- 4 files changed, 88 insertions(+), 73 deletions(-) create mode 100644 plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/copy/GroovyCopyClassTest.groovy delete mode 100644 plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/copy/GroovyCopyClassTest.java diff --git a/java/java-impl/src/com/intellij/refactoring/copy/CopyClassesHandler.java b/java/java-impl/src/com/intellij/refactoring/copy/CopyClassesHandler.java index aa7658215630..164d43dce300 100644 --- a/java/java-impl/src/com/intellij/refactoring/copy/CopyClassesHandler.java +++ b/java/java-impl/src/com/intellij/refactoring/copy/CopyClassesHandler.java @@ -128,7 +128,9 @@ public class CopyClassesHandler extends CopyHandlerDelegateBase { if (classes != null) { topLevelClasses = ArrayUtil.mergeArrays(classes, topLevelClasses, PsiClass.ARRAY_FACTORY); } - result.put(containingFile, topLevelClasses); + if (topLevelClasses != null) { + result.put(containingFile, topLevelClasses); + } } public void doCopy(PsiElement[] elements, PsiDirectory defaultTargetDirectory) { diff --git a/platform/lang-impl/src/com/intellij/refactoring/copy/CopyFilesOrDirectoriesHandler.java b/platform/lang-impl/src/com/intellij/refactoring/copy/CopyFilesOrDirectoriesHandler.java index ea3e0c896ecf..b0c1ec443a6b 100644 --- a/platform/lang-impl/src/com/intellij/refactoring/copy/CopyFilesOrDirectoriesHandler.java +++ b/platform/lang-impl/src/com/intellij/refactoring/copy/CopyFilesOrDirectoriesHandler.java @@ -73,11 +73,22 @@ public class CopyFilesOrDirectoriesHandler extends CopyHandlerDelegateBase { } public static void copyAsFiles(PsiElement[] elements, PsiDirectory defaultTargetDirectory, Project project) { - CopyFilesOrDirectoriesDialog dialog = new CopyFilesOrDirectoriesDialog(elements, defaultTargetDirectory, project, false); - dialog.show(); - if (dialog.isOK()) { - final String newName = elements.length == 1 ? dialog.getNewName() : null; - final PsiDirectory targetDirectory = dialog.getTargetDirectory(); + PsiDirectory targetDirectory = null; + String newName = null; + + if (ApplicationManager.getApplication().isUnitTestMode()) { + targetDirectory = defaultTargetDirectory; + } + else { + CopyFilesOrDirectoriesDialog dialog = new CopyFilesOrDirectoriesDialog(elements, defaultTargetDirectory, project, false); + dialog.show(); + if (dialog.isOK()) { + newName = elements.length == 1 ? dialog.getNewName() : null; + targetDirectory = dialog.getTargetDirectory(); + } + } + + if (targetDirectory != null) { try { for (PsiElement element : elements) { PsiFileSystemItem psiElement = (PsiFileSystemItem)element; @@ -155,7 +166,10 @@ public class CopyFilesOrDirectoriesHandler extends CopyHandlerDelegateBase { * @param newName can be not null only if elements.length == 1 * @param targetDirectory */ - private static void copyImpl(final PsiElement[] elements, final String newName, final PsiDirectory targetDirectory, final boolean doClone) { + private static void copyImpl(@NotNull final PsiElement[] elements, + @Nullable final String newName, + @NotNull final PsiDirectory targetDirectory, + final boolean doClone) { if (doClone && elements.length != 1) { throw new IllegalArgumentException("invalid number of elements to clone:" + elements.length); } diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/copy/GroovyCopyClassTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/copy/GroovyCopyClassTest.groovy new file mode 100644 index 000000000000..0e5a7605e992 --- /dev/null +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/copy/GroovyCopyClassTest.groovy @@ -0,0 +1,65 @@ +/* + * Copyright 2000-2009 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jetbrains.plugins.groovy.refactoring.copy + +import com.intellij.openapi.application.Result +import com.intellij.openapi.command.WriteCommandAction +import com.intellij.psi.PsiClass +import com.intellij.psi.PsiFile +import com.intellij.psi.search.GlobalSearchScope +import com.intellij.refactoring.copy.CopyClassesHandler +import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase +import org.jetbrains.plugins.groovy.util.TestUtils + +/** + * @author peter + */ +public class GroovyCopyClassTest extends LightCodeInsightFixtureTestCase { + + @Override + protected String getBasePath() { + return "${TestUtils.testDataPath}refactoring/copy/"; + } + + public void testBetweenPackages() throws Throwable { + final String testName = getTestName(false); + myFixture.copyFileToProject("${testName}.groovy", "foo/${testName}.groovy"); + myFixture.addClass("package foo; public class Bar {}"); + myFixture.addClass("package bar; public class Bar {}"); + + final PsiClass srcClass = myFixture.javaFacade.findClass("foo.$testName", GlobalSearchScope.allScope(project)); + assertTrue(CopyClassesHandler.canCopyClass(srcClass)); + new WriteCommandAction(project, [] as PsiFile[]) { + @Override + protected void run(Result result) throws Throwable { + def map = Collections.singletonMap(srcClass.navigationElement.containingFile, [srcClass] as PsiClass[]) + def dir = srcClass.manager.findDirectory(myFixture.tempDirFixture.getFile("bar")) + CopyClassesHandler.doCopyClasses(map, "${testName}_after", dir, project); + } + }.execute(); + + myFixture.checkResultByFile("bar/${testName}_after.groovy", "${testName}_after.groovy", true); + } + + public void testCopyScript() throws Throwable { + final String testName = getTestName(false); + def file = myFixture.copyFileToProject("${testName}.groovy", "/foo/${testName}.groovy"); + def psiFile = myFixture.psiManager.findFile(file) + //would be copied as file + assertFalse(CopyClassesHandler.canCopyClass(myFixture.javaFacade.findClass("foo.$testName", GlobalSearchScope.allScope(project)))); + assertFalse(CopyClassesHandler.canCopyClass(psiFile)); + } +} diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/copy/GroovyCopyClassTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/copy/GroovyCopyClassTest.java deleted file mode 100644 index cc774f8591cb..000000000000 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/copy/GroovyCopyClassTest.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2000-2009 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jetbrains.plugins.groovy.refactoring.copy; - -import com.intellij.openapi.application.Result; -import com.intellij.openapi.command.WriteCommandAction; -import com.intellij.psi.PsiClass; -import com.intellij.psi.search.GlobalSearchScope; -import com.intellij.refactoring.copy.CopyClassesHandler; -import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase; -import org.jetbrains.plugins.groovy.util.TestUtils; - -import java.util.Collections; - -/** - * @author peter - */ -public class GroovyCopyClassTest extends LightCodeInsightFixtureTestCase { - - @Override - protected String getBasePath() { - return TestUtils.getTestDataPath() + "refactoring/copy/"; - } - - public void testBetweenPackages() throws Throwable { - final String testName = getTestName(false); - myFixture.copyFileToProject(testName + ".groovy", "foo/" + testName + ".groovy"); - myFixture.addClass("package foo; public class Bar {}"); - myFixture.addClass("package bar; public class Bar {}"); - - final PsiClass srcClass = myFixture.getJavaFacade().findClass("foo." + testName, GlobalSearchScope.allScope(getProject())); - assertTrue(CopyClassesHandler.canCopyClass(srcClass)); - new WriteCommandAction(getProject()) { - @Override - protected void run(Result result) throws Throwable { - CopyClassesHandler.doCopyClasses(Collections.singletonMap(srcClass.getNavigationElement().getContainingFile(), new PsiClass[]{srcClass}), testName + "_after", srcClass.getManager().findDirectory(myFixture.getTempDirFixture().getFile("bar")), - getProject()); - } - }.execute(); - - myFixture.checkResultByFile("bar/" + testName + "_after.groovy", testName + "_after.groovy", true); - } - - public void testCopyScript() throws Throwable { - final String testName = getTestName(false); - myFixture.copyFileToProject(testName + ".groovy", "/foo/" + testName + ".groovy"); - - //would be copied as file - assertFalse( - CopyClassesHandler.canCopyClass(myFixture.getJavaFacade().findClass("foo." + testName, GlobalSearchScope.allScope(getProject())))); - } - -}