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.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getStartOffsetIn
|
||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||
|
||||
class RemoveSetterParameterTypeInspection()
|
||||
@@ -48,10 +47,14 @@ class RemoveExplicitTypeIntention : SelfTargetingRangeIntention<KtCallableDeclar
|
||||
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
|
||||
|
||||
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?) {
|
||||
@@ -64,4 +67,4 @@ class RemoveExplicitTypeIntention : SelfTargetingRangeIntention<KtCallableDeclar
|
||||
|
||||
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"
|
||||
// ACTION: Create test
|
||||
// ACTION: Remove explicit type specification
|
||||
import java.io.File
|
||||
|
||||
var File.<caret>name: String
|
||||
get() = getName()
|
||||
set(value) {}
|
||||
set(value) {}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// "Delete redundant extension property" "false"
|
||||
// ACTION: Convert property to function
|
||||
// ACTION: Move to companion object
|
||||
// ACTION: Remove explicit type specification
|
||||
|
||||
class C : Thread() {
|
||||
val Thread.<caret>priority: Int
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// "Delete redundant extension property" "false"
|
||||
// ACTION: Move to companion object
|
||||
// ACTION: Remove explicit type specification
|
||||
|
||||
class C : Thread() {
|
||||
var Thread.<caret>priority: Int
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
// "Delete redundant extension property" "false"
|
||||
// ACTION: Create test
|
||||
// ACTION: Remove explicit type specification
|
||||
|
||||
var Thread.<caret>priority: Int
|
||||
get() = getPriority() + 1
|
||||
set(value) {
|
||||
setPriority(value)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// "Delete redundant extension property" "false"
|
||||
// ACTION: Convert property to function
|
||||
// ACTION: Create test
|
||||
// ACTION: Remove explicit type specification
|
||||
import java.io.File
|
||||
|
||||
public val File.<caret>parent: File?
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// "Delete redundant extension property" "false"
|
||||
// ACTION: Create test
|
||||
// ACTION: Remove explicit type specification
|
||||
|
||||
var Thread.<caret>priority: Int
|
||||
get() = this.getPriority()
|
||||
|
||||
@@ -11030,6 +11030,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
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")
|
||||
public void testRemoveUnresolvedType() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeExplicitType/removeUnresolvedType.kt");
|
||||
|
||||
Reference in New Issue
Block a user