Add intention to indent a raw string #KT-9943 Fixed

This commit is contained in:
Toshiaki Kameyama
2018-04-19 12:07:29 +03:00
committed by Mikhail Glukhikh
parent a2d45062c2
commit a7110a1517
18 changed files with 171 additions and 0 deletions
@@ -0,0 +1,7 @@
fun test() {
val s = <spot>"""
foo
bar
baz
""".trimIndent()</spot>
}
@@ -0,0 +1,5 @@
fun test() {
val s = <spot>"""foo
bar
baz"""</spot>
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention indents a raw string.
</body>
</html>
+5
View File
@@ -1596,6 +1596,11 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.IndentRawStringIntention</className>
<category>Kotlin</category>
</intentionAction>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
displayName="Object literal can be converted to lambda"
groupPath="Kotlin"
+5
View File
@@ -1596,6 +1596,11 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.IndentRawStringIntention</className>
<category>Kotlin</category>
</intentionAction>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
displayName="Object literal can be converted to lambda"
groupPath="Kotlin"
+5
View File
@@ -1597,6 +1597,11 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.IndentRawStringIntention</className>
<category>Kotlin</category>
</intentionAction>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
displayName="Object literal can be converted to lambda"
groupPath="Kotlin"
+5
View File
@@ -1597,6 +1597,11 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.IndentRawStringIntention</className>
<category>Kotlin</category>
</intentionAction>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
displayName="Object literal can be converted to lambda"
groupPath="Kotlin"
+5
View File
@@ -1596,6 +1596,11 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.IndentRawStringIntention</className>
<category>Kotlin</category>
</intentionAction>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
displayName="Object literal can be converted to lambda"
groupPath="Kotlin"
+5
View File
@@ -1596,6 +1596,11 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.IndentRawStringIntention</className>
<category>Kotlin</category>
</intentionAction>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
displayName="Object literal can be converted to lambda"
groupPath="Kotlin"
@@ -0,0 +1,49 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor
import com.intellij.psi.codeStyle.CodeStyleManager
import com.intellij.psi.codeStyle.CodeStyleSettingsManager
import org.jetbrains.kotlin.idea.KotlinFileType
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForReceiver
import org.jetbrains.kotlin.psi.psiUtil.startOffset
class IndentRawStringIntention : SelfTargetingIntention<KtStringTemplateExpression>(
KtStringTemplateExpression::class.java, "Indent raw string"
) {
override fun isApplicableTo(element: KtStringTemplateExpression, caretOffset: Int): Boolean {
if (!element.text.startsWith("\"\"\"")) return false
if (element.getQualifiedExpressionForReceiver() != null) return false
val entries = element.entries
if (entries.size <= 1 || entries.any { it.text.startsWith(" ") || it.text.startsWith("\t") }) return false
return true
}
override fun applyTo(element: KtStringTemplateExpression, editor: Editor?) {
val file = element.containingKtFile
val project = file.project
val indentOptions = CodeStyleSettingsManager.getInstance(project).currentSettings.getIndentOptions(KotlinFileType.INSTANCE)
val parentIndent = CodeStyleManager.getInstance(project).getLineIndent(file, element.parent.startOffset) ?: ""
val indent = if (indentOptions.USE_TAB_CHARACTER) "$parentIndent\t" else "$parentIndent${" ".repeat(indentOptions.INDENT_SIZE)}"
val newString = buildString {
val maxIndex = element.entries.size - 1
element.entries.forEachIndexed { index, entry ->
if (index == 0) append("\n$indent")
append(entry.text)
if (entry.text == "\n") append(indent)
if (index == maxIndex) append("\n$indent")
}
}
element.replace(KtPsiFactory(element).createExpression("\"\"\"$newString\"\"\".trimIndent()"))
}
}
+1
View File
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.IndentRawStringIntention
+6
View File
@@ -0,0 +1,6 @@
// IS_APPLICABLE: false
fun test() {
val foo = <caret>"""foo
bar
baz"""
}
@@ -0,0 +1,4 @@
// IS_APPLICABLE: false
fun test() {
val foo = <caret>"foo"
}
+7
View File
@@ -0,0 +1,7 @@
// IS_APPLICABLE: false
// WITH_RUNTIME
fun test() {
val foo = <caret>"""foo
bar
baz""".trimIndent()
}
+6
View File
@@ -0,0 +1,6 @@
// WITH_RUNTIME
fun test() {
val foo = <caret>"""foo
bar
baz"""
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
fun test() {
val foo = """
foo
bar
baz
""".trimIndent()
}
@@ -0,0 +1,4 @@
// IS_APPLICABLE: false
fun test() {
val foo = <caret>"""foo"""
}
@@ -8387,6 +8387,45 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/indentRawString")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class IndentRawString extends AbstractIntentionTest {
public void testAllFilesPresentInIndentRawString() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/indentRawString"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("hasIndent.kt")
public void testHasIndent() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/indentRawString/hasIndent.kt");
doTest(fileName);
}
@TestMetadata("notRawString.kt")
public void testNotRawString() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/indentRawString/notRawString.kt");
doTest(fileName);
}
@TestMetadata("receiver.kt")
public void testReceiver() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/indentRawString/receiver.kt");
doTest(fileName);
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/indentRawString/simple.kt");
doTest(fileName);
}
@TestMetadata("singleLine.kt")
public void testSingleLine() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/indentRawString/singleLine.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/intentions/infixCallToOrdinary")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)