Copy: Fix Kotlin file copying. Drop useless JetCopyClassHandler

#KT-7515 Fixed
This commit is contained in:
Alexey Sedunov
2015-06-16 20:10:50 +03:00
parent 683a8895df
commit 1a9003c325
4 changed files with 55 additions and 71 deletions
+1 -1
View File
@@ -278,7 +278,7 @@
<refactoring.moveInnerClassUsagesHandler
implementationClass="org.jetbrains.kotlin.idea.refactoring.move.MoveJavaInnerClassKotlinUsagesHandler"
language="jet" />
<refactoring.copyHandler implementation="org.jetbrains.kotlin.idea.refactoring.copy.JetCopyClassHandler"/>
<refactoring.copyHandler implementation="org.jetbrains.kotlin.idea.refactoring.copy.CopyKotlinFileHandler" order="first" />
<refactoring.changeSignatureUsageProcessor implementation="org.jetbrains.kotlin.idea.refactoring.changeSignature.JetChangeSignatureUsageProcessor"/>
<refactoring.introduceParameterMethodUsagesProcessor
implementation="org.jetbrains.kotlin.idea.refactoring.introduce.introduceParameter.KotlinIntroduceParameterMethodUsageProcessor"/>
@@ -0,0 +1,54 @@
/*
* Copyright 2010-2015 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.kotlin.idea.refactoring.copy
import com.intellij.psi.PsiDirectory
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.refactoring.copy.CopyFilesOrDirectoriesHandler
import com.intellij.refactoring.copy.CopyHandlerDelegateBase
import org.jetbrains.kotlin.psi.JetClassOrObject
import org.jetbrains.kotlin.psi.JetFile
public class CopyKotlinFileHandler : CopyHandlerDelegateBase() {
private val delegate = CopyFilesOrDirectoriesHandler()
private fun adjustElements(elements: Array<out PsiElement>): Array<PsiElement>? {
val adjustedElements = elements
.map {
val file = it.getContainingFile()
when {
it is JetFile -> it
it is JetClassOrObject && it.isTopLevel() && (file as JetFile).getDeclarations().size() == 1 -> file
else -> return null
}
}
.toTypedArray()
return adjustedElements
}
override fun canCopy(elements: Array<out PsiElement>, fromUpdate: Boolean): Boolean {
return delegate.canCopy(adjustElements(elements) ?: return false, fromUpdate)
}
override fun doCopy(elements: Array<out PsiElement>, defaultTargetDirectory: PsiDirectory?) {
return delegate.doCopy(adjustElements(elements) ?: return, defaultTargetDirectory)
}
override fun doClone(element: PsiElement?) = delegate.doClone(element)
}
@@ -1,64 +0,0 @@
/*
* Copyright 2010-2015 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.kotlin.idea.refactoring.copy;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
import com.intellij.refactoring.copy.CopyFilesOrDirectoriesHandler;
import com.intellij.refactoring.copy.CopyHandlerDelegateBase;
import org.jetbrains.kotlin.idea.refactoring.move.moveFilesOrDirectories.KotlinMoveFilesOrDirectoriesHandler;
import org.jetbrains.kotlin.psi.JetClassOrObject;
import java.util.ArrayList;
public class JetCopyClassHandler extends CopyHandlerDelegateBase {
private CopyFilesOrDirectoriesHandler delegate = new CopyFilesOrDirectoriesHandler();
private static PsiElement[] replaceElements(PsiElement[] elements) {
ArrayList<PsiElement> result = new ArrayList<PsiElement>();
for (PsiElement element : elements) {
result.add(replaceElement(element));
}
return result.toArray(new PsiElement[result.size()]);
}
private static PsiElement replaceElement(PsiElement element) {
if (element instanceof JetClassOrObject &&
KotlinMoveFilesOrDirectoriesHandler.Companion.isMovableClass((JetClassOrObject) element)) {
return element.getContainingFile();
}
else {
return element;
}
}
@Override
public boolean canCopy(PsiElement[] elements, boolean fromUpdate) {
return delegate.canCopy(replaceElements(elements), fromUpdate);
}
@Override
public void doCopy(PsiElement[] elements, PsiDirectory defaultTargetDirectory) {
delegate.doCopy(replaceElements(elements), defaultTargetDirectory);
}
@Override
public void doClone(PsiElement element) {
delegate.doClone(replaceElement(element));
}
}
@@ -50,10 +50,4 @@ public class KotlinMoveFilesOrDirectoriesHandler : MoveFilesOrDirectoriesHandler
callback?.refactoringCompleted()
}
}
companion object {
public fun isMovableClass(clazz: JetClassOrObject): Boolean {
return clazz.isTopLevel() && clazz.getContainingJetFile().getDeclarations().all { it !is JetClassOrObject || it == clazz }
}
}
}