"To raw/ordinary string literal" intention: Move cursor to same offset after conversion #KT-18106 Fixed (#1675)

This commit is contained in:
Toshiaki Kameyama
2018-06-21 21:56:18 +09:00
committed by Vyacheslav Gerasimov
parent 51c01c15b8
commit 5a4cba7528
31 changed files with 133 additions and 8 deletions
@@ -19,19 +19,26 @@ package org.jetbrains.kotlin.idea.intentions
import com.intellij.codeInsight.intention.LowPriorityAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.util.text.StringUtil
import org.jetbrains.kotlin.idea.core.replaced
import org.jetbrains.kotlin.psi.KtLiteralStringTemplateEntry
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.startOffset
class ToOrdinaryStringLiteralIntention : SelfTargetingOffsetIndependentIntention<KtStringTemplateExpression>(
KtStringTemplateExpression::class.java,
"To ordinary string literal"
KtStringTemplateExpression::class.java,
"To ordinary string literal"
), LowPriorityAction {
override fun isApplicableTo(element: KtStringTemplateExpression): Boolean {
return element.text.startsWith("\"\"\"")
}
override fun applyTo(element: KtStringTemplateExpression, editor: Editor?) {
val startOffset = element.startOffset
val endOffset = element.endOffset
val currentOffset = editor?.caretModel?.currentCaret?.offset ?: startOffset
val text = buildString {
append("\"")
@@ -42,14 +49,20 @@ class ToOrdinaryStringLiteralIntention : SelfTargetingOffsetIndependentIntention
text = text.replace("\"", "\\\"")
text = StringUtil.convertLineSeparators(text, "\\n")
append(text)
}
else {
} else {
append(entry.text)
}
}
append("\"")
}
element.replace(KtPsiFactory(element).createExpression(text))
val replaced = element.replaced(KtPsiFactory(element).createExpression(text))
val offset = when {
currentOffset - startOffset < 2 -> startOffset
endOffset - currentOffset < 2 -> replaced.endOffset
else -> maxOf(currentOffset - 2, replaced.startOffset)
}
editor?.caretModel?.moveToOffset(offset)
}
}
@@ -19,14 +19,17 @@ package org.jetbrains.kotlin.idea.intentions
import com.intellij.codeInsight.intention.LowPriorityAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.util.text.StringUtilRt
import org.jetbrains.kotlin.idea.core.replaced
import org.jetbrains.kotlin.psi.KtEscapeStringTemplateEntry
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.KtStringTemplateEntry
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.startOffset
class ToRawStringLiteralIntention : SelfTargetingOffsetIndependentIntention<KtStringTemplateExpression>(
KtStringTemplateExpression::class.java,
"To raw string literal"
KtStringTemplateExpression::class.java,
"To raw string literal"
), LowPriorityAction {
override fun isApplicableTo(element: KtStringTemplateExpression): Boolean {
val text = element.text
@@ -43,8 +46,19 @@ class ToRawStringLiteralIntention : SelfTargetingOffsetIndependentIntention<KtSt
}
override fun applyTo(element: KtStringTemplateExpression, editor: Editor?) {
val startOffset = element.startOffset
val endOffset = element.endOffset
val currentOffset = editor?.caretModel?.currentCaret?.offset ?: startOffset
val text = convertContent(element)
element.replace(KtPsiFactory(element).createExpression("\"\"\"" + text + "\"\"\""))
val replaced = element.replaced(KtPsiFactory(element).createExpression("\"\"\"" + text + "\"\"\""))
val offset = when {
startOffset == currentOffset -> startOffset
endOffset == currentOffset -> replaced.endOffset
else -> minOf(currentOffset + 2, replaced.endOffset)
}
editor?.caretModel?.moveToOffset(offset)
}
private fun convertContent(element: KtStringTemplateExpression): String {
@@ -0,0 +1 @@
val s = <caret>"""foo bar baz"""
@@ -0,0 +1 @@
val s = <caret>"foo bar baz"
@@ -0,0 +1 @@
val s = "<caret>""foo bar baz"""
@@ -0,0 +1 @@
val s = <caret>"foo bar baz"
@@ -0,0 +1 @@
val s = ""<caret>"foo bar baz"""
@@ -0,0 +1 @@
val s = <caret>"foo bar baz"
@@ -0,0 +1 @@
val s = """<caret>foo bar baz"""
@@ -0,0 +1 @@
val s = "<caret>foo bar baz"
@@ -0,0 +1 @@
val s = """foo <caret>bar baz"""
@@ -0,0 +1 @@
val s = "foo <caret>bar baz"
@@ -0,0 +1 @@
val s = """foo bar baz<caret>"""
@@ -0,0 +1 @@
val s = "foo bar baz<caret>"
@@ -0,0 +1 @@
val s = """foo bar baz"<caret>""
@@ -0,0 +1 @@
val s = "foo bar baz"<caret>
@@ -0,0 +1 @@
val s = """foo bar baz""<caret>"
@@ -0,0 +1 @@
val s = "foo bar baz"<caret>
@@ -0,0 +1 @@
val s = """foo bar baz"""<caret>
@@ -0,0 +1 @@
val s = "foo bar baz"<caret>
+1
View File
@@ -0,0 +1 @@
val s = <caret>"foo bar baz"
@@ -0,0 +1 @@
val s = <caret>"""foo bar baz"""
+1
View File
@@ -0,0 +1 @@
val s = "<caret>foo bar baz"
@@ -0,0 +1 @@
val s = """<caret>foo bar baz"""
+1
View File
@@ -0,0 +1 @@
val s = "foo <caret>bar baz"
@@ -0,0 +1 @@
val s = """foo <caret>bar baz"""
+1
View File
@@ -0,0 +1 @@
val s = "foo bar baz<caret>"
@@ -0,0 +1 @@
val s = """foo bar baz<caret>"""
+1
View File
@@ -0,0 +1 @@
val s = "foo bar baz"<caret>
@@ -0,0 +1 @@
val s = """foo bar baz"""<caret>
@@ -15471,6 +15471,51 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/toOrdinaryStringLiteral"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("caret1.kt")
public void testCaret1() throws Exception {
runTest("idea/testData/intentions/toOrdinaryStringLiteral/caret1.kt");
}
@TestMetadata("caret2.kt")
public void testCaret2() throws Exception {
runTest("idea/testData/intentions/toOrdinaryStringLiteral/caret2.kt");
}
@TestMetadata("caret3.kt")
public void testCaret3() throws Exception {
runTest("idea/testData/intentions/toOrdinaryStringLiteral/caret3.kt");
}
@TestMetadata("caret4.kt")
public void testCaret4() throws Exception {
runTest("idea/testData/intentions/toOrdinaryStringLiteral/caret4.kt");
}
@TestMetadata("caret5.kt")
public void testCaret5() throws Exception {
runTest("idea/testData/intentions/toOrdinaryStringLiteral/caret5.kt");
}
@TestMetadata("caret6.kt")
public void testCaret6() throws Exception {
runTest("idea/testData/intentions/toOrdinaryStringLiteral/caret6.kt");
}
@TestMetadata("caret7.kt")
public void testCaret7() throws Exception {
runTest("idea/testData/intentions/toOrdinaryStringLiteral/caret7.kt");
}
@TestMetadata("caret8.kt")
public void testCaret8() throws Exception {
runTest("idea/testData/intentions/toOrdinaryStringLiteral/caret8.kt");
}
@TestMetadata("caret9.kt")
public void testCaret9() throws Exception {
runTest("idea/testData/intentions/toOrdinaryStringLiteral/caret9.kt");
}
@TestMetadata("lineBreakInExpression.kt")
public void testLineBreakInExpression() throws Exception {
runTest("idea/testData/intentions/toOrdinaryStringLiteral/lineBreakInExpression.kt");
@@ -15509,6 +15554,31 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
runTest("idea/testData/intentions/toRawStringLiteral/alreadyRaw.kt");
}
@TestMetadata("caret1.kt")
public void testCaret1() throws Exception {
runTest("idea/testData/intentions/toRawStringLiteral/caret1.kt");
}
@TestMetadata("caret2.kt")
public void testCaret2() throws Exception {
runTest("idea/testData/intentions/toRawStringLiteral/caret2.kt");
}
@TestMetadata("caret3.kt")
public void testCaret3() throws Exception {
runTest("idea/testData/intentions/toRawStringLiteral/caret3.kt");
}
@TestMetadata("caret4.kt")
public void testCaret4() throws Exception {
runTest("idea/testData/intentions/toRawStringLiteral/caret4.kt");
}
@TestMetadata("caret5.kt")
public void testCaret5() throws Exception {
runTest("idea/testData/intentions/toRawStringLiteral/caret5.kt");
}
@TestMetadata("dollar.kt")
public void testDollar() throws Exception {
runTest("idea/testData/intentions/toRawStringLiteral/dollar.kt");