Browse Source

wip refactor extensions

Steven Silvester 8 years ago
parent
commit
12d8825d61

+ 16 - 1
src/application/plugin.ts → src/application-extension/index.ts

@@ -2,7 +2,7 @@
 // Distributed under the terms of the Modified BSD License.
 
 import {
-  JupyterLab, JupyterLabPlugin, CommandIDs
+  JupyterLab, JupyterLabPlugin
 } from './';
 
 import {
@@ -10,6 +10,21 @@ import {
 } from '../commandpalette';
 
 
+/**
+ * The command IDs used by the application plugin.
+ */
+namespace CommandIDs {
+  export
+  const closeAll: string = 'main-jupyterlab:close-all';
+
+  export
+  const activateNextTab: string = 'main-jupyterlab:activate-next-tab';
+
+  export
+  const activatePreviousTab: string = 'main-jupyterlab:activate-previous-tab';
+};
+
+
 /**
  * The main extension.
  */

+ 0 - 16
src/application/index.ts

@@ -17,22 +17,6 @@ export { ModuleLoader } from './loader';
 export { ApplicationShell } from './shell';
 
 
-/**
- * The command IDs used by the application plugin.
- */
-export
-namespace CommandIDs {
-  export
-  const closeAll: string = 'main-jupyterlab:close-all';
-
-  export
-  const activateNextTab: string = 'main-jupyterlab:activate-next-tab';
-
-  export
-  const activatePreviousTab: string = 'main-jupyterlab:activate-previous-tab';
-};
-
-
 /**
  * The type for all JupyterLab plugins.
  */

+ 1 - 0
src/apputils/index.css

@@ -9,4 +9,5 @@
 @import './hoverbox.css';
 @import './iframe.css';
 @import './jsoneditor.css';
+@import './materialcolors.css';
 @import './styling.css';

+ 0 - 0
src/basestyle/materialcolors.css → src/apputils/materialcolors.css


+ 16 - 1
src/codemirror/plugin.ts → src/codemirror-extension/index.ts

@@ -31,10 +31,25 @@ import {
 } from '../mainmenu';
 
 import {
-  editorServices, CodeMirrorEditor, CommandIDs
+  editorServices, CodeMirrorEditor
 } from '.';
 
 
+/**
+ * The command IDs used by the codemirror plugin.
+ */
+namespace CommandIDs {
+  export
+  const matchBrackets = 'codemirror:match-brackets';
+
+  export
+  const vimMode = 'codemirror:vim-mode';
+
+  export
+  const changeTheme = 'codemirror:change-theme';
+};
+
+
 /**
  * The editor services.
  */

+ 0 - 16
src/codemirror/index.ts

@@ -27,19 +27,3 @@ const editorServices: IEditorServices = {
   factoryService: new CodeMirrorEditorFactory(),
   mimeTypeService: new CodeMirrorMimeTypeService()
 };
-
-
-/**
- * The command IDs used by the codemirror plugin.
- */
-export
-namespace CommandIDs {
-  export
-  const matchBrackets = 'codemirror:match-brackets';
-
-  export
-  const vimMode = 'codemirror:vim-mode';
-
-  export
-  const changeTheme = 'codemirror:change-theme';
-};

+ 243 - 0
src/completer-extension/index.ts

@@ -0,0 +1,243 @@
+// Copyright (c) Jupyter Development Team.
+// Distributed under the terms of the Modified BSD License.
+
+import {
+  Widget
+} from '@phosphor/widgets';
+
+import {
+  JupyterLab, JupyterLabPlugin
+} from '../application';
+
+import {
+  IConsoleTracker
+} from '../console';
+
+import {
+  INotebookTracker
+} from '../notebook';
+
+import {
+  COMPLETER_ACTIVE_CLASS, CompleterModel,
+  CompleterWidget, CompletionHandler, ICompletionManager
+} from './';
+
+
+/**
+ * The command IDs used by the completer plugin.
+ */
+namespace CommandIDs {
+  export
+  const invoke: string = 'completer:invoke';
+
+  export
+  const invokeConsole: string = 'completer:invoke-console';
+
+  export
+  const invokeNotebook: string = 'completer:invoke-notebook';
+
+  export
+  const select: string = 'completer:select';
+
+  export
+  const selectConsole: string = 'completer:select-console';
+
+  export
+  const selectNotebook: string = 'completer:select-notebook';
+}
+
+
+/**
+ * A service providing code completion for editors.
+ */
+const service: JupyterLabPlugin<ICompletionManager> = {
+  id: 'jupyter.services.completer',
+  autoStart: true,
+  provides: ICompletionManager,
+  activate: (app: JupyterLab): ICompletionManager => {
+    const handlers: { [id: string]: CompletionHandler } = {};
+
+    app.commands.addCommand(CommandIDs.invoke, {
+      execute: args => {
+        let id = args && (args['id'] as string);
+        if (!id) {
+          return;
+        }
+
+        const handler = handlers[id];
+        if (handler) {
+          handler.invoke();
+        }
+      }
+    });
+
+    app.commands.addCommand(CommandIDs.select, {
+      execute: args => {
+        let id = args && (args['id'] as string);
+        if (!id) {
+          return;
+        }
+
+        const handler = handlers[id];
+        if (handler) {
+          handler.completer.selectActive();
+        }
+      }
+    });
+
+    return {
+      register: (completable: ICompletionManager.ICompletable): ICompletionManager.ICompletableAttributes => {
+        const { editor, kernel, parent } = completable;
+        const model = new CompleterModel();
+        const completer = new CompleterWidget({ editor, model });
+        const handler = new CompletionHandler({ completer, kernel });
+        const id = parent.id;
+
+        // Hide the widget when it first loads.
+        completer.hide();
+
+        // Associate the handler with the parent widget.
+        handlers[id] = handler;
+
+        // Set the handler's editor.
+        handler.editor = editor;
+
+        // Attach the completer widget.
+        Widget.attach(completer, document.body);
+
+        // Listen for parent disposal.
+        parent.disposed.connect(() => {
+          delete handlers[id];
+          model.dispose();
+          completer.dispose();
+          handler.dispose();
+        });
+
+        return handler;
+      }
+    };
+  }
+};
+
+
+/**
+ * An extension that registers consoles for code completion.
+ */
+const consolePlugin: JupyterLabPlugin<void> = {
+  id: 'jupyter.extensions.console-completer',
+  requires: [ICompletionManager, IConsoleTracker],
+  autoStart: true,
+  activate: (app: JupyterLab, manager: ICompletionManager, consoles: IConsoleTracker): void => {
+    // Create a handler for each console that is created.
+    consoles.widgetAdded.connect((sender, panel) => {
+      const anchor = panel.console;
+      const cell = anchor.prompt;
+      const editor = cell && cell.editor;
+      const kernel = anchor.session.kernel;
+      const parent = panel;
+      const handler = manager.register({ editor, kernel, parent });
+
+      // Listen for prompt creation.
+      anchor.promptCreated.connect((sender, cell) => {
+        handler.editor = cell && cell.editor;
+      });
+
+      // Listen for kernel changes.
+      anchor.session.kernelChanged.connect((sender, kernel) => {
+        handler.kernel = kernel;
+      });
+    });
+
+    // Add console completer invoke command.
+    app.commands.addCommand(CommandIDs.invokeConsole, {
+      execute: () => {
+        const id = consoles.currentWidget && consoles.currentWidget.id;
+        if (!id) {
+          return;
+        }
+        return app.commands.execute(CommandIDs.invoke, { id });
+      }
+    });
+
+    // Add console completer select command.
+    app.commands.addCommand(CommandIDs.selectConsole, {
+      execute: () => {
+        const id = consoles.currentWidget && consoles.currentWidget.id;
+        if (!id) {
+          return;
+        }
+        return app.commands.execute(CommandIDs.select, { id });
+      }
+    });
+
+    // Set enter key for console completer select command.
+    app.commands.addKeyBinding({
+      command: CommandIDs.selectConsole,
+      keys: ['Enter'],
+      selector: `.jp-ConsolePanel .${COMPLETER_ACTIVE_CLASS}`
+    });
+  }
+};
+
+/**
+ * An extension that registers notebooks for code completion.
+ */
+const notebookPlugin: JupyterLabPlugin<void> = {
+  id: 'jupyter.extensions.notebook-completer',
+  requires: [ICompletionManager, INotebookTracker],
+  autoStart: true,
+  activate: (app: JupyterLab, manager: ICompletionManager, notebooks: INotebookTracker): void => {
+    // Create a handler for each notebook that is created.
+    notebooks.widgetAdded.connect((sender, panel) => {
+      const cell = panel.notebook.activeCell;
+      const editor = cell && cell.editor;
+      const kernel = panel.kernel;
+      const parent = panel;
+      const handler = manager.register({ editor, kernel, parent });
+
+      // Listen for active cell changes.
+      panel.notebook.activeCellChanged.connect((sender, cell) => {
+        handler.editor = cell && cell.editor;
+      });
+
+      // Listen for kernel changes.
+      panel.kernelChanged.connect((sender, kernel) => {
+        handler.kernel = kernel;
+      });
+    });
+
+    // Add notebook completer command.
+    app.commands.addCommand(CommandIDs.invokeNotebook, {
+      execute: () => {
+        const id = notebooks.currentWidget && notebooks.currentWidget.id;
+        return app.commands.execute(CommandIDs.invoke, { id });
+      }
+    });
+
+    // Add notebook completer select command.
+    app.commands.addCommand(CommandIDs.selectNotebook, {
+      execute: () => {
+        const id = notebooks.currentWidget && notebooks.currentWidget.id;
+        if (!id) {
+          return;
+        }
+        return app.commands.execute(CommandIDs.select, { id });
+      }
+    });
+
+    // Set enter key for notebook completer select command.
+    app.commands.addKeyBinding({
+      command: CommandIDs.selectNotebook,
+      keys: ['Enter'],
+      selector: `.jp-Notebook .${COMPLETER_ACTIVE_CLASS}`
+    });
+  }
+};
+
+/**
+ * Export the plugins as default.
+ */
+const plugins: JupyterLabPlugin<any>[] = [
+  service, consolePlugin, notebookPlugin
+];
+export default plugins;

+ 0 - 25
src/completer/index.ts

@@ -23,31 +23,6 @@ export * from './model';
 export * from './widget';
 
 
-/**
- * The command IDs used by the completer plugin.
- */
-export
-namespace CommandIDs {
-  export
-  const invoke: string = 'completer:invoke';
-
-  export
-  const invokeConsole: string = 'completer:invoke-console';
-
-  export
-  const invokeNotebook: string = 'completer:invoke-notebook';
-
-  export
-  const select: string = 'completer:select';
-
-  export
-  const selectConsole: string = 'completer:select-console';
-
-  export
-  const selectNotebook: string = 'completer:select-notebook';
-}
-
-
 
 /* tslint:disable */
 /**

+ 2 - 6
src/default-theme/index.css

@@ -4,8 +4,8 @@
 |----------------------------------------------------------------------------*/
 
 
-/* Variables and mixins */
-@import '../basestyle/materialcolors.css';
+/* Variables and common functions */
+@import '../apputils/index.css';
 @import './variables.css';
 
 
@@ -13,10 +13,6 @@
 @import '../application/index.css';
 
 
-/* Commonly shared functions */
-@import '../apputils/index.css';
-
-
 /* Individual plugins */
 @import '../about-extension/index.css';
 @import '../cells/index.css';