@@ -243,7 +243,14 @@ public class KtProperty extends KtTypeParameterListOwnerStub<KotlinPropertyStub>
|
||||
return (KtExpression) oldInitializer.replace(initializer);
|
||||
}
|
||||
else {
|
||||
deleteChildRange(findChildByType(EQ), oldInitializer);
|
||||
PsiElement nextSibling = oldInitializer.getNextSibling();
|
||||
PsiElement last =
|
||||
nextSibling != null
|
||||
&& nextSibling.getNode() != null
|
||||
&& nextSibling.getNode().getElementType() == KtTokens.SEMICOLON
|
||||
? nextSibling : oldInitializer;
|
||||
|
||||
deleteChildRange(findChildByType(EQ), last);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
class A {
|
||||
val foo: Int
|
||||
<spot>get() = 1</spot>
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
class A {
|
||||
val foo: Int = <spot>1</spot>
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This intention converts property initializer to getter.
|
||||
</body>
|
||||
</html>
|
||||
@@ -1000,6 +1000,11 @@
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.kotlin.idea.intentions.ConvertPropertyInitializerToGetterIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.kotlin.idea.intentions.InvertIfConditionIntention</className>
|
||||
<category>Kotlin</category>
|
||||
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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 org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
|
||||
import org.jetbrains.kotlin.psi.KtClassOrObject
|
||||
import org.jetbrains.kotlin.psi.KtProperty
|
||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.isExtensionDeclaration
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
|
||||
class ConvertPropertyInitializerToGetterIntention : SelfTargetingIntention<KtProperty>(javaClass(), "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
|
||||
}
|
||||
|
||||
override fun applyTo(property: KtProperty, editor: Editor) {
|
||||
convertPropertyInitializerToGetter(property, editor)
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun convertPropertyInitializerToGetter(property: KtProperty, editor: Editor?) {
|
||||
val type = SpecifyTypeExplicitlyIntention.getTypeForDeclaration(property)
|
||||
SpecifyTypeExplicitlyIntention.addTypeAnnotation(editor, property, type)
|
||||
|
||||
val initializer = property.initializer!!
|
||||
val getter = KtPsiFactory(property).createPropertyGetter(initializer)
|
||||
val setter = property.setter
|
||||
|
||||
if (setter != null) {
|
||||
property.addBefore(getter, setter)
|
||||
}
|
||||
else {
|
||||
property.add(getter)
|
||||
}
|
||||
|
||||
property.setInitializer(null)
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-14
@@ -20,6 +20,7 @@ import com.intellij.codeInsight.intention.IntentionAction
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.idea.intentions.ConvertPropertyInitializerToGetterIntention
|
||||
import org.jetbrains.kotlin.idea.intentions.SpecifyTypeExplicitlyIntention
|
||||
import org.jetbrains.kotlin.psi.KtExpression
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
@@ -34,20 +35,7 @@ class ConvertExtensionPropertyInitializerToGetterFix(element: KtExpression) : Ko
|
||||
|
||||
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
||||
val property = element.getParentOfType<KtProperty>(true) ?: return
|
||||
val type = SpecifyTypeExplicitlyIntention.getTypeForDeclaration(property)
|
||||
SpecifyTypeExplicitlyIntention.addTypeAnnotation(editor, property, type)
|
||||
|
||||
val getter = KtPsiFactory(element).createPropertyGetter(element)
|
||||
val setter = property.setter
|
||||
|
||||
if (setter != null) {
|
||||
property.addBefore(getter, setter)
|
||||
}
|
||||
else {
|
||||
property.add(getter)
|
||||
}
|
||||
|
||||
property.setInitializer(null)
|
||||
ConvertPropertyInitializerToGetterIntention.convertPropertyInitializerToGetter(property, editor)
|
||||
}
|
||||
|
||||
companion object : KotlinSingleIntentionActionFactory() {
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.idea.intentions.ConvertPropertyInitializerToGetterIntention
|
||||
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
// IS_APPLICABLE: false
|
||||
// ERROR: Extension property cannot be initialized because it has no backing field
|
||||
|
||||
class A
|
||||
|
||||
val A.a: Int = 0<caret>
|
||||
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
// IS_APPLICABLE: false
|
||||
|
||||
fun foo() {
|
||||
val x: Int = 1<caret>
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// IS_APPLICABLE: false
|
||||
|
||||
class A {
|
||||
init {
|
||||
val x: Int = 1<caret>
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
// IS_APPLICABLE: false
|
||||
|
||||
abstract class A {
|
||||
abstract val <caret>a: Int
|
||||
}
|
||||
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
class A {
|
||||
var a = <caret>1
|
||||
set(value) {
|
||||
field = value
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
class A {
|
||||
var a: Int
|
||||
get() = 1
|
||||
set(value) {
|
||||
field = value
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
class A {
|
||||
val a: Int = <caret>1
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
class A {
|
||||
val a: Int
|
||||
get() = 1
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class A {
|
||||
val a: Int = 1<caret>;
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
class A {
|
||||
val a: Int
|
||||
get() = 1
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
val x: Int = 1<caret>
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
val x: Int
|
||||
get() = 1
|
||||
+1
@@ -1,4 +1,5 @@
|
||||
// "class org.jetbrains.kotlin.idea.quickfix.AutoImportFix" "false"
|
||||
// ACTION: Convert property initializer to getter
|
||||
// ACTION: Create class 'SomeTest'
|
||||
// ACTION: Rename reference
|
||||
// ERROR: Unresolved reference: SomeTest
|
||||
|
||||
Vendored
+1
@@ -1,6 +1,7 @@
|
||||
// "class org.jetbrains.kotlin.idea.quickfix.AutoImportFix" "false"
|
||||
// ERROR: Unresolved reference: SomeTest
|
||||
// ERROR: Expression expected, but a package name found
|
||||
// ACTION: Convert property initializer to getter
|
||||
// ACTION: Create class 'SomeTest'
|
||||
// ACTION: Replace safe access expression with 'if' expression
|
||||
// ACTION: Rename reference
|
||||
|
||||
+1
@@ -1,4 +1,5 @@
|
||||
// "class org.jetbrains.kotlin.idea.quickfix.AutoImportFix" "false"
|
||||
// ACTION: Convert property initializer to getter
|
||||
// ACTION: Create class 'SomeClass'
|
||||
// ACTION: Create function 'SomeClass'
|
||||
// ACTION: Rename reference
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
// "class org.jetbrains.kotlin.idea.quickfix.AutoImportFix" "false"
|
||||
// ACTION: Convert property initializer to getter
|
||||
// ACTION: Create class 'ExcludedClass'
|
||||
// ACTION: Create function 'ExcludedClass'
|
||||
// ACTION: Rename reference
|
||||
|
||||
+1
@@ -1,4 +1,5 @@
|
||||
// "class org.jetbrains.kotlin.idea.quickfix.AutoImportFix" "false"
|
||||
// ACTION: Convert property initializer to getter
|
||||
// ACTION: Create function 'someFunction'
|
||||
// ACTION: Rename reference
|
||||
// ERROR: Unresolved reference: someFunction
|
||||
|
||||
+1
@@ -1,4 +1,5 @@
|
||||
// "Create local variable 'foo'" "false"
|
||||
// ACTION: Convert property initializer to getter
|
||||
// ACTION: Create parameter 'foo'
|
||||
// ACTION: Create property 'foo'
|
||||
// ACTION: Rename reference
|
||||
|
||||
+1
@@ -1,4 +1,5 @@
|
||||
// "Create local variable 'foo'" "false"
|
||||
// ACTION: Convert property initializer to getter
|
||||
// ACTION: Create property 'foo'
|
||||
// ACTION: Rename reference
|
||||
// ERROR: Unresolved reference: foo
|
||||
|
||||
+1
@@ -1,4 +1,5 @@
|
||||
// "Create parameter 'foo'" "false"
|
||||
// ACTION: Convert property initializer to getter
|
||||
// ACTION: Create property 'foo'
|
||||
// ACTION: Rename reference
|
||||
// ERROR: Unresolved reference: foo
|
||||
|
||||
Vendored
+1
@@ -1,4 +1,5 @@
|
||||
// "Create parameter 'foo'" "false"
|
||||
// ACTION: Convert property initializer to getter
|
||||
// ACTION: Create property 'foo'
|
||||
// ACTION: Rename reference
|
||||
// ERROR: Unresolved reference: foo
|
||||
|
||||
Vendored
+1
@@ -1,4 +1,5 @@
|
||||
// "Create parameter 'foo'" "false"
|
||||
// ACTION: Convert property initializer to getter
|
||||
// ACTION: Create property 'foo'
|
||||
// ACTION: Rename reference
|
||||
// ERROR: Unresolved reference: foo
|
||||
|
||||
@@ -3707,6 +3707,63 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/convertPropertyInitializerToGetter")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class ConvertPropertyInitializerToGetter extends AbstractIntentionTest {
|
||||
public void testAllFilesPresentInConvertPropertyInitializerToGetter() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/convertPropertyInitializerToGetter"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("inapplicableIfExtensionProperty.kt")
|
||||
public void testInapplicableIfExtensionProperty() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertPropertyInitializerToGetter/inapplicableIfExtensionProperty.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("inapplicableIfLocalVariableInFun.kt")
|
||||
public void testInapplicableIfLocalVariableInFun() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertPropertyInitializerToGetter/inapplicableIfLocalVariableInFun.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("inapplicableIfLocalVariableInInitBlock.kt")
|
||||
public void testInapplicableIfLocalVariableInInitBlock() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertPropertyInitializerToGetter/inapplicableIfLocalVariableInInitBlock.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("inapplicableIfNoInitializer.kt")
|
||||
public void testInapplicableIfNoInitializer() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertPropertyInitializerToGetter/inapplicableIfNoInitializer.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("propertyWithInitializerWithSetter.kt")
|
||||
public void testPropertyWithInitializerWithSetter() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertPropertyInitializerToGetter/propertyWithInitializerWithSetter.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("propertyWithInitializerWithoutAccessors.kt")
|
||||
public void testPropertyWithInitializerWithoutAccessors() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertPropertyInitializerToGetter/propertyWithInitializerWithoutAccessors.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("semicolon.kt")
|
||||
public void testSemicolon() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertPropertyInitializerToGetter/semicolon.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("topLevelDeclaration.kt")
|
||||
public void testTopLevelDeclaration() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertPropertyInitializerToGetter/topLevelDeclaration.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/convertPropertyToFunction")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user