ConvertPropertyInitializerToGetterIntention to not be available inside lambda initializer
This commit is contained in:
+13
-7
@@ -17,19 +17,25 @@
|
|||||||
package org.jetbrains.kotlin.idea.intentions
|
package org.jetbrains.kotlin.idea.intentions
|
||||||
|
|
||||||
import com.intellij.openapi.editor.Editor
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import com.intellij.openapi.util.TextRange
|
||||||
|
import com.intellij.psi.PsiElement
|
||||||
|
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||||
import org.jetbrains.kotlin.psi.KtProperty
|
import org.jetbrains.kotlin.psi.KtProperty
|
||||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.isExtensionDeclaration
|
import org.jetbrains.kotlin.psi.psiUtil.isExtensionDeclaration
|
||||||
|
|
||||||
class ConvertPropertyInitializerToGetterIntention : SelfTargetingIntention<KtProperty>(KtProperty::class.java, "Convert property initializer to getter") {
|
class ConvertPropertyInitializerToGetterIntention : SelfTargetingRangeIntention<KtProperty>(KtProperty::class.java, "Convert property initializer to getter") {
|
||||||
override fun isApplicableTo(element: KtProperty, caretOffset: Int): Boolean {
|
|
||||||
return element.initializer != null
|
override fun applicabilityRange(element: KtProperty): TextRange? {
|
||||||
&& element.initializer?.textRange?.containsOffset(caretOffset) == true
|
val initializer = element.initializer
|
||||||
&& element.getter == null
|
if (initializer != null && element.getter == null && !element.isExtensionDeclaration() && !element.isLocal)
|
||||||
&& !element.isExtensionDeclaration()
|
return initializer.textRange
|
||||||
&& !element.isLocal
|
else
|
||||||
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun allowCaretInsideElement(element: PsiElement) = element !is KtDeclaration // do not work inside lambda's in initializer - they can be too big
|
||||||
|
|
||||||
override fun applyTo(element: KtProperty, editor: Editor?) {
|
override fun applyTo(element: KtProperty, editor: Editor?) {
|
||||||
convertPropertyInitializerToGetter(element, editor)
|
convertPropertyInitializerToGetter(element, editor)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
// WITH_RUNTIME
|
||||||
|
val x: Int = <caret>run {
|
||||||
|
<caret>1
|
||||||
|
}
|
||||||
@@ -3913,6 +3913,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("notInLambda.kt")
|
||||||
|
public void testNotInLambda() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertPropertyInitializerToGetter/notInLambda.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("propertyWithInitializerWithSetter.kt")
|
@TestMetadata("propertyWithInitializerWithSetter.kt")
|
||||||
public void testPropertyWithInitializerWithSetter() throws Exception {
|
public void testPropertyWithInitializerWithSetter() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertPropertyInitializerToGetter/propertyWithInitializerWithSetter.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertPropertyInitializerToGetter/propertyWithInitializerWithSetter.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user