Minor changes after code review
This commit is contained in:
+17
-13
@@ -44,6 +44,7 @@ import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||
import org.jetbrains.kotlin.psi.KtStringTemplateEntryWithExpression
|
||||
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
|
||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
|
||||
import java.awt.datatransfer.Transferable
|
||||
import java.util.*
|
||||
@@ -71,11 +72,9 @@ class ConvertJavaCopyPasteProcessor : CopyPastePostProcessor<TextBlockTransferab
|
||||
|
||||
override fun processTransferableData(project: Project, editor: Editor, bounds: RangeMarker, caretOffset: Int, indented: Ref<Boolean>, values: List<TextBlockTransferableData>) {
|
||||
if (DumbService.getInstance(project).isDumb) return
|
||||
val jetEditorOptions = KotlinEditorOptions.getInstance()
|
||||
if (!jetEditorOptions.isEnableJavaToKotlinConversion) return
|
||||
if (!KotlinEditorOptions.getInstance().isEnableJavaToKotlinConversion) return
|
||||
|
||||
val data = values.single()
|
||||
if (data !is CopiedJavaCode) return
|
||||
val data = values.single() as CopiedJavaCode
|
||||
|
||||
val document = editor.document
|
||||
val targetFile = PsiDocumentManager.getInstance(project).getPsiFile(document) as? KtFile ?: return
|
||||
@@ -128,8 +127,7 @@ class ConvertJavaCopyPasteProcessor : CopyPastePostProcessor<TextBlockTransferab
|
||||
if (doConversionAndInsertImportsIfUnchanged()) return
|
||||
}
|
||||
|
||||
val needConvert = jetEditorOptions.isDonTShowConversionDialog || okFromDialog(project)
|
||||
if (needConvert) {
|
||||
if (confirmConvertJavaOnPaste(project, isPlainText = false)) {
|
||||
if (conversionResult == null) {
|
||||
if (doConversionAndInsertImportsIfUnchanged()) return
|
||||
}
|
||||
@@ -187,12 +185,6 @@ class ConvertJavaCopyPasteProcessor : CopyPastePostProcessor<TextBlockTransferab
|
||||
}
|
||||
}
|
||||
|
||||
private fun okFromDialog(project: Project): Boolean {
|
||||
val dialog = KotlinPasteFromJavaDialog(project, false)
|
||||
dialog.show()
|
||||
return dialog.isOK
|
||||
}
|
||||
|
||||
companion object {
|
||||
@TestOnly var conversionPerformed: Boolean = false
|
||||
}
|
||||
@@ -251,10 +243,22 @@ internal fun convertCopiedCodeToKotlin(elementsAndTexts: Collection<Any>, projec
|
||||
internal fun isNoConversionPosition(file: KtFile, offset: Int): Boolean {
|
||||
if (offset == 0) return false
|
||||
val token = file.findElementAt(offset - 1)!!
|
||||
|
||||
if (token !is PsiWhiteSpace && token.endOffset != offset) return true // pasting into the middle of token
|
||||
|
||||
for (element in token.parentsWithSelf) {
|
||||
if (element is PsiComment) return true
|
||||
if (element is KtStringTemplateEntryWithExpression) return false
|
||||
if (element is KtStringTemplateExpression) return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
internal fun confirmConvertJavaOnPaste(project: Project, isPlainText: Boolean): Boolean {
|
||||
if (KotlinEditorOptions.getInstance().isDonTShowConversionDialog) return true
|
||||
|
||||
val dialog = KotlinPasteFromJavaDialog(project, isPlainText)
|
||||
dialog.show()
|
||||
return dialog.isOK
|
||||
}
|
||||
|
||||
|
||||
+8
-29
@@ -48,18 +48,6 @@ import org.jetbrains.kotlin.utils.addToStdlib.check
|
||||
import java.awt.datatransfer.DataFlavor
|
||||
import java.awt.datatransfer.Transferable
|
||||
|
||||
class CopiedKotlinCodeTransferableData : 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(CopiedKotlinCodeTransferableData::class.java, "class: KotlinCodeTransferableData")
|
||||
}
|
||||
}
|
||||
|
||||
class ConvertTextJavaCopyPasteProcessor : CopyPastePostProcessor<TextBlockTransferableData>() {
|
||||
private val LOG = Logger.getInstance(ConvertTextJavaCopyPasteProcessor::class.java)
|
||||
|
||||
@@ -77,17 +65,16 @@ class ConvertTextJavaCopyPasteProcessor : CopyPastePostProcessor<TextBlockTransf
|
||||
}
|
||||
|
||||
override fun collectTransferableData(file: PsiFile, editor: Editor, startOffsets: IntArray, endOffsets: IntArray): List<TextBlockTransferableData> {
|
||||
if (file is KtFile) {
|
||||
return listOf(CopiedKotlinCodeTransferableData())
|
||||
}
|
||||
return emptyList()
|
||||
}
|
||||
|
||||
override fun extractTransferableData(content: Transferable): List<TextBlockTransferableData> {
|
||||
try {
|
||||
if (content.isDataFlavorSupported(DataFlavor.stringFlavor)) {
|
||||
if (content.isDataFlavorSupported(CopiedJavaCode.DATA_FLAVOR)) return emptyList() // it's handled by ConvertJavaCopyPasteProcessor
|
||||
if (content.isDataFlavorSupported(CopiedKotlinCodeTransferableData.DATA_FLAVOR)) return emptyList() // just an optimization
|
||||
// check if it's copied from within IDEA
|
||||
if (!Companion.convertOnCopyInsideIDE && content.transferDataFlavors.any { TextBlockTransferableData::class.java.isAssignableFrom(it.representationClass) }) {
|
||||
return emptyList()
|
||||
}
|
||||
|
||||
val text = content.getTransferData(DataFlavor.stringFlavor) as String
|
||||
return listOf(MyTransferableData(text))
|
||||
@@ -101,11 +88,9 @@ class ConvertTextJavaCopyPasteProcessor : CopyPastePostProcessor<TextBlockTransf
|
||||
|
||||
override fun processTransferableData(project: Project, editor: Editor, bounds: RangeMarker, caretOffset: Int, indented: Ref<Boolean>, values: List<TextBlockTransferableData>) {
|
||||
if (DumbService.getInstance(project).isDumb) return
|
||||
val options = KotlinEditorOptions.getInstance()
|
||||
if (!options.isEnableJavaToKotlinConversion) return //TODO: use another option?
|
||||
if (!KotlinEditorOptions.getInstance().isEnableJavaToKotlinConversion) return //TODO: use another option?
|
||||
|
||||
val data = values.single() as MyTransferableData
|
||||
val text = data.text
|
||||
val text = (values.single() as MyTransferableData).text
|
||||
|
||||
val psiDocumentManager = PsiDocumentManager.getInstance(project)
|
||||
psiDocumentManager.commitDocument(editor.document)
|
||||
@@ -115,8 +100,7 @@ class ConvertTextJavaCopyPasteProcessor : CopyPastePostProcessor<TextBlockTransf
|
||||
|
||||
val conversionContext = detectConversionContext(pasteContext, text, project) ?: return
|
||||
|
||||
val needConvert = options.isDonTShowConversionDialog || okFromDialog(project)
|
||||
if (!needConvert) return
|
||||
if (!confirmConvertJavaOnPaste(project, isPlainText = true)) return
|
||||
|
||||
val convertedText = convertCodeToKotlin(text, conversionContext, project)
|
||||
|
||||
@@ -240,13 +224,8 @@ class ConvertTextJavaCopyPasteProcessor : CopyPastePostProcessor<TextBlockTransf
|
||||
return CopiedJavaCode(fileText, intArrayOf(index), intArrayOf(index + text.length))
|
||||
}
|
||||
|
||||
private fun okFromDialog(project: Project): Boolean {
|
||||
val dialog = KotlinPasteFromJavaDialog(project, true)
|
||||
dialog.show()
|
||||
return dialog.isOK
|
||||
}
|
||||
|
||||
companion object {
|
||||
@TestOnly var conversionPerformed: Boolean = false
|
||||
@TestOnly var convertOnCopyInsideIDE: Boolean = false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
b<caret>ar()
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
b<caret>ar()
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
ArrayList<String> list = new ArrayList<String>();
|
||||
// NO_CONVERSION_EXPECTED
|
||||
+9
-4
@@ -24,7 +24,6 @@ import org.jetbrains.kotlin.idea.test.PluginTestCaseBase
|
||||
import org.jetbrains.kotlin.test.InTextDirectivesUtils
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils
|
||||
import java.io.File
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
abstract class AbstractTextJavaToKotlinCopyPasteConversionTest : AbstractCopyPasteTest() {
|
||||
private val BASE_PATH = PluginTestCaseBase.getTestDataPathBase() + "/copyPaste/plainTextConversion"
|
||||
@@ -61,11 +60,17 @@ abstract class AbstractTextJavaToKotlinCopyPasteConversionTest : AbstractCopyPas
|
||||
configureTargetFile(testName + ".to.kt")
|
||||
|
||||
ConvertTextJavaCopyPasteProcessor.conversionPerformed = false
|
||||
ConvertTextJavaCopyPasteProcessor.convertOnCopyInsideIDE = true
|
||||
|
||||
myFixture.performEditorAction(IdeActions.ACTION_PASTE)
|
||||
try {
|
||||
myFixture.performEditorAction(IdeActions.ACTION_PASTE)
|
||||
}
|
||||
finally {
|
||||
ConvertTextJavaCopyPasteProcessor.convertOnCopyInsideIDE = false
|
||||
}
|
||||
|
||||
assertEquals(noConversionExpected, !ConvertTextJavaCopyPasteProcessor.conversionPerformed,
|
||||
if (noConversionExpected) "Conversion to Kotlin should not be suggested" else "No conversion to Kotlin suggested")
|
||||
kotlin.test.assertEquals(noConversionExpected, !ConvertTextJavaCopyPasteProcessor.conversionPerformed,
|
||||
if (noConversionExpected) "Conversion to Kotlin should not be suggested" else "No conversion to Kotlin suggested")
|
||||
|
||||
KotlinTestUtils.assertEqualsToFile(File(path.replace(".txt", ".expected.kt")), myFixture.file.text)
|
||||
}
|
||||
|
||||
+6
@@ -47,6 +47,12 @@ public class TextJavaToKotlinCopyPasteConversionTestGenerated extends AbstractTe
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("InsideIdentifier.txt")
|
||||
public void testInsideIdentifier() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/copyPaste/plainTextConversion/InsideIdentifier.txt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("IntoComment.txt")
|
||||
public void testIntoComment() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/copyPaste/plainTextConversion/IntoComment.txt");
|
||||
|
||||
Reference in New Issue
Block a user