Intention action to convert string to raw string

(half of KT-5208)
This commit is contained in:
Valentin Kipyatkov
2016-02-20 14:05:13 +01:00
parent 86c551b27b
commit 3d422b47cb
24 changed files with 189 additions and 0 deletions
+5
View File
@@ -1189,6 +1189,11 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ToRawStringLiteralIntention</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,80 @@
/*
* 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.StringUtilRt
import org.jetbrains.kotlin.psi.KtEscapeStringTemplateEntry
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.KtStringTemplateEntry
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
class ToRawStringLiteralIntention : SelfTargetingOffsetIndependentIntention<KtStringTemplateExpression>(
KtStringTemplateExpression::class.java,
"To raw string literal"
), LowPriorityAction {
override fun isApplicableTo(element: KtStringTemplateExpression): Boolean {
val text = element.text
if (text.startsWith("\"\"\"")) return false // already raw
val escapeEntries = element.entries.filterIsInstance<KtEscapeStringTemplateEntry>()
for (entry in escapeEntries) {
val c = entry.unescapedValue.singleOrNull() ?: return false
if (Character.isISOControl(c) && c != '\n' && c != '\r') return false
}
val converted = convertContent(element)
return !converted.contains("\"\"\"") && !hasTrailingSpaces(converted)
}
override fun applyTo(element: KtStringTemplateExpression, editor: Editor?) {
val text = convertContent(element)
element.replace(KtPsiFactory(element).createExpression("\"\"\"" + text + "\"\"\""))
}
private fun convertContent(element: KtStringTemplateExpression): String {
val text = buildString {
val entries = element.entries
for ((index, entry) in entries.withIndex()) {
val value = entry.value()
if (value.endsWith("$") && index < entries.size - 1) {
val nextChar = entries[index + 1].value().first()
if (nextChar.isJavaIdentifierStart() || nextChar == '{') {
append("\${\"$\"}")
continue
}
}
append(value)
}
}
return StringUtilRt.convertLineSeparators(text, "\n")
}
private fun hasTrailingSpaces(text: String): Boolean {
var afterSpace = true
for (c in text) {
if ((c == '\n' || c == '\r') && afterSpace) return true
afterSpace = c == ' ' || c == '\t'
}
return false
}
private fun KtStringTemplateEntry.value() = if (this is KtEscapeStringTemplateEntry) this.unescapedValue else text
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.ToRawStringLiteralIntention
@@ -0,0 +1,2 @@
// IS_APPLICABLE: false
val v = <caret>"\"\"\"Hello, world!\"\"\""
@@ -0,0 +1,2 @@
// IS_APPLICABLE: false
val v = <caret>""" """
+1
View File
@@ -0,0 +1 @@
val v = <caret>"\$abc"
@@ -0,0 +1 @@
val v = <caret>"""${"$"}abc"""
@@ -0,0 +1 @@
val v = <caret>" $\u0041 $ \${} $"
@@ -0,0 +1 @@
val v = """ ${"$"}A $ ${"$"}{} $"""
@@ -0,0 +1 @@
val v = <caret>"\"Hello, world!\" \\ \'a\'"
@@ -0,0 +1 @@
val v = <caret>""""Hello, world!" \ 'a'"""
+1
View File
@@ -0,0 +1 @@
val v = <caret>"\"\\Hello,\nworld!\\\""
@@ -0,0 +1,2 @@
val v = <caret>""""\Hello,
world!\""""
@@ -0,0 +1,2 @@
// IS_APPLICABLE: false
val v = <caret>"\u0000"
@@ -0,0 +1,2 @@
// IS_APPLICABLE: false
val v = <caret>"\t"
@@ -0,0 +1,2 @@
// IS_APPLICABLE: false
val v = <caret>"\"\\Hello, \nworld!\\\""
@@ -0,0 +1 @@
val v = <caret>"Hello, world!\n "
@@ -0,0 +1,2 @@
val v = <caret>"""Hello, world!
"""
@@ -0,0 +1,2 @@
// IS_APPLICABLE: false
val v = <caret>"\n Hello, world!"
@@ -1,6 +1,7 @@
// FILE: first.before.kt
// "Import" "false"
// ERROR: Missing 'getValue(Nothing?, KProperty<*>)' method on delegate of type 'String'
// ACTION: To raw string literal
package b
@@ -3,6 +3,7 @@
// ACTION: Add parameter to constructor 'Foo'
// ACTION: Create secondary constructor
// ERROR: Too many arguments for public constructor Foo(a: Int) defined in Foo
// ACTION: To raw string literal
class Foo(a: Int)
@@ -3,5 +3,6 @@
// ACTION: Create test
// ACTION: Make internal
// ACTION: Make private
// ACTION: To raw string literal
@ArrAnn(<caret>"123") class My
@@ -1,4 +1,5 @@
// "Change parameter 'z' type of function 'foo' to '(Int) -> String'" "false"
// ACTION: To raw string literal
fun foo(y: Int = 0, z: (Int) -> String = {""}) {
foo {
@@ -9231,6 +9231,81 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/toRawStringLiteral")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ToRawStringLiteral extends AbstractIntentionTest {
@TestMetadata("3quotes.kt")
public void test3quotes() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/toRawStringLiteral/3quotes.kt");
doTest(fileName);
}
public void testAllFilesPresentInToRawStringLiteral() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/toRawStringLiteral"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("alreadyRaw.kt")
public void testAlreadyRaw() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/toRawStringLiteral/alreadyRaw.kt");
doTest(fileName);
}
@TestMetadata("dollar.kt")
public void testDollar() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/toRawStringLiteral/dollar.kt");
doTest(fileName);
}
@TestMetadata("dollar2.kt")
public void testDollar2() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/toRawStringLiteral/dollar2.kt");
doTest(fileName);
}
@TestMetadata("quotesAndSlashes.kt")
public void testQuotesAndSlashes() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/toRawStringLiteral/quotesAndSlashes.kt");
doTest(fileName);
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/toRawStringLiteral/simple.kt");
doTest(fileName);
}
@TestMetadata("specialChar.kt")
public void testSpecialChar() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/toRawStringLiteral/specialChar.kt");
doTest(fileName);
}
@TestMetadata("tabCharacter.kt")
public void testTabCharacter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/toRawStringLiteral/tabCharacter.kt");
doTest(fileName);
}
@TestMetadata("trailingSpace.kt")
public void testTrailingSpace() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/toRawStringLiteral/trailingSpace.kt");
doTest(fileName);
}
@TestMetadata("trailingSpace2.kt")
public void testTrailingSpace2() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/toRawStringLiteral/trailingSpace2.kt");
doTest(fileName);
}
@TestMetadata("trailingSpace3.kt")
public void testTrailingSpace3() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/toRawStringLiteral/trailingSpace3.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/intentions/usePropertyAccessSyntax")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)