Remove 'val'/'var' from Parameter Quick-Fix: Convert to Kotlin & refactor
This commit is contained in:
@@ -128,7 +128,6 @@ migrate.class.object.to.companion.in.whole.project.modal.title=Replacing 'class'
|
||||
migrate.class.object.to.companion.in.whole.project.family=Replace 'class' Keyword with 'companion' Modifier in Whole Project
|
||||
migrate.lambda.syntax=Migrate lambda syntax
|
||||
migrate.lambda.syntax.family=Migrate lambda syntax
|
||||
remove.val.var.from.parameter=Remove ''{0}'' from parameter
|
||||
change.to.property.name.family.name=Change to property name
|
||||
change.to.property.name.action=Change ''{0}'' to ''{1}''
|
||||
create.from.usage.family=Create from usage
|
||||
|
||||
@@ -165,12 +165,11 @@ class QuickFixRegistrar : QuickFixContributor {
|
||||
VAL_REASSIGNMENT.registerFactory(ChangeVariableMutabilityFix.VAL_REASSIGNMENT_FACTORY)
|
||||
VAR_OVERRIDDEN_BY_VAL.registerFactory(ChangeVariableMutabilityFix.VAR_OVERRIDDEN_BY_VAL_FACTORY)
|
||||
|
||||
val removeValVarFromParameterFixFactory = RemoveValVarFromParameterFix.createFactory()
|
||||
VAL_OR_VAR_ON_FUN_PARAMETER.registerFactory(removeValVarFromParameterFixFactory)
|
||||
VAL_OR_VAR_ON_LOOP_PARAMETER.registerFactory(removeValVarFromParameterFixFactory)
|
||||
VAL_OR_VAR_ON_LOOP_MULTI_PARAMETER.registerFactory(removeValVarFromParameterFixFactory)
|
||||
VAL_OR_VAR_ON_CATCH_PARAMETER.registerFactory(removeValVarFromParameterFixFactory)
|
||||
VAL_OR_VAR_ON_SECONDARY_CONSTRUCTOR_PARAMETER.registerFactory(removeValVarFromParameterFixFactory)
|
||||
VAL_OR_VAR_ON_FUN_PARAMETER.registerFactory(RemoveValVarFromParameterFix)
|
||||
VAL_OR_VAR_ON_LOOP_PARAMETER.registerFactory(RemoveValVarFromParameterFix)
|
||||
VAL_OR_VAR_ON_LOOP_MULTI_PARAMETER.registerFactory(RemoveValVarFromParameterFix)
|
||||
VAL_OR_VAR_ON_CATCH_PARAMETER.registerFactory(RemoveValVarFromParameterFix)
|
||||
VAL_OR_VAR_ON_SECONDARY_CONSTRUCTOR_PARAMETER.registerFactory(RemoveValVarFromParameterFix)
|
||||
|
||||
VIRTUAL_MEMBER_HIDDEN.registerFactory(AddOverrideToEqualsHashCodeToStringActionFactory)
|
||||
|
||||
|
||||
@@ -14,58 +14,37 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.quickfix;
|
||||
package org.jetbrains.kotlin.idea.quickfix
|
||||
|
||||
import com.intellij.codeInsight.intention.IntentionAction;
|
||||
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.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic;
|
||||
import org.jetbrains.kotlin.idea.KotlinBundle;
|
||||
import org.jetbrains.kotlin.psi.KtFile;
|
||||
import org.jetbrains.kotlin.psi.KtValVarKeywordOwner;
|
||||
import org.jetbrains.kotlin.psi.psiUtil.PsiUtilsKt;
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.KtValVarKeywordOwner
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext
|
||||
|
||||
public class RemoveValVarFromParameterFix extends KotlinQuickFixAction<KtValVarKeywordOwner> {
|
||||
private final String varOrVal;
|
||||
class RemoveValVarFromParameterFix(element: KtValVarKeywordOwner) : KotlinQuickFixAction<KtValVarKeywordOwner>(element) {
|
||||
private val varOrVal: String
|
||||
|
||||
public RemoveValVarFromParameterFix(@NotNull KtValVarKeywordOwner element) {
|
||||
super(element);
|
||||
PsiElement valOrVarNode = element.getValOrVarKeyword();
|
||||
assert valOrVarNode != null : "Val or var node not found for " + PsiUtilsKt.getElementTextWithContext(element);
|
||||
varOrVal = valOrVarNode.getText();
|
||||
init {
|
||||
val valOrVarNode = element.valOrVarKeyword
|
||||
assert(valOrVarNode != null) { "Val or var node not found for " + element.getElementTextWithContext() }
|
||||
varOrVal = valOrVarNode!!.text
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getText() {
|
||||
return KotlinBundle.message("remove.val.var.from.parameter", varOrVal);
|
||||
override fun getFamilyName() = "Remove 'val/var' from parameter"
|
||||
|
||||
override fun getText(): String {
|
||||
val varOrVal = element.valOrVarKeyword?.text ?: return familyName
|
||||
return "Remove '$varOrVal' from parameter"
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getFamilyName() {
|
||||
return KotlinBundle.message("remove.val.var.from.parameter", "val/var");
|
||||
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
||||
element.valOrVarKeyword?.delete()
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void invoke(@NotNull Project project, Editor editor, @NotNull KtFile file) throws IncorrectOperationException {
|
||||
PsiElement keyword = getElement().getValOrVarKeyword();
|
||||
if (keyword == null) return;
|
||||
keyword.delete();
|
||||
}
|
||||
|
||||
|
||||
public static KotlinSingleIntentionActionFactory createFactory() {
|
||||
return new KotlinSingleIntentionActionFactory() {
|
||||
@Nullable
|
||||
@Override
|
||||
public IntentionAction createAction(@NotNull Diagnostic diagnostic) {
|
||||
return new RemoveValVarFromParameterFix((KtValVarKeywordOwner) diagnostic.getPsiElement().getParent());
|
||||
}
|
||||
};
|
||||
companion object : KotlinSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic) =
|
||||
RemoveValVarFromParameterFix(diagnostic.psiElement.parent as KtValVarKeywordOwner)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user