Refactored replace with safe/dot call fixes
This commit is contained in:
@@ -11,7 +11,6 @@ remove.parts.from.property.family=Remove parts from property
|
|||||||
remove.useless.nullable=Remove useless '?'
|
remove.useless.nullable=Remove useless '?'
|
||||||
replace.operation.in.binary.expression=Replace operation in a binary expression
|
replace.operation.in.binary.expression=Replace operation in a binary expression
|
||||||
replace.cast.with.static.assert=Replace a cast with a static assert
|
replace.cast.with.static.assert=Replace a cast with a static assert
|
||||||
replace.with.dot.call=Replace with dot call
|
|
||||||
introduce.non.null.assertion=Add non-null asserted (!!) call
|
introduce.non.null.assertion=Add non-null asserted (!!) call
|
||||||
remove.unnecessary.non.null.assertion=Remove unnecessary non-null assertion (!!)
|
remove.unnecessary.non.null.assertion=Remove unnecessary non-null assertion (!!)
|
||||||
change.to.backing.field=Change reference to backing field
|
change.to.backing.field=Change reference to backing field
|
||||||
|
|||||||
@@ -19,64 +19,48 @@ package org.jetbrains.kotlin.idea.quickfix
|
|||||||
import com.intellij.codeInsight.intention.IntentionAction
|
import com.intellij.codeInsight.intention.IntentionAction
|
||||||
import com.intellij.openapi.editor.Editor
|
import com.intellij.openapi.editor.Editor
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.psi.PsiElement
|
|
||||||
import com.intellij.psi.PsiFile
|
import com.intellij.psi.PsiFile
|
||||||
import com.intellij.psi.util.PsiTreeUtil
|
|
||||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||||
import org.jetbrains.kotlin.idea.KotlinBundle
|
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
||||||
|
|
||||||
abstract class ReplaceCallFix protected constructor(val psiElement: PsiElement) : IntentionAction {
|
abstract class ReplaceCallFix(
|
||||||
|
expression: KtQualifiedExpression,
|
||||||
|
private val operation: String
|
||||||
|
) : KotlinQuickFixAction<KtQualifiedExpression>(expression) {
|
||||||
|
|
||||||
override fun getFamilyName(): String = text
|
override fun getFamilyName() = text
|
||||||
|
|
||||||
override fun isAvailable(project: Project, editor: Editor?, file: PsiFile): Boolean {
|
override fun isAvailable(project: Project, editor: Editor?, file: PsiFile): Boolean {
|
||||||
if (file is KtFile) {
|
return super.isAvailable(project, editor, file) && element.selectorExpression != null
|
||||||
return getCallExpression() != null
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun invoke(project: Project, editor: Editor?, file: PsiFile) {
|
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
||||||
val callExpression = getCallExpression() ?: return
|
val newExpression = KtPsiFactory(element).createExpressionByPattern("$0$operation$1",
|
||||||
|
element.receiverExpression, element.selectorExpression!!)
|
||||||
val selector = callExpression.selectorExpression
|
element.replace(newExpression)
|
||||||
if (selector != null) {
|
|
||||||
val newElement = KtPsiFactory(callExpression).createExpression(
|
|
||||||
callExpression.receiverExpression.text + operation + selector.text) as KtQualifiedExpression
|
|
||||||
|
|
||||||
callExpression.replace(newElement)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun startInWriteAction(): Boolean = true
|
|
||||||
|
|
||||||
private fun getCallExpression(): KtQualifiedExpression? {
|
|
||||||
return PsiTreeUtil.getParentOfType(psiElement, classToReplace)
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract val operation: String
|
|
||||||
abstract val classToReplace: Class<out KtQualifiedExpression>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class ReplaceWithSafeCallFix(psiElement: PsiElement): ReplaceCallFix(psiElement) {
|
class ReplaceWithSafeCallFix(expression: KtDotQualifiedExpression): ReplaceCallFix(expression, "?.") {
|
||||||
override fun getText(): String = "Replace with safe (?.) call"
|
|
||||||
override val operation: String get() = "?."
|
override fun getText() = "Replace with safe (?.) call"
|
||||||
override val classToReplace: Class<out KtQualifiedExpression> get() = KtDotQualifiedExpression::class.java
|
|
||||||
|
|
||||||
companion object : KotlinSingleIntentionActionFactory() {
|
companion object : KotlinSingleIntentionActionFactory() {
|
||||||
override fun createAction(diagnostic: Diagnostic): IntentionAction
|
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
||||||
= ReplaceWithSafeCallFix(diagnostic.psiElement)
|
val qualifiedExpression = diagnostic.psiElement.getParentOfType<KtDotQualifiedExpression>(strict = false) ?: return null
|
||||||
|
return ReplaceWithSafeCallFix(qualifiedExpression)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ReplaceWithDotCallFix(psiElement: PsiElement): ReplaceCallFix(psiElement), CleanupFix {
|
class ReplaceWithDotCallFix(expression: KtSafeQualifiedExpression): ReplaceCallFix(expression, "."), CleanupFix {
|
||||||
override fun getText(): String = KotlinBundle.message("replace.with.dot.call")
|
override fun getText() = "Replace with dot call"
|
||||||
override val operation: String get() = "."
|
|
||||||
override val classToReplace: Class<out KtQualifiedExpression> get() = KtSafeQualifiedExpression::class.java
|
|
||||||
|
|
||||||
companion object : KotlinSingleIntentionActionFactory() {
|
companion object : KotlinSingleIntentionActionFactory() {
|
||||||
override fun createAction(diagnostic: Diagnostic): IntentionAction
|
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
||||||
= ReplaceWithDotCallFix(diagnostic.psiElement)
|
val qualifiedExpression = diagnostic.psiElement.getParentOfType<KtSafeQualifiedExpression>(strict = false) ?: return null
|
||||||
|
return ReplaceWithDotCallFix(qualifiedExpression)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user