Introduce intention: trimMargin <--> trimIndent #KT-25056 Fixed

This commit is contained in:
Toshiaki Kameyama
2018-07-03 21:22:18 +09:00
committed by Nikolay Krasko
parent df6d4f358a
commit 9e83506c02
37 changed files with 496 additions and 0 deletions
@@ -0,0 +1,7 @@
fun test() {
val x =
<spot>"""
|a
|b
""".trimMargin()</spot>
}
@@ -0,0 +1,7 @@
fun test() {
val x =
<spot>"""
a
b
""".trimIndent()</spot>
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention converts an <b>trimIndent()</b> call to an <b>trimMargin()</b> call.
</body>
</html>
@@ -0,0 +1,7 @@
fun test() {
val x =
<spot>"""
a
b
""".trimIndent()</spot>
}
@@ -0,0 +1,7 @@
fun test() {
val x =
<spot>"""
|a
|b
""".trimMargin()</spot>
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention converts an <b>trimMargin()</b> call to an <b>trimIndent()</b> call.
</body>
</html>
+10
View File
@@ -1643,6 +1643,16 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertTrimMarginToTrimIndentIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertTrimIndentToTrimMarginIntention</className>
<category>Kotlin</category>
</intentionAction>
<lang.inspectionSuppressor language="kotlin" implementationClass="org.jetbrains.kotlin.idea.inspections.KotlinInspectionSuppressor"/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
+10
View File
@@ -1640,6 +1640,16 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertTrimMarginToTrimIndentIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertTrimIndentToTrimMarginIntention</className>
<category>Kotlin</category>
</intentionAction>
<lang.inspectionSuppressor language="kotlin" implementationClass="org.jetbrains.kotlin.idea.inspections.KotlinInspectionSuppressor"/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
+10
View File
@@ -1643,6 +1643,16 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertTrimMarginToTrimIndentIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertTrimIndentToTrimMarginIntention</className>
<category>Kotlin</category>
</intentionAction>
<lang.inspectionSuppressor language="kotlin" implementationClass="org.jetbrains.kotlin.idea.inspections.KotlinInspectionSuppressor"/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
+10
View File
@@ -1644,6 +1644,16 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertTrimMarginToTrimIndentIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertTrimIndentToTrimMarginIntention</className>
<category>Kotlin</category>
</intentionAction>
<lang.inspectionSuppressor language="kotlin" implementationClass="org.jetbrains.kotlin.idea.inspections.KotlinInspectionSuppressor"/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
+10
View File
@@ -1643,6 +1643,16 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertTrimMarginToTrimIndentIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertTrimIndentToTrimMarginIntention</className>
<category>Kotlin</category>
</intentionAction>
<lang.inspectionSuppressor language="kotlin" implementationClass="org.jetbrains.kotlin.idea.inspections.KotlinInspectionSuppressor"/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
+10
View File
@@ -1643,6 +1643,16 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertTrimMarginToTrimIndentIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertTrimIndentToTrimMarginIntention</className>
<category>Kotlin</category>
</intentionAction>
<lang.inspectionSuppressor language="kotlin" implementationClass="org.jetbrains.kotlin.idea.inspections.KotlinInspectionSuppressor"/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
+10
View File
@@ -1642,6 +1642,16 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertTrimMarginToTrimIndentIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertTrimIndentToTrimMarginIntention</className>
<category>Kotlin</category>
</intentionAction>
<lang.inspectionSuppressor language="kotlin" implementationClass="org.jetbrains.kotlin.idea.inspections.KotlinInspectionSuppressor"/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
@@ -0,0 +1,53 @@
/*
* 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 org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelector
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
class ConvertTrimIndentToTrimMarginIntention : SelfTargetingIntention<KtCallExpression>(
KtCallExpression::class.java, "Convert to 'trimMargin'"
) {
override fun isApplicableTo(element: KtCallExpression, caretOffset: Int): Boolean {
val template = (element.getQualifiedExpressionForSelector()?.receiverExpression as? KtStringTemplateExpression) ?: return false
if (!template.text.startsWith("\"\"\"")) return false
val callee = element.calleeExpression ?: return false
if (callee.text != "trimIndent" || callee.getCallableDescriptor()?.fqNameSafe != FqName("kotlin.text.trimIndent")) return false
val entries = template.entries
return listOfNotNull(entries.firstOrNull(), entries.lastOrNull()).all { it.text.isLineBreakOrBlank() }
}
override fun applyTo(element: KtCallExpression, editor: Editor?) {
val qualifiedExpression = element.getQualifiedExpressionForSelector()
val template = (qualifiedExpression?.receiverExpression as? KtStringTemplateExpression) ?: return
val indent = template.entries.asSequence().mapNotNull { stringTemplateEntry ->
val text = stringTemplateEntry.text
if (text.isLineBreakOrBlank()) null else text.takeWhile { it.isWhitespace() }
}.minBy { it.length } ?: ""
val newTemplate = buildString {
template.entries.forEach { entry ->
val text = entry.text
if (text.isLineBreakOrBlank()) {
append(text)
} else {
append(indent)
append("|")
append(text.drop(indent.length))
}
}
}
qualifiedExpression.replace(KtPsiFactory(element).createExpression("\"\"\"$newTemplate\"\"\".trimMargin()"))
}
}
private fun String.isLineBreakOrBlank() = this == "\n" || this.isBlank()
@@ -0,0 +1,72 @@
/*
* 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 org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelector
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
class ConvertTrimMarginToTrimIndentIntention : SelfTargetingIntention<KtCallExpression>(
KtCallExpression::class.java, "Convert to 'trimIndent'"
) {
override fun isApplicableTo(element: KtCallExpression, caretOffset: Int): Boolean {
val template = (element.getQualifiedExpressionForSelector()?.receiverExpression as? KtStringTemplateExpression) ?: return false
if (!template.text.startsWith("\"\"\"")) return false
val callee = element.calleeExpression ?: return false
if (callee.text != "trimMargin" || callee.getCallableDescriptor()?.fqNameSafe != FqName("kotlin.text.trimMargin")) return false
val marginPrefix = element.marginPrefix() ?: return false
val entries = template.entries
if (!listOfNotNull(entries.firstOrNull(), entries.lastOrNull()).all { it.text.isLineBreakOrBlank() }) {
return false
}
return entries.drop(1).dropLast(1).all { stringTemplateEntry ->
val text = stringTemplateEntry.text
text.isLineBreakOrBlank() || text.dropWhile { it.isWhitespace() }.startsWith(marginPrefix)
}
}
override fun applyTo(element: KtCallExpression, editor: Editor?) {
val qualifiedExpression = element.getQualifiedExpressionForSelector()
val template = (qualifiedExpression?.receiverExpression as? KtStringTemplateExpression) ?: return
val marginPrefix = element.marginPrefix() ?: return
val indent = template.entries.asSequence().mapNotNull { stringTemplateEntry ->
val text = stringTemplateEntry.text
if (text.isLineBreakOrBlank()) null else text.takeWhile { it.isWhitespace() }
}.minBy { it.length } ?: ""
val newTemplate = buildString {
template.entries.forEach { entry ->
val text = entry.text
if (text.isLineBreakOrBlank()) {
append(text)
} else {
append(indent)
append(entry.text.dropWhile { it.isWhitespace() }.replaceFirst(marginPrefix, ""))
}
}
}
qualifiedExpression.replace(KtPsiFactory(element).createExpression("\"\"\"$newTemplate\"\"\".trimIndent()"))
}
}
private fun KtCallExpression.marginPrefix(): String? {
val argument = valueArguments.firstOrNull()?.getArgumentExpression()
if (argument != null) {
if (argument !is KtStringTemplateExpression) return null
val entry = argument.entries.toList().singleOrNull() as? KtLiteralStringTemplateEntry ?: return null
return entry.text.replace("\"", "")
}
return "|"
}
private fun String.isLineBreakOrBlank() = this == "\n" || this.isBlank()
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.ConvertTrimIndentToTrimMarginIntention
@@ -0,0 +1,9 @@
// WITH_RUNTIME
fun test() {
val x =
"""
a
b
c
""".<caret>trimIndent()
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
fun test() {
val x =
"""
|a
| b
| c
""".trimMargin()
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: false
// WITH_RUNTIME
fun test() {
val x =
"""1
a
b
""".<caret>trimIndent()
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: false
// WITH_RUNTIME
fun test() {
val x =
"""
a
b
1""".<caret>trimIndent()
}
@@ -0,0 +1,5 @@
// IS_APPLICABLE: false
// WITH_RUNTIME
fun test() {
val x = " a".<caret>trimIndent()
}
@@ -0,0 +1,10 @@
// INTENTION_TEXT: "Convert to 'trimMargin'"
// WITH_RUNTIME
fun test() {
val x =
"""
a
b
""".<caret>trimIndent()
}
@@ -0,0 +1,10 @@
// INTENTION_TEXT: "Convert to 'trimMargin'"
// WITH_RUNTIME
fun test() {
val x =
"""
|a
|b
""".trimMargin()
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.ConvertTrimMarginToTrimIndentIntention
@@ -0,0 +1,9 @@
// WITH_RUNTIME
fun test() {
val x =
"""
|a
| b
|c
""".<caret>trimMargin()
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
fun test() {
val x =
"""
a
b
c
""".trimIndent()
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: false
// WITH_RUNTIME
fun test() {
val x =
"""
\a
\b
""".<caret>trimMargin("\\")
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: false
// WITH_RUNTIME
fun test() {
val x =
"""
|a
b
""".<caret>trimMargin()
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: false
// WITH_RUNTIME
fun test() {
val x =
"""1
|a
|b
""".<caret>trimMargin()
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: false
// WITH_RUNTIME
fun test() {
val x =
"""
|a
|b
1"""<caret>.trimMargin()
}
@@ -0,0 +1,5 @@
// IS_APPLICABLE: false
// WITH_RUNTIME
fun test() {
val x = " |a".<caret>trimMargin()
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: false
// WITH_RUNTIME
fun test(marginPrefix: String) {
val x =
"""
|a
|b
""".<caret>trimMargin(marginPrefix)
}
@@ -0,0 +1,9 @@
// INTENTION_TEXT: "Convert to 'trimIndent'"
// WITH_RUNTIME
fun test() {
val x =
"""
|a
|b
""".<caret>trimMargin()
}
@@ -0,0 +1,9 @@
// INTENTION_TEXT: "Convert to 'trimIndent'"
// WITH_RUNTIME
fun test() {
val x =
"""
a
b
""".trimIndent()
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
fun test() {
val x =
"""
#a
#b
""".<caret>trimMargin("#")
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
fun test() {
val x =
"""
a
b
""".trimIndent()
}
@@ -7294,6 +7294,102 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/convertTrimIndentToTrimMargin")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ConvertTrimIndentToTrimMargin extends AbstractIntentionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInConvertTrimIndentToTrimMargin() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/convertTrimIndentToTrimMargin"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("differentIndent.kt")
public void testDifferentIndent() throws Exception {
runTest("idea/testData/intentions/convertTrimIndentToTrimMargin/differentIndent.kt");
}
@TestMetadata("notBlankFirst.kt")
public void testNotBlankFirst() throws Exception {
runTest("idea/testData/intentions/convertTrimIndentToTrimMargin/notBlankFirst.kt");
}
@TestMetadata("notBlankLast.kt")
public void testNotBlankLast() throws Exception {
runTest("idea/testData/intentions/convertTrimIndentToTrimMargin/notBlankLast.kt");
}
@TestMetadata("notRawString.kt")
public void testNotRawString() throws Exception {
runTest("idea/testData/intentions/convertTrimIndentToTrimMargin/notRawString.kt");
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
runTest("idea/testData/intentions/convertTrimIndentToTrimMargin/simple.kt");
}
}
@TestMetadata("idea/testData/intentions/convertTrimMarginToTrimIndent")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ConvertTrimMarginToTrimIndent extends AbstractIntentionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInConvertTrimMarginToTrimIndent() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/convertTrimMarginToTrimIndent"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("differentIndent.kt")
public void testDifferentIndent() throws Exception {
runTest("idea/testData/intentions/convertTrimMarginToTrimIndent/differentIndent.kt");
}
@TestMetadata("escapedMarginPrefixArgument.kt")
public void testEscapedMarginPrefixArgument() throws Exception {
runTest("idea/testData/intentions/convertTrimMarginToTrimIndent/escapedMarginPrefixArgument.kt");
}
@TestMetadata("noMarginPrefix.kt")
public void testNoMarginPrefix() throws Exception {
runTest("idea/testData/intentions/convertTrimMarginToTrimIndent/noMarginPrefix.kt");
}
@TestMetadata("notBlankFirst.kt")
public void testNotBlankFirst() throws Exception {
runTest("idea/testData/intentions/convertTrimMarginToTrimIndent/notBlankFirst.kt");
}
@TestMetadata("notBlankLast.kt")
public void testNotBlankLast() throws Exception {
runTest("idea/testData/intentions/convertTrimMarginToTrimIndent/notBlankLast.kt");
}
@TestMetadata("notRawString.kt")
public void testNotRawString() throws Exception {
runTest("idea/testData/intentions/convertTrimMarginToTrimIndent/notRawString.kt");
}
@TestMetadata("referenceMarginPrefixArgument.kt")
public void testReferenceMarginPrefixArgument() throws Exception {
runTest("idea/testData/intentions/convertTrimMarginToTrimIndent/referenceMarginPrefixArgument.kt");
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
runTest("idea/testData/intentions/convertTrimMarginToTrimIndent/simple.kt");
}
@TestMetadata("simple2.kt")
public void testSimple2() throws Exception {
runTest("idea/testData/intentions/convertTrimMarginToTrimIndent/simple2.kt");
}
}
@TestMetadata("idea/testData/intentions/convertTryFinallyToUseCall")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)