From a4fbe95112769149f46e7fb687ee4f6e68f74830 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Fri, 15 Dec 2017 11:24:30 +0900 Subject: [PATCH] KT-21770 Pasting into an interpolated string shouldn't escape $ --- .../kotlin/idea/editor/KotlinLiteralCopyPasteProcessor.kt | 6 ++++-- idea/testData/copyPaste/literal/DontEscapeThis.expected.kt | 3 +++ idea/testData/copyPaste/literal/DontEscapeThis.kt | 3 +++ idea/testData/copyPaste/literal/DontEscapeThis.to.kt | 3 +++ .../copy/LiteralKotlinToKotlinCopyPasteTestGenerated.java | 6 ++++++ 5 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 idea/testData/copyPaste/literal/DontEscapeThis.expected.kt create mode 100644 idea/testData/copyPaste/literal/DontEscapeThis.kt create mode 100644 idea/testData/copyPaste/literal/DontEscapeThis.to.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinLiteralCopyPasteProcessor.kt b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinLiteralCopyPasteProcessor.kt index c645dc14f8d..a017ba33bdb 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinLiteralCopyPasteProcessor.kt +++ b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinLiteralCopyPasteProcessor.kt @@ -177,7 +177,8 @@ private class TemplateTokenSequence(private val inputString: String) : Sequence< } else if (this.length > 1 && this[0] == '$') { val guessedIdentifier = substring(1) - KotlinLexer().apply { start(guessedIdentifier) }.tokenType == KtTokens.IDENTIFIER + val tokenType = KotlinLexer().apply { start(guessedIdentifier) }.tokenType + tokenType == KtTokens.IDENTIFIER || tokenType == KtTokens.THIS_KEYWORD } else { false @@ -190,7 +191,8 @@ private class TemplateTokenSequence(private val inputString: String) : Sequence< when (lexer.tokenType) { KtTokens.SHORT_TEMPLATE_ENTRY_START -> { lexer.advance() - return if (lexer.tokenType == KtTokens.IDENTIFIER) { + val tokenType = lexer.tokenType + return if (tokenType == KtTokens.IDENTIFIER || tokenType == KtTokens.THIS_KEYWORD) { from + lexer.tokenEnd - 1 } else { diff --git a/idea/testData/copyPaste/literal/DontEscapeThis.expected.kt b/idea/testData/copyPaste/literal/DontEscapeThis.expected.kt new file mode 100644 index 00000000000..a1a47a4f6c2 --- /dev/null +++ b/idea/testData/copyPaste/literal/DontEscapeThis.expected.kt @@ -0,0 +1,3 @@ +class Bar() { + val a = "begin This is $this end" +} \ No newline at end of file diff --git a/idea/testData/copyPaste/literal/DontEscapeThis.kt b/idea/testData/copyPaste/literal/DontEscapeThis.kt new file mode 100644 index 00000000000..b777e583817 --- /dev/null +++ b/idea/testData/copyPaste/literal/DontEscapeThis.kt @@ -0,0 +1,3 @@ +class Foo() { + val a = "This is $this" +} \ No newline at end of file diff --git a/idea/testData/copyPaste/literal/DontEscapeThis.to.kt b/idea/testData/copyPaste/literal/DontEscapeThis.to.kt new file mode 100644 index 00000000000..928d6230e53 --- /dev/null +++ b/idea/testData/copyPaste/literal/DontEscapeThis.to.kt @@ -0,0 +1,3 @@ +class Bar() { + val a = "begin end" +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/conversion/copy/LiteralKotlinToKotlinCopyPasteTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/conversion/copy/LiteralKotlinToKotlinCopyPasteTestGenerated.java index 94e1eb75d72..9d59f0147d9 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/conversion/copy/LiteralKotlinToKotlinCopyPasteTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/conversion/copy/LiteralKotlinToKotlinCopyPasteTestGenerated.java @@ -54,6 +54,12 @@ public class LiteralKotlinToKotlinCopyPasteTestGenerated extends AbstractLiteral doTest(fileName); } + @TestMetadata("DontEscapeThis.kt") + public void testDontEscapeThis() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/copyPaste/literal/DontEscapeThis.kt"); + doTest(fileName); + } + @TestMetadata("DontUnescapeLiteralWIthCode.kt") public void testDontUnescapeLiteralWIthCode() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/copyPaste/literal/DontUnescapeLiteralWIthCode.kt");