Skip to content

Commit

Permalink
[dynamic plugins] required plugins should be effectively enabled #IDE…
Browse files Browse the repository at this point in the history
…A-246104

GitOrigin-RevId: 90103f134a1ce3361ea4f3344240fea4576446de
  • Loading branch information
adkozlov authored and intellij-monorepo-bot committed Oct 20, 2020
1 parent 4caed08 commit d3bdfe2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1665,7 +1665,7 @@ command.fixing.line.separators=fixing line separators
dialog.title.cannot.open.file=Cannot Open File
message.this.action.isn.t.supported.on.the.current.platform=This action isn't supported on the current platform
popup.title.maintenance=Maintenance
notification.title.required.plugins.weren.t.loaded=Required plugins weren't loaded
notification.title.required.plugins.not.loaded=Required plugins have not been loaded
dialog.title.delete.0=Delete {0}
message.do.you.want.to.delete.0.1=Do you want to delete "{0}" {1}?
dialog.title.activity.monitor=Activity Monitor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

import javax.swing.event.HyperlinkEvent.EventType;
import javax.swing.event.HyperlinkEvent;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.intellij.openapi.util.text.StringUtil.join;
import static com.intellij.util.containers.ContainerUtil.map;

final class CheckRequiredPluginsActivity implements StartupActivity {
final class CheckRequiredPluginsActivity implements StartupActivity.RequiredForSmartMode {
private static final Logger LOG = Logger.getInstance(CheckRequiredPluginsActivity.class);
private static final @NonNls String NOTIFICATION_GROUP_ID = "Required Plugins";
private static final @NonNls String ENABLE = "enable";
Expand Down Expand Up @@ -122,12 +121,7 @@ public static void runCheck(@NotNull Project project, @NotNull ExternalDependenc
}

if (!pluginsToEnableWithoutRestart.isEmpty()) {
List<PluginId> pluginIds = map(pluginsToEnableWithoutRestart, IdeaPluginDescriptor::getPluginId);
LOG.info("Automatically enabling plugins required for this project: " + join(pluginIds, ", "));
for (PluginId pluginId : pluginIds) {
pluginTracker.changeEnableDisable(pluginId, PluginEnabledState.ENABLED_FOR_PROJECT);
}
ApplicationManager.getApplication().invokeLater(() -> PluginEnabler.enablePlugins(project, pluginsToEnableWithoutRestart, true));
ApplicationManager.getApplication().invokeLater(() -> enablePlugins(project, pluginsToEnableWithoutRestart));
}

if (errorMessages.isEmpty()) {
Expand All @@ -148,34 +142,62 @@ public static void runCheck(@NotNull Project project, @NotNull ExternalDependenc
errorMessages.add(HtmlChunk.link(target, text).toString());
}

Set<PluginId> problemPluginIds = new HashSet<>(notInstalled);
for (IdeaPluginDescriptor descriptor : disabled) {
problemPluginIds.add(descriptor.getPluginId());
}
NotificationListener listener = notInstalled.isEmpty() ?
createEnableNotificationListener(project, disabled) :
createInstallNotificationListener(notInstalled, disabled);

NotificationGroupManager.getInstance()
.getNotificationGroup(NOTIFICATION_GROUP_ID)
.createNotification(
IdeBundle.message("notification.title.required.plugins.weren.t.loaded"),
IdeBundle.message("notification.title.required.plugins.not.loaded"),
join(errorMessages, "<br>"),
NotificationType.ERROR,
createListener(project, problemPluginIds)
listener
).notify(project);
}

private static @NotNull NotificationListener createListener(@NotNull Project project,
@NotNull Set<PluginId> pluginIds) {
private static void enablePlugins(@NotNull Project project,
@NotNull List<? extends IdeaPluginDescriptor> plugins) {
LOG.info(PluginEnabler.getLogMessage("Required plugins to enable", plugins));

ProjectPluginTracker pluginTracker = ProjectPluginTracker.getInstance(project);
for (IdeaPluginDescriptor descriptor : plugins) {
pluginTracker.changeEnableDisable(descriptor.getPluginId(), PluginEnabledState.ENABLED);
}

PluginEnabler.enablePlugins(project, plugins, true);
}

private static @NotNull NotificationListener createEnableNotificationListener(@NotNull Project project,
@NotNull List<IdeaPluginDescriptor> disabled) {
return (notification, event) -> {
if (event.getEventType() == EventType.ACTIVATED) {
if (ENABLE.equals(event.getDescription())) {
notification.expire();
DisabledPluginsState.enablePluginsById(pluginIds, true);
PluginManagerMain.notifyPluginsUpdated(project);
}
else {
PluginsAdvertiser.installAndEnable(pluginIds, () -> notification.expire());
}
}
if (!isApplicable(event, ENABLE)) return;

notification.expire();
enablePlugins(project, disabled);
PluginManagerMain.notifyPluginsUpdated(project);
};
}

private static @NotNull NotificationListener createInstallNotificationListener(@NotNull Set<PluginId> notInstalled,
@NotNull List<? extends IdeaPluginDescriptor> disabled) {

HashSet<PluginId> pluginIds = new HashSet<>(notInstalled);
pluginIds.addAll(notInstalled);
for (IdeaPluginDescriptor descriptor : disabled) {
pluginIds.add(descriptor.getPluginId());
}

return (notification, event) -> {
if (!isApplicable(event, INSTALL)) return;

PluginsAdvertiser.installAndEnable(pluginIds, () -> notification.expire());
};
}

private static boolean isApplicable(@NotNull HyperlinkEvent event,
@NotNull @NonNls String description) {
return HyperlinkEvent.EventType.ACTIVATED == event.getEventType() &&
description.equals(event.getDescription());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,19 @@ public static boolean updatePluginEnabledState(@Nullable Project project,

private static @NotNull String getLogMessage(@NotNull List<? extends IdeaPluginDescriptor> pluginsToEnable,
boolean enable) {
StringBuilder buffer = new StringBuilder("Plugins to ")
.append(enable ? "enable" : "disable")
return getLogMessage(
"Plugins to " + (enable ? "enable" : "disable"),
pluginsToEnable
);
}

public static @NotNull String getLogMessage(@NotNull String message,
@NotNull List<? extends IdeaPluginDescriptor> pluginsToEnable) {
StringBuilder buffer = new StringBuilder(message)
.append(':')
.append(' ')
.append('[');

join(
pluginsToEnable,
descriptor -> descriptor.getPluginId().getIdString(),
Expand Down

0 comments on commit d3bdfe2

Please sign in to comment.