Intention to convert raw string literal to ordinary one

#KT-5208 Fixed
This commit is contained in:
Valentin Kipyatkov
2016-03-03 13:09:11 +03:00
parent ecebb80bbe
commit 384183ad7b
13 changed files with 106 additions and 0 deletions
@@ -0,0 +1 @@
val s = <spot>"\"Kotlin\nis\ngood!\""</spot>
@@ -0,0 +1,3 @@
val s = <spot>""""Kotlin
is
good!""""</spot>
@@ -0,0 +1,5 @@
<html>
<body>
This intention converts a raw string literal (one surrounded with triple quote characters) to ordinary one (surrounded with single quote characters).
</body>
</html>
+5
View File
@@ -1194,6 +1194,11 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ToOrdinaryStringLiteralIntention</className>
<category>Kotlin</category>
</intentionAction>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
displayName="Object literal can be converted to lambda"
groupName="Kotlin"
@@ -0,0 +1,55 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.psi.KtLiteralStringTemplateEntry
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
class ToOrdinaryStringLiteralIntention : SelfTargetingOffsetIndependentIntention<KtStringTemplateExpression>(
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 text = buildString {
append("\"")
for (entry in element.entries) {
if (entry is KtLiteralStringTemplateEntry) {
var text = entry.text
text = text.replace("\\", "\\\\")
text = text.replace("\"", "\\\"")
text = StringUtil.convertLineSeparators(text, "\\n")
append(text)
}
else {
append(entry.text)
}
}
append("\"")
}
element.replace(KtPsiFactory(element).createExpression(text))
}
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.ToOrdinaryStringLiteralIntention
@@ -0,0 +1,2 @@
val v = <caret>"""${2 +
2}"""
@@ -0,0 +1,2 @@
val v = "${2 +
2}"
@@ -0,0 +1 @@
val v = <caret>""""Hello, world!" \ 'a'"""
@@ -0,0 +1 @@
val v = <caret>"\"Hello, world!\" \\ 'a'"
@@ -0,0 +1,2 @@
val v = <caret>""""\Hello,
world!\""""
@@ -0,0 +1 @@
val v = <caret>"\"\\Hello,\nworld!\\\""
@@ -9231,6 +9231,33 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/toOrdinaryStringLiteral")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ToOrdinaryStringLiteral extends AbstractIntentionTest {
public void testAllFilesPresentInToOrdinaryStringLiteral() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/toOrdinaryStringLiteral"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("lineBreakInExpression.kt")
public void testLineBreakInExpression() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/toOrdinaryStringLiteral/lineBreakInExpression.kt");
doTest(fileName);
}
@TestMetadata("quotesAndSlashes.kt")
public void testQuotesAndSlashes() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/toOrdinaryStringLiteral/quotesAndSlashes.kt");
doTest(fileName);
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/toOrdinaryStringLiteral/simple.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/intentions/toRawStringLiteral")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)