KT-13529 Java function is not always converted to Kotlin after paste
#KT-13529 Fixed
This commit is contained in:
@@ -39,6 +39,7 @@ import org.jetbrains.kotlin.j2k.AfterConversionPass
|
||||
import org.jetbrains.kotlin.j2k.ConverterSettings
|
||||
import org.jetbrains.kotlin.j2k.JavaToKotlinConverter
|
||||
import org.jetbrains.kotlin.j2k.ParseContext
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||
@@ -247,7 +248,9 @@ internal fun isNoConversionPosition(file: KtFile, offset: Int): Boolean {
|
||||
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 PsiComment) {
|
||||
return element.node.elementType == KtTokens.EOL_COMMENT || offset != element.endOffset
|
||||
}
|
||||
if (element is KtStringTemplateEntryWithExpression) return false
|
||||
if (element is KtStringTemplateExpression) return true
|
||||
}
|
||||
|
||||
+5
-3
@@ -31,7 +31,6 @@ import com.intellij.psi.PsiDocumentManager
|
||||
import com.intellij.psi.PsiErrorElement
|
||||
import com.intellij.psi.PsiFile
|
||||
import com.intellij.psi.PsiFileFactory
|
||||
import com.intellij.psi.util.PsiTreeUtil
|
||||
import com.intellij.util.LocalTimeCounter
|
||||
import org.jetbrains.annotations.TestOnly
|
||||
import org.jetbrains.kotlin.idea.KotlinFileType
|
||||
@@ -39,6 +38,7 @@ import org.jetbrains.kotlin.idea.editor.KotlinEditorOptions
|
||||
import org.jetbrains.kotlin.idea.j2k.J2kPostProcessor
|
||||
import org.jetbrains.kotlin.idea.util.application.runWriteAction
|
||||
import org.jetbrains.kotlin.j2k.AfterConversionPass
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.KtBlockExpression
|
||||
import org.jetbrains.kotlin.psi.KtClassBody
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
@@ -128,8 +128,10 @@ class ConvertTextJavaCopyPasteProcessor : CopyPastePostProcessor<TextBlockTransf
|
||||
val newFileText = fileText.substring(0, startOffset) + " " + dummyDeclarationText + "\n" + fileText.substring(endOffset)
|
||||
|
||||
val newFile = parseAsFile(newFileText, KotlinFileType.INSTANCE, file.project)
|
||||
val declaration = PsiTreeUtil.findElementOfClassAtRange(newFile, startOffset + 1, startOffset + 1 + dummyDeclarationText.length, KtFunction::class.java) ?: return null
|
||||
if (declaration.textLength != dummyDeclarationText.length) return null
|
||||
|
||||
val funKeyword = newFile.findElementAt(startOffset + 1) ?: return null
|
||||
if (funKeyword.node.elementType != KtTokens.FUN_KEYWORD) return null
|
||||
val declaration = funKeyword.parent as? KtFunction ?: return null
|
||||
|
||||
val parent = declaration.parent
|
||||
return when (parent) {
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.bignerdranch.beatbox.data
|
||||
|
||||
/**
|
||||
* Created by Panel on 18.08.2016.
|
||||
*/
|
||||
object BeatBox {
|
||||
private val TAG = "BeatBox"
|
||||
private val SOUNDS_FOLDER = "sample_sounds"
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.bignerdranch.beatbox.data
|
||||
|
||||
/**
|
||||
* Created by Panel on 18.08.2016.
|
||||
*/
|
||||
<caret>
|
||||
@@ -0,0 +1,4 @@
|
||||
public class BeatBox {
|
||||
private static final String TAG = "BeatBox";
|
||||
private static final String SOUNDS_FOLDER = "sample_sounds";
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
bar(/*comment*/ArrayList<String>())
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fun foo() {
|
||||
bar(/*comment*/<caret>)
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
new ArrayList<String>()
|
||||
+12
@@ -71,6 +71,18 @@ public class TextJavaToKotlinCopyPasteConversionTestGenerated extends AbstractTe
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("KT13529.txt")
|
||||
public void testKT13529() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/copyPaste/plainTextConversion/KT13529.txt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("KT13529_1.txt")
|
||||
public void testKT13529_1() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/copyPaste/plainTextConversion/KT13529_1.txt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("MembersIntoClass.txt")
|
||||
public void testMembersIntoClass() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/copyPaste/plainTextConversion/MembersIntoClass.txt");
|
||||
|
||||
Reference in New Issue
Block a user