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

import {
  each, map, toArray
} from 'phosphor/lib/algorithm/iteration';

import {
  DisposableSet
} from 'phosphor/lib/core/disposable';

import {
  Menu
} from 'phosphor/lib/ui/menu';

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

import {
  ICommandPalette
} from '../commandpalette';

import {
  IDocumentManager
} from '../docmanager';

import {
  IDocumentRegistry
} from '../docregistry';

import {
  IInstanceRestorer
} from '../instancerestorer';

import {
  IMainMenu
} from '../mainmenu';

import {
  IServiceManager
} from '../services';

import {
  IStateDB
} from '../statedb';

import {
  FileBrowserModel, FileBrowser, IPathTracker
} from './';


/**
 * The default file browser provider.
 */
const plugin: JupyterLabPlugin<IPathTracker> = {
  activate,
  id: 'jupyter.services.file-browser',
  provides: IPathTracker,
  requires: [
    IServiceManager,
    IDocumentManager,
    IDocumentRegistry,
    IMainMenu,
    ICommandPalette,
    IInstanceRestorer,
    IStateDB
  ],
  autoStart: true
};


/**
 * Export the plugin as default.
 */
export default plugin;


/**
 * The map of command ids used by the file browser.
 */
const cmdIds = {
  save: 'file-operations:save',
  restoreCheckpoint: 'file-operations:restore-checkpoint',
  saveAs: 'file-operations:save-as',
  close: 'file-operations:close',
  closeAllFiles: 'file-operations:close-all-files',
  open: 'file-operations:open',
  showBrowser: 'file-browser:activate',
  hideBrowser: 'file-browser:hide',
  toggleBrowser: 'file-browser:toggle'
};

/**
 * The filebrowser plugin state namespace.
 */
const NAMESPACE = 'filebrowser';


/**
 * Activate the file browser.
 */
function activate(app: JupyterLab, manager: IServiceManager, documentManager: IDocumentManager, registry: IDocumentRegistry, mainMenu: IMainMenu, palette: ICommandPalette, restorer: IInstanceRestorer, state: IStateDB): IPathTracker {
  const { commands, keymap } = app;
  let fbModel = new FileBrowserModel({ manager });
  let fbWidget = new FileBrowser({
    commands, keymap,
    manager: documentManager,
    model: fbModel
  });

  // Let the application restorer track the file browser for restoration of
  // application state (e.g. setting the file browser as the current side bar
  // widget).
  restorer.add(fbWidget, NAMESPACE);

  let category = 'File Operations';
  let creatorCmds: { [key: string]: DisposableSet } = Object.create(null);
  let addCreator = (name: string) => {
    let disposables = creatorCmds[name] = new DisposableSet();
    let command = Private.commandForName(name);
    disposables.add(commands.addCommand(command, {
      execute: () => fbWidget.createFrom(name),
      label: `New ${name}`
    }));
    disposables.add(palette.addItem({ command, category }));
  };

  // Restore the state of the file browser on reload.
  let key = `${NAMESPACE}:cwd`;
  Promise.all([state.fetch(key), app.started, manager.ready]).then(([cwd]) => {
    if (!cwd) {
      return;
    }

    let path = cwd['path'] as string;
    return manager.contents.get(path)
      .then(() => { fbModel.cd(path); })
      .catch(() => { state.remove(key); });
  }).then(() => {
    // Save the subsequent state of the file browser in the state database.
    fbModel.pathChanged.connect((sender, args) => {
      state.save(`${NAMESPACE}:cwd`, { path: args.newValue });
    });
  });

  each(registry.creators(), creator => { addCreator(creator.name); });

  // Add a context menu to the dir listing.
  let node = fbWidget.node.getElementsByClassName('jp-DirListing-content')[0];
  node.addEventListener('contextmenu', (event: MouseEvent) => {
    event.preventDefault();
    let path = fbWidget.pathForClick(event) || '';
    let ext = '.' + path.split('.').pop();
    let factories = registry.preferredWidgetFactories(ext);<commit_msg>Handle state database rejections in the file browser plugin.<commit_after>// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

import {
  each, map, toArray
} from 'phosphor/lib/algorithm/iteration';

import {
  DisposableSet
} from 'phosphor/lib/core/disposable';

import {
  Menu
} from 'phosphor/lib/ui/menu';

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

import {
  ICommandPalette
} from '../commandpalette';

import {
  IDocumentManager
} from '../docmanager';

import {
  IDocumentRegistry
} from '../docregistry';

import {
  IInstanceRestorer
} from '../instancerestorer';

import {
  IMainMenu
} from '../mainmenu';

import {
  IServiceManager
} from '../services';

import {
  IStateDB
} from '../statedb';

import {
  FileBrowserModel, FileBrowser, IPathTracker
} from './';


/**
 * The default file browser provider.
 */
const plugin: JupyterLabPlugin<IPathTracker> = {
  activate,
  id: 'jupyter.services.file-browser',
  provides: IPathTracker,
  requires: [
    IServiceManager,
    IDocumentManager,
    IDocumentRegistry,
    IMainMenu,
    ICommandPalette,
    IInstanceRestorer,
    IStateDB
  ],
  autoStart: true
};


/**
 * Export the plugin as default.
 */
export default plugin;


/**
 * The map of command ids used by the file browser.
 */
const cmdIds = {
  save: 'file-operations:save',
  restoreCheckpoint: 'file-operations:restore-checkpoint',
  saveAs: 'file-operations:save-as',
  close: 'file-operations:close',
  closeAllFiles: 'file-operations:close-all-files',
  open: 'file-operations:open',
  showBrowser: 'file-browser:activate',
  hideBrowser: 'file-browser:hide',
  toggleBrowser: 'file-browser:toggle'
};

/**
 * The filebrowser plugin state namespace.
 */
const NAMESPACE = 'filebrowser';


/**
 * Activate the file browser.
 */
function activate(app: JupyterLab, manager: IServiceManager, documentManager: IDocumentManager, registry: IDocumentRegistry, mainMenu: IMainMenu, palette: ICommandPalette, restorer: IInstanceRestorer, state: IStateDB): IPathTracker {
  const { commands, keymap } = app;
  let fbModel = new FileBrowserModel({ manager });
  let fbWidget = new FileBrowser({
    commands, keymap,
    manager: documentManager,
    model: fbModel
  });

  // Let the application restorer track the file browser for restoration of
  // application state (e.g. setting the file browser as the current side bar
  // widget).
  restorer.add(fbWidget, NAMESPACE);

  let category = 'File Operations';
  let creatorCmds: { [key: string]: DisposableSet } = Object.create(null);
  let addCreator = (name: string) => {
    let disposables = creatorCmds[name] = new DisposableSet();
    let command = Private.commandForName(name);
    disposables.add(commands.addCommand(command, {
      execute: () => fbWidget.createFrom(name),
      label: `New ${name}`
    }));
    disposables.add(palette.addItem({ command, category }));
  };

  // Restore the state of the file browser on reload.
  let key = `${NAMESPACE}:cwd`;
  let connect = () => {
    // Save the subsequent state of the file browser in the state database.
    fbModel.pathChanged.connect((sender, args) => {
      state.save(`${NAMESPACE}:cwd`, { path: args.newValue });
    });
  };
  Promise.all([state.fetch(key), app.started, manager.ready]).then(([cwd]) => {
    if (!cwd) {
      return;
    }
    let path = cwd['path'] as string;
    return manager.contents.get(path)
      .then(() => { fbModel.cd(path); })
      .catch(() => { state.remove(key); });
  }).then(connect)
    .catch(() => { state.remove(key).then(connect); });

  each(registry.creators(), creator => { addCreator(creator.name); });

  // Add a context menu to the dir listing.
  let node = fbWidget.node.getElementsByClassName('jp-DirListing-content')[0];
  node.addEventListener('contextmenu', (event: MouseEvent) => {
    event.preventDefault();
    let path = fbWidget.pathForClick(event) || '';
    let ext = '.' + path.split('.').pop();
    let factories = registry.preferredWidgetFactories(ext);
{'fraction_non_alphanumeric': 0.14266032064128256, 'fraction_numerical': 0.000250501002004008, 'mean_word_length': 3.7843019772318756, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 24, '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': '4505901', 'n_tokens_mistral': 2531, 'n_tokens_neox': 2485, 'n_words': 627}