KT-18051: Allow copy-pasting from IDEA java to kotlin conversion
#KT-18051 fixed
This commit is contained in:
+5
-5
@@ -65,16 +65,17 @@ class ConvertTextJavaCopyPasteProcessor : CopyPastePostProcessor<TextBlockTransf
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun collectTransferableData(file: PsiFile, editor: Editor, startOffsets: IntArray, endOffsets: IntArray): List<TextBlockTransferableData> {
|
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()
|
return emptyList()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun extractTransferableData(content: Transferable): List<TextBlockTransferableData> {
|
override fun extractTransferableData(content: Transferable): List<TextBlockTransferableData> {
|
||||||
try {
|
try {
|
||||||
if (content.isDataFlavorSupported(DataFlavor.stringFlavor)) {
|
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) }) {
|
if (content.isDataFlavorSupported(CopiedKotlinCode.DATA_FLAVOR) ||
|
||||||
return emptyList()
|
/* Handled by ConvertJavaCopyPasteProcessor */
|
||||||
}
|
content.isDataFlavorSupported(CopiedJavaCode.DATA_FLAVOR)) return emptyList()
|
||||||
|
|
||||||
val text = content.getTransferData(DataFlavor.stringFlavor) as String
|
val text = content.getTransferData(DataFlavor.stringFlavor) as String
|
||||||
return listOf(MyTransferableData(text))
|
return listOf(MyTransferableData(text))
|
||||||
@@ -284,6 +285,5 @@ class ConvertTextJavaCopyPasteProcessor : CopyPastePostProcessor<TextBlockTransf
|
|||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@TestOnly var conversionPerformed: Boolean = false
|
@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")
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-7
@@ -63,14 +63,8 @@ abstract class AbstractTextJavaToKotlinCopyPasteConversionTest : AbstractCopyPas
|
|||||||
configureTargetFile(testName + ".to.kt")
|
configureTargetFile(testName + ".to.kt")
|
||||||
|
|
||||||
ConvertTextJavaCopyPasteProcessor.conversionPerformed = false
|
ConvertTextJavaCopyPasteProcessor.conversionPerformed = false
|
||||||
ConvertTextJavaCopyPasteProcessor.convertOnCopyInsideIDE = true
|
|
||||||
|
|
||||||
try {
|
myFixture.performEditorAction(IdeActions.ACTION_PASTE)
|
||||||
myFixture.performEditorAction(IdeActions.ACTION_PASTE)
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
ConvertTextJavaCopyPasteProcessor.convertOnCopyInsideIDE = false
|
|
||||||
}
|
|
||||||
|
|
||||||
kotlin.test.assertEquals(noConversionExpected, !ConvertTextJavaCopyPasteProcessor.conversionPerformed,
|
kotlin.test.assertEquals(noConversionExpected, !ConvertTextJavaCopyPasteProcessor.conversionPerformed,
|
||||||
if (noConversionExpected) "Conversion to Kotlin should not be suggested" else "No conversion to Kotlin suggested")
|
if (noConversionExpected) "Conversion to Kotlin should not be suggested" else "No conversion to Kotlin suggested")
|
||||||
|
|||||||
Reference in New Issue
Block a user