From 9a8aef6b6d186470223c5fda2ab7bfa5f1324246 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Wed, 6 Jan 2016 13:56:10 +0300 Subject: [PATCH] Remove Right Part of Binary Expression Quick-Fix: Convert to Kotlin & refactor --- .../kotlin/idea/KotlinBundle.properties | 2 - .../kotlin/idea/quickfix/QuickFixRegistrar.kt | 4 +- .../RemoveRightPartOfBinaryExpressionFix.kt | 107 ++++++------------ 3 files changed, 39 insertions(+), 74 deletions(-) diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/KotlinBundle.properties b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/KotlinBundle.properties index c2ced4cda32..370cbb85146 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/KotlinBundle.properties +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/KotlinBundle.properties @@ -10,8 +10,6 @@ insert.delegation.call=Insert ''{0}()'' call remove.parts.from.property=Remove {0} from property remove.parts.from.property.family=Remove parts from property remove.psi.element.family=Remove element -remove.right.part.of.binary.expression=Remove right part of a binary expression -remove.elvis.operator=Remove elvis operator remove.type.arguments=Remove type arguments remove.useless.nullable=Remove useless '?' remove.spread.sign=Remove '*' diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt index 1dfcdfe0edb..37bc431cd0a 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt @@ -94,12 +94,12 @@ class QuickFixRegistrar : QuickFixContributor { AddFunctionToSupertypeFix) VIRTUAL_MEMBER_HIDDEN.registerFactory(AddModifierFix.createFactory(OVERRIDE_KEYWORD)) - USELESS_CAST.registerFactory(RemoveRightPartOfBinaryExpressionFix.createRemoveTypeFromBinaryExpressionFactory("Remove cast")) + USELESS_CAST.registerFactory(RemoveRightPartOfBinaryExpressionFix.RemoveCastFactory) WRONG_SETTER_PARAMETER_TYPE.registerFactory(ChangeAccessorTypeFix) WRONG_GETTER_RETURN_TYPE.registerFactory(ChangeAccessorTypeFix) - USELESS_ELVIS.registerFactory(RemoveRightPartOfBinaryExpressionFix.createRemoveElvisOperatorFactory()) + USELESS_ELVIS.registerFactory(RemoveRightPartOfBinaryExpressionFix.RemoveElvisOperatorFactory) val removeRedundantModifierFactory = RemoveModifierFix.createRemoveModifierFactory(true) REDUNDANT_MODIFIER.registerFactory(removeRedundantModifierFactory) diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveRightPartOfBinaryExpressionFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveRightPartOfBinaryExpressionFix.kt index e233cd65c3b..ceb8d68b18b 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveRightPartOfBinaryExpressionFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveRightPartOfBinaryExpressionFix.kt @@ -14,87 +14,54 @@ * limitations under the License. */ -package org.jetbrains.kotlin.idea.quickfix; +package org.jetbrains.kotlin.idea.quickfix -import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.project.Project; -import com.intellij.psi.PsiElement; -import com.intellij.util.IncorrectOperationException; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.diagnostics.Diagnostic; -import org.jetbrains.kotlin.idea.KotlinBundle; -import org.jetbrains.kotlin.idea.core.quickfix.QuickFixUtil; -import org.jetbrains.kotlin.psi.*; -import org.jetbrains.kotlin.psi.psiUtil.PsiUtilsKt; +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.project.Project +import com.intellij.util.IncorrectOperationException +import org.jetbrains.kotlin.diagnostics.Diagnostic +import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext +import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType -public class RemoveRightPartOfBinaryExpressionFix extends KotlinQuickFixAction implements CleanupFix { - private final String message; - - public RemoveRightPartOfBinaryExpressionFix(@NotNull T element, String message) { - super(element); - this.message = message; +class RemoveRightPartOfBinaryExpressionFix( + element: T, + private val message: String +) : KotlinQuickFixAction(element), CleanupFix { + override fun getFamilyName() = "Remove right part of a binary expression" + + override fun getText(): String = message + + public override fun invoke(project: Project, editor: Editor?, file: KtFile) { + invoke() } - @NotNull - @Override - public String getText() { - return message; - } - - @NotNull - @Override - public String getFamilyName() { - return KotlinBundle.message("remove.right.part.of.binary.expression"); - } - - @Override - public void invoke(@NotNull Project project, Editor editor, @NotNull KtFile file) throws IncorrectOperationException { - invoke(); - } - - @NotNull - public KtExpression invoke() throws IncorrectOperationException { - KtExpression newExpression = null; - - if (getElement() instanceof KtBinaryExpression) { - //noinspection ConstantConditions - newExpression = (KtExpression) getElement().replace(((KtBinaryExpression) getElement().copy()).getLeft()); - } - else if (getElement() instanceof KtBinaryExpressionWithTypeRHS) { - newExpression = (KtExpression) getElement().replace(((KtBinaryExpressionWithTypeRHS) getElement().copy()).getLeft()); + fun invoke(): KtExpression { + val newExpression = when (element) { + is KtBinaryExpression -> element.replace((element.copy() as KtBinaryExpression).left!!) as KtExpression + is KtBinaryExpressionWithTypeRHS -> element.replace((element.copy() as KtBinaryExpressionWithTypeRHS).left) as KtExpression + else -> throw IncorrectOperationException("Unexpected element: " + element.getElementTextWithContext()) } - PsiElement parent = newExpression != null ? newExpression.getParent() : null; - if (parent instanceof KtParenthesizedExpression && KtPsiUtil.areParenthesesUseless((KtParenthesizedExpression) parent)) { - newExpression = (KtExpression) parent.replace(newExpression); + val parent = newExpression.parent + if (parent is KtParenthesizedExpression && KtPsiUtil.areParenthesesUseless(parent)) { + return parent.replace(newExpression) as KtExpression } - - if (newExpression == null) { - throw new IncorrectOperationException("Unexpected element: " + PsiUtilsKt.getElementTextWithContext(getElement())); - } - - return newExpression; + return newExpression } - public static KotlinSingleIntentionActionFactory createRemoveTypeFromBinaryExpressionFactory(final String message) { - return new KotlinSingleIntentionActionFactory() { - @Override - public KotlinQuickFixAction createAction(@NotNull Diagnostic diagnostic) { - KtBinaryExpressionWithTypeRHS expression = QuickFixUtil.getParentElementOfType(diagnostic, KtBinaryExpressionWithTypeRHS.class); - if (expression == null) return null; - return new RemoveRightPartOfBinaryExpressionFix(expression, message); - } - }; + object RemoveCastFactory : KotlinSingleIntentionActionFactory() { + public override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction? { + val expression = diagnostic.psiElement.getNonStrictParentOfType() ?: return null + return RemoveRightPartOfBinaryExpressionFix(expression, "Remove cast") + } } - public static KotlinSingleIntentionActionFactory createRemoveElvisOperatorFactory() { - return new KotlinSingleIntentionActionFactory() { - @Override - public KotlinQuickFixAction createAction(@NotNull Diagnostic diagnostic) { - KtBinaryExpression expression = (KtBinaryExpression) diagnostic.getPsiElement(); - return new RemoveRightPartOfBinaryExpressionFix(expression, KotlinBundle.message("remove.elvis.operator")); - } - }; + object RemoveElvisOperatorFactory : KotlinSingleIntentionActionFactory() { + public override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction? { + val expression = diagnostic.psiElement as? KtBinaryExpression ?: return null + return RemoveRightPartOfBinaryExpressionFix(expression, "Remove elvis operator") + } } }