[java-inspections] InsertMissingTokenFix, MakeVarargParameterLastFix: ModCommand

GitOrigin-RevId: 96119c9bc878c26e2770414553c4b5805df7da7c
This commit is contained in:
Tagir Valeev
2023-07-18 12:27:36 +00:00
committed by intellij-monorepo-bot
parent b47a759d98
commit 0ab9627c63
4 changed files with 40 additions and 72 deletions
@@ -1,16 +1,15 @@
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.daemon.impl.actions.IntentionActionWithFixAllOption;
import com.intellij.codeInsight.intention.LowPriorityAction;
import com.intellij.codeInsight.intention.PriorityAction;
import com.intellij.codeInspection.ModCommands;
import com.intellij.ide.IdeBundle;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;
import com.intellij.util.IncorrectOperationException;
import com.intellij.modcommand.ModCommand;
import com.intellij.modcommand.ModCommandAction;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class InsertMissingTokenFix implements IntentionActionWithFixAllOption, LowPriorityAction {
public class InsertMissingTokenFix implements ModCommandAction {
private final String myToken;
public InsertMissingTokenFix(String token) {
@@ -18,34 +17,19 @@ public class InsertMissingTokenFix implements IntentionActionWithFixAllOption, L
}
@Override
public @NotNull String getText() {
return IdeBundle.message("quickfix.text.insert.0", myToken);
public @Nullable Presentation getPresentation(@NotNull ActionContext context) {
return Presentation.of(getFamilyName())
.withPriority(PriorityAction.Priority.LOW)
.withFixAllOption(this, action -> action instanceof InsertMissingTokenFix tokenFix && tokenFix.myToken.equals(myToken));
}
@Override
public @NotNull String getFamilyName() {
return getText();
return IdeBundle.message("quickfix.text.insert.0", myToken);
}
@Override
public boolean isAvailable(@NotNull Project project,
Editor editor,
PsiFile file) {
return true;
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
editor.getDocument().insertString(editor.getCaretModel().getOffset(), myToken);
}
@Override
public boolean startInWriteAction() {
return true;
}
@Override
public boolean belongsToMyFamily(@NotNull IntentionActionWithFixAllOption action) {
return action instanceof InsertMissingTokenFix && ((InsertMissingTokenFix)action).myToken.equals(myToken);
public @NotNull ModCommand perform(@NotNull ActionContext context) {
return ModCommands.psiUpdate(context.file(), f -> f.getViewProvider().getDocument().insertString(context.offset(), myToken));
}
}
@@ -2,35 +2,20 @@
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.daemon.impl.actions.IntentionActionWithFixAllOption;
import com.intellij.codeInsight.intention.FileModifier;
import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.codeInspection.PsiUpdateModCommandAction;
import com.intellij.modcommand.ModPsiUpdater;
import com.intellij.psi.PsiVariable;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class MakeVarargParameterLastFix implements IntentionActionWithFixAllOption {
private final @NotNull PsiVariable myParameter;
public class MakeVarargParameterLastFix extends PsiUpdateModCommandAction<PsiVariable> {
public MakeVarargParameterLastFix(@NotNull PsiVariable parameter) {
myParameter = parameter;
super(parameter);
}
@Override
public @Nullable FileModifier getFileModifierForPreview(@NotNull PsiFile target) {
return new MakeVarargParameterLastFix(PsiTreeUtil.findSameElementInCopy(myParameter, target));
}
@Override
@NotNull
public String getText() {
return QuickFixBundle.message("make.vararg.parameter.last.text", myParameter.getName());
protected @Nullable Presentation getPresentation(@NotNull ActionContext context, @NotNull PsiVariable parameter) {
return Presentation.of(QuickFixBundle.message("make.vararg.parameter.last.text", parameter.getName()));
}
@Override
@@ -40,24 +25,8 @@ public class MakeVarargParameterLastFix implements IntentionActionWithFixAllOpti
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
return myParameter.isValid() && BaseIntentionAction.canModify(myParameter);
}
@NotNull
@Override
public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
return myParameter.getContainingFile();
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
myParameter.getParent().add(myParameter);
myParameter.delete();
}
@Override
public boolean startInWriteAction() {
return true;
protected void invoke(@NotNull ActionContext context, @NotNull PsiVariable parameter, @NotNull ModPsiUpdater updater) {
parameter.getParent().add(parameter);
parameter.delete();
}
}
@@ -638,7 +638,7 @@ public final class QuickFixFactoryImpl extends QuickFixFactory {
@NotNull
@Override
public IntentionAction createMakeVarargParameterLastFix(@NotNull PsiVariable parameter) {
return new MakeVarargParameterLastFix(parameter);
return new MakeVarargParameterLastFix(parameter).asIntention();
}
@NotNull
@@ -241,7 +241,7 @@ public interface ModCommandAction extends CommonIntentionAction {
/**
* @param thisAction the action the presentation is created for
* @return a presentation for an action that has a standard "Fix all" option
* to fix all the issues like this in the file.
* to fix all the issues like this in the file. Inapplicable to intention quick-fixes.
*/
public @NotNull Presentation withFixAllOption(@NotNull ModCommandAction thisAction) {
FixAllOption fixAllOption = new FixAllOption(
@@ -250,6 +250,21 @@ public interface ModCommandAction extends CommonIntentionAction {
return new Presentation(name, priority, rangesToHighlight, icon, fixAllOption);
}
/**
* @param thisAction the action the presentation is created for
* @param belongsToMyFamily a predicate that checks if another action belongs to this action family,
* and should be applied together with this action
* @return a presentation for an action that has a standard "Fix all" option
* to fix all the issues like this in the file. Inapplicable to intention quick-fixes.
*/
public @NotNull Presentation withFixAllOption(@NotNull ModCommandAction thisAction,
@NotNull Predicate<@NotNull ModCommandAction> belongsToMyFamily) {
FixAllOption fixAllOption = new FixAllOption(
AnalysisBundle.message("intention.name.apply.all.fixes.in.file", thisAction.getFamilyName()),
belongsToMyFamily);
return new Presentation(name, priority, rangesToHighlight, icon, fixAllOption);
}
/**
* @param ranges the ranges to highlight in the current file
* @return a presentation that highlights the specified ranges