Intention to replace a = b with b.also { a = it }
^KT-22420 Fixed
This commit is contained in:
committed by
Vladimir Dolzhenko
parent
e387d56d93
commit
94970e2d1e
+4
@@ -0,0 +1,4 @@
|
|||||||
|
var x = 5
|
||||||
|
fun foo() {
|
||||||
|
<spot>10.also { x = it }</spot>
|
||||||
|
}
|
||||||
+4
@@ -0,0 +1,4 @@
|
|||||||
|
var x = 5
|
||||||
|
fun foo() {
|
||||||
|
<spot>x = 10</spot>
|
||||||
|
}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
This intention converts a <b>variable assignment</b> to a variable assignment expression.
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -1749,6 +1749,7 @@ convert.to.0.as.1=Convert to ''{0} as {1}''
|
|||||||
convert.to.0.unsafecast.1=Convert to ''{0}.unsafeCast<{1}>()''
|
convert.to.0.unsafecast.1=Convert to ''{0}.unsafeCast<{1}>()''
|
||||||
convert.to.unsafecast.call=Convert to unsafeCast() call
|
convert.to.unsafecast.call=Convert to unsafeCast() call
|
||||||
convert.to.array.parameter=Convert to array parameter
|
convert.to.array.parameter=Convert to array parameter
|
||||||
|
convert.to.assignment.expression=Convert assignment to assignment expression
|
||||||
create.kotlin.subclass=Create Kotlin subclass
|
create.kotlin.subclass=Create Kotlin subclass
|
||||||
use.destructuring.declaration=Use destructuring declaration
|
use.destructuring.declaration=Use destructuring declaration
|
||||||
implement.as.constructor.parameter=Implement as constructor parameter
|
implement.as.constructor.parameter=Implement as constructor parameter
|
||||||
|
|||||||
@@ -585,6 +585,11 @@
|
|||||||
<category>Kotlin</category>
|
<category>Kotlin</category>
|
||||||
</intentionAction>
|
</intentionAction>
|
||||||
|
|
||||||
|
<intentionAction>
|
||||||
|
<className>org.jetbrains.kotlin.idea.intentions.ConvertVariableAssignmentToExpressionIntention</className>
|
||||||
|
<category>Kotlin</category>
|
||||||
|
</intentionAction>
|
||||||
|
|
||||||
<intentionAction>
|
<intentionAction>
|
||||||
<className>org.jetbrains.kotlin.idea.intentions.MergeIfsIntention</className>
|
<className>org.jetbrains.kotlin.idea.intentions.MergeIfsIntention</className>
|
||||||
<category>Kotlin</category>
|
<category>Kotlin</category>
|
||||||
|
|||||||
+29
@@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.idea.intentions
|
||||||
|
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import org.jetbrains.kotlin.idea.KotlinBundle
|
||||||
|
import org.jetbrains.kotlin.idea.references.KtSimpleReference
|
||||||
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
|
import org.jetbrains.kotlin.psi.*
|
||||||
|
|
||||||
|
class ConvertVariableAssignmentToExpressionIntention : SelfTargetingIntention<KtBinaryExpression>(
|
||||||
|
KtBinaryExpression::class.java,
|
||||||
|
KotlinBundle.lazyMessage("convert.to.assignment.expression"),
|
||||||
|
) {
|
||||||
|
override fun isApplicableTo(element: KtBinaryExpression, caretOffset: Int): Boolean {
|
||||||
|
if (element.operationToken == KtTokens.EQ) return true
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun applyTo(element: KtBinaryExpression, editor: Editor?) {
|
||||||
|
val left = element.left ?: return
|
||||||
|
val right = element.right ?: return
|
||||||
|
val newElement = KtPsiFactory(element.project).createExpressionByPattern("$0.also { $1 = it }", right, left)
|
||||||
|
element.replace(newElement)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
org.jetbrains.kotlin.idea.intentions.ConvertVariableAssignmentToExpressionIntention
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
object X {
|
||||||
|
var string = "foo"
|
||||||
|
}
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
X.string <caret>= "bar"
|
||||||
|
}
|
||||||
+8
@@ -0,0 +1,8 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
object X {
|
||||||
|
var string = "foo"
|
||||||
|
}
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
"bar".also { X.string = it }
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
object X {
|
||||||
|
var string = "foo"
|
||||||
|
}
|
||||||
|
|
||||||
|
var target = "baz"
|
||||||
|
fun main() {
|
||||||
|
target <caret>= X.string
|
||||||
|
}
|
||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
object X {
|
||||||
|
var string = "foo"
|
||||||
|
}
|
||||||
|
|
||||||
|
var target = "baz"
|
||||||
|
fun main() {
|
||||||
|
X.string.also { target = it }
|
||||||
|
}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
var x = 0
|
||||||
|
fun main() {
|
||||||
|
x <caret>== 10
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
var x = 5
|
||||||
|
fun foo() {
|
||||||
|
x <caret>= 10
|
||||||
|
}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
var x = 5
|
||||||
|
fun foo() {
|
||||||
|
10.also { x = it }
|
||||||
|
}
|
||||||
@@ -8240,6 +8240,39 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("idea/testData/intentions/convertVariableAssignmentToExpression")
|
||||||
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
public static class ConvertVariableAssignmentToExpression extends AbstractIntentionTest {
|
||||||
|
private void runTest(String testDataFilePath) throws Exception {
|
||||||
|
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testAllFilesPresentInConvertVariableAssignmentToExpression() throws Exception {
|
||||||
|
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/intentions/convertVariableAssignmentToExpression"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), null, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("complexLhs.kt")
|
||||||
|
public void testComplexLhs() throws Exception {
|
||||||
|
runTest("idea/testData/intentions/convertVariableAssignmentToExpression/complexLhs.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("complexRhs.kt")
|
||||||
|
public void testComplexRhs() throws Exception {
|
||||||
|
runTest("idea/testData/intentions/convertVariableAssignmentToExpression/complexRhs.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("noAssignment.kt")
|
||||||
|
public void testNoAssignment() throws Exception {
|
||||||
|
runTest("idea/testData/intentions/convertVariableAssignmentToExpression/noAssignment.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("simple.kt")
|
||||||
|
public void testSimple() throws Exception {
|
||||||
|
runTest("idea/testData/intentions/convertVariableAssignmentToExpression/simple.kt");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("idea/testData/intentions/copyConcatenatedStringToClipboard")
|
@TestMetadata("idea/testData/intentions/copyConcatenatedStringToClipboard")
|
||||||
@TestDataPath("$PROJECT_ROOT")
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
@RunWith(JUnit3RunnerWithInners.class)
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
|||||||
Reference in New Issue
Block a user