浏览代码

Handle non-existing kernels with getCodeId

Jeremy Tuloup 4 年之前
父节点
当前提交
1039554156
共有 2 个文件被更改,包括 42 次插入6 次删除
  1. 8 1
      src/service.ts
  2. 34 5
      src/sources.ts

+ 8 - 1
src/service.ts

@@ -124,7 +124,14 @@ export class DebuggerService implements IDebugger, IDisposable {
    * @param code The source code.
    */
   getCodeId(code: string): string {
-    return this._config.getCodeId(code, this.session?.connection?.kernel?.name);
+    try {
+      return this._config.getCodeId(
+        code,
+        this.session?.connection?.kernel?.name
+      );
+    } catch {
+      return '';
+    }
   }
 
   /**

+ 34 - 5
src/sources.ts

@@ -108,8 +108,11 @@ export class DebuggerSources implements IDebugger.ISources {
       cells.forEach((cell, i) => {
         // check the event is for the correct cell
         const code = cell.model.value.text;
-        const cellId = this._config.getCodeId(code, kernel);
-        if (source !== cellId) {
+        const codeId = this._getCodeId(code, kernel);
+        if (!codeId) {
+          return;
+        }
+        if (source !== codeId) {
           return;
         }
         if (focus) {
@@ -149,7 +152,10 @@ export class DebuggerSources implements IDebugger.ISources {
       const cells = consoleWidget.console.cells;
       each(cells, cell => {
         const code = cell.model.value.text;
-        const codeId = this._config.getCodeId(code, kernel);
+        const codeId = this._getCodeId(code, kernel);
+        if (!codeId) {
+          return;
+        }
         if (source !== codeId) {
           return;
         }
@@ -188,7 +194,10 @@ export class DebuggerSources implements IDebugger.ISources {
       }
 
       const code = editor.model.value.text;
-      const codeId = this._config.getCodeId(code, kernel);
+      const codeId = this._getCodeId(code, kernel);
+      if (!codeId) {
+        return;
+      }
       if (source !== codeId) {
         return;
       }
@@ -218,7 +227,11 @@ export class DebuggerSources implements IDebugger.ISources {
       }
 
       const code = editor.model.value.text;
-      const codeId = this._config.getCodeId(code, kernel);
+      const codeId = this._getCodeId(code, kernel);
+      if (!codeId) {
+        return;
+      }
+
       if (widget.title.caption !== source && source !== codeId) {
         return;
       }
@@ -229,6 +242,22 @@ export class DebuggerSources implements IDebugger.ISources {
     });
     return editors;
   }
+
+  /**
+   * Get the code id for a given source and kernel,
+   * and handle the case of a kernel without parameters.
+   *
+   * @param code The source code.
+   * @param kernel The name of the kernel.
+   */
+  private _getCodeId(code: string, kernel: string): string | undefined {
+    try {
+      return this._config.getCodeId(code, kernel);
+    } catch {
+      return undefined;
+    }
+  }
+
   private _shell: JupyterFrontEnd.IShell;
   private _readOnlyEditorTracker: WidgetTracker<
     MainAreaWidget<CodeEditorWrapper>