KT-18674 Join Lines should join strings (#1305)
* Join Lines should join strings #KT-18674 Fixed * #KT-18674 Fixed
This commit is contained in:
committed by
Dmitry Jemerov
parent
16695c1af5
commit
eb12cfd444
@@ -727,6 +727,7 @@
|
|||||||
<joinLinesHandler implementation="org.jetbrains.kotlin.idea.joinLines.JoinDeclarationAndAssignmentHandler"/>
|
<joinLinesHandler implementation="org.jetbrains.kotlin.idea.joinLines.JoinDeclarationAndAssignmentHandler"/>
|
||||||
<joinLinesHandler implementation="org.jetbrains.kotlin.idea.joinLines.JoinBlockIntoSingleStatementHandler"/>
|
<joinLinesHandler implementation="org.jetbrains.kotlin.idea.joinLines.JoinBlockIntoSingleStatementHandler"/>
|
||||||
<joinLinesHandler implementation="org.jetbrains.kotlin.idea.joinLines.JoinStatementsAddSemicolonHandler"/>
|
<joinLinesHandler implementation="org.jetbrains.kotlin.idea.joinLines.JoinStatementsAddSemicolonHandler"/>
|
||||||
|
<joinLinesHandler implementation="org.jetbrains.kotlin.idea.joinLines.JoinToStringTemplateHandler"/>
|
||||||
|
|
||||||
<targetElementEvaluator
|
<targetElementEvaluator
|
||||||
language="kotlin"
|
language="kotlin"
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ open class ConvertToStringTemplateIntention : SelfTargetingOffsetIndependentInte
|
|||||||
}
|
}
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
protected fun buildReplacement(expression: KtBinaryExpression): KtStringTemplateExpression {
|
fun buildReplacement(expression: KtBinaryExpression): KtStringTemplateExpression {
|
||||||
val rightText = buildText(expression.right, false)
|
val rightText = buildText(expression.right, false)
|
||||||
return fold(expression.left, rightText, KtPsiFactory(expression))
|
return fold(expression.left, rightText, KtPsiFactory(expression))
|
||||||
}
|
}
|
||||||
@@ -130,7 +130,7 @@ open class ConvertToStringTemplateIntention : SelfTargetingOffsetIndependentInte
|
|||||||
return "\${$expressionText}"
|
return "\${$expressionText}"
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun isApplicableToNoParentCheck(expression: KtBinaryExpression): Boolean {
|
fun isApplicableToNoParentCheck(expression: KtBinaryExpression): Boolean {
|
||||||
if (expression.operationToken != KtTokens.PLUS) return false
|
if (expression.operationToken != KtTokens.PLUS) return false
|
||||||
val expressionType = expression.analyze(BodyResolveMode.PARTIAL).getType(expression)
|
val expressionType = expression.analyze(BodyResolveMode.PARTIAL).getType(expression)
|
||||||
if (!KotlinBuiltIns.isString(expressionType)) return false
|
if (!KotlinBuiltIns.isString(expressionType)) return false
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
/*
|
||||||
|
* 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.joinLines
|
||||||
|
|
||||||
|
import com.intellij.codeInsight.editorActions.JoinRawLinesHandlerDelegate
|
||||||
|
import com.intellij.openapi.editor.Document
|
||||||
|
import com.intellij.psi.PsiFile
|
||||||
|
import org.jetbrains.kotlin.idea.intentions.ConvertToStringTemplateIntention
|
||||||
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
|
import org.jetbrains.kotlin.psi.KtBinaryExpression
|
||||||
|
import org.jetbrains.kotlin.psi.KtFile
|
||||||
|
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||||
|
|
||||||
|
class JoinToStringTemplateHandler : JoinRawLinesHandlerDelegate {
|
||||||
|
override fun tryJoinRawLines(document: Document, file: PsiFile, start: Int, end: Int): Int {
|
||||||
|
if (file !is KtFile) return -1
|
||||||
|
|
||||||
|
if (start == 0) return -1
|
||||||
|
val c = document.charsSequence[start]
|
||||||
|
val index = if (c == '\n') start - 1 else start
|
||||||
|
|
||||||
|
val plus = file.findElementAt(index)?.takeIf { it.node?.elementType == KtTokens.PLUS } ?: return -1
|
||||||
|
val expression = (plus.parent.parent as? KtBinaryExpression) ?: return -1
|
||||||
|
val left = expression.left ?: return -1
|
||||||
|
val right = expression.right ?: return -1
|
||||||
|
if (left !is KtStringTemplateExpression || right !is KtStringTemplateExpression) return -1
|
||||||
|
if (!ConvertToStringTemplateIntention.isApplicableToNoParentCheck(expression)) return -1
|
||||||
|
|
||||||
|
val offset = left.endOffset - 1
|
||||||
|
expression.replace(ConvertToStringTemplateIntention.buildReplacement(expression))
|
||||||
|
return offset
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun tryJoinLines(document: Document, file: PsiFile, start: Int, end: Int): Int = -1
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
fun test() {
|
||||||
|
val foo = "1"
|
||||||
|
val s = foo +<caret>
|
||||||
|
"bar"
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
fun test() {
|
||||||
|
val foo = "1"
|
||||||
|
val s = foo +<caret> "bar"
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
fun test() {
|
||||||
|
val foo = "1"
|
||||||
|
val s = "bar" +<caret>
|
||||||
|
foo
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
fun test() {
|
||||||
|
val foo = "1"
|
||||||
|
val s = "bar" +<caret> foo
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
fun test() {
|
||||||
|
val s = "foo" +<caret>
|
||||||
|
"bar"
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
fun test() {
|
||||||
|
val s = "foo<caret>bar"
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
fun test() {
|
||||||
|
val s = "foo" +<caret>
|
||||||
|
"bar" +
|
||||||
|
"baz"
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
fun test() {
|
||||||
|
val s = "foo<caret>bar" +
|
||||||
|
"baz"
|
||||||
|
}
|
||||||
+33
@@ -425,4 +425,37 @@ public class JoinLinesTestGenerated extends AbstractJoinLinesTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("idea/testData/joinLines/stringTemplate")
|
||||||
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
public static class StringTemplate extends AbstractJoinLinesTest {
|
||||||
|
public void testAllFilesPresentInStringTemplate() throws Exception {
|
||||||
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/joinLines/stringTemplate"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("firstLineVariable.kt")
|
||||||
|
public void testFirstLineVariable() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/joinLines/stringTemplate/firstLineVariable.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("secondLineVariable.kt")
|
||||||
|
public void testSecondLineVariable() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/joinLines/stringTemplate/secondLineVariable.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("simple.kt")
|
||||||
|
public void testSimple() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/joinLines/stringTemplate/simple.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("threeLines.kt")
|
||||||
|
public void testThreeLines() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/joinLines/stringTemplate/threeLines.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user