Remove Right Part of Binary Expression Quick-Fix: Convert to Kotlin & refactor
This commit is contained in:
@@ -10,8 +10,6 @@ insert.delegation.call=Insert ''{0}()'' call
|
|||||||
remove.parts.from.property=Remove {0} from property
|
remove.parts.from.property=Remove {0} from property
|
||||||
remove.parts.from.property.family=Remove parts from property
|
remove.parts.from.property.family=Remove parts from property
|
||||||
remove.psi.element.family=Remove element
|
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.type.arguments=Remove type arguments
|
||||||
remove.useless.nullable=Remove useless '?'
|
remove.useless.nullable=Remove useless '?'
|
||||||
remove.spread.sign=Remove '*'
|
remove.spread.sign=Remove '*'
|
||||||
|
|||||||
@@ -94,12 +94,12 @@ class QuickFixRegistrar : QuickFixContributor {
|
|||||||
AddFunctionToSupertypeFix)
|
AddFunctionToSupertypeFix)
|
||||||
VIRTUAL_MEMBER_HIDDEN.registerFactory(AddModifierFix.createFactory(OVERRIDE_KEYWORD))
|
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_SETTER_PARAMETER_TYPE.registerFactory(ChangeAccessorTypeFix)
|
||||||
WRONG_GETTER_RETURN_TYPE.registerFactory(ChangeAccessorTypeFix)
|
WRONG_GETTER_RETURN_TYPE.registerFactory(ChangeAccessorTypeFix)
|
||||||
|
|
||||||
USELESS_ELVIS.registerFactory(RemoveRightPartOfBinaryExpressionFix.createRemoveElvisOperatorFactory())
|
USELESS_ELVIS.registerFactory(RemoveRightPartOfBinaryExpressionFix.RemoveElvisOperatorFactory)
|
||||||
|
|
||||||
val removeRedundantModifierFactory = RemoveModifierFix.createRemoveModifierFactory(true)
|
val removeRedundantModifierFactory = RemoveModifierFix.createRemoveModifierFactory(true)
|
||||||
REDUNDANT_MODIFIER.registerFactory(removeRedundantModifierFactory)
|
REDUNDANT_MODIFIER.registerFactory(removeRedundantModifierFactory)
|
||||||
|
|||||||
+36
-69
@@ -14,87 +14,54 @@
|
|||||||
* limitations under the License.
|
* 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.editor.Editor
|
||||||
import com.intellij.openapi.project.Project;
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.psi.PsiElement;
|
import com.intellij.util.IncorrectOperationException
|
||||||
import com.intellij.util.IncorrectOperationException;
|
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.diagnostics.Diagnostic;
|
import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext
|
||||||
import org.jetbrains.kotlin.idea.KotlinBundle;
|
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
|
||||||
import org.jetbrains.kotlin.idea.core.quickfix.QuickFixUtil;
|
|
||||||
import org.jetbrains.kotlin.psi.*;
|
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.PsiUtilsKt;
|
|
||||||
|
|
||||||
public class RemoveRightPartOfBinaryExpressionFix<T extends KtExpression> extends KotlinQuickFixAction<T> implements CleanupFix {
|
class RemoveRightPartOfBinaryExpressionFix<T : KtExpression>(
|
||||||
private final String message;
|
element: T,
|
||||||
|
private val message: String
|
||||||
|
) : KotlinQuickFixAction<T>(element), CleanupFix {
|
||||||
|
override fun getFamilyName() = "Remove right part of a binary expression"
|
||||||
|
|
||||||
public RemoveRightPartOfBinaryExpressionFix(@NotNull T element, String message) {
|
override fun getText(): String = message
|
||||||
super(element);
|
|
||||||
this.message = message;
|
public override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
||||||
|
invoke()
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
fun invoke(): KtExpression {
|
||||||
@Override
|
val newExpression = when (element) {
|
||||||
public String getText() {
|
is KtBinaryExpression -> element.replace((element.copy() as KtBinaryExpression).left!!) as KtExpression
|
||||||
return message;
|
is KtBinaryExpressionWithTypeRHS -> element.replace((element.copy() as KtBinaryExpressionWithTypeRHS).left) as KtExpression
|
||||||
}
|
else -> throw IncorrectOperationException("Unexpected element: " + element.getElementTextWithContext())
|
||||||
|
|
||||||
@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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PsiElement parent = newExpression != null ? newExpression.getParent() : null;
|
val parent = newExpression.parent
|
||||||
if (parent instanceof KtParenthesizedExpression && KtPsiUtil.areParenthesesUseless((KtParenthesizedExpression) parent)) {
|
if (parent is KtParenthesizedExpression && KtPsiUtil.areParenthesesUseless(parent)) {
|
||||||
newExpression = (KtExpression) parent.replace(newExpression);
|
return parent.replace(newExpression) as KtExpression
|
||||||
}
|
}
|
||||||
|
return newExpression
|
||||||
if (newExpression == null) {
|
|
||||||
throw new IncorrectOperationException("Unexpected element: " + PsiUtilsKt.getElementTextWithContext(getElement()));
|
|
||||||
}
|
|
||||||
|
|
||||||
return newExpression;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static KotlinSingleIntentionActionFactory createRemoveTypeFromBinaryExpressionFactory(final String message) {
|
object RemoveCastFactory : KotlinSingleIntentionActionFactory() {
|
||||||
return new KotlinSingleIntentionActionFactory() {
|
public override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtBinaryExpressionWithTypeRHS>? {
|
||||||
@Override
|
val expression = diagnostic.psiElement.getNonStrictParentOfType<KtBinaryExpressionWithTypeRHS>() ?: return null
|
||||||
public KotlinQuickFixAction<KtBinaryExpressionWithTypeRHS> createAction(@NotNull Diagnostic diagnostic) {
|
return RemoveRightPartOfBinaryExpressionFix(expression, "Remove cast")
|
||||||
KtBinaryExpressionWithTypeRHS expression = QuickFixUtil.getParentElementOfType(diagnostic, KtBinaryExpressionWithTypeRHS.class);
|
}
|
||||||
if (expression == null) return null;
|
|
||||||
return new RemoveRightPartOfBinaryExpressionFix<KtBinaryExpressionWithTypeRHS>(expression, message);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static KotlinSingleIntentionActionFactory createRemoveElvisOperatorFactory() {
|
object RemoveElvisOperatorFactory : KotlinSingleIntentionActionFactory() {
|
||||||
return new KotlinSingleIntentionActionFactory() {
|
public override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtBinaryExpression>? {
|
||||||
@Override
|
val expression = diagnostic.psiElement as? KtBinaryExpression ?: return null
|
||||||
public KotlinQuickFixAction<KtBinaryExpression> createAction(@NotNull Diagnostic diagnostic) {
|
return RemoveRightPartOfBinaryExpressionFix(expression, "Remove elvis operator")
|
||||||
KtBinaryExpression expression = (KtBinaryExpression) diagnostic.getPsiElement();
|
}
|
||||||
return new RemoveRightPartOfBinaryExpressionFix<KtBinaryExpression>(expression, KotlinBundle.message("remove.elvis.operator"));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user