Parcourir la source

Convert command to async function.

Also change the logic to have early returns, thanks to review comments.
Jason Grout il y a 6 ans
Parent
commit
36dbd92c8c
1 fichiers modifiés avec 47 ajouts et 48 suppressions
  1. 47 48
      packages/notebook-extension/src/index.ts

+ 47 - 48
packages/notebook-extension/src/index.ts

@@ -735,62 +735,61 @@ function addCommands(
   });
   commands.addCommand(CommandIDs.runInConsole, {
     label: 'Run Selected Text or Current Line in Console',
-    execute: args => {
+    execute: async args => {
       // Default to not activating the notebook (thereby putting the notebook
       // into command mode)
       const current = getCurrent({ activate: false, ...args });
 
-      if (current) {
-        const { context, content } = current;
+      if (!current) {
+        return;
+      }
 
-        let cell = content.activeCell;
-        let path = context.path;
-        // ignore action in non-code cell
-        if (!cell || cell.model.type !== 'code') {
-          return Promise.resolve(void 0);
-        }
+      const { context, content } = current;
 
-        let code = '';
-        const editor = cell.editor;
-        const selection = editor.getSelection();
-        const { start, end } = selection;
-        let selected = start.column !== end.column || start.line !== end.line;
-
-        if (selected) {
-          // Get the selected code from the editor.
-          const start = editor.getOffsetAt(selection.start);
-          const end = editor.getOffsetAt(selection.end);
-          code = editor.model.value.text.substring(start, end);
-        } else {
-          // no selection, submit whole line and advance
-          code = editor.getLine(selection.start.line);
-          const cursor = editor.getCursorPosition();
-          if (cursor.line + 1 !== editor.lineCount) {
-            editor.setCursorPosition({
-              line: cursor.line + 1,
-              column: cursor.column
-            });
-          }
-        }
-        // open a console, create if needed
-        if (code) {
-          return commands
-            .execute('console:open', {
-              path,
-              insertMode: 'split-bottom',
-              activate: false
-            })
-            .then(panel => {
-              commands.execute('console:inject', {
-                activate: false,
-                code,
-                path
-              });
-            });
-        } else {
-          return Promise.resolve(void 0);
+      let cell = content.activeCell;
+      let path = context.path;
+      // ignore action in non-code cell
+      if (!cell || cell.model.type !== 'code') {
+        return;
+      }
+
+      let code: string;
+      const editor = cell.editor;
+      const selection = editor.getSelection();
+      const { start, end } = selection;
+      let selected = start.column !== end.column || start.line !== end.line;
+
+      if (selected) {
+        // Get the selected code from the editor.
+        const start = editor.getOffsetAt(selection.start);
+        const end = editor.getOffsetAt(selection.end);
+        code = editor.model.value.text.substring(start, end);
+      } else {
+        // no selection, submit whole line and advance
+        code = editor.getLine(selection.start.line);
+        const cursor = editor.getCursorPosition();
+        if (cursor.line + 1 !== editor.lineCount) {
+          editor.setCursorPosition({
+            line: cursor.line + 1,
+            column: cursor.column
+          });
         }
       }
+
+      if (!code) {
+        return;
+      }
+
+      await commands.execute('console:open', {
+        activate: false,
+        insertMode: 'split-bottom',
+        path
+      });
+      await commands.execute('console:inject', {
+        activate: false,
+        code,
+        path
+      });
     },
     isEnabled
   });