<commit_before>import {
  Emitter,
  CompositeDisposable,
  Disposable,
  Point,
  TextEditor,
  Grammar,
} from "atom";
import { StatusBar } from "atom/status-bar";
import _ from "lodash";
import { autorun } from "mobx";
import React from "react";
import InspectorPane from "./panes/inspector";
import WatchesPane from "./panes/watches";
import OutputPane from "./panes/output-area";
import KernelMonitorPane from "./panes/kernel-monitor";
import Config from "./config";
import ZMQKernel from "./zmq-kernel";
import WSKernel from "./ws-kernel";
import Kernel from "./kernel";
import KernelPicker from "./kernel-picker";
import WSKernelPicker from "./ws-kernel-picker";
import ExistingKernelPicker from "./existing-kernel-picker";
import HydrogenProvider from "./plugin-api/hydrogen-provider";
import store, { Store, StoreLike } from "./store";
import { KernelManager } from "./kernel-manager";
import services from "./services";
import * as commands from "./commands";
import * as codeManager from "./code-manager";
import * as result from "./result";
import {
  log,
  isMultilanguageGrammar,
  INSPECTOR_URI,
  WATCHES_URI,
  OUTPUT_AREA_URI,
  KERNEL_MONITOR_URI,
  hotReloadPackage,
  openOrShowDock,
  kernelSpecProvidesGrammar,
} from "./utils";
import { exportNotebook } from "./export-notebook";
import { importNotebook, ipynbOpener } from "./import-notebook";
import type { KernelspecMetadata } from "@nteract/types";

export const config = Config.schema;
let emitter: Emitter<{}, { "did-change-kernel": Kernel }> | undefined;
let kernelPicker: KernelPicker | undefined;
let existingKernelPicker: ExistingKernelPicker | undefined;
let wsKernelPicker: WSKernelPicker | undefined;
let hydrogenProvider: HydrogenProvider | undefined;
const kernelManager = new KernelManager();

export function activate() {
  emitter = new Emitter();
  let skipLanguageMappingsChange = false;
  store.subscriptions.add(
    atom.config.onDidChange(
      "Hydrogen.languageMappings",
      ({ newValue, oldValue }) => {
        if (skipLanguageMappingsChange) {
          skipLanguageMappingsChange = false;
          return;
        }

        if (store.runningKernels.length != 0) {
          skipLanguageMappingsChange = true;
          atom.config.set("Hydrogen.languageMappings", oldValue);
          atom.notifications.addError("Hydrogen", {
            description:
              "`languageMappings` cannot be updated while kernels are running",
            dismissable: false,
          });
        }
      }
    )
  );
  store.subscriptions.add(
    atom.config.observe("Hydrogen.statusBarDisable", (newValue) => {
      store.setConfigValue("Hydrogen.statusBarDisable", Boolean(newValue));
    }),
    atom.config.observe("Hydrogen.statusBarKernelInfo", (newValue) => {
      store.setConfigValue("Hydrogen.statusBarKernelInfo", Boolean(newValue));
    })
  );
  store.subscriptions.add(
    atom.commands.add<"atom-text-editor:not([mini])">(
      "atom-text-editor:not([mini])",
      {
        "hydrogen:run": () => run(),
        "hydrogen:run-all": () => runAll(),
        "hydrogen:run-all-above": () => runAllAbove(),
        "hydrogen:run-and-move-down": () => run(true),
        "hydrogen:run-cell": () => runCell(),
        "hydrogen:run-cell-and-move-down": () => runCell(true),
        "hydrogen:toggle-watches": () => atom.workspace.toggle(WATCHES_URI),
        "hydrogen:toggle-output-area": () => commands.toggleOutputMode(),
        "hydrogen:toggle-kernel-monitor": async () => {
          const lastItem = atom.workspace.getActivePaneItem();
          const lastPane = atom.workspace.paneForItem(lastItem);
          await atom.workspace.toggle(KERNEL_MONITOR_URI);
          if (lastPane) {
            lastPane.activate();
          }
        },
        "hydrogen:start-local-kernel": () => startZMQKernel(),
        "hydrogen:connect-to-remote-kernel": () => connectToWSKernel(),
        "hydrogen:connect-to-existing-kernel": () => connectToExistingKernel(),
        "hydrogen:add-watch": () => {
          if (store.kernel) {
            store.kernel.watchesStore.addWatchFromEditor(store.editor);
            openOrShowDock(WATCHES_URI);
          }
        },
        "hydrogen:remove-watch": () => {
          if (store.kernel) {
            store.kernel.watchesStore.removeWatch();
            openOrShowDock(WATCHES_URI);
          }
        },
        "hydrogen:update-kernels": async () => {
          await kernelManager.updateKernelSpecs();
        },
        "hydrogen:toggle-inspector": () => commands.toggleInspector(store),
        "hydrogen:interrupt-kernel": () =>
          handleKernelCommand(
            {
              command: "interrupt-kernel",
            },
            store
          ),
        "hydrogen:restart-kernel": () =>
          handleKernelCommand(
            {
              command: "restart-kernel",
            },
            store
          ),
        "hydrogen:shutdown-kernel": () =>
          handleKernelCommand(
            {
              command: "shutdown-kernel",
            },
            store
          ),
        "hydrogen:clear-result": () => result.clearResult(store),
        "hydrogen:export-notebook": () => exportNotebook(),
        "hydrogen:fold-current-cell": () => foldCurrentCell(),
        "hydrogen:fold-all-but-current-cell": () => foldAllButCurrentCell(),
        "hydrogen:clear-results": () => result.clearResults(store),
      }
    )
  );
  store.subscriptions.add(
    atom.commands.add("atom-workspace", {
      "hydrogen:import-notebook": importNotebook,
    })
  );

  if (atom.inDevMode()) {
    store.subscriptions.add(
      atom.commands.add("atom-workspace", {
        "hydrogen:hot-reload-package": () => hotReloadPackage(),
      })
    );
  }

  store.subscriptions.add(
    atom.workspace.observeActiveTextEditor((editor) => {
      store.updateEditor(editor);
    })
  );
  store.subscriptions.add(
    atom.workspace.observeTextEditors((editor) => {
      const editorSubscriptions = new CompositeDisposable();
      editorSubscriptions.add(
        editor.onDidChangeGrammar(() => {
          store.setGrammar(editor);
        })
      );

      if (isMultilanguageGrammar(editor.getGrammar())) {
        editorSubscriptions.add(
          editor.onDidChangeCursorPosition(
            _.debounce(() => {
              store.setGrammar(editor);
            }, 75)
          )
        );
      }

      editorSubscriptions.add(
        editor.onDidDestroy(() => {
          editorSubscriptions.dispose();
        })
      );
      editorSubscriptions.add(
        editor.onDidChangeTitle((newTitle) => store.forceEditorUpdate())
      );
      store.subscriptions.add(editorSubscriptions);
    })
  );
  hydrogenProvider = null;
  store.subscriptions.add(
    atom.workspace.addOpener((uri) => {
      switch (uri) {
        case INSPECTOR_URI:
          return new InspectorPane(store);

        case WATCHES_URI:
          return new WatchesPane(store);<commit_msg>fix: import only lodash/debounce in main<commit_after>import {
  Emitter,
  CompositeDisposable,
  Disposable,
  Point,
  TextEditor,
  Grammar,
} from "atom";
import { StatusBar } from "atom/status-bar";
import debounce from "lodash/debounce";
import { autorun } from "mobx";
import React from "react";
import InspectorPane from "./panes/inspector";
import WatchesPane from "./panes/watches";
import OutputPane from "./panes/output-area";
import KernelMonitorPane from "./panes/kernel-monitor";
import Config from "./config";
import ZMQKernel from "./zmq-kernel";
import WSKernel from "./ws-kernel";
import Kernel from "./kernel";
import KernelPicker from "./kernel-picker";
import WSKernelPicker from "./ws-kernel-picker";
import ExistingKernelPicker from "./existing-kernel-picker";
import HydrogenProvider from "./plugin-api/hydrogen-provider";
import store, { Store, StoreLike } from "./store";
import { KernelManager } from "./kernel-manager";
import services from "./services";
import * as commands from "./commands";
import * as codeManager from "./code-manager";
import * as result from "./result";
import {
  log,
  isMultilanguageGrammar,
  INSPECTOR_URI,
  WATCHES_URI,
  OUTPUT_AREA_URI,
  KERNEL_MONITOR_URI,
  hotReloadPackage,
  openOrShowDock,
  kernelSpecProvidesGrammar,
} from "./utils";
import { exportNotebook } from "./export-notebook";
import { importNotebook, ipynbOpener } from "./import-notebook";
import type { KernelspecMetadata } from "@nteract/types";

export const config = Config.schema;
let emitter: Emitter<{}, { "did-change-kernel": Kernel }> | undefined;
let kernelPicker: KernelPicker | undefined;
let existingKernelPicker: ExistingKernelPicker | undefined;
let wsKernelPicker: WSKernelPicker | undefined;
let hydrogenProvider: HydrogenProvider | undefined;
const kernelManager = new KernelManager();

export function activate() {
  emitter = new Emitter();
  let skipLanguageMappingsChange = false;
  store.subscriptions.add(
    atom.config.onDidChange(
      "Hydrogen.languageMappings",
      ({ newValue, oldValue }) => {
        if (skipLanguageMappingsChange) {
          skipLanguageMappingsChange = false;
          return;
        }

        if (store.runningKernels.length != 0) {
          skipLanguageMappingsChange = true;
          atom.config.set("Hydrogen.languageMappings", oldValue);
          atom.notifications.addError("Hydrogen", {
            description:
              "`languageMappings` cannot be updated while kernels are running",
            dismissable: false,
          });
        }
      }
    )
  );
  store.subscriptions.add(
    atom.config.observe("Hydrogen.statusBarDisable", (newValue) => {
      store.setConfigValue("Hydrogen.statusBarDisable", Boolean(newValue));
    }),
    atom.config.observe("Hydrogen.statusBarKernelInfo", (newValue) => {
      store.setConfigValue("Hydrogen.statusBarKernelInfo", Boolean(newValue));
    })
  );
  store.subscriptions.add(
    atom.commands.add<"atom-text-editor:not([mini])">(
      "atom-text-editor:not([mini])",
      {
        "hydrogen:run": () => run(),
        "hydrogen:run-all": () => runAll(),
        "hydrogen:run-all-above": () => runAllAbove(),
        "hydrogen:run-and-move-down": () => run(true),
        "hydrogen:run-cell": () => runCell(),
        "hydrogen:run-cell-and-move-down": () => runCell(true),
        "hydrogen:toggle-watches": () => atom.workspace.toggle(WATCHES_URI),
        "hydrogen:toggle-output-area": () => commands.toggleOutputMode(),
        "hydrogen:toggle-kernel-monitor": async () => {
          const lastItem = atom.workspace.getActivePaneItem();
          const lastPane = atom.workspace.paneForItem(lastItem);
          await atom.workspace.toggle(KERNEL_MONITOR_URI);
          if (lastPane) {
            lastPane.activate();
          }
        },
        "hydrogen:start-local-kernel": () => startZMQKernel(),
        "hydrogen:connect-to-remote-kernel": () => connectToWSKernel(),
        "hydrogen:connect-to-existing-kernel": () => connectToExistingKernel(),
        "hydrogen:add-watch": () => {
          if (store.kernel) {
            store.kernel.watchesStore.addWatchFromEditor(store.editor);
            openOrShowDock(WATCHES_URI);
          }
        },
        "hydrogen:remove-watch": () => {
          if (store.kernel) {
            store.kernel.watchesStore.removeWatch();
            openOrShowDock(WATCHES_URI);
          }
        },
        "hydrogen:update-kernels": async () => {
          await kernelManager.updateKernelSpecs();
        },
        "hydrogen:toggle-inspector": () => commands.toggleInspector(store),
        "hydrogen:interrupt-kernel": () =>
          handleKernelCommand(
            {
              command: "interrupt-kernel",
            },
            store
          ),
        "hydrogen:restart-kernel": () =>
          handleKernelCommand(
            {
              command: "restart-kernel",
            },
            store
          ),
        "hydrogen:shutdown-kernel": () =>
          handleKernelCommand(
            {
              command: "shutdown-kernel",
            },
            store
          ),
        "hydrogen:clear-result": () => result.clearResult(store),
        "hydrogen:export-notebook": () => exportNotebook(),
        "hydrogen:fold-current-cell": () => foldCurrentCell(),
        "hydrogen:fold-all-but-current-cell": () => foldAllButCurrentCell(),
        "hydrogen:clear-results": () => result.clearResults(store),
      }
    )
  );
  store.subscriptions.add(
    atom.commands.add("atom-workspace", {
      "hydrogen:import-notebook": importNotebook,
    })
  );

  if (atom.inDevMode()) {
    store.subscriptions.add(
      atom.commands.add("atom-workspace", {
        "hydrogen:hot-reload-package": () => hotReloadPackage(),
      })
    );
  }

  store.subscriptions.add(
    atom.workspace.observeActiveTextEditor((editor) => {
      store.updateEditor(editor);
    })
  );
  store.subscriptions.add(
    atom.workspace.observeTextEditors((editor) => {
      const editorSubscriptions = new CompositeDisposable();
      editorSubscriptions.add(
        editor.onDidChangeGrammar(() => {
          store.setGrammar(editor);
        })
      );

      if (isMultilanguageGrammar(editor.getGrammar())) {
        editorSubscriptions.add(
          editor.onDidChangeCursorPosition(
            debounce(() => {
              store.setGrammar(editor);
            }, 75)
          )
        );
      }

      editorSubscriptions.add(
        editor.onDidDestroy(() => {
          editorSubscriptions.dispose();
        })
      );
      editorSubscriptions.add(
        editor.onDidChangeTitle((newTitle) => store.forceEditorUpdate())
      );
      store.subscriptions.add(editorSubscriptions);
    })
  );
  hydrogenProvider = null;
  store.subscriptions.add(
    atom.workspace.addOpener((uri) => {
      switch (uri) {
        case INSPECTOR_URI:
          return new InspectorPane(store);

        case WATCHES_URI:
          return new WatchesPane(store);
{'fraction_non_alphanumeric': 0.13968435335285295, 'fraction_numerical': 0.0004284796115118189, 'mean_word_length': 2.68042049934297, 'pattern_counts': {'":': 54, '<': 7, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1715720', 'n_tokens_mistral': 4223, 'n_tokens_neox': 3991, 'n_words': 677}