Java: preview for "Excluding hierarchy" inspection option fix

for "Unused declaration" inspection

GitOrigin-RevId: fb54546f111678ade6540179c0cdf1b86895da31
This commit is contained in:
Bas Leijdekkers
2023-02-05 14:06:26 +00:00
committed by intellij-monorepo-bot
parent 1726e20764
commit eb5233512d
2 changed files with 28 additions and 16 deletions
@@ -1,4 +1,4 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// 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.intention.impl.config;
import com.intellij.codeInsight.CodeInsightWorkspaceSettings;
@@ -19,6 +19,7 @@ import com.intellij.codeInsight.intention.AbstractIntentionAction;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInsight.intention.QuickFixFactory;
import com.intellij.codeInsight.intention.impl.*;
import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo;
import com.intellij.codeInspection.*;
import com.intellij.codeInspection.actions.UnimplementInterfaceAction;
import com.intellij.codeInspection.dataFlow.fix.DeleteSwitchLabelFix;
@@ -729,30 +730,34 @@ public final class QuickFixFactoryImpl extends QuickFixFactory {
@Override
public @NotNull IntentionAction createSafeDeleteUnusedParameterInHierarchyFix(@NotNull PsiParameter parameter, boolean excludingHierarchy) {
IntentionAction intentionAction;
if (excludingHierarchy) {
intentionAction = new AbstractIntentionAction() {
final SetInspectionOptionFix myFix =
SetInspectionOptionFix.createFix(UnusedSymbolLocalInspectionBase.SHORT_NAME,
"myCheckParameterExcludingHierarchy",
JavaErrorBundle.message("parameter.excluding.hierarchy.disable.text"), false,
profileEntry -> profileEntry instanceof UnusedDeclarationInspectionBase
? ((UnusedDeclarationInspectionBase)profileEntry).getSharedLocalInspectionTool()
: profileEntry);
return new AbstractIntentionAction() {
@Override
public @NotNull String getText() {
return JavaErrorBundle.message("parameter.excluding.hierarchy.disable.text");
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
SetInspectionOptionFix.createFix(UnusedSymbolLocalInspectionBase.SHORT_NAME,
"myCheckParameterExcludingHierarchy",
JavaErrorBundle.message("parameter.excluding.hierarchy.disable.text"), false,
profileEntry -> profileEntry instanceof UnusedDeclarationInspectionBase
? ((UnusedDeclarationInspectionBase)profileEntry).getSharedLocalInspectionTool()
: profileEntry)
.applyFix(project, file);
public void invoke(@NotNull Project project, Editor editor, PsiFile file) {
myFix.applyFix(project, file);
}
@Override
public @NotNull IntentionPreviewInfo generatePreview(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
return myFix.generatePreview(project, file, "members.");
}
};
}
else {
intentionAction = new SafeDeleteFix(parameter);
return new SafeDeleteFix(parameter);
}
return intentionAction;
}
@NotNull
@@ -1,4 +1,4 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// 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.codeInspection;
import com.intellij.analysis.AnalysisBundle;
@@ -18,6 +18,7 @@ import com.intellij.openapi.util.text.HtmlBuilder;
import com.intellij.openapi.util.text.HtmlChunk;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.profile.codeInspection.InspectionProfileManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.util.ObjectUtils;
@@ -98,11 +99,17 @@ public class SetInspectionOptionFix implements OnTheFlyLocalFix, LowPriorityActi
@Override
public @NotNull IntentionPreviewInfo generatePreview(@NotNull Project project,
@NotNull ProblemDescriptor previewDescriptor) {
PsiElement element = previewDescriptor.getPsiElement();
return generatePreview(project, element, "");
}
@NotNull
public IntentionPreviewInfo generatePreview(@NotNull Project project, PsiElement element, String prefix) {
InspectionToolWrapper<?, ?> tool =
InspectionProfileManager.getInstance(project).getCurrentProfile().getInspectionTool(myShortName, previewDescriptor.getPsiElement());
InspectionProfileManager.getInstance(project).getCurrentProfile().getInspectionTool(myShortName, element);
if (tool == null) return IntentionPreviewInfo.EMPTY;
OptPane pane = tool.getTool().getOptionsPane();
OptCheckbox control = ObjectUtils.tryCast(pane.findControl(myProperty), OptCheckbox.class);
OptCheckbox control = ObjectUtils.tryCast(pane.findControl(prefix + myProperty), OptCheckbox.class);
if (control == null) return IntentionPreviewInfo.EMPTY;
HtmlChunk label = HtmlChunk.text(control.label().label());
HtmlChunk.Element checkbox = HtmlChunk.tag("input").attr("type", "checkbox").attr("readonly", "true");