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

import {
  FileBrowserWidget, FileBrowserModel
} from 'jupyter-js-ui/lib/filebrowser';

import {
  DocumentManager, DocumentRegistry, DocumentWidget
} from 'jupyter-js-ui/lib/docmanager';

import {
  Application
} from 'phosphide/lib/core/application';

import {
  Menu, MenuItem
} from 'phosphor-menus';

import {
  TabPanel
} from 'phosphor-tabs';

import {
  JupyterServices
} from '../services/plugin';


/**
 * The default file browser extension.
 */
export
const fileBrowserExtension = {
  id: 'jupyter.extensions.fileBrowser',
  requires: [JupyterServices, DocumentRegistry],
  activate: activateFileBrowser
};


/**
 * Activate the file browser.
 */
function activateFileBrowser(app: Application, provider: JupyterServices, registry: DocumentRegistry): Promise<void> {
  let contents = provider.contentsManager;
  let sessions = provider.notebookSessionManager;
  let widgets: DocumentWidget[] = [];
  let activeWidget: DocumentWidget;
  let id = 0;

  let opener = {
    open: (widget: DocumentWidget) => {
      if (!widget.id) {
        widget.id = `document-manager-${++id}`;
      }
      if (!widget.isAttached) {
        app.shell.addToMainArea(widget);
      }
      // TODO: Move this logic to the shell.
      let stack = widget.parent;
      if (!stack) {
        return;
      }
      let tabs = stack.parent;
      if (tabs instanceof TabPanel) {
        tabs.currentWidget = widget;
      }
      activeWidget = widget;
      widget.disposed.connect((w: DocumentWidget) => {
        let index = widgets.indexOf(w);
        widgets.splice(index, 1);
      });
    }
  };

  // TODO: Move focus tracking to the shell.
  document.addEventListener('focus', event => {
    for (let i = 0; i < widgets.length; i++) {
      let widget = widgets[i];
      if (widget.node.contains(event.target as HTMLElement)) {
        activeWidget = widget;
        break;
      }
    }
  });

  let docManager = new DocumentManager(
    registry, contents, sessions, provider.kernelspecs, opener
  );
  let model = new FileBrowserModel(contents, sessions);
  let widget = new FileBrowserWidget(model, docManager, opener);
  let menu = createMenu(widget);

  // Add a context menu to the dir listing.
  let node = widget.node.getElementsByClassName('jp-DirListing-content')[0];
  node.addEventListener('contextmenu', (event: MouseEvent) => {
    event.preventDefault();
    let x = event.clientX;
    let y = event.clientY;
    menu.popup(x, y);
  });

  // Add the command for a new items.
  let newTextFileId = 'file-operations:new-text-file';

  app.commands.add([
    {
      id: newTextFileId,
      handler: () => {
        model.newUntitled('file').then(contents => {
          let widget = docManager.createNew(contents.path);
          opener.open(widget);
          model.refresh();
        });
      }
    }
  ]);

  let newNotebookId = 'file-operations:new-notebook';

  app.commands.add([
  {
    id: newNotebookId,
    handler: () => {
      model.newUntitled('notebook').then(contents => {
        let widget = docManager.createNew(contents.path);
        opener.open(widget);
        model.refresh();
      });
    }
  }]);


  // Add the command for saving a document.
  let saveDocumentId = 'file-operations:save';

  app.commands.add([
    {
      id: saveDocumentId,
      handler: () => {
        if (!activeWidget) {
          return;
        }
        activeWidget.context.save();
      }
    }
  ]);
  app.palette.add([
    {
      command: saveDocumentId,
      category: 'File Operations',
      text: 'Save Document',
      caption: 'Save the current document'
    }<commit_msg>Clean up refreshes<commit_after>// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
'use strict';

import {
  FileBrowserWidget, FileBrowserModel
} from 'jupyter-js-ui/lib/filebrowser';

import {
  DocumentManager, DocumentRegistry, DocumentWidget
} from 'jupyter-js-ui/lib/docmanager';

import {
  Application
} from 'phosphide/lib/core/application';

import {
  Menu, MenuItem
} from 'phosphor-menus';

import {
  TabPanel
} from 'phosphor-tabs';

import {
  JupyterServices
} from '../services/plugin';


/**
 * The default file browser extension.
 */
export
const fileBrowserExtension = {
  id: 'jupyter.extensions.fileBrowser',
  requires: [JupyterServices, DocumentRegistry],
  activate: activateFileBrowser
};


/**
 * Activate the file browser.
 */
function activateFileBrowser(app: Application, provider: JupyterServices, registry: DocumentRegistry): Promise<void> {
  let contents = provider.contentsManager;
  let sessions = provider.notebookSessionManager;
  let widgets: DocumentWidget[] = [];
  let activeWidget: DocumentWidget;
  let id = 0;

  let opener = {
    open: (widget: DocumentWidget) => {
      if (!widget.id) {
        widget.id = `document-manager-${++id}`;
      }
      if (!widget.isAttached) {
        app.shell.addToMainArea(widget);
      }
      // TODO: Move this logic to the shell.
      let stack = widget.parent;
      if (!stack) {
        return;
      }
      let tabs = stack.parent;
      if (tabs instanceof TabPanel) {
        tabs.currentWidget = widget;
      }
      activeWidget = widget;
      widget.disposed.connect((w: DocumentWidget) => {
        let index = widgets.indexOf(w);
        widgets.splice(index, 1);
      });
    }
  };

  // TODO: Move focus tracking to the shell.
  document.addEventListener('focus', event => {
    for (let i = 0; i < widgets.length; i++) {
      let widget = widgets[i];
      if (widget.node.contains(event.target as HTMLElement)) {
        activeWidget = widget;
        break;
      }
    }
  });

  let docManager = new DocumentManager(
    registry, contents, sessions, provider.kernelspecs, opener
  );
  let model = new FileBrowserModel(contents, sessions, provider.kernelspecs);
  let widget = new FileBrowserWidget(model, docManager, opener);
  let menu = createMenu(widget);

  // Add a context menu to the dir listing.
  let node = widget.node.getElementsByClassName('jp-DirListing-content')[0];
  node.addEventListener('contextmenu', (event: MouseEvent) => {
    event.preventDefault();
    let x = event.clientX;
    let y = event.clientY;
    menu.popup(x, y);
  });

  // Add the command for a new items.
  let newTextFileId = 'file-operations:new-text-file';

  app.commands.add([
    {
      id: newTextFileId,
      handler: () => {
        model.newUntitled('file').then(contents => {
          widget.openPath(contents.path);
        });
      }
    }
  ]);

  let newNotebookId = 'file-operations:new-notebook';

  app.commands.add([
  {
    id: newNotebookId,
    handler: () => {
      model.newUntitled('notebook').then(contents => {
        widget.openPath(contents.path);
      });
    }
  }]);


  // Add the command for saving a document.
  let saveDocumentId = 'file-operations:save';

  app.commands.add([
    {
      id: saveDocumentId,
      handler: () => {
        if (!activeWidget) {
          return;
        }
        activeWidget.context.save();
      }
    }
  ]);
  app.palette.add([
    {
      command: saveDocumentId,
      category: 'File Operations',
      text: 'Save Document',
      caption: 'Save the current document'
    }
{'fraction_non_alphanumeric': 0.1299796057104011, 'fraction_numerical': 0.001087695445275323, 'mean_word_length': 2.7435114503816793, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, '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': '7288353', 'n_tokens_mistral': 2279, 'n_tokens_neox': 2225, 'n_words': 529}