"move assignment to initializer" quickfix for assigning to properties in constructors/init blocks
This commit is contained in:
@@ -185,12 +185,11 @@ public inline fun <reified T : PsiElement> PsiElement.findDescendantOfType(noinl
|
||||
|
||||
public inline fun <reified T : PsiElement> PsiElement.findDescendantOfType(crossinline canGoInside: (PsiElement) -> Boolean, noinline predicate: (T) -> Boolean = { true }): T? {
|
||||
var result: T? = null
|
||||
this.accept(object : PsiRecursiveElementVisitor() {
|
||||
this.accept(object : PsiRecursiveElementWalkingVisitor() {
|
||||
override fun visitElement(element: PsiElement) {
|
||||
if (result != null) return
|
||||
|
||||
if (element is T && predicate(element)) {
|
||||
result = element
|
||||
stopWalking()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -310,4 +309,11 @@ public fun PsiElement.getTextWithLocation(): String = "'${this.getText()}' at ${
|
||||
public fun SearchScope.contains(element: PsiElement): Boolean = PsiSearchScopeUtil.isInScope(this, element)
|
||||
|
||||
public fun <E : PsiElement> E.createSmartPointer(): SmartPsiElementPointer<E> =
|
||||
SmartPointerManager.getInstance(getProject()).createSmartPsiElementPointer(this)
|
||||
SmartPointerManager.getInstance(getProject()).createSmartPsiElementPointer(this)
|
||||
|
||||
fun PsiElement.resolveAllReferences(): Sequence<PsiElement?> {
|
||||
return PsiReferenceService.getService().getReferences(this, PsiReferenceService.Hints.NO_HINTS)
|
||||
.asSequence()
|
||||
.map { it.resolve() }
|
||||
}
|
||||
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
class A {
|
||||
var i: Int <spot>= 5</spot>
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
class A {
|
||||
var i: Int
|
||||
|
||||
init {
|
||||
<spot>i = 5</spot>
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This intention moves an assignment to a property into the initializer of that property.
|
||||
</body>
|
||||
</html>
|
||||
@@ -1033,6 +1033,11 @@
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.kotlin.idea.intentions.MoveAssignmentToInitializerIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.DeprecatedCallableAddReplaceWithInspection"
|
||||
displayName="Add 'replaceWith' argument to 'deprecated' annotation"
|
||||
groupName="Kotlin"
|
||||
|
||||
@@ -30,7 +30,6 @@ import org.jetbrains.kotlin.idea.quickfix.JetSingleIntentionActionFactory
|
||||
import org.jetbrains.kotlin.lexer.JetTokens
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.prevLeaf
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
|
||||
class IntroduceBackingPropertyIntention(): JetSelfTargetingIntention<JetProperty>(javaClass(), "Introduce backing property") {
|
||||
@@ -82,7 +81,7 @@ class IntroduceBackingPropertyIntention(): JetSelfTargetingIntention<JetProperty
|
||||
}
|
||||
}
|
||||
|
||||
element.removeInitializer()
|
||||
element.setInitializer(null)
|
||||
|
||||
replaceBackingFieldReferences(element)
|
||||
}
|
||||
@@ -99,16 +98,6 @@ class IntroduceBackingPropertyIntention(): JetSelfTargetingIntention<JetProperty
|
||||
element.add(newSetter)
|
||||
}
|
||||
|
||||
private fun JetProperty.removeInitializer() {
|
||||
val initializer = initializer
|
||||
if (initializer != null) {
|
||||
val eq = initializer.prevLeaf { it.node.elementType == JetTokens.EQ }
|
||||
if (eq != null) {
|
||||
initializer.parent.deleteChildRange(eq, initializer)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun createBackingProperty(element: JetProperty) {
|
||||
val backingPropertyText = StringBuilder {
|
||||
append("private ")
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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 com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.TokenType
|
||||
import com.intellij.psi.util.PsiTreeUtil
|
||||
import org.jetbrains.kotlin.lexer.JetTokens
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.resolveAllReferences
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
|
||||
|
||||
public class MoveAssignmentToInitializerIntention :
|
||||
JetSelfTargetingIntention<JetBinaryExpression>(javaClass(), "Move assignment to initializer") {
|
||||
|
||||
override fun isApplicableTo(element: JetBinaryExpression, caretOffset: Int): Boolean {
|
||||
if (element.operationToken != JetTokens.EQ) {
|
||||
return false
|
||||
}
|
||||
val rhs = element.right ?: return false
|
||||
|
||||
val block = PsiTreeUtil.getParentOfType(element,
|
||||
JetClassInitializer::class.java,
|
||||
JetSecondaryConstructor::class.java) ?: return false
|
||||
|
||||
val target = findTargetProperty(element)
|
||||
return target != null && target.initializer == null && target.receiverTypeReference == null &&
|
||||
target.getNonStrictParentOfType<JetClassOrObject>() == element.getNonStrictParentOfType<JetClassOrObject>() &&
|
||||
hasNoLocalDependencies(rhs, block)
|
||||
|
||||
}
|
||||
|
||||
override fun applyTo(element: JetBinaryExpression, editor: Editor) {
|
||||
val prop = findTargetProperty(element) ?: return
|
||||
val initializer = element.right ?: return
|
||||
prop.setInitializer(initializer)
|
||||
|
||||
val initializerBlock = element.getStrictParentOfType<JetClassInitializer>()
|
||||
element.delete()
|
||||
if (initializerBlock != null && (initializerBlock.body as? JetBlockExpression)?.isEmpty() == true) {
|
||||
initializerBlock.delete()
|
||||
}
|
||||
}
|
||||
|
||||
private fun findTargetProperty(expr: JetBinaryExpression): JetProperty? {
|
||||
val lhs = expr.left as? JetSimpleNameExpression ?: return null
|
||||
return lhs.resolveAllReferences().firstIsInstanceOrNull<JetProperty>()
|
||||
}
|
||||
|
||||
fun JetBlockExpression.isEmpty(): Boolean {
|
||||
var node = node?.firstChildNode
|
||||
while (node != null) {
|
||||
if (node.elementType != TokenType.WHITE_SPACE &&
|
||||
node.elementType != JetTokens.LBRACE &&
|
||||
node.elementType != JetTokens.RBRACE) {
|
||||
return false
|
||||
}
|
||||
node = node.treeNext
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
private fun hasNoLocalDependencies(element: JetElement, localContext: PsiElement): Boolean {
|
||||
return !element.anyDescendantOfType<PsiElement> { child ->
|
||||
child.resolveAllReferences().any { it != null && PsiTreeUtil.isAncestor(localContext, it, false) }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.idea.intentions.MoveAssignmentToInitializerIntention
|
||||
@@ -0,0 +1,9 @@
|
||||
class A {
|
||||
var a: Int
|
||||
var b: Int
|
||||
|
||||
init {
|
||||
<caret>$a = 1
|
||||
b = 2
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
class A {
|
||||
var a: Int = 1
|
||||
var b: Int
|
||||
|
||||
init {
|
||||
b = 2
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
class A {
|
||||
var a: Int
|
||||
|
||||
init {
|
||||
// Initialize a
|
||||
<caret>a = 1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
class A {
|
||||
var a: Int = 1
|
||||
|
||||
init {
|
||||
// Initialize a
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
class A {
|
||||
var a: Int
|
||||
|
||||
init {
|
||||
<caret>a = 1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
class A {
|
||||
var a: Int = 1
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
class A {
|
||||
var a: Int
|
||||
var b: Int
|
||||
|
||||
init {
|
||||
<caret>a = 1
|
||||
b = 2
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
class A {
|
||||
var a: Int = 1
|
||||
var b: Int
|
||||
|
||||
init {
|
||||
b = 2
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// IS_APPLICABLE: false
|
||||
class A {
|
||||
var b: Int
|
||||
|
||||
init {
|
||||
val i = 0
|
||||
b = <caret>i
|
||||
}
|
||||
}
|
||||
@@ -5416,6 +5416,45 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/moveAssignmentToInitializer")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class MoveAssignmentToInitializer extends AbstractIntentionTest {
|
||||
public void testAllFilesPresentInMoveAssignmentToInitializer() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/moveAssignmentToInitializer"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("backingField.kt")
|
||||
public void testBackingField() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/moveAssignmentToInitializer/backingField.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("comment.kt")
|
||||
public void testComment() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/moveAssignmentToInitializer/comment.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("deleteInitBlock.kt")
|
||||
public void testDeleteInitBlock() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/moveAssignmentToInitializer/deleteInitBlock.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("simple.kt")
|
||||
public void testSimple() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/moveAssignmentToInitializer/simple.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("usedLocal.kt")
|
||||
public void testUsedLocal() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/moveAssignmentToInitializer/usedLocal.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/moveLambdaInsideParentheses")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user