Adjust "Remove explicit type" intention to properties with getters
#KT-14794 Fixed
This commit is contained in:
@@ -22,7 +22,6 @@ import com.intellij.openapi.util.TextRange
|
|||||||
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
|
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getStartOffsetIn
|
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||||
|
|
||||||
class RemoveSetterParameterTypeInspection()
|
class RemoveSetterParameterTypeInspection()
|
||||||
@@ -48,10 +47,14 @@ class RemoveExplicitTypeIntention : SelfTargetingRangeIntention<KtCallableDeclar
|
|||||||
return element.textRange
|
return element.textRange
|
||||||
}
|
}
|
||||||
|
|
||||||
val initializer = (element as? KtDeclarationWithInitializer)?.initializer ?: return null
|
val initializer = (element as? KtDeclarationWithInitializer)?.initializer
|
||||||
if (element !is KtProperty && (element !is KtNamedFunction || element.hasBlockBody())) return null
|
if (element !is KtProperty && (element !is KtNamedFunction || element.hasBlockBody())) return null
|
||||||
|
|
||||||
return TextRange(element.startOffset, initializer.startOffset - 1)
|
return when {
|
||||||
|
initializer != null -> TextRange(element.startOffset, initializer.startOffset - 1)
|
||||||
|
element is KtProperty && element.getter != null -> TextRange(element.startOffset, element.typeReference!!.endOffset)
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun applyTo(element: KtCallableDeclaration, editor: Editor?) {
|
override fun applyTo(element: KtCallableDeclaration, editor: Editor?) {
|
||||||
@@ -64,4 +67,4 @@ class RemoveExplicitTypeIntention : SelfTargetingRangeIntention<KtCallableDeclar
|
|||||||
|
|
||||||
private val KtParameter.isSetterParameter: Boolean get() = (parent?.parent as? KtPropertyAccessor)?.isSetter ?: false
|
private val KtParameter.isSetterParameter: Boolean get() = (parent?.parent as? KtPropertyAccessor)?.isSetter ?: false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
val x: Int<caret> get() = 1
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
val x get() = 1
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
// "Delete redundant extension property" "false"
|
// "Delete redundant extension property" "false"
|
||||||
// ACTION: Create test
|
// ACTION: Create test
|
||||||
|
// ACTION: Remove explicit type specification
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
var File.<caret>name: String
|
var File.<caret>name: String
|
||||||
get() = getName()
|
get() = getName()
|
||||||
set(value) {}
|
set(value) {}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
// "Delete redundant extension property" "false"
|
// "Delete redundant extension property" "false"
|
||||||
// ACTION: Convert property to function
|
// ACTION: Convert property to function
|
||||||
// ACTION: Move to companion object
|
// ACTION: Move to companion object
|
||||||
|
// ACTION: Remove explicit type specification
|
||||||
|
|
||||||
class C : Thread() {
|
class C : Thread() {
|
||||||
val Thread.<caret>priority: Int
|
val Thread.<caret>priority: Int
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
// "Delete redundant extension property" "false"
|
// "Delete redundant extension property" "false"
|
||||||
// ACTION: Move to companion object
|
// ACTION: Move to companion object
|
||||||
|
// ACTION: Remove explicit type specification
|
||||||
|
|
||||||
class C : Thread() {
|
class C : Thread() {
|
||||||
var Thread.<caret>priority: Int
|
var Thread.<caret>priority: Int
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
// "Delete redundant extension property" "false"
|
// "Delete redundant extension property" "false"
|
||||||
// ACTION: Create test
|
// ACTION: Create test
|
||||||
|
// ACTION: Remove explicit type specification
|
||||||
|
|
||||||
var Thread.<caret>priority: Int
|
var Thread.<caret>priority: Int
|
||||||
get() = getPriority() + 1
|
get() = getPriority() + 1
|
||||||
set(value) {
|
set(value) {
|
||||||
setPriority(value)
|
setPriority(value)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
// "Delete redundant extension property" "false"
|
// "Delete redundant extension property" "false"
|
||||||
// ACTION: Convert property to function
|
// ACTION: Convert property to function
|
||||||
// ACTION: Create test
|
// ACTION: Create test
|
||||||
|
// ACTION: Remove explicit type specification
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
public val File.<caret>parent: File?
|
public val File.<caret>parent: File?
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
// "Delete redundant extension property" "false"
|
// "Delete redundant extension property" "false"
|
||||||
// ACTION: Create test
|
// ACTION: Create test
|
||||||
|
// ACTION: Remove explicit type specification
|
||||||
|
|
||||||
var Thread.<caret>priority: Int
|
var Thread.<caret>priority: Int
|
||||||
get() = this.getPriority()
|
get() = this.getPriority()
|
||||||
|
|||||||
@@ -11030,6 +11030,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("propertyTypeFromGetter.kt")
|
||||||
|
public void testPropertyTypeFromGetter() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeExplicitType/propertyTypeFromGetter.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("removeUnresolvedType.kt")
|
@TestMetadata("removeUnresolvedType.kt")
|
||||||
public void testRemoveUnresolvedType() throws Exception {
|
public void testRemoveUnresolvedType() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeExplicitType/removeUnresolvedType.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeExplicitType/removeUnresolvedType.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user