<commit_before>// Copyright 2000-2020 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.
package com.intellij.ide.plugins;

import com.intellij.diagnostic.LoadingState;
import com.intellij.openapi.components.Service;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.extensions.PluginId;
import com.intellij.openapi.updateSettings.impl.PluginDownloader;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.SmartHashSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;

/**
 * A service to hold a state of plugin changes in a current session (i.e. before the changes are applied on restart).
 */
@Service
public final class InstalledPluginsState {
  @Nullable
  public static InstalledPluginsState getInstanceIfLoaded() {
    return LoadingState.COMPONENTS_LOADED.isOccurred() ? getInstance() : null;
  }

  public static InstalledPluginsState getInstance() {
    return ServiceManager.getService(InstalledPluginsState.class);
  }

  private final Object myLock = new Object();
  private final Map<PluginId, IdeaPluginDescriptor> myInstalledPlugins = ContainerUtil.newIdentityHashMap();
  private final Set<PluginId> myInstalledWithoutRestartPlugins = new HashSet<>();
  private final Set<PluginId> myUpdatedPlugins = new HashSet<>();
  private final Set<PluginId> myUninstalledWithoutRestartPlugins = new HashSet<>();
  private final Set<String> myOutdatedPlugins = new SmartHashSet<>();
  private boolean myInstallationInProgress = false;
  private boolean myRestartRequired = false;

  private Runnable myShutdownCallback;

  private static List<IdeaPluginDescriptor> myPreInstalledPlugins;

  public static void addPreInstalledPlugin(@NotNull IdeaPluginDescriptor descriptor) {
    if (myPreInstalledPlugins == null) {
      myPreInstalledPlugins = new ArrayList<>();
    }
    myPreInstalledPlugins.add(descriptor);
  }

  public InstalledPluginsState() {
    if (myPreInstalledPlugins != null) {
      for (IdeaPluginDescriptor plugin : myPreInstalledPlugins) {
        onPluginInstall(plugin, false, true);
      }
      //noinspection AssignmentToStaticFieldFromInstanceMethod
      myPreInstalledPlugins = null;
    }
  }

  @NotNull
  public Collection<IdeaPluginDescriptor> getInstalledPlugins() {
    synchronized (myLock) {
      return Collections.unmodifiableCollection(myInstalledPlugins.values());
    }
  }

  @NotNull
  public Collection<PluginId> getUpdatedPlugins() {
    synchronized (myLock) {
      return Collections.unmodifiableCollection(myUpdatedPlugins);<commit_msg>Don't show preinstalled plugins as requiring restart; don't add them to list if they're already loaded<commit_after>// Copyright 2000-2020 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.
package com.intellij.ide.plugins;

import com.intellij.diagnostic.LoadingState;
import com.intellij.openapi.components.Service;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.extensions.PluginId;
import com.intellij.openapi.updateSettings.impl.PluginDownloader;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.SmartHashSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;

/**
 * A service to hold a state of plugin changes in a current session (i.e. before the changes are applied on restart).
 */
@Service
public final class InstalledPluginsState {
  @Nullable
  public static InstalledPluginsState getInstanceIfLoaded() {
    return LoadingState.COMPONENTS_LOADED.isOccurred() ? getInstance() : null;
  }

  public static InstalledPluginsState getInstance() {
    return ServiceManager.getService(InstalledPluginsState.class);
  }

  private final Object myLock = new Object();
  private final Map<PluginId, IdeaPluginDescriptor> myInstalledPlugins = ContainerUtil.newIdentityHashMap();
  private final Set<PluginId> myInstalledWithoutRestartPlugins = new HashSet<>();
  private final Set<PluginId> myUpdatedPlugins = new HashSet<>();
  private final Set<PluginId> myUninstalledWithoutRestartPlugins = new HashSet<>();
  private final Set<String> myOutdatedPlugins = new SmartHashSet<>();
  private boolean myInstallationInProgress = false;
  private boolean myRestartRequired = false;

  private Runnable myShutdownCallback;

  private static List<IdeaPluginDescriptor> myPreInstalledPlugins;

  public static void addPreInstalledPlugin(@NotNull IdeaPluginDescriptor descriptor) {
    if (myPreInstalledPlugins == null) {
      myPreInstalledPlugins = new ArrayList<>();
    }
    myPreInstalledPlugins.add(descriptor);
  }

  public InstalledPluginsState() {
    if (myPreInstalledPlugins != null) {
      for (IdeaPluginDescriptor plugin : myPreInstalledPlugins) {
        if (!PluginManagerCore.isPluginInstalled(plugin.getPluginId())) {
          onPluginInstall(plugin, false, false);
        }
      }
      //noinspection AssignmentToStaticFieldFromInstanceMethod
      myPreInstalledPlugins = null;
    }
  }

  @NotNull
  public Collection<IdeaPluginDescriptor> getInstalledPlugins() {
    synchronized (myLock) {
      return Collections.unmodifiableCollection(myInstalledPlugins.values());
    }
  }

  @NotNull
  public Collection<PluginId> getUpdatedPlugins() {
    synchronized (myLock) {
      return Collections.unmodifiableCollection(myUpdatedPlugins);
{'fraction_non_alphanumeric': 0.08414414414414415, 'fraction_numerical': 0.0036036036036036037, 'mean_word_length': 5.086622807017544, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4943135', 'n_tokens_mistral': 1588, 'n_tokens_neox': 1537, 'n_words': 384}