[kotlin] Restore "convert trimIndent() to trimMargin() and vice versa" intentions

^KTIJ-1742 Fixed

Initially removed in:
https://github.com/JetBrains/kotlin/commit/0b9b49d7bc4c418e94b5b918c7ca3939e5a36eda
https://github.com/JetBrains/kotlin/commit/26478b1374fc96d45713108668336c5eb531bff1

closes https://github.com/JetBrains/intellij-community/pull/2119

GitOrigin-RevId: 408ec0f1c6f6bd6081edc695c3f16295eee3e7de
This commit is contained in:
Xiang Rong Lin
2022-08-24 16:49:55 +00:00
committed by intellij-monorepo-bot
parent e8b87c8c6d
commit 408dd57e71
32 changed files with 442 additions and 0 deletions
@@ -1698,6 +1698,8 @@ convert.to.0.unsafecast.1=Convert to ''{0}.unsafeCast<{1}>()''
convert.to.unsafecast.call=Convert to unsafeCast() call
convert.to.array.parameter=Convert to array parameter
convert.to.assignment.expression=Converts the assignment statement to an expression
convert.to.trim.indent=Convert to trimIndent() call
convert.to.trim.margin=Convert to trimMargin() call
create.kotlin.subclass=Create Kotlin subclass
use.destructuring.declaration=Use destructuring declaration
implement.as.constructor.parameter=Implement as constructor parameter
@@ -0,0 +1,7 @@
fun foo() {
<spot>val x =
"""
a
b
""".trimIndent()</spot>
}
@@ -0,0 +1,7 @@
fun foo() {
<spot>val x =
"""
|a
|b
""".trimMargin()</spot>
}
@@ -0,0 +1,6 @@
<!-- Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. -->
<html>
<body>
Converts a <code>trimIndent()</code> call to a <code>trimMargin()</code> call.
</body>
</html>
@@ -0,0 +1,7 @@
fun foo() {
<spot>val x =
"""
|a
|b
""".trimMargin()</spot>
}
@@ -0,0 +1,7 @@
fun foo() {
<spot>val x =
"""
a
b
""".trimIndent()</spot>
}
@@ -0,0 +1,6 @@
<!-- Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. -->
<html>
<body>
Converts a <code>trimMargin()</code> call to a <code>trimIndent()</code> call.
</body>
</html>
@@ -0,0 +1,61 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.base.resources.KotlinBundle
import org.jetbrains.kotlin.idea.codeinsight.api.classic.intentions.SelfTargetingIntention
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelector
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
class ConvertTrimIndentToTrimMarginIntention : SelfTargetingIntention<KtCallExpression>(
KtCallExpression::class.java, KotlinBundle.lazyMessage("convert.to.trim.margin")
) {
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
return template.isSurroundedByLineBreaksOrBlanks()
}
override fun applyTo(element: KtCallExpression, editor: Editor?) {
val qualifiedExpression = element.getQualifiedExpressionForSelector()
val template = (qualifiedExpression?.receiverExpression as? KtStringTemplateExpression) ?: return
val indent = template.calculateIndent()
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()"))
}
companion object {
fun KtStringTemplateExpression.isSurroundedByLineBreaksOrBlanks(): Boolean {
val entries = entries
return listOfNotNull(entries.firstOrNull(), entries.lastOrNull()).all { it.text.isLineBreakOrBlank() }
}
fun String.isLineBreakOrBlank(): Boolean =
this == "\n" || this.isBlank()
fun KtStringTemplateExpression.calculateIndent(): String =
entries.asSequence().mapNotNull { stringTemplateEntry ->
val text = stringTemplateEntry.text
if (text.isLineBreakOrBlank()) null else text.takeWhile { it.isWhitespace() }
}.minByOrNull { it.length } ?: ""
}
}
@@ -0,0 +1,65 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.base.resources.KotlinBundle
import org.jetbrains.kotlin.idea.codeinsight.api.classic.intentions.SelfTargetingIntention
import org.jetbrains.kotlin.idea.intentions.ConvertTrimIndentToTrimMarginIntention.Companion.calculateIndent
import org.jetbrains.kotlin.idea.intentions.ConvertTrimIndentToTrimMarginIntention.Companion.isLineBreakOrBlank
import org.jetbrains.kotlin.idea.intentions.ConvertTrimIndentToTrimMarginIntention.Companion.isSurroundedByLineBreaksOrBlanks
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtLiteralStringTemplateEntry
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelector
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
class ConvertTrimMarginToTrimIndentIntention : SelfTargetingIntention<KtCallExpression>(
KtCallExpression::class.java, KotlinBundle.lazyMessage("convert.to.trim.indent")
) {
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
if (!template.isSurroundedByLineBreaksOrBlanks()) return false
val marginPrefix = element.marginPrefix() ?: return false
return template.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.calculateIndent()
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 "|"
}
@@ -9404,6 +9404,92 @@ public abstract class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@RunWith(JUnit3RunnerWithInners.class)
@TestMetadata("testData/intentions/convertTrimIndentToTrimMargin")
public static class ConvertTrimIndentToTrimMargin extends AbstractIntentionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
@TestMetadata("differentIndent.kt")
public void testDifferentIndent() throws Exception {
runTest("testData/intentions/convertTrimIndentToTrimMargin/differentIndent.kt");
}
@TestMetadata("notBlankFirst.kt")
public void testNotBlankFirst() throws Exception {
runTest("testData/intentions/convertTrimIndentToTrimMargin/notBlankFirst.kt");
}
@TestMetadata("notBlankLast.kt")
public void testNotBlankLast() throws Exception {
runTest("testData/intentions/convertTrimIndentToTrimMargin/notBlankLast.kt");
}
@TestMetadata("notRawString.kt")
public void testNotRawString() throws Exception {
runTest("testData/intentions/convertTrimIndentToTrimMargin/notRawString.kt");
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
runTest("testData/intentions/convertTrimIndentToTrimMargin/simple.kt");
}
}
@RunWith(JUnit3RunnerWithInners.class)
@TestMetadata("testData/intentions/convertTrimMarginToTrimIndent")
public static class ConvertTrimMarginToTrimIndent extends AbstractIntentionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
@TestMetadata("differentIndent.kt")
public void testDifferentIndent() throws Exception {
runTest("testData/intentions/convertTrimMarginToTrimIndent/differentIndent.kt");
}
@TestMetadata("escapedMarginPrefixArgument.kt")
public void testEscapedMarginPrefixArgument() throws Exception {
runTest("testData/intentions/convertTrimMarginToTrimIndent/escapedMarginPrefixArgument.kt");
}
@TestMetadata("noMarginPrefix.kt")
public void testNoMarginPrefix() throws Exception {
runTest("testData/intentions/convertTrimMarginToTrimIndent/noMarginPrefix.kt");
}
@TestMetadata("notBlankFirst.kt")
public void testNotBlankFirst() throws Exception {
runTest("testData/intentions/convertTrimMarginToTrimIndent/notBlankFirst.kt");
}
@TestMetadata("notBlankLast.kt")
public void testNotBlankLast() throws Exception {
runTest("testData/intentions/convertTrimMarginToTrimIndent/notBlankLast.kt");
}
@TestMetadata("notRawString.kt")
public void testNotRawString() throws Exception {
runTest("testData/intentions/convertTrimMarginToTrimIndent/notRawString.kt");
}
@TestMetadata("referenceMarginPrefixArgument.kt")
public void testReferenceMarginPrefixArgument() throws Exception {
runTest("testData/intentions/convertTrimMarginToTrimIndent/referenceMarginPrefixArgument.kt");
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
runTest("testData/intentions/convertTrimMarginToTrimIndent/simple.kt");
}
@TestMetadata("simple2.kt")
public void testSimple2() throws Exception {
runTest("testData/intentions/convertTrimMarginToTrimIndent/simple2.kt");
}
}
@RunWith(JUnit3RunnerWithInners.class)
@TestMetadata("testData/intentions/convertTryFinallyToUseCall")
public static class ConvertTryFinallyToUseCall extends AbstractIntentionTest {
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.ConvertTrimIndentToTrimMarginIntention
@@ -0,0 +1,10 @@
// AFTER-WARNING: Variable 'x' is never used
// WITH_STDLIB
fun test() {
val x =
"""
a
b
c
""".<caret>trimIndent()
}
@@ -0,0 +1,10 @@
// AFTER-WARNING: Variable 'x' is never used
// WITH_STDLIB
fun test() {
val x =
"""
|a
| b
| c
""".trimMargin()
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: false
// WITH_STDLIB
fun test() {
val x =
"""1
a
b
""".<caret>trimIndent()
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: false
// WITH_STDLIB
fun test() {
val x =
"""
a
b
1""".<caret>trimIndent()
}
@@ -0,0 +1,5 @@
// IS_APPLICABLE: false
// WITH_STDLIB
fun test() {
val x = " a".<caret>trimIndent()
}
@@ -0,0 +1,10 @@
// AFTER-WARNING: Variable 'x' is never used
// INTENTION_TEXT: "Convert to trimMargin() call"
// WITH_STDLIB
fun test() {
val x =
"""
a
b
""".<caret>trimIndent()
}
@@ -0,0 +1,10 @@
// AFTER-WARNING: Variable 'x' is never used
// INTENTION_TEXT: "Convert to trimMargin() call"
// WITH_STDLIB
fun test() {
val x =
"""
|a
|b
""".trimMargin()
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.ConvertTrimMarginToTrimIndentIntention
@@ -0,0 +1,10 @@
// AFTER-WARNING: Variable 'x' is never used
// WITH_STDLIB
fun test() {
val x =
"""
|a
| b
|c
""".<caret>trimMargin()
}
@@ -0,0 +1,10 @@
// AFTER-WARNING: Variable 'x' is never used
// WITH_STDLIB
fun test() {
val x =
"""
a
b
c
""".trimIndent()
}
@@ -0,0 +1,10 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// IS_APPLICABLE: false
// WITH_STDLIB
fun test() {
val x =
"""
\a
\b
""".<caret>trimMargin("\\")
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: false
// WITH_STDLIB
fun test() {
val x =
"""
|a
b
""".<caret>trimMargin()
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: false
// WITH_STDLIB
fun test() {
val x =
"""1
|a
|b
""".<caret>trimMargin()
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: false
// WITH_STDLIB
fun test() {
val x =
"""
|a
|b
1"""<caret>.trimMargin()
}
@@ -0,0 +1,5 @@
// IS_APPLICABLE: false
// WITH_STDLIB
fun test() {
val x = " |a".<caret>trimMargin()
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: false
// WITH_STDLIB
fun test(marginPrefix: String) {
val x =
"""
|a
|b
""".<caret>trimMargin(marginPrefix)
}
@@ -0,0 +1,10 @@
// AFTER-WARNING: Variable 'x' is never used
// INTENTION_TEXT: "Convert to trimIndent() call"
// WITH_STDLIB
fun test() {
val x =
"""
|a
|b
""".<caret>trimMargin()
}
@@ -0,0 +1,10 @@
// AFTER-WARNING: Variable 'x' is never used
// INTENTION_TEXT: "Convert to trimIndent() call"
// WITH_STDLIB
fun test() {
val x =
"""
a
b
""".trimIndent()
}
@@ -0,0 +1,9 @@
// AFTER-WARNING: Variable 'x' is never used
// WITH_STDLIB
fun test() {
val x =
"""
#a
#b
""".<caret>trimMargin("#")
}
@@ -0,0 +1,9 @@
// AFTER-WARNING: Variable 'x' is never used
// WITH_STDLIB
fun test() {
val x =
"""
a
b
""".trimIndent()
}
@@ -924,6 +924,20 @@
<categoryKey>group.names.kotlin</categoryKey>
</intentionAction>
<intentionAction>
<language>kotlin</language>
<className>org.jetbrains.kotlin.idea.intentions.ConvertTrimMarginToTrimIndentIntention</className>
<bundleName>messages.KotlinBundle</bundleName>
<categoryKey>group.names.kotlin</categoryKey>
</intentionAction>
<intentionAction>
<language>kotlin</language>
<className>org.jetbrains.kotlin.idea.intentions.ConvertTrimIndentToTrimMarginIntention</className>
<bundleName>messages.KotlinBundle</bundleName>
<categoryKey>group.names.kotlin</categoryKey>
</intentionAction>
<intentionAction>
<language>kotlin</language>
<className>org.jetbrains.kotlin.idea.intentions.ConvertPropertyGetterToInitializerIntention</className>