IDEA-78924 Groovy: Copy+Paste a groovy script file doubles the file extension

This commit is contained in:
Maxim.Medvedev
2011-12-17 22:49:31 +04:00
parent 28c93ce970
commit 78ee8593f5
4 changed files with 88 additions and 73 deletions
@@ -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) {
@@ -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);
}
@@ -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));
}
}
@@ -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()))));
}
}