Copy: Enable file copy for mixed Java-Kotlin case

Also merge CopyKotlinFileHandler with CopyKotlinDeclarationsHandler
as it currently can't be invoked for such cases

 #KT-18200 Fixed
This commit is contained in:
Alexey Sedunov
2017-06-05 13:41:57 +03:00
parent 13212f06b4
commit e5717d3e96
3 changed files with 25 additions and 49 deletions
-4
View File
@@ -410,10 +410,6 @@
id="kotlinClass"
implementation="org.jetbrains.kotlin.idea.refactoring.copy.CopyKotlinDeclarationsHandler"
order="first" />
<refactoring.copyHandler
id="kotlinFile"
implementation="org.jetbrains.kotlin.idea.refactoring.copy.CopyKotlinFileHandler"
order="after kotlinClass" />
<refactoring.changeSignatureUsageProcessor
implementation="org.jetbrains.kotlin.idea.refactoring.changeSignature.KotlinChangeSignatureUsageProcessor"
order="after javaProcessor" />
@@ -25,11 +25,13 @@ import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiDirectory
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiFileSystemItem
import com.intellij.psi.search.LocalSearchScope
import com.intellij.psi.search.searches.ReferencesSearch
import com.intellij.refactoring.BaseRefactoringProcessor
import com.intellij.refactoring.RefactoringBundle
import com.intellij.refactoring.copy.CopyFilesOrDirectoriesDialog
import com.intellij.refactoring.copy.CopyFilesOrDirectoriesHandler
import com.intellij.refactoring.copy.CopyHandlerDelegateBase
import com.intellij.refactoring.util.MoveRenameUsageInfo
import com.intellij.usageView.UsageInfo
@@ -71,7 +73,20 @@ class CopyKotlinDeclarationsHandler : CopyHandlerDelegateBase() {
}
}
override fun canCopy(elements: Array<out PsiElement>, fromUpdate: Boolean): Boolean {
private val copyFilesHandler by lazy { CopyFilesOrDirectoriesHandler() }
private fun getSourceFiles(elements: Array<out PsiElement>): Array<PsiElement>? {
return elements
.map { it.containingFile ?: it as? PsiFileSystemItem ?: return null }
.toTypedArray()
}
private fun canCopyFiles(elements: Array<out PsiElement>, fromUpdate: Boolean): Boolean {
val sourceFiles = getSourceFiles(elements) ?: return false
return copyFilesHandler.canCopy(sourceFiles, fromUpdate)
}
private fun canCopyDeclarations(elements: Array<out PsiElement>): Boolean {
val containingFile =
elements
.flatMap { it.getElementsToCopy().ifEmpty { return false } }
@@ -81,6 +96,10 @@ class CopyKotlinDeclarationsHandler : CopyHandlerDelegateBase() {
return containingFile.sourceRoot != null
}
override fun canCopy(elements: Array<out PsiElement>, fromUpdate: Boolean): Boolean {
return canCopyDeclarations(elements) || canCopyFiles(elements, fromUpdate)
}
enum class ExistingFilePolicy {
APPEND, OVERWRITE, SKIP
}
@@ -149,6 +168,11 @@ class CopyKotlinDeclarationsHandler : CopyHandlerDelegateBase() {
}
override fun doCopy(elements: Array<out PsiElement>, defaultTargetDirectory: PsiDirectory?) {
if (!canCopyDeclarations(elements)) {
val sourceFiles = getSourceFiles(elements) ?: return
return copyFilesHandler.doCopy(sourceFiles, defaultTargetDirectory)
}
val elementsToCopy = elements.flatMap { it.getElementsToCopy() }
if (elementsToCopy.isEmpty()) return
@@ -1,44 +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.psi.KtFile
class CopyKotlinFileHandler : CopyHandlerDelegateBase() {
private val delegate = CopyFilesOrDirectoriesHandler()
private fun adjustElements(elements: Array<out PsiElement>): Array<PsiElement>? {
return elements
.map { (if (it.isValid) it.containingFile as? KtFile else null) ?: return null }
.toTypedArray()
}
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)
}