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
|
||||
|
||||
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.KtPsiFactory
|
||||
import org.jetbrains.kotlin.psi.psiUtil.isExtensionDeclaration
|
||||
|
||||
class ConvertPropertyInitializerToGetterIntention : SelfTargetingIntention<KtProperty>(KtProperty::class.java, "Convert property initializer to getter") {
|
||||
override fun isApplicableTo(element: KtProperty, caretOffset: Int): Boolean {
|
||||
return element.initializer != null
|
||||
&& element.initializer?.textRange?.containsOffset(caretOffset) == true
|
||||
&& element.getter == null
|
||||
&& !element.isExtensionDeclaration()
|
||||
&& !element.isLocal
|
||||
class ConvertPropertyInitializerToGetterIntention : SelfTargetingRangeIntention<KtProperty>(KtProperty::class.java, "Convert property initializer to getter") {
|
||||
|
||||
override fun applicabilityRange(element: KtProperty): TextRange? {
|
||||
val initializer = element.initializer
|
||||
if (initializer != null && element.getter == null && !element.isExtensionDeclaration() && !element.isLocal)
|
||||
return initializer.textRange
|
||||
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?) {
|
||||
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);
|
||||
}
|
||||
|
||||
@TestMetadata("notInLambda.kt")
|
||||
public void testNotInLambda() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertPropertyInitializerToGetter/notInLambda.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("propertyWithInitializerWithSetter.kt")
|
||||
public void testPropertyWithInitializerWithSetter() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertPropertyInitializerToGetter/propertyWithInitializerWithSetter.kt");
|
||||
|
||||
Reference in New Issue
Block a user