Intentions to generate accessors for a property #KT-17322 Fixed

This commit is contained in:
Toshiaki Kameyama
2017-09-05 14:01:47 +03:00
committed by Mikhail Glukhikh
parent fe0f44da94
commit 5a5a27b983
55 changed files with 539 additions and 1 deletions
@@ -262,7 +262,10 @@ class KtPsiFactory @JvmOverloads constructor(private val project: Project, val m
}
fun createPropertySetter(expression: KtExpression): KtPropertyAccessor {
val property = createProperty("val x get() = 1\nset(value) = TODO()")
val property = if (expression is KtBlockExpression)
createProperty("val x get() = 1\nset(value) {\n field = value\n }")
else
createProperty("val x get() = 1\nset(value) = TODO()")
val setter = property.setter!!
val bodyExpression = setter.bodyExpression!!
@@ -0,0 +1,5 @@
var x = 1
<spot>get() = field
set(value) {
field = value
}</spot>
@@ -0,0 +1,5 @@
<html>
<body>
This intention adds a property getter and setter.
</body>
</html>
@@ -0,0 +1,2 @@
val x = 1
<spot>get() = field</spot>
@@ -0,0 +1,5 @@
<html>
<body>
This intention adds a property getter.
</body>
</html>
@@ -0,0 +1,4 @@
var x = 1
<spot>set(value) {
field = value
}</spot>
@@ -0,0 +1,5 @@
<html>
<body>
This intention adds a property setter.
</body>
</html>
+15
View File
@@ -1630,6 +1630,21 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.AddPropertyAccessorsIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.AddPropertyGetterIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.AddPropertySetterIntention</className>
<category>Kotlin</category>
</intentionAction>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
displayName="Object literal can be converted to lambda"
groupPath="Kotlin"
@@ -0,0 +1,73 @@
/*
* Copyright 2010-2017 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.codeInsight.intention.LowPriorityAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.idea.refactoring.isAbstract
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtPsiFactory
abstract class AbstractAddAccessorsIntention(
private val addGetter: Boolean,
private val addSetter: Boolean
) : SelfTargetingRangeIntention<KtProperty>(KtProperty::class.java, createFamilyName(addGetter, addSetter)) {
override fun applicabilityRange(element: KtProperty): TextRange? {
if (element.isLocal || element.isAbstract() || element.hasDelegate() ||
element.hasModifier(KtTokens.LATEINIT_KEYWORD) ||
element.hasModifier(KtTokens.HEADER_KEYWORD) ||
element.hasModifier(KtTokens.CONST_KEYWORD)) {
return null
}
if (addSetter && (!element.isVar || element.setter != null)) return null
if (addGetter && element.getter != null) return null
return element.nameIdentifier?.textRange
}
override fun applyTo(element: KtProperty, editor: Editor?) {
val psiFactory = KtPsiFactory(element)
if (addGetter) {
val expression = psiFactory.createExpression("field")
val getter = psiFactory.createPropertyGetter(expression)
if (element.setter != null)
element.addBefore(getter, element.setter)
else
element.add(getter)
}
if (addSetter) {
val expression = psiFactory.createBlock("field = value")
val setter = psiFactory.createPropertySetter(expression)
element.add(setter)
}
}
}
private fun createFamilyName(addGetter: Boolean, addSetter: Boolean): String = when {
addGetter && addSetter -> "Add getter and setter"
addGetter -> "Add getter"
addSetter -> "Add setter"
else -> throw AssertionError("At least one from (addGetter, addSetter) should be true")
}
class AddPropertyAccessorsIntention : AbstractAddAccessorsIntention(true, true), LowPriorityAction
class AddPropertyGetterIntention : AbstractAddAccessorsIntention(true, false)
class AddPropertySetterIntention : AbstractAddAccessorsIntention(false, true)
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.AddPropertyAccessorsIntention
@@ -0,0 +1,4 @@
// IS_APPLICABLE: false
abstract class Test {
abstract var x<caret>: Int
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: false
import kotlin.reflect.KProperty
class Test {
var x<caret>: String by Delegate()
}
class Delegate {
operator fun getValue(thisRef: Any?, property: KProperty<*>): String = ""
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {}
}
@@ -0,0 +1,8 @@
// IS_APPLICABLE: false
class Test {
var x<caret> = 1
get() = field
set(value) {
field = value
}
}
@@ -0,0 +1,5 @@
// IS_APPLICABLE: false
class Test {
var x<caret> = 1
get() = field
}
@@ -0,0 +1,7 @@
// IS_APPLICABLE: false
class Test {
var x<caret> = 1
set(value) {
field = value
}
}
@@ -0,0 +1,5 @@
// IS_APPLICABLE: false
class Foo
class Test {
lateinit var x<caret>: Foo
}
@@ -0,0 +1,6 @@
// IS_APPLICABLE: false
class Test {
fun test() {
var x<caret> = 1
}
}
@@ -0,0 +1 @@
var x<caret> = 1
@@ -0,0 +1,5 @@
var x = 1
get() = field
set(value) {
field = value
}
@@ -0,0 +1,4 @@
// IS_APPLICABLE: false
class Test {
val x<caret> = 1
}
@@ -0,0 +1,3 @@
class Test {
var x<caret> = 1
}
@@ -0,0 +1,7 @@
class Test {
var x<caret> = 1
get() = field
set(value) {
field = value
}
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.AddPropertyGetterIntention
@@ -0,0 +1,4 @@
// IS_APPLICABLE: false
abstract class Test {
abstract var x<caret>: Int
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: false
import kotlin.reflect.KProperty
class Test {
var x<caret>: String by Delegate()
}
class Delegate {
operator fun getValue(thisRef: Any?, property: KProperty<*>): String = ""
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {}
}
@@ -0,0 +1,8 @@
// IS_APPLICABLE: false
class Test {
var x<caret> = 1
get() = field
set(value) {
field = value
}
}
@@ -0,0 +1,5 @@
// IS_APPLICABLE: false
class Test {
var x<caret> = 1
get() = field
}
@@ -0,0 +1,6 @@
class Test {
var x<caret> = 1
set(value) {
field = value
}
}
@@ -0,0 +1,7 @@
class Test {
var x<caret> = 1
get() = field
set(value) {
field = value
}
}
@@ -0,0 +1,5 @@
// IS_APPLICABLE: false
class Foo
class Test {
lateinit var x<caret>: Foo
}
@@ -0,0 +1,6 @@
// IS_APPLICABLE: false
class Test {
fun test() {
var x<caret> = 1
}
}
@@ -0,0 +1 @@
var x<caret> = 1
@@ -0,0 +1,2 @@
var x = 1
get() = field
@@ -0,0 +1,3 @@
class Test {
val x<caret> = 1
}
@@ -0,0 +1,4 @@
class Test {
val x<caret> = 1
get() = field
}
@@ -0,0 +1,3 @@
class Test {
var x<caret> = 1
}
@@ -0,0 +1,4 @@
class Test {
var x<caret> = 1
get() = field
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.AddPropertySetterIntention
@@ -0,0 +1,4 @@
// IS_APPLICABLE: false
abstract class Test {
abstract var x<caret>: Int
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: false
import kotlin.reflect.KProperty
class Test {
var x<caret>: String by Delegate()
}
class Delegate {
operator fun getValue(thisRef: Any?, property: KProperty<*>): String = ""
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {}
}
@@ -0,0 +1,8 @@
// IS_APPLICABLE: false
class Test {
var x<caret> = 1
get() = field
set(value) {
field = value
}
}
@@ -0,0 +1,4 @@
class Test {
var x<caret> = 1
get() = field
}
@@ -0,0 +1,7 @@
class Test {
var x<caret> = 1
get() = field
set(value) {
field = value
}
}
@@ -0,0 +1,7 @@
// IS_APPLICABLE: false
class Test {
var x<caret> = 1
set(value) {
field = value
}
}
@@ -0,0 +1,5 @@
// IS_APPLICABLE: false
class Foo
class Test {
lateinit var x<caret>: Foo
}
@@ -0,0 +1,6 @@
// IS_APPLICABLE: false
class Test {
fun test() {
var x<caret> = 1
}
}
@@ -0,0 +1 @@
var x<caret> = 1
@@ -0,0 +1,4 @@
var x = 1
set(value) {
field = value
}
@@ -0,0 +1,4 @@
// IS_APPLICABLE: false
class Test {
val x<caret> = 1
}
@@ -0,0 +1,3 @@
class Test {
var x<caret> = 1
}
@@ -0,0 +1,6 @@
class Test {
var x<caret> = 1
set(value) {
field = value
}
}
@@ -738,6 +738,222 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/addPropertyAccessors")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class AddPropertyAccessors extends AbstractIntentionTest {
public void testAllFilesPresentInAddPropertyAccessors() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/addPropertyAccessors"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("idea/testData/intentions/addPropertyAccessors/both")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Both extends AbstractIntentionTest {
@TestMetadata("abstract.kt")
public void testAbstract() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/both/abstract.kt");
doTest(fileName);
}
public void testAllFilesPresentInBoth() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/addPropertyAccessors/both"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("delegate.kt")
public void testDelegate() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/both/delegate.kt");
doTest(fileName);
}
@TestMetadata("hasAccessor.kt")
public void testHasAccessor() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/both/hasAccessor.kt");
doTest(fileName);
}
@TestMetadata("hasGetter.kt")
public void testHasGetter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/both/hasGetter.kt");
doTest(fileName);
}
@TestMetadata("hasSetter.kt")
public void testHasSetter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/both/hasSetter.kt");
doTest(fileName);
}
@TestMetadata("lateinit.kt")
public void testLateinit() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/both/lateinit.kt");
doTest(fileName);
}
@TestMetadata("local.kt")
public void testLocal() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/both/local.kt");
doTest(fileName);
}
@TestMetadata("top.kt")
public void testTop() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/both/top.kt");
doTest(fileName);
}
@TestMetadata("val.kt")
public void testVal() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/both/val.kt");
doTest(fileName);
}
@TestMetadata("var.kt")
public void testVar() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/both/var.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/intentions/addPropertyAccessors/getter")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Getter extends AbstractIntentionTest {
@TestMetadata("abstract.kt")
public void testAbstract() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/getter/abstract.kt");
doTest(fileName);
}
public void testAllFilesPresentInGetter() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/addPropertyAccessors/getter"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("delegate.kt")
public void testDelegate() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/getter/delegate.kt");
doTest(fileName);
}
@TestMetadata("hasAccessor.kt")
public void testHasAccessor() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/getter/hasAccessor.kt");
doTest(fileName);
}
@TestMetadata("hasGetter.kt")
public void testHasGetter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/getter/hasGetter.kt");
doTest(fileName);
}
@TestMetadata("hasSetter.kt")
public void testHasSetter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/getter/hasSetter.kt");
doTest(fileName);
}
@TestMetadata("lateinit.kt")
public void testLateinit() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/getter/lateinit.kt");
doTest(fileName);
}
@TestMetadata("local.kt")
public void testLocal() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/getter/local.kt");
doTest(fileName);
}
@TestMetadata("top.kt")
public void testTop() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/getter/top.kt");
doTest(fileName);
}
@TestMetadata("val.kt")
public void testVal() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/getter/val.kt");
doTest(fileName);
}
@TestMetadata("var.kt")
public void testVar() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/getter/var.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/intentions/addPropertyAccessors/setter")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Setter extends AbstractIntentionTest {
@TestMetadata("abstract.kt")
public void testAbstract() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/setter/abstract.kt");
doTest(fileName);
}
public void testAllFilesPresentInSetter() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/addPropertyAccessors/setter"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("delegate.kt")
public void testDelegate() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/setter/delegate.kt");
doTest(fileName);
}
@TestMetadata("hasAccessor.kt")
public void testHasAccessor() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/setter/hasAccessor.kt");
doTest(fileName);
}
@TestMetadata("hasGetter.kt")
public void testHasGetter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/setter/hasGetter.kt");
doTest(fileName);
}
@TestMetadata("hasSetter.kt")
public void testHasSetter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/setter/hasSetter.kt");
doTest(fileName);
}
@TestMetadata("lateinit.kt")
public void testLateinit() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/setter/lateinit.kt");
doTest(fileName);
}
@TestMetadata("local.kt")
public void testLocal() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/setter/local.kt");
doTest(fileName);
}
@TestMetadata("top.kt")
public void testTop() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/setter/top.kt");
doTest(fileName);
}
@TestMetadata("val.kt")
public void testVal() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/setter/val.kt");
doTest(fileName);
}
@TestMetadata("var.kt")
public void testVar() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addPropertyAccessors/setter/var.kt");
doTest(fileName);
}
}
}
@TestMetadata("idea/testData/intentions/addValOrVar")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)