KT-18051: Allow copy-pasting from IDEA java to kotlin conversion

#KT-18051 fixed
This commit is contained in:
Simon Ogorodnik
2017-06-09 20:11:53 +03:00
parent fa3f87492c
commit 75e5dca735
3 changed files with 40 additions and 12 deletions
@@ -65,16 +65,17 @@ class ConvertTextJavaCopyPasteProcessor : CopyPastePostProcessor<TextBlockTransf
}
override fun collectTransferableData(file: PsiFile, editor: Editor, startOffsets: IntArray, endOffsets: IntArray): List<TextBlockTransferableData> {
if (file is KtFile) return listOf(CopiedKotlinCode(file.text, startOffsets, endOffsets))
return emptyList()
}
override fun extractTransferableData(content: Transferable): List<TextBlockTransferableData> {
try {
if (content.isDataFlavorSupported(DataFlavor.stringFlavor)) {
// check if it's copied from within IDEA
if (!Companion.convertOnCopyInsideIDE && content.transferDataFlavors.any { TextBlockTransferableData::class.java.isAssignableFrom(it.representationClass) }) {
return emptyList()
}
if (content.isDataFlavorSupported(CopiedKotlinCode.DATA_FLAVOR) ||
/* Handled by ConvertJavaCopyPasteProcessor */
content.isDataFlavorSupported(CopiedJavaCode.DATA_FLAVOR)) return emptyList()
val text = content.getTransferData(DataFlavor.stringFlavor) as String
return listOf(MyTransferableData(text))
@@ -284,6 +285,5 @@ class ConvertTextJavaCopyPasteProcessor : CopyPastePostProcessor<TextBlockTransf
companion object {
@TestOnly var conversionPerformed: Boolean = false
@TestOnly var convertOnCopyInsideIDE: Boolean = false
}
}
@@ -0,0 +1,34 @@
/*
* Copyright 2010-2017 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.conversion.copy
import com.intellij.codeInsight.editorActions.TextBlockTransferableData
import java.awt.datatransfer.DataFlavor
class CopiedKotlinCode(val fileText: String, val startOffsets: IntArray, val endOffsets: IntArray) : TextBlockTransferableData {
override fun getFlavor() = DATA_FLAVOR
override fun getOffsetCount() = 0
override fun getOffsets(offsets: IntArray?, index: Int) = index
override fun setOffsets(offsets: IntArray?, index: Int) = index
companion object {
val DATA_FLAVOR: DataFlavor = DataFlavor(CopiedKotlinCode::class.java, "Copied kotlin code")
}
}
@@ -63,14 +63,8 @@ abstract class AbstractTextJavaToKotlinCopyPasteConversionTest : AbstractCopyPas
configureTargetFile(testName + ".to.kt")
ConvertTextJavaCopyPasteProcessor.conversionPerformed = false
ConvertTextJavaCopyPasteProcessor.convertOnCopyInsideIDE = true
try {
myFixture.performEditorAction(IdeActions.ACTION_PASTE)
}
finally {
ConvertTextJavaCopyPasteProcessor.convertOnCopyInsideIDE = false
}
myFixture.performEditorAction(IdeActions.ACTION_PASTE)
kotlin.test.assertEquals(noConversionExpected, !ConvertTextJavaCopyPasteProcessor.conversionPerformed,
if (noConversionExpected) "Conversion to Kotlin should not be suggested" else "No conversion to Kotlin suggested")