Introduce Variable: Parenthesize binary expressions if operation references starts with a new line
#KT-3933 Fixed
This commit is contained in:
+11
-2
@@ -132,6 +132,13 @@ object KotlinIntroduceVariableHandler : KotlinIntroduceHandlerBase() {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun KtExpression.mustBeParenthesizedInInitializerPosition(): Boolean {
|
||||||
|
if (this !is KtBinaryExpression) return false
|
||||||
|
|
||||||
|
if (left?.mustBeParenthesizedInInitializerPosition() ?: false) return true
|
||||||
|
return PsiChildRange(left, operationReference).any { (it is PsiWhiteSpace) && it.textContains('\n') }
|
||||||
|
}
|
||||||
|
|
||||||
private fun runRefactoring (
|
private fun runRefactoring (
|
||||||
expression: KtExpression,
|
expression: KtExpression,
|
||||||
commonContainer: PsiElement,
|
commonContainer: PsiElement,
|
||||||
@@ -139,11 +146,13 @@ object KotlinIntroduceVariableHandler : KotlinIntroduceHandlerBase() {
|
|||||||
allReplaces: List<KtExpression>
|
allReplaces: List<KtExpression>
|
||||||
) {
|
) {
|
||||||
val initializer = (expression as? KtParenthesizedExpression)?.expression ?: expression
|
val initializer = (expression as? KtParenthesizedExpression)?.expression ?: expression
|
||||||
|
val initializerText = if (initializer.mustBeParenthesizedInInitializerPosition()) "(${initializer.text})" else initializer.text
|
||||||
|
|
||||||
var property: KtDeclaration = if (componentFunctions.isNotEmpty()) {
|
var property: KtDeclaration = if (componentFunctions.isNotEmpty()) {
|
||||||
buildString {
|
buildString {
|
||||||
componentFunctions.indices.joinTo(this, prefix = "val (", postfix = ")") { nameSuggestions[it].first() }
|
componentFunctions.indices.joinTo(this, prefix = "val (", postfix = ")") { nameSuggestions[it].first() }
|
||||||
append(" = ")
|
append(" = ")
|
||||||
append(initializer.text)
|
append(initializerText)
|
||||||
}.let { psiFactory.createDestructuringDeclaration(it) }
|
}.let { psiFactory.createDestructuringDeclaration(it) }
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -155,7 +164,7 @@ object KotlinIntroduceVariableHandler : KotlinIntroduceHandlerBase() {
|
|||||||
append(": ").append(IdeDescriptorRenderers.SOURCE_CODE.renderType(typeToRender))
|
append(": ").append(IdeDescriptorRenderers.SOURCE_CODE.renderType(typeToRender))
|
||||||
}
|
}
|
||||||
append(" = ")
|
append(" = ")
|
||||||
append(initializer.text)
|
append(initializerText)
|
||||||
}.let { psiFactory.createProperty(it) }
|
}.let { psiFactory.createProperty(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
fun foo(i: Int) { }
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
foo(<selection>1
|
||||||
|
+ 2</selection>)
|
||||||
|
}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
fun foo(i: Int) { }
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
val i = (1
|
||||||
|
+ 2)
|
||||||
|
foo(i)
|
||||||
|
}
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
fun foo(i: Int) { }
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
foo(<selection>1 // abc
|
||||||
|
/*def*/ + 2</selection>)
|
||||||
|
}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
fun foo(i: Int) { }
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
val i = (1 // abc
|
||||||
|
/*def*/ + 2)
|
||||||
|
foo(i)
|
||||||
|
}
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
fun foo(i: Int) { }
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
foo(<selection>1
|
||||||
|
+ 2 - 3</selection>)
|
||||||
|
}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
fun foo(i: Int) { }
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
val i = (1
|
||||||
|
+ 2 - 3)
|
||||||
|
foo(i)
|
||||||
|
}
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
fun foo(i: Int) { }
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
foo(<selection>1 + 2
|
||||||
|
- 3</selection>)
|
||||||
|
}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
fun foo(i: Int) { }
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
val i = (1 + 2
|
||||||
|
- 3)
|
||||||
|
foo(i)
|
||||||
|
}
|
||||||
+24
@@ -235,6 +235,30 @@ public class ExtractionTestGenerated extends AbstractExtractionTest {
|
|||||||
doIntroduceVariableTest(fileName);
|
doIntroduceVariableTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("multilineBinaryExpression.kt")
|
||||||
|
public void testMultilineBinaryExpression() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/multilineBinaryExpression.kt");
|
||||||
|
doIntroduceVariableTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("multilineBinaryExpressionWithComments.kt")
|
||||||
|
public void testMultilineBinaryExpressionWithComments() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/multilineBinaryExpressionWithComments.kt");
|
||||||
|
doIntroduceVariableTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("multilineNestedBinaryExpression1.kt")
|
||||||
|
public void testMultilineNestedBinaryExpression1() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/multilineNestedBinaryExpression1.kt");
|
||||||
|
doIntroduceVariableTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("multilineNestedBinaryExpression2.kt")
|
||||||
|
public void testMultilineNestedBinaryExpression2() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/multilineNestedBinaryExpression2.kt");
|
||||||
|
doIntroduceVariableTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("NameSuggestionBug1.kt")
|
@TestMetadata("NameSuggestionBug1.kt")
|
||||||
public void testNameSuggestionBug1() throws Exception {
|
public void testNameSuggestionBug1() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/NameSuggestionBug1.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/introduceVariable/NameSuggestionBug1.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user