浏览代码

add and debeug consoletracker plugin

Borys Palka 5 年之前
父节点
当前提交
7f50def3ee
共有 4 个文件被更改,包括 129 次插入23 次删除
  1. 11 2
      src/cellManeger/index.ts
  2. 94 0
      src/consoleTracker/index.ts
  3. 10 11
      src/index.ts
  4. 14 10
      src/notebookTracker/index.ts

+ 11 - 2
src/cellManeger/index.ts

@@ -12,14 +12,23 @@ export class CellManager {
     this.activeCell = options.activeCell;
     this.debuggerSessionId = options.sessionId;
     this.onActiveCellChanged();
+    console.log('new Cell manager');
   }
 
-  previousCell: CodeCell;
+  private _previousCell: CodeCell;
   previousLineCount: number;
   private _debuggerSessionId: string;
   breakpointService: BreakpointsService;
   private _activeCell: CodeCell;
 
+  set previousCell(cell: CodeCell) {
+    this._previousCell = cell;
+  }
+
+  get previousCell() {
+    return this._previousCell;
+  }
+
   set debuggerSessionId(id: string) {
     this._debuggerSessionId = id;
   }
@@ -30,6 +39,7 @@ export class CellManager {
 
   set activeCell(cell: CodeCell) {
     this._activeCell = cell;
+    this.onActiveCellChanged();
   }
 
   get activeCell(): CodeCell {
@@ -46,7 +56,6 @@ export class CellManager {
   }
 
   onActiveCellChanged() {
-    // this run before change note, consider how to resolve this
     if (this.activeCell && this.activeCell.editor && this.debuggerSessionId) {
       this.breakpointService.onSelectedBreakpoints(
         this.debuggerSessionId,

+ 94 - 0
src/consoleTracker/index.ts

@@ -0,0 +1,94 @@
+// Copyright (c) Jupyter Development Team.
+// Distributed under the terms of the Modified BSD License.
+// import { CodeCell } from '@jupyterlab/cells';
+
+import {
+  IConsoleTracker,
+  ConsolePanel,
+  CodeConsole
+} from '@jupyterlab/console';
+import { DebugSession } from '../session';
+import { IClientSession, WidgetTracker } from '@jupyterlab/apputils';
+import { BreakpointsService } from '../breakpointsService';
+import { CellManager } from '../cellManeger';
+import { CodeCell } from '@jupyterlab/cells';
+
+export class DebuggerConsoleTracker {
+  constructor(options: DebuggerNotebookTracker.IOptions) {
+    this.breakpointService = options.breakpointService;
+    this.consoleTracker = options.consoleTracker;
+
+    this.consoleTracker.currentChanged.connect(
+      (sender: WidgetTracker<ConsolePanel>, consolePanel: ConsolePanel) => {
+        this.newDebuggerSession(consolePanel, sender);
+      }
+    );
+  }
+
+  consoleTracker: IConsoleTracker;
+  debuggerSession: DebugSession;
+  breakpointService: BreakpointsService;
+  cellManager: CellManager;
+  consoleSession: IClientSession | Boolean;
+  previousConsole: ConsolePanel;
+
+  protected newDebuggerSession(
+    consolePanel: ConsolePanel,
+    widgetTrack: WidgetTracker
+  ) {
+    console.log('current change');
+    this.consoleSession = consolePanel ? consolePanel.session : false;
+    if (this.debuggerSession) {
+      this.debuggerSession.dispose();
+      this.debuggerSession = new DebugSession({
+        client: this.consoleSession as IClientSession
+      });
+      if (this.cellManager && this.previousConsole) {
+        this.previousConsole.console.promptCellCreated.disconnect(
+          this.promptCellCreated,
+          this
+        );
+        if (consolePanel.console.promptCell) {
+          this.cellManager.debuggerSessionId = this.debuggerSession.id;
+          this.cellManager.previousCell = this.cellManager.activeCell;
+          this.cellManager.activeCell = consolePanel.console.promptCell;
+        }
+      }
+    } else {
+      this.debuggerSession = new DebugSession({
+        client: this.consoleSession as IClientSession
+      });
+    }
+
+    if (this.consoleSession) {
+      consolePanel.console.promptCellCreated.connect(
+        this.promptCellCreated,
+        this
+      );
+      this.previousConsole = consolePanel;
+    }
+  }
+
+  protected promptCellCreated(sender: CodeConsole, update: CodeCell) {
+    if (
+      this.cellManager &&
+      this.debuggerSession.id === (this.consoleSession as IClientSession).name
+    ) {
+      this.cellManager.previousCell = this.cellManager.activeCell;
+      this.cellManager.activeCell = update;
+    } else if (!this.cellManager) {
+      this.cellManager = new CellManager({
+        activeCell: update,
+        breakpointService: this.breakpointService,
+        sessionId: this.debuggerSession.id
+      });
+    }
+  }
+}
+
+export namespace DebuggerNotebookTracker {
+  export interface IOptions {
+    consoleTracker: IConsoleTracker;
+    breakpointService: BreakpointsService;
+  }
+}

+ 10 - 11
src/index.ts

@@ -26,6 +26,7 @@ import { Debugger } from './debugger';
 import { IDebugger, IDebuggerSidebar } from './tokens';
 import { DebuggerNotebookTracker } from './notebookTracker';
 import { BreakpointsService } from './breakpointsService';
+import { DebuggerConsoleTracker } from './consoleTracker';
 
 // import { ClientSession, IClientSession } from '@jupyterlab/apputils';
 
@@ -45,7 +46,7 @@ export namespace CommandIDs {
 }
 
 // Service for controll state of breakpoints in extensione
-const service = new BreakpointsService();
+const breakpointService = new BreakpointsService();
 
 /**
  * A plugin that provides visual debugging support for consoles.
@@ -53,14 +54,12 @@ const service = new BreakpointsService();
 const consoles: JupyterFrontEndPlugin<void> = {
   id: '@jupyterlab/debugger:consoles',
   autoStart: true,
-  requires: [IDebugger],
-  optional: [IConsoleTracker],
-  activate: (_, debug, tracker: IConsoleTracker | null) => {
-    if (!tracker) {
-      console.log(`${consoles.id} load failed. There is no console tracker.`);
-      return;
-    }
-    console.log(`${consoles.id} has not been implemented.`, debug);
+  requires: [IDebugger, IConsoleTracker, IEditorTracker],
+  activate: (_, debug, tracker: IConsoleTracker) => {
+    new DebuggerConsoleTracker({
+      consoleTracker: tracker,
+      breakpointService: breakpointService
+    });
   }
 };
 
@@ -103,7 +102,7 @@ const notebooks: JupyterFrontEndPlugin<void> = {
   ) => {
     new DebuggerNotebookTracker({
       notebookTracker: notebook,
-      breakpointService: service
+      breakpointService: breakpointService
     });
 
     // console.log(debugetNoteTracker);
@@ -132,7 +131,7 @@ const sidebar: JupyterFrontEndPlugin<Debugger> = {
     const { shell } = app;
     const label = 'Environment';
     const namespace = 'jp-debugger-sidebar';
-    const sidebar = new Debugger({ breakpointsService: service });
+    const sidebar = new Debugger({ breakpointsService: breakpointService });
     sidebar.id = namespace;
     sidebar.title.label = label;
     shell.add(sidebar, 'right', { activate: false });

+ 14 - 10
src/notebookTracker/index.ts

@@ -20,7 +20,8 @@ export class DebuggerNotebookTracker {
 
     this.notebookTracker.currentChanged.connect(
       (sender, notePanel: NotebookPanel) => {
-        this.newDebuggerSession(notePanel.session, sender);
+        const session = notePanel ? notePanel.session : false;
+        this.newDebuggerSession(session, sender);
       }
     );
   }
@@ -30,7 +31,7 @@ export class DebuggerNotebookTracker {
   breakpointService: BreakpointsService;
   cellManager: CellManager;
 
-  protected test(noteTracker: NotebookTracker, codeCell: CodeCell) {
+  protected onNewCell(noteTracker: NotebookTracker, codeCell: CodeCell) {
     setTimeout(() => {
       if (this.cellManager) {
         this.cellManager.debuggerSessionId = this.debuggerSession.id;
@@ -44,21 +45,24 @@ export class DebuggerNotebookTracker {
         };
         this.cellManager = new CellManager(options);
       }
-      // console.log('active cell changed', this);
     });
   }
 
-  protected newDebuggerSession(client: IClientSession, note: INotebookTracker) {
+  protected newDebuggerSession(
+    client: IClientSession | Boolean,
+    note: INotebookTracker
+  ) {
     if (this.debuggerSession) {
       this.debuggerSession.dispose();
-      note.activeCellChanged.disconnect(this.test, this);
+      note.activeCellChanged.disconnect(this.onNewCell, this);
     }
     // create new session. Just changing client make sometimes that kernel is not attach to note
-    this.debuggerSession = new DebugSession({
-      client: client
-    });
-
-    note.activeCellChanged.connect(this.test, this);
+    if (client) {
+      this.debuggerSession = new DebugSession({
+        client: client as IClientSession
+      });
+      note.activeCellChanged.connect(this.onNewCell, this);
+    }
   }
 }