KT-21811 Convert string concatenation into multiline string (#2772)

New J2K: Convert string concatenation into multiline string

#KT-21811 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-11-28 19:14:48 +09:00
committed by Ilya Kirillov
parent c14affd6cd
commit 3a576ce14f
7 changed files with 36 additions and 6 deletions
@@ -64,7 +64,7 @@ open class ConvertToStringTemplateIntention : SelfTargetingOffsetIndependentInte
}
@JvmStatic
protected fun buildReplacement(expression: KtBinaryExpression): KtStringTemplateExpression {
fun buildReplacement(expression: KtBinaryExpression): KtStringTemplateExpression {
val rightText = buildText(expression.right, false)
return fold(expression.left, rightText, KtPsiFactory(expression))
}
@@ -17,11 +17,11 @@ import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForReceiver
import org.jetbrains.kotlin.psi.psiUtil.parents
import org.jetbrains.kotlin.psi.psiUtil.startOffset
class IndentRawStringIntention : SelfTargetingIntention<KtStringTemplateExpression>(
class IndentRawStringIntention : SelfTargetingOffsetIndependentIntention<KtStringTemplateExpression>(
KtStringTemplateExpression::class.java, "Indent raw string"
) {
override fun isApplicableTo(element: KtStringTemplateExpression, caretOffset: Int): Boolean {
override fun isApplicableTo(element: KtStringTemplateExpression): Boolean {
if (!element.text.startsWith("\"\"\"")) return false
if (element.parents.any { it is KtAnnotationEntry || (it as? KtProperty)?.hasModifier(KtTokens.CONST_KEYWORD) == true }) return false
if (element.getQualifiedExpressionForReceiver() != null) return false
@@ -6,7 +6,6 @@
package org.jetbrains.kotlin.nj2k.postProcessing
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.editor.RangeMarker
import com.intellij.openapi.progress.ProcessCanceledException
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.idea.caches.resolve.resolveImportReference
@@ -30,6 +29,7 @@ import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.nj2k.NewJ2kConverterContext
import org.jetbrains.kotlin.nj2k.postProcessing.processings.*
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.parents
class NewJ2kPostProcessor : PostProcessor {
@Suppress("PrivatePropertyName")
@@ -221,7 +221,15 @@ private val inspectionLikePostProcessingGroup =
RemoveForExpressionLoopParameterTypeProcessing(),
intentionBasedProcessing(ReplaceMapGetOrDefaultIntention()),
inspectionBasedProcessing(ReplaceGuardClauseWithFunctionCallInspection()),
inspectionBasedProcessing(SortModifiersInspection())
inspectionBasedProcessing(SortModifiersInspection()),
intentionBasedProcessing(ConvertToRawStringTemplateIntention()) { element ->
element.parents.none {
(it as? KtProperty)?.hasModifier(KtTokens.CONST_KEYWORD) == true
} && ConvertToStringTemplateIntention.buildReplacement(element).entries.any {
(it as? KtEscapeStringTemplateEntry)?.unescapedValue == "\n"
}
},
intentionBasedProcessing(IndentRawStringIntention())
)
@@ -1,2 +1,7 @@
@Deprecated("Ph'nglui mglw'nafh\n" + " Cthulhu R'lyeh wgah'nagl fhtagn.\n" + " 'In His House at R'lyeh\n" + " Dead Cthulhu waits dreaming,\n" + " yet He shall rise and His kingdom\n" + " shall cover the Earth.'")
@Deprecated("""Ph'nglui mglw'nafh
Cthulhu R'lyeh wgah'nagl fhtagn.
'In His House at R'lyeh
Dead Cthulhu waits dreaming,
yet He shall rise and His kingdom
shall cover the Earth.'""")
class TestDeprecatedInJavadocWithMultilineMessage
@@ -0,0 +1,5 @@
public class Test {
String s = "asdf\n" +
"nadfadsf\n" +
"asdfasdf";
}
@@ -0,0 +1,7 @@
class Test {
var s = """
asdf
nadfadsf
asdfasdf
""".trimIndent()
}
@@ -4176,6 +4176,11 @@ public class NewJavaToKotlinConverterSingleFileTestGenerated extends AbstractNew
runTest("nj2k/testData/newJ2k/postProcessing/AnonymousObject.java");
}
@TestMetadata("ConvertConcatenationToRawString.java")
public void testConvertConcatenationToRawString() throws Exception {
runTest("nj2k/testData/newJ2k/postProcessing/ConvertConcatenationToRawString.java");
}
@TestMetadata("GetOperator.java")
public void testGetOperator() throws Exception {
runTest("nj2k/testData/newJ2k/postProcessing/GetOperator.java");