Parcourir la source

Finish the console for editor

Steven Silvester il y a 8 ans
Parent
commit
5004de0797
4 fichiers modifiés avec 86 ajouts et 29 suppressions
  1. 19 3
      src/console/plugin.ts
  2. 61 26
      src/editorwidget/plugin.ts
  3. 1 0
      src/editorwidget/widget.ts
  4. 5 0
      src/shortcuts/plugin.ts

+ 19 - 3
src/console/plugin.ts

@@ -143,8 +143,8 @@ function activateConsole(app: JupyterLab, services: IServiceManager, rendermime:
     commands.addCommand(command, {
       label: `${displayName} console`,
       execute: () => {
-        let kernelName = `${kernelNameMap[displayName]}`;
-        commands.execute('console:create', { kernelName });
+        let name = `${kernelNameMap[displayName]}`;
+        commands.execute('console:create', { kernel: { name } });
       }
     });
     palette.addItem({ command, category });
@@ -234,6 +234,7 @@ function activateConsole(app: JupyterLab, services: IServiceManager, rendermime:
       if (args.sessionId) {
         return manager.connectTo(args.sessionId).then(session => {
           createConsole(session);
+          return session.id;
         });
       }
 
@@ -246,7 +247,7 @@ function activateConsole(app: JupyterLab, services: IServiceManager, rendermime:
       path = `${path}/console-${utils.uuid()}`;
 
       // Get the kernel model.
-      getKernel(args).then(kernel => {
+      return getKernel(args).then(kernel => {
         // Start the session.
         let options: ISession.IOptions = {
           path,
@@ -255,11 +256,25 @@ function activateConsole(app: JupyterLab, services: IServiceManager, rendermime:
         };
         return manager.startNew(options).then(session => {
           createConsole(session);
+          return session.id;
         });
       });
     }
   });
 
+  command = 'console:inject';
+  commands.addCommand(command, {
+    execute: (args: JSONObject) => {
+      let id = args['id'];
+      for (let i = 0; i < tracker.widgets.length; i++) {
+        let widget = tracker.widgets.at(i);
+        if (widget.content.session.id === id) {
+          widget.content.inject(args['code'] as string);
+        }
+      }
+    }
+  });
+
   /**
    * Get the kernel given the create args.
    */
@@ -323,6 +338,7 @@ function activateConsole(app: JupyterLab, services: IServiceManager, rendermime:
       captionOptions.executed = null;
       panel.title.caption = Private.caption(captionOptions);
     });
+    tracker.add(panel);
   }
 
   command = 'console:switch-kernel';

+ 61 - 26
src/editorwidget/plugin.ts

@@ -5,6 +5,10 @@ import {
   each
 } from 'phosphor/lib/algorithm/iteration';
 
+import {
+  AttachedProperty
+} from 'phosphor/lib/core/properties';
+
 import {
   FocusTracker
 } from 'phosphor/lib/ui/focustracker';
@@ -81,7 +85,8 @@ const cmdIds = {
   vimMode: 'editor:vim-mode',
   closeAll: 'editor:close-all',
   changeTheme: 'editor:change-theme',
-  startConsole: 'editor:startConsole'
+  startConsole: 'editor:start-console',
+  runCode: 'editor:run-code'
 };
 
 
@@ -107,46 +112,36 @@ function activateEditorHandler(app: JupyterLab, registry: IDocumentRegistry, mai
 
   mainMenu.addMenu(createMenu(app, tracker), {rank: 30});
 
-  addCommands(app, tracker);
-
-  [
-    cmdIds.lineNumbers,
-    cmdIds.lineWrap,
-    cmdIds.matchBrackets,
-    cmdIds.vimMode,
-    cmdIds.closeAll,
-    cmdIds.startConsole
-  ].forEach(command => palette.addItem({ command, category: 'Editor' }));
-
-  return tracker;
-}
+  let commands = app.commands;
 
-
-/**
- * Add the editor commands to the application's command registry.
- */
-function addCommands(app: JupyterLab, tracker: IEditorTracker): void {
-  app.commands.addCommand(cmdIds.lineNumbers, {
+  commands.addCommand(cmdIds.lineNumbers, {
     execute: () => { toggleLineNums(tracker); },
     label: 'Toggle Line Numbers',
   });
-  app.commands.addCommand(cmdIds.lineWrap, {
+
+  commands.addCommand(cmdIds.lineWrap, {
     execute: () => { toggleLineWrap(tracker); },
     label: 'Toggle Line Wrap',
   });
-  app.commands.addCommand(cmdIds.matchBrackets, {
+
+  commands.addCommand(cmdIds.matchBrackets, {
     execute: () => { toggleMatchBrackets(tracker); },
     label: 'Toggle Match Brackets',
   });
-  app.commands.addCommand(cmdIds.vimMode, {
+
+  commands.addCommand(cmdIds.vimMode, {
     execute: () => { toggleVim(tracker); },
     label: 'Toggle Vim Mode'
   });
-  app.commands.addCommand(cmdIds.closeAll, {
+
+  commands.addCommand(cmdIds.closeAll, {
     execute: () => { closeAllFiles(tracker); },
     label: 'Close all files'
   });
-  app.commands.addCommand(cmdIds.startConsole, {
+
+  // TODO: add an attached property to the widget with the session id.
+
+  commands.addCommand(cmdIds.startConsole, {
     execute: () => {
       let widget = tracker.currentWidget;
       if (!widget) {
@@ -156,13 +151,53 @@ function addCommands(app: JupyterLab, tracker: IEditorTracker): void {
         path: widget.context.path,
         preferredLanguage: widget.context.model.defaultKernelLanguage
       };
-      app.commands.execute('console:create', options);
+      commands.execute('console:create', options).then(id => {
+        sessionIdProperty.set(widget, id);
+      });
     },
     label: 'Start Console for Editor'
   });
+
+  commands.addCommand(cmdIds.runCode, {
+    execute: () => {
+      let widget = tracker.currentWidget;
+      if (!widget) {
+        return;
+      }
+      // Get the session id.
+      let id = sessionIdProperty.get(widget);
+      if (!id) {
+        return;
+      }
+      // Get the selected code from the editor.
+      let code = widget.editor.getDoc().getSelection();
+      commands.execute('console:inject', { id, code });
+    },
+    label: 'Run Code',
+  });
+
+  [
+    cmdIds.lineNumbers,
+    cmdIds.lineWrap,
+    cmdIds.matchBrackets,
+    cmdIds.vimMode,
+    cmdIds.closeAll,
+    cmdIds.startConsole,
+    cmdIds.runCode,
+  ].forEach(command => palette.addItem({ command, category: 'Editor' }));
+
+  return tracker;
 }
 
 
+
+/**
+ * An attached property for the session id associated with an editor widget.
+ */
+const sessionIdProperty = new AttachedProperty<EditorWidget, string>({ name: 'sessionId' });
+
+
+
 /**
  * Toggle editor line numbers
  */

+ 1 - 0
src/editorwidget/widget.ts

@@ -70,6 +70,7 @@ class EditorWidget extends CodeMirrorWidget {
     super({
       extraKeys: {
         'Tab': 'indentMore',
+        'Shift-Enter': () => { /* no-op */ }
       },
       indentUnit: 4,
       theme: DEFAULT_CODEMIRROR_THEME,

+ 5 - 0
src/shortcuts/plugin.ts

@@ -35,6 +35,11 @@ const SHORTCUTS = [
     selector: 'body',
     keys: ['Accel Shift P']
   },
+  {
+    command: 'editor:run-code',
+    selector: '.jp-EditorWidget',
+    keys: ['Shift Enter']
+  },
   {
     command: 'file-browser:toggle',
     selector: 'body',