<commit_before>// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

import {
  Widget
} from 'phosphor/lib/ui/widget';

import {
  JupyterLab, JupyterLabPlugin
} from '../application';

import {
  IConsoleTracker
} from '../console';

import {
  INotebookTracker
} from '../notebook';

import {
  CompleterModel, CompleterWidget, CompletionHandler
} from './';


/**
 * An extension that registers consoles for code completion.
 */
const consolePlugin: JupyterLabPlugin<void> = {
  id: 'jupyter.extensions.console-completer',
  requires: [IConsoleTracker],
  autoStart: true,
  activate: (app: JupyterLab, consoles: IConsoleTracker): void => {
    // Create a handler for each console that is created.
    consoles.widgetAdded.connect((sender, parent) => {
      const session = parent.console.session;
      const kernel = session.kernel;
      const model = new CompleterModel();
      const completer = new CompleterWidget({ anchor: parent.console, model });
      const handler = new CompletionHandler({ completer, kernel });

      // Set the initial editor.
      let cell = parent.console.prompt;
      handler.editor = cell && cell.editor;

      // Listen for prompt creation.
      parent.console.promptCreated.connect((sender, cell) => {
        handler.editor = cell && cell.editor;
      });

      // Listen for kernel changes.
      session.kernelChanged.connect((sender, kernel) => {
        handler.kernel = kernel;
      });

      // Attach the completer widget.
      Widget.attach(completer, document.body);

      // Listen for parent disposal.
      parent.disposed.connect(() => {
        model.dispose();
        completer.dispose();
        handler.dispose();
      });
    });
  }
};

/**
 * An extension that registers notebooks for code completion.
 */
const notebookPlugin: JupyterLabPlugin<void> = {
  id: 'jupyter.extensions.notebook-completer',
  requires: [INotebookTracker],
  autoStart: true,
  activate: (app: JupyterLab, notebooks: INotebookTracker): void => {
    // Create a handler for each notebook that is created.
    notebooks.widgetAdded.connect((sender, parent) => {
      const kernel = parent.kernel;
      const model = new CompleterModel();
      const completer = new CompleterWidget({ anchor: parent.notebook, model });
      const handler = new CompletionHandler({ completer, kernel });

      // Set the initial editor.
      let cell = parent.notebook.activeCell;
      handler.editor = cell && cell.editor;

      // Listen for active cell changes.
      parent.notebook.activeCellChanged.connect((sender, cell) => {
        handler.editor = cell && cell.editor;
      });

      // Listen for kernel changes.
      parent.kernelChanged.connect((sender, kernel) => {
        handler.kernel = kernel;
      });

      // Attach the completer widget.
      Widget.attach(completer, document.body);

      // Listen for parent disposal.
      parent.disposed.connect(() => {
        model.dispose();
        completer.dispose();
        handler.dispose();
      });
    });
  }
};

/**
 * Export the plugins as default.
 */
const plugins: JupyterLabPlugin<any>[] = [consolePlugin, notebookPlugin];
export default plugins;<commit_msg>Refactor plugin.<commit_after>// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

import {
  AttachedProperty
} from 'phosphor/lib/core/properties';

import {
  Keymap
} from 'phosphor/lib/ui/keymap';

import {
  Widget
} from 'phosphor/lib/ui/widget';

import {
  JupyterLab, JupyterLabPlugin
} from '../application';

import {
  IConsoleTracker
} from '../console';

import {
  INotebookTracker
} from '../notebook';

import {
  CommandIDs, COMPLETABLE_CLASS,
  CompleterModel, CompleterWidget, CompletionHandler
} from './';


/**
 * The keyboard shortcut used to invoke a completer.
 */
const shortcut = 'Tab';


/**
 * An extension that registers consoles for code completion.
 */
const consolePlugin: JupyterLabPlugin<void> = {
  id: 'jupyter.extensions.console-completer',
  requires: [IConsoleTracker],
  autoStart: true,
  activate: (app: JupyterLab, consoles: IConsoleTracker): void => {
    const { layout } = app.keymap;
    const interrupt = (instance: any, event: KeyboardEvent): boolean => {
      return Keymap.keystrokeForKeydownEvent(event, layout) === shortcut;
    };

    // Create a handler for each console that is created.
    consoles.widgetAdded.connect((sender, parent) => {
      const session = parent.console.session;
      const kernel = session.kernel;
      const model = new CompleterModel();
      const completer = new CompleterWidget({ anchor: parent.console, model });
      const handler = new CompletionHandler({ completer, interrupt, kernel });

      // Associate the handler with the parent widget.
      Private.handlers.set(parent, handler);

      // Set the initial editor.
      let cell = parent.console.prompt;
      handler.editor = cell && cell.editor;

      // Listen for prompt creation.
      parent.console.promptCreated.connect((sender, cell) => {
        handler.editor = cell && cell.editor;
      });

      // Listen for kernel changes.
      session.kernelChanged.connect((sender, kernel) => {
        handler.kernel = kernel;
      });

      // Attach the completer widget.
      Widget.attach(completer, document.body);

      // Listen for parent disposal.
      parent.disposed.connect(() => {
        model.dispose();
        completer.dispose();
        handler.dispose();
      });
    });

    app.commands.addCommand(CommandIDs.invokeConsole, {
      execute: () => {
        const handler = Private.handlers.get(consoles.currentWidget);
        if (!handler) {
          return;
        }
        if (handler.interrupter) {
          handler.interrupter.dispose();
        }
        handler.invoke();
      }
    });
    app.keymap.addBinding({
      command: CommandIDs.invokeConsole,
      keys: [shortcut],
      selector: `.jp-ConsolePanel .${COMPLETABLE_CLASS}`
    });
  }
};

/**
 * An extension that registers notebooks for code completion.
 */
const notebookPlugin: JupyterLabPlugin<void> = {
  id: 'jupyter.extensions.notebook-completer',
  requires: [INotebookTracker],
  autoStart: true,
  activate: (app: JupyterLab, notebooks: INotebookTracker): void => {
    const { layout } = app.keymap;
    const interrupt = (instance: any, event: KeyboardEvent): boolean => {
      return Keymap.keystrokeForKeydownEvent(event, layout) === shortcut;
    };

    // Create a handler for each notebook that is created.
    notebooks.widgetAdded.connect((sender, parent) => {
      const kernel = parent.kernel;
      const model = new CompleterModel();
      const completer = new CompleterWidget({ anchor: parent.notebook, model });
      const handler = new CompletionHandler({ completer, interrupt, kernel });

      // Associate the handler with the parent widget.
      Private.handlers.set(parent, handler);

      // Set the initial editor.
      let cell = parent.notebook.activeCell;
      handler.editor = cell && cell.editor;

      // Listen for active cell changes.
      parent.notebook.activeCellChanged.connect((sender, cell) => {
        handler.editor = cell && cell.editor;
      });

      // Listen for kernel changes.
      parent.kernelChanged.connect((sender, kernel) => {
        handler.kernel = kernel;
      });

      // Attach the completer widget.
      Widget.attach(completer, document.body);

      // Listen for parent disposal.
      parent.disposed.connect(() => {
        model.dispose();
        completer.dispose();
        handler.dispose();
      });
    });

    app.commands.addCommand(CommandIDs.invokeNotebook, {
      execute: () => {
        const handler = Private.handlers.get(notebooks.currentWidget);
        if (!handler) {
          return;
        }
        if (handler.interrupter) {
          handler.interrupter.dispose();
        }
        handler.invoke();
      }
    });
    app.keymap.addBinding({
      command: CommandIDs.invokeNotebook,
      keys: [shortcut],
      selector: `.jp-Notebook .${COMPLETABLE_CLASS}`
    });
  }
};

/**
 * Export the plugins as default.
 */
const plugins: JupyterLabPlugin<any>[] = [consolePlugin, notebookPlugin];
export default plugins;


/**
 * A namespace for private data.
 */
namespace Private {
  /**
   * A property that associates completion handlers with their referent widgets.
   */
  export
  const handlers = new AttachedProperty<Widget, CompletionHandler>({
    name: 'handler'
  });
}
{'fraction_non_alphanumeric': 0.12450362064938099, 'fraction_numerical': 0.0, 'mean_word_length': 2.7855879752431476, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 34, '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': '4297762', 'n_tokens_mistral': 2502, 'n_tokens_neox': 2413, 'n_words': 622}