Parcourir la source

Expose as DebuggerEvaluateDialog.getCode()

Jeremy Tuloup il y a 4 ans
Parent
commit
ea4cdba7f2

+ 12 - 26
packages/debugger-extension/src/index.ts

@@ -13,7 +13,6 @@ import {
 } from '@jupyterlab/application';
 
 import {
-  Dialog,
   ICommandPalette,
   IThemeManager,
   MainAreaWidget,
@@ -32,8 +31,7 @@ import {
   IDebuggerConfig,
   IDebuggerSources,
   IDebuggerSidebar,
-  EvaluateDialog,
-  EvaluateDialogBody
+  DebuggerEvaluateDialog
 } from '@jupyterlab/debugger';
 
 import { DocumentWidget } from '@jupyterlab/docregistry';
@@ -445,8 +443,7 @@ const main: JupyterFrontEndPlugin<void> = {
       }
     }
 
-    const rendermime = new RenderMimeRegistry({ initialFactories });
-
+    // get the mime type of the kernel language for the current debug session
     const getMimeType = async (): Promise<string> => {
       const kernel = service.session?.connection?.kernel;
       if (!kernel) {
@@ -459,25 +456,7 @@ const main: JupyterFrontEndPlugin<void> = {
       return mimeType;
     };
 
-    const getCodeToEvaluate = async (): Promise<string> => {
-      const mimeType = await getMimeType();
-      const options = {
-        title: trans.__('Evaluate Code'),
-        body: new EvaluateDialogBody({ rendermime, mimeType }),
-        buttons: [
-          Dialog.cancelButton(),
-          Dialog.okButton({ label: trans.__('Evaluate') })
-        ]
-      };
-
-      const dialog = new EvaluateDialog(options);
-      const result = await dialog.launch();
-      if (result.button.accept) {
-        return result.value ?? '';
-      }
-
-      return '';
-    };
+    const rendermime = new RenderMimeRegistry({ initialFactories });
 
     commands.addCommand(CommandIDs.evaluate, {
       label: trans.__('Evaluate Code'),
@@ -487,8 +466,15 @@ const main: JupyterFrontEndPlugin<void> = {
         return service.hasStoppedThreads();
       },
       execute: async () => {
-        const code = await getCodeToEvaluate();
-        if (!code) {
+        const mimeType = await getMimeType();
+        const result = await DebuggerEvaluateDialog.getCode({
+          title: trans.__('Evaluate Code'),
+          okLabel: trans.__('Evaluate'),
+          mimeType,
+          rendermime
+        });
+        const code = result.value;
+        if (!result.button.accept || !code) {
           return;
         }
         await service.evaluate(code);

+ 64 - 28
packages/debugger/src/dialogs/evaluate.ts

@@ -8,7 +8,64 @@ import { Message } from '@lumino/messaging';
 
 import { Widget } from '@lumino/widgets';
 
-export class EvaluateDialog extends Dialog<string> {
+/**
+ * A namespace for DebuggerEvaluateDialog statics.
+ */
+export namespace DebuggerEvaluateDialog {
+  /**
+   * Instantiation options for the evaluate dialog.
+   */
+  export interface IOptions {
+    /**
+     * The top level text for the dialog. Defaults to an empty string.
+     */
+    title: string;
+
+    /**
+     * The mime renderer for the cell widget.
+     */
+    rendermime: IRenderMimeRegistry;
+
+    /**
+     * The mime type for the cell widget content.
+     */
+    mimeType?: string;
+
+    /**
+     * Label for ok button.
+     */
+    okLabel?: string;
+
+    /**
+     * Label for cancel button.
+     */
+    cancelLabel?: string;
+  }
+
+  /**
+   * Create and show a dialog to prompt user for code.
+   *
+   * @param options - The dialog setup options.
+   *
+   * @returns A promise that resolves with whether the dialog was accepted
+   */
+  export function getCode(options: IOptions): Promise<Dialog.IResult<string>> {
+    const dialog = new EvaluateDialog({
+      ...options,
+      body: new EvaluateDialogBody(options),
+      buttons: [
+        Dialog.cancelButton({ label: options.cancelLabel }),
+        Dialog.okButton({ label: options.okLabel })
+      ]
+    });
+    return dialog.launch();
+  }
+}
+
+/**
+ * A dialog to prompt users for code to evaluate.
+ */
+class EvaluateDialog extends Dialog<string> {
   /**
    * Handle the DOM events for the Evaluate dialog.
    *
@@ -32,20 +89,19 @@ export class EvaluateDialog extends Dialog<string> {
 /**
  * Widget body with a code cell prompt in a dialog
  */
-export class EvaluateDialogBody
-  extends Widget
-  implements Dialog.IBodyWidget<string> {
+class EvaluateDialogBody extends Widget implements Dialog.IBodyWidget<string> {
   /**
    * CodePromptDialog constructor
    *
    * @param options Constructor options
    */
-  constructor(options: EvaluateDialogBody.IOptions) {
-    const { rendermime, mimeType, ...opts } = options;
-    super(opts);
+  constructor(options: DebuggerEvaluateDialog.IOptions) {
+    super();
+
+    const { rendermime, mimeType } = options;
 
     const model = new CodeCellModel({});
-    model.mimeType = mimeType;
+    model.mimeType = mimeType ?? '';
     this._prompt = new CodeCell({
       rendermime,
       model
@@ -71,23 +127,3 @@ export class EvaluateDialogBody
 
   private _prompt: CodeCell;
 }
-
-/**
- * A namespace for CodePromptDialogBody statics
- */
-export namespace EvaluateDialogBody {
-  /**
-   * The options used to initialize a CodePromptDialogBody object.
-   */
-  export interface IOptions {
-    /**
-     * The mime renderer for the cell widget.
-     */
-    rendermime: IRenderMimeRegistry;
-
-    /**
-     * The mime type for the cell widget content.
-     */
-    mimeType: string;
-  }
-}

+ 1 - 1
packages/debugger/src/index.ts

@@ -14,4 +14,4 @@ export {
   IDebuggerSidebar
 } from './tokens';
 
-export { EvaluateDialog, EvaluateDialogBody } from './dialogs/evaluate';
+export { DebuggerEvaluateDialog } from './dialogs/evaluate';