Remove Element Quick-Fixes: Convert to Kotlin & refactor

This commit is contained in:
Alexey Sedunov
2016-01-06 14:27:28 +03:00
parent 28a0e8ebe6
commit ed679fef9d
3 changed files with 49 additions and 105 deletions
@@ -9,12 +9,7 @@ add.init.keyword.in.whole.project.family=Add 'init' keyword in whole project
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.type.arguments=Remove type arguments
remove.useless.nullable=Remove useless '?'
remove.spread.sign=Remove '*'
remove.conflicting.import=Remove conflicting import for ''{0}''
remove.conflicting.import.family=Remove Conflicting Import
replace.operation.in.binary.expression=Replace operation in a binary expression
replace.cast.with.static.assert=Replace a cast with a static assert
replace.with.dot.call=Replace with dot call
@@ -135,8 +130,6 @@ surround.with.when.template=when (expr) {}
surround.with.runtime.type.cast.template=(expr as RuntimeType)
surround.with.function.template={ }
surround.with.cannot.perform.action=Cannot perform Surround With action to the current contextsurround.with.function.template={ }
remove.variable.family.name=Remove variable
remove.variable.action=Remove variable ''{0}''
kotlin.code.transformations=Kotlin Code Transformations
fold.if.to.call=Replace 'if' expression with method call
fold.if.to.call.family=Replace 'if' Expression with Method Call
@@ -83,7 +83,7 @@ class QuickFixRegistrar : QuickFixContributor {
NON_ABSTRACT_FUNCTION_WITH_NO_BODY.registerFactory(addAbstractModifierFactory, AddFunctionBodyFix)
NON_VARARG_SPREAD.registerFactory(RemovePsiElementSimpleFix.createRemoveSpreadFactory())
NON_VARARG_SPREAD.registerFactory(RemovePsiElementSimpleFix.RemoveSpreadFactory)
MIXING_NAMED_AND_POSITIONED_ARGUMENTS.registerFactory(AddNameToArgumentFix)
@@ -137,8 +137,7 @@ class QuickFixRegistrar : QuickFixContributor {
NO_GET_METHOD.registerFactory(MissingArrayAccessorAutoImportFix)
NO_SET_METHOD.registerFactory(MissingArrayAccessorAutoImportFix)
val removeImportFixFactory = RemovePsiElementSimpleFix.createRemoveImportFactory()
CONFLICTING_IMPORT.registerFactory(removeImportFixFactory)
CONFLICTING_IMPORT.registerFactory(RemovePsiElementSimpleFix.RemoveImportFactory)
SUPERTYPE_NOT_INITIALIZED.registerFactory(SuperClassNotInitialized)
FUNCTION_CALL_EXPECTED.registerFactory(ChangeToFunctionInvocationFix)
@@ -173,7 +172,7 @@ class QuickFixRegistrar : QuickFixContributor {
VIRTUAL_MEMBER_HIDDEN.registerFactory(AddOverrideToEqualsHashCodeToStringActionFactory)
UNUSED_VARIABLE.registerFactory(RemovePsiElementSimpleFix.createRemoveVariableFactory())
UNUSED_VARIABLE.registerFactory(RemovePsiElementSimpleFix.RemoveVariableFactory)
UNNECESSARY_SAFE_CALL.registerFactory(ReplaceWithDotCallFix)
UNSAFE_CALL.registerFactory(ReplaceWithSafeCallFix)
@@ -192,7 +191,7 @@ class QuickFixRegistrar : QuickFixContributor {
NO_TYPE_ARGUMENTS_ON_RHS.registerFactory(AddStarProjectionsFix.IsExpressionFactory)
WRONG_NUMBER_OF_TYPE_ARGUMENTS.registerFactory(AddStarProjectionsFix.JavaClassFactory)
TYPE_ARGUMENTS_REDUNDANT_IN_SUPER_QUALIFIER.registerFactory(RemovePsiElementSimpleFix.createRemoveTypeArgumentsFactory())
TYPE_ARGUMENTS_REDUNDANT_IN_SUPER_QUALIFIER.registerFactory(RemovePsiElementSimpleFix.RemoveTypeArgumentsFactory)
UNCHECKED_CAST.registerFactory(ChangeToStarProjectionFix)
CANNOT_CHECK_FOR_ERASED.registerFactory(ChangeToStarProjectionFix)
@@ -14,113 +14,65 @@
* 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.psi.impl.source.tree.LeafPsiElement;
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.lexer.KtTokens;
import org.jetbrains.kotlin.psi.*;
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtImportDirective
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtTypeArgumentList
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
public class RemovePsiElementSimpleFix extends KotlinQuickFixAction<PsiElement> {
open class RemovePsiElementSimpleFix(element: PsiElement, private val text: String) : KotlinQuickFixAction<PsiElement>(element) {
override fun getFamilyName() = "Remove element"
private final PsiElement element;
private final String text;
override fun getText() = text
public RemovePsiElementSimpleFix(@NotNull PsiElement el, @NotNull String txt) {
super(el);
element = el;
text = txt;
public override fun invoke(project: Project, editor: Editor?, file: KtFile) {
element.delete()
}
@NotNull
@Override
public String getText() {
return text;
object RemoveImportFactory : KotlinSingleIntentionActionFactory() {
public override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<PsiElement>? {
val directive = diagnostic.psiElement.getNonStrictParentOfType<KtImportDirective>() ?: return null
val refText = directive.importedReference?.let { "for '${it.text}'" } ?: ""
return RemovePsiElementSimpleFix(directive, "Remove conflicting import $refText")
}
}
@NotNull
@Override
public String getFamilyName() {
return KotlinBundle.message("remove.psi.element.family");
object RemoveSpreadFactory : KotlinSingleIntentionActionFactory() {
public override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<PsiElement>? {
val element = diagnostic.psiElement
if (element.node.elementType != KtTokens.MUL) return null
return RemovePsiElementSimpleFix(element, "Remove '*'")
}
}
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull KtFile file) throws IncorrectOperationException {
element.delete();
object RemoveTypeArgumentsFactory : KotlinSingleIntentionActionFactory() {
public override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<PsiElement>? {
val element = diagnostic.psiElement.getNonStrictParentOfType<KtTypeArgumentList>() ?: return null
return RemovePsiElementSimpleFix(element, "Remove type arguments")
}
}
public static KotlinSingleIntentionActionFactory createRemoveImportFactory() {
return new KotlinSingleIntentionActionFactory() {
@Override
public KotlinQuickFixAction<PsiElement> createAction(@NotNull Diagnostic diagnostic) {
KtImportDirective directive = QuickFixUtil.getParentElementOfType(diagnostic, KtImportDirective.class);
if (directive == null) return null;
else {
KtExpression exp = directive.getImportedReference();
if (exp != null) {
return new RemovePsiElementSimpleFix(directive,
KotlinBundle.message("remove.conflicting.import", exp.getText()));
object RemoveVariableFactory : KotlinSingleIntentionActionFactory() {
public override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<PsiElement>? {
val expression = diagnostic.psiElement.getNonStrictParentOfType<KtProperty>() ?: return null
return object : RemovePsiElementSimpleFix(expression, "Remove variable '${expression.name}'") {
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
val initializer = expression.initializer
if (initializer != null) {
expression.replace(initializer)
}
else {
expression.delete()
}
return new RemovePsiElementSimpleFix(directive,
KotlinBundle.message("remove.conflicting.import", ""));
}
}
};
}
public static KotlinSingleIntentionActionFactory createRemoveSpreadFactory() {
return new KotlinSingleIntentionActionFactory() {
@Override
public KotlinQuickFixAction<PsiElement> createAction(@NotNull Diagnostic diagnostic) {
PsiElement element = diagnostic.getPsiElement();
if ((element instanceof LeafPsiElement) && ((LeafPsiElement) element).getElementType() == KtTokens.MUL) {
return new RemovePsiElementSimpleFix(element, KotlinBundle.message("remove.spread.sign"));
}
else return null;
}
};
}
public static KotlinSingleIntentionActionFactory createRemoveTypeArgumentsFactory() {
return new KotlinSingleIntentionActionFactory() {
@Override
public KotlinQuickFixAction<PsiElement> createAction(@NotNull Diagnostic diagnostic) {
KtTypeArgumentList element = QuickFixUtil.getParentElementOfType(diagnostic, KtTypeArgumentList.class);
if (element == null) return null;
return new RemovePsiElementSimpleFix(element,
KotlinBundle.message("remove.type.arguments"));
}
};
}
public static KotlinSingleIntentionActionFactory createRemoveVariableFactory() {
return new KotlinSingleIntentionActionFactory() {
@Override
public KotlinQuickFixAction<PsiElement> createAction(@NotNull Diagnostic diagnostic) {
final KtProperty expression = QuickFixUtil.getParentElementOfType(diagnostic, KtProperty.class);
if (expression == null) return null;
return new RemovePsiElementSimpleFix(expression,
KotlinBundle.message("remove.variable.action", (expression.getName()))) {
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull KtFile file) throws IncorrectOperationException {
KtExpression initializer = expression.getInitializer();
if (initializer != null) {
expression.replace(initializer);
}
else {
expression.delete();
}
}
};
}
};
}
}
}