Add intention to convert concatenation to raw string #KT-17503 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
50085722d1
commit
76507caf4b
+2
@@ -0,0 +1,2 @@
|
||||
val x = "World"
|
||||
val y = <spot>"""Hello $x"""</spot>
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
val x = "World"
|
||||
val y = <spot>"Hello " + x</spot>
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
Converts a statement that builds a String using '+' to one that builds a String using a raw String
|
||||
</body>
|
||||
</html>
|
||||
@@ -1169,6 +1169,11 @@
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.kotlin.idea.intentions.ConvertToRawStringTemplateIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.kotlin.idea.intentions.OperatorToFunctionIntention</className>
|
||||
<category>Kotlin</category>
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright 2010-2017 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.openapi.editor.Editor
|
||||
import org.jetbrains.kotlin.idea.core.replaced
|
||||
import org.jetbrains.kotlin.psi.KtBinaryExpression
|
||||
|
||||
class ConvertToRawStringTemplateIntention : ConvertToStringTemplateIntention() {
|
||||
init {
|
||||
text = "Convert concatenation to raw string"
|
||||
}
|
||||
|
||||
override fun applyTo(element: KtBinaryExpression, editor: Editor?) {
|
||||
val replaced = element.replaced(buildReplacement(element))
|
||||
ToRawStringLiteralIntention().applyTo(replaced, editor)
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010-2015 JetBrains s.r.o.
|
||||
* Copyright 2010-2017 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.
|
||||
@@ -33,7 +33,7 @@ class ConvertToStringTemplateInspection : IntentionBasedInspection<KtBinaryExpre
|
||||
{ it -> ConvertToStringTemplateIntention.shouldSuggestToConvert(it) }
|
||||
)
|
||||
|
||||
class ConvertToStringTemplateIntention : SelfTargetingOffsetIndependentIntention<KtBinaryExpression>(KtBinaryExpression::class.java, "Convert concatenation to template") {
|
||||
open class ConvertToStringTemplateIntention : SelfTargetingOffsetIndependentIntention<KtBinaryExpression>(KtBinaryExpression::class.java, "Convert concatenation to template") {
|
||||
override fun isApplicableTo(element: KtBinaryExpression): Boolean {
|
||||
if (!isApplicableToNoParentCheck(element)) return false
|
||||
|
||||
@@ -56,7 +56,8 @@ class ConvertToStringTemplateIntention : SelfTargetingOffsetIndependentIntention
|
||||
&& !expression.textContains('\n')
|
||||
}
|
||||
|
||||
private fun buildReplacement(expression: KtBinaryExpression): KtStringTemplateExpression {
|
||||
@JvmStatic
|
||||
protected fun buildReplacement(expression: KtBinaryExpression): KtStringTemplateExpression {
|
||||
val rightText = buildText(expression.right, false)
|
||||
return fold(expression.left, rightText, KtPsiFactory(expression))
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.idea.intentions.ConvertToRawStringTemplateIntention
|
||||
@@ -0,0 +1,2 @@
|
||||
val foo = "abc\n" <caret>+
|
||||
"xyz"
|
||||
@@ -0,0 +1,2 @@
|
||||
val foo = """abc
|
||||
xyz"""
|
||||
@@ -6570,6 +6570,21 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/convertToRawStringTemplate")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class ConvertToRawStringTemplate extends AbstractIntentionTest {
|
||||
public void testAllFilesPresentInConvertToRawStringTemplate() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/convertToRawStringTemplate"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("basic.kt")
|
||||
public void testBasic() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToRawStringTemplate/basic.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/convertToRun")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user