diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinLiteralCopyPasteProcessor.kt b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinLiteralCopyPasteProcessor.kt index 3552a149f70..9da6ea1cdc1 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinLiteralCopyPasteProcessor.kt +++ b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinLiteralCopyPasteProcessor.kt @@ -28,7 +28,6 @@ import com.intellij.psi.PsiElement import com.intellij.psi.PsiFile import org.jetbrains.annotations.TestOnly import org.jetbrains.kotlin.idea.editor.fixers.range -import org.jetbrains.kotlin.idea.inspections.collections.isCalling import org.jetbrains.kotlin.idea.intentions.callExpression import org.jetbrains.kotlin.lexer.KotlinLexer import org.jetbrains.kotlin.lexer.KtTokens @@ -132,11 +131,13 @@ class KotlinLiteralCopyPasteProcessor : CopyPastePreProcessor { return text } + val templateTokenSequence = TemplateTokenSequence(text) + return if (beginTp.isSingleQuoted()) { val res = StringBuilder() val lineBreak = "\\n\"+\n \"" var endsInLineBreak = false - TemplateTokenSequence(text).forEach { + templateTokenSequence.forEach { when (it) { is LiteralChunk -> StringUtil.escapeStringCharacters(it.text.length, it.text, "\$\"", res) is EntryChunk -> res.append(it.text) @@ -150,9 +151,16 @@ class KotlinLiteralCopyPasteProcessor : CopyPastePreProcessor { res.toString() } } else { + fun TemplateChunk?.indent() = when (this) { + is LiteralChunk -> this.text + is EntryChunk -> this.text + else -> "" + }.takeWhile { it.isWhitespace() } + val indent = if (beginTp.firstChild?.text == "\"\"\"" && - beginTp.getQualifiedExpressionForReceiver()?.callExpression?.calleeExpression?.text == "trimIndent" + beginTp.getQualifiedExpressionForReceiver()?.callExpression?.calleeExpression?.text == "trimIndent" && + templateTokenSequence.firstOrNull()?.indent() == templateTokenSequence.lastOrNull()?.indent() ) { begin.parent?.prevSibling?.takeIf { it.text != "\n" }?.text } else { @@ -160,7 +168,7 @@ class KotlinLiteralCopyPasteProcessor : CopyPastePreProcessor { } ?: "" val tripleQuoteRe = Regex("[\"]{3,}") - TemplateTokenSequence(text).mapIndexed { index, chunk -> + templateTokenSequence.mapIndexed { index, chunk -> when (chunk) { is LiteralChunk -> { val replaced = chunk.text.replace("\$", "\${'$'}").let { escapedDollar -> diff --git a/idea/testData/copyPaste/plainTextLiteral/TrimIndent3.expected.kt b/idea/testData/copyPaste/plainTextLiteral/TrimIndent3.expected.kt new file mode 100644 index 00000000000..c3222acdb64 --- /dev/null +++ b/idea/testData/copyPaste/plainTextLiteral/TrimIndent3.expected.kt @@ -0,0 +1,4 @@ +val ti = """ + fun indented() { + } +""".trimIndent() \ No newline at end of file diff --git a/idea/testData/copyPaste/plainTextLiteral/TrimIndent3.kt b/idea/testData/copyPaste/plainTextLiteral/TrimIndent3.kt new file mode 100644 index 00000000000..80f179ae4df --- /dev/null +++ b/idea/testData/copyPaste/plainTextLiteral/TrimIndent3.kt @@ -0,0 +1,3 @@ +val ti = """ + +""".trimIndent() \ No newline at end of file diff --git a/idea/testData/copyPaste/plainTextLiteral/TrimIndent3.txt b/idea/testData/copyPaste/plainTextLiteral/TrimIndent3.txt new file mode 100644 index 00000000000..98146c61a91 --- /dev/null +++ b/idea/testData/copyPaste/plainTextLiteral/TrimIndent3.txt @@ -0,0 +1,2 @@ +fun indented() { + } \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/conversion/copy/LiteralTextToKotlinCopyPasteTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/conversion/copy/LiteralTextToKotlinCopyPasteTestGenerated.java index e6a95cf60b0..735c1794550 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/conversion/copy/LiteralTextToKotlinCopyPasteTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/conversion/copy/LiteralTextToKotlinCopyPasteTestGenerated.java @@ -73,6 +73,11 @@ public class LiteralTextToKotlinCopyPasteTestGenerated extends AbstractLiteralTe runTest("idea/testData/copyPaste/plainTextLiteral/TrimIndent2.txt"); } + @TestMetadata("TrimIndent3.txt") + public void testTrimIndent3() throws Exception { + runTest("idea/testData/copyPaste/plainTextLiteral/TrimIndent3.txt"); + } + @TestMetadata("WithBackslashes.txt") public void testWithBackslashes() throws Exception { runTest("idea/testData/copyPaste/plainTextLiteral/WithBackslashes.txt");