Show undo action after requirements have been (accidentally) marked as ignored (PY-26817)

This commit is contained in:
Semyon Proshev
2018-11-08 14:04:15 +03:00
parent 15d52e6175
commit fce39cb7c6
@@ -3,8 +3,13 @@ package com.jetbrains.python.inspections;
import com.google.common.collect.ImmutableSet;
import com.intellij.codeInspection.*;
import com.intellij.codeInspection.ex.EditInspectionToolsSettingsAction;
import com.intellij.codeInspection.ex.InspectionProfileImpl;
import com.intellij.codeInspection.ui.ListEditForm;
import com.intellij.execution.ExecutionException;
import com.intellij.notification.NotificationDisplayType;
import com.intellij.notification.NotificationGroup;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.module.Module;
@@ -47,6 +52,10 @@ import java.util.*;
public class PyPackageRequirementsInspection extends PyInspection {
public JDOMExternalizableStringList ignoredPackages = new JDOMExternalizableStringList();
@NotNull
private static final NotificationGroup BALLOON_NOTIFICATIONS =
new NotificationGroup("Package requirements", NotificationDisplayType.BALLOON, false);
@NotNull
@Override
public String getDisplayName() {
@@ -551,9 +560,11 @@ public class PyPackageRequirementsInspection extends PyInspection {
private static class IgnoreRequirementFix implements LocalQuickFix {
@NotNull private final Set<String> myPackageNames;
IgnoreRequirementFix(@NotNull Set<String> packageNames) {
@NotNull
private final Set<String> myPackageNames;
private IgnoreRequirementFix(@NotNull Set<String> packageNames) {
myPackageNames = packageNames;
}
@@ -575,16 +586,43 @@ public class PyPackageRequirementsInspection extends PyInspection {
if (element != null) {
final PyPackageRequirementsInspection inspection = getInstance(element);
if (inspection != null) {
final JDOMExternalizableStringList ignoredPackages = inspection.ignoredPackages;
boolean changed = false;
for (String name : myPackageNames) {
if (!ignoredPackages.contains(name)) {
ignoredPackages.add(name);
changed = true;
}
final Set<String> packagesToIgnore = new HashSet<>(myPackageNames);
for (String pkg : inspection.ignoredPackages) {
packagesToIgnore.remove(pkg);
}
if (changed) {
ProjectInspectionProfileManager.getInstance(project).fireProfileChanged();
if (!packagesToIgnore.isEmpty()) {
inspection.ignoredPackages.addAll(packagesToIgnore);
final ProjectInspectionProfileManager profileManager = ProjectInspectionProfileManager.getInstance(project);
profileManager.fireProfileChanged();
BALLOON_NOTIFICATIONS
.createNotification(
packagesToIgnore.size() == 1
? "'" + packagesToIgnore.iterator().next() + "' has been ignored"
: "Requirements have been ignored",
"<a href=\"#undo\">Undo</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"#settings\">Settings</a>",
NotificationType.INFORMATION,
(notification, event) -> {
try {
switch (event.getDescription()) {
case "#undo":
inspection.ignoredPackages.removeAll(packagesToIgnore);
profileManager.fireProfileChanged();
break;
case "#settings":
final InspectionProfileImpl profile = profileManager.getCurrentProfile();
final String toolName = PyPackageRequirementsInspection.class.getSimpleName();
EditInspectionToolsSettingsAction.editToolSettings(project, profile, toolName);
break;
}
}
finally {
notification.expire();
}
}
)
.notify(project);
}
}
}