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 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.type.arguments=Remove type arguments
remove.useless.nullable=Remove useless '?' 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.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 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.runtime.type.cast.template=(expr as RuntimeType)
surround.with.function.template={ } surround.with.function.template={ }
surround.with.cannot.perform.action=Cannot perform Surround With action to the current contextsurround.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 kotlin.code.transformations=Kotlin Code Transformations
fold.if.to.call=Replace 'if' expression with method call fold.if.to.call=Replace 'if' expression with method call
fold.if.to.call.family=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_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) MIXING_NAMED_AND_POSITIONED_ARGUMENTS.registerFactory(AddNameToArgumentFix)
@@ -137,8 +137,7 @@ class QuickFixRegistrar : QuickFixContributor {
NO_GET_METHOD.registerFactory(MissingArrayAccessorAutoImportFix) NO_GET_METHOD.registerFactory(MissingArrayAccessorAutoImportFix)
NO_SET_METHOD.registerFactory(MissingArrayAccessorAutoImportFix) NO_SET_METHOD.registerFactory(MissingArrayAccessorAutoImportFix)
val removeImportFixFactory = RemovePsiElementSimpleFix.createRemoveImportFactory() CONFLICTING_IMPORT.registerFactory(RemovePsiElementSimpleFix.RemoveImportFactory)
CONFLICTING_IMPORT.registerFactory(removeImportFixFactory)
SUPERTYPE_NOT_INITIALIZED.registerFactory(SuperClassNotInitialized) SUPERTYPE_NOT_INITIALIZED.registerFactory(SuperClassNotInitialized)
FUNCTION_CALL_EXPECTED.registerFactory(ChangeToFunctionInvocationFix) FUNCTION_CALL_EXPECTED.registerFactory(ChangeToFunctionInvocationFix)
@@ -173,7 +172,7 @@ class QuickFixRegistrar : QuickFixContributor {
VIRTUAL_MEMBER_HIDDEN.registerFactory(AddOverrideToEqualsHashCodeToStringActionFactory) VIRTUAL_MEMBER_HIDDEN.registerFactory(AddOverrideToEqualsHashCodeToStringActionFactory)
UNUSED_VARIABLE.registerFactory(RemovePsiElementSimpleFix.createRemoveVariableFactory()) UNUSED_VARIABLE.registerFactory(RemovePsiElementSimpleFix.RemoveVariableFactory)
UNNECESSARY_SAFE_CALL.registerFactory(ReplaceWithDotCallFix) UNNECESSARY_SAFE_CALL.registerFactory(ReplaceWithDotCallFix)
UNSAFE_CALL.registerFactory(ReplaceWithSafeCallFix) UNSAFE_CALL.registerFactory(ReplaceWithSafeCallFix)
@@ -192,7 +191,7 @@ class QuickFixRegistrar : QuickFixContributor {
NO_TYPE_ARGUMENTS_ON_RHS.registerFactory(AddStarProjectionsFix.IsExpressionFactory) NO_TYPE_ARGUMENTS_ON_RHS.registerFactory(AddStarProjectionsFix.IsExpressionFactory)
WRONG_NUMBER_OF_TYPE_ARGUMENTS.registerFactory(AddStarProjectionsFix.JavaClassFactory) 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) UNCHECKED_CAST.registerFactory(ChangeToStarProjectionFix)
CANNOT_CHECK_FOR_ERASED.registerFactory(ChangeToStarProjectionFix) CANNOT_CHECK_FOR_ERASED.registerFactory(ChangeToStarProjectionFix)
@@ -14,113 +14,65 @@
* 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.psi.PsiElement
import com.intellij.psi.impl.source.tree.LeafPsiElement; import org.jetbrains.kotlin.diagnostics.Diagnostic
import com.intellij.util.IncorrectOperationException; import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.diagnostics.Diagnostic; import org.jetbrains.kotlin.psi.KtImportDirective
import org.jetbrains.kotlin.idea.KotlinBundle; import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.idea.core.quickfix.QuickFixUtil; import org.jetbrains.kotlin.psi.KtTypeArgumentList
import org.jetbrains.kotlin.lexer.KtTokens; import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
import org.jetbrains.kotlin.psi.*;
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; override fun getText() = text
private final String text;
public override fun invoke(project: Project, editor: Editor?, file: KtFile) {
public RemovePsiElementSimpleFix(@NotNull PsiElement el, @NotNull String txt) { element.delete()
super(el);
element = el;
text = txt;
} }
@NotNull object RemoveImportFactory : KotlinSingleIntentionActionFactory() {
@Override public override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<PsiElement>? {
public String getText() { val directive = diagnostic.psiElement.getNonStrictParentOfType<KtImportDirective>() ?: return null
return text; val refText = directive.importedReference?.let { "for '${it.text}'" } ?: ""
return RemovePsiElementSimpleFix(directive, "Remove conflicting import $refText")
}
} }
@NotNull object RemoveSpreadFactory : KotlinSingleIntentionActionFactory() {
@Override public override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<PsiElement>? {
public String getFamilyName() { val element = diagnostic.psiElement
return KotlinBundle.message("remove.psi.element.family"); if (element.node.elementType != KtTokens.MUL) return null
return RemovePsiElementSimpleFix(element, "Remove '*'")
}
} }
@Override object RemoveTypeArgumentsFactory : KotlinSingleIntentionActionFactory() {
public void invoke(@NotNull Project project, Editor editor, @NotNull KtFile file) throws IncorrectOperationException { public override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<PsiElement>? {
element.delete(); val element = diagnostic.psiElement.getNonStrictParentOfType<KtTypeArgumentList>() ?: return null
return RemovePsiElementSimpleFix(element, "Remove type arguments")
}
} }
public static KotlinSingleIntentionActionFactory createRemoveImportFactory() { object RemoveVariableFactory : KotlinSingleIntentionActionFactory() {
return new KotlinSingleIntentionActionFactory() { public override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<PsiElement>? {
@Override val expression = diagnostic.psiElement.getNonStrictParentOfType<KtProperty>() ?: return null
public KotlinQuickFixAction<PsiElement> createAction(@NotNull Diagnostic diagnostic) { return object : RemovePsiElementSimpleFix(expression, "Remove variable '${expression.name}'") {
KtImportDirective directive = QuickFixUtil.getParentElementOfType(diagnostic, KtImportDirective.class); override fun invoke(project: Project, editor: Editor?, file: KtFile) {
if (directive == null) return null; val initializer = expression.initializer
else { if (initializer != null) {
KtExpression exp = directive.getImportedReference(); expression.replace(initializer)
if (exp != null) { }
return new RemovePsiElementSimpleFix(directive, else {
KotlinBundle.message("remove.conflicting.import", exp.getText())); 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();
}
}
};
}
};
} }
} }