瀏覽代碼

Merge pull request #201 from jtpio/debugger-flag

Add support for the debugger flag in kernel info
Jeremy Tuloup 5 年之前
父節點
當前提交
84e0753dc6
共有 5 個文件被更改,包括 91 次插入4 次删除
  1. 3 1
      src/index.ts
  2. 8 1
      src/service.ts
  3. 11 0
      src/session.ts
  4. 23 2
      src/tokens.ts
  5. 46 0
      tests/src/service.spec.ts

+ 3 - 1
src/index.ts

@@ -82,7 +82,9 @@ async function setDebugSession(
   } else {
     debug.session.client = client;
   }
-  await debug.restoreState(true);
+  if (debug.isDebuggingEnabled) {
+    await debug.restoreState(true);
+  }
   app.commands.notifyCommandChanged();
 }
 

+ 8 - 1
src/service.ts

@@ -42,6 +42,13 @@ export class DebugService implements IDebugger {
     return this._isDisposed;
   }
 
+  /**
+   * Whether debugging is enabled for the current session.
+   */
+  get isDebuggingEnabled(): boolean {
+    return this._session.kernelInfo.debugger || false;
+  }
+
   /**
    * Returns the mode of the debugger UI.
    *
@@ -166,7 +173,7 @@ export class DebugService implements IDebugger {
 
   /**
    * Starts a debugger.
-   * Precondition: canStart() && !isStarted()
+   * Precondition: !isStarted()
    */
   async start(): Promise<void> {
     await this.session.start();

+ 11 - 0
src/session.ts

@@ -67,6 +67,17 @@ export class DebugSession implements IDebugger.ISession {
     }
   }
 
+  /**
+   * Return the kernel info for the debug session.
+   */
+  get kernelInfo(): IDebugger.ISession.IInfoReply | null {
+    const kernel = this.client.kernel;
+    if (!kernel) {
+      return null;
+    }
+    return kernel.info as IDebugger.ISession.IInfoReply;
+  }
+
   /**
    * Whether the debug session is started
    */

+ 23 - 2
src/tokens.ts

@@ -3,7 +3,7 @@
 
 import { IClientSession } from '@jupyterlab/apputils';
 
-import { Session } from '@jupyterlab/services';
+import { KernelMessage, Session } from '@jupyterlab/services';
 
 import { Token } from '@phosphor/coreutils';
 
@@ -22,6 +22,11 @@ import { Debugger } from './debugger';
  * An interface describing an application's visual debugger.
  */
 export interface IDebugger extends IDisposable {
+  /**
+   * Whether debugging is enabled for the current session.
+   */
+  readonly isDebuggingEnabled: boolean;
+
   /**
    * The mode of the debugger UI.
    *
@@ -73,7 +78,7 @@ export interface IDebugger extends IDisposable {
 
   /**
    * Starts a debugger.
-   * Precondition: canStart() && !isStarted()
+   * Precondition: !isStarted()
    */
   start(): Promise<void>;
 
@@ -150,10 +155,16 @@ export namespace IDebugger {
      */
     client: IClientSession | Session.ISession;
 
+    /**
+     * The kernel info for the debug session.
+     */
+    readonly kernelInfo: IDebugger.ISession.IInfoReply | null;
+
     /**
      * Whether the debug session is started
      */
     readonly isStarted: boolean;
+
     /**
      * Signal emitted for debug event messages.
      */
@@ -187,6 +198,16 @@ export namespace IDebugger {
   }
 
   export namespace ISession {
+    /**
+     * Response to the 'kernel_info_request' request.
+     * This interface extends the IInfoReply by adding the `debugger` key
+     * that isn't part of the protocol yet.
+     * See this pull request for more info: https://github.com/jupyter/jupyter_client/pull/486
+     */
+    export interface IInfoReply extends KernelMessage.IInfoReply {
+      debugger: boolean;
+    }
+
     /**
      * Arguments for 'dumpCell' request.
      * This is an addition to the Debug Adapter Protocol to support

+ 46 - 0
tests/src/service.spec.ts

@@ -14,6 +14,52 @@ import { DebugSession } from '../../lib/session';
 
 import { IDebugger } from '../../lib/tokens';
 
+describe('Debugging Support', () => {
+  let xpythonClient: IClientSession;
+  let ipykernelClient: IClientSession;
+
+  beforeAll(async () => {
+    xpythonClient = await createClientSession({
+      kernelPreference: {
+        name: 'xpython'
+      }
+    });
+    ipykernelClient = await createClientSession({
+      kernelPreference: {
+        name: 'python3'
+      }
+    });
+    await Promise.all([
+      (xpythonClient as ClientSession).initialize(),
+      (ipykernelClient as ClientSession).initialize()
+    ]);
+    await Promise.all([
+      xpythonClient.kernel.ready,
+      ipykernelClient.kernel.ready
+    ]);
+  });
+
+  afterAll(async () => {
+    await Promise.all([xpythonClient.shutdown(), ipykernelClient.shutdown()]);
+  });
+
+  describe('#isDebuggingEnabled', () => {
+    it('should return true for kernels that have support for debugging', () => {
+      const debugSession = new DebugSession({ client: xpythonClient });
+      let service = new DebugService();
+      service.session = debugSession;
+      expect(service.isDebuggingEnabled).to.be.true;
+    });
+
+    it('should return false for kernels that do not have support for debugging', () => {
+      const debugSession = new DebugSession({ client: ipykernelClient });
+      let service = new DebugService();
+      service.session = debugSession;
+      expect(service.isDebuggingEnabled).to.be.false;
+    });
+  });
+});
+
 describe('DebugService', () => {
   let client: IClientSession;
   let model: Debugger.Model;