Prechádzať zdrojové kódy

Merge branch 'session' into debugger-ui

Borys Palka 5 rokov pred
rodič
commit
c24b512820

+ 9 - 27
src/breakpointsService.ts

@@ -8,7 +8,7 @@ import { LineInfo } from './cellManeger';
 export class BreakpointsService {
   constructor() {}
 
-  state: any = {};
+  selectedType: SessionTypes;
   selectedBreakpoints: Breakpoints.IBreakpoint[] = [];
 
   selectedBreakpointsChanged = new Signal<this, Breakpoints.IBreakpoint[]>(
@@ -16,7 +16,7 @@ export class BreakpointsService {
   );
   breakpointChanged = new Signal<this, Breakpoints.IBreakpoint>(this);
 
-  addBreakpoint(session_id: string, editor_id: string, lineInfo: LineInfo) {
+  addBreakpoint(session_id: string, type: string, lineInfo: LineInfo) {
     const breakpoint: Breakpoints.IBreakpoint = {
       line: lineInfo.line,
       active: true,
@@ -33,16 +33,12 @@ export class BreakpointsService {
     return this.selectedBreakpoints;
   }
 
-  onSelectedBreakpoints(session_id: string, editor_id: string) {
-    if (!this.state[session_id]) {
-      this.state[session_id] = {};
-      if (!this.state[session_id][editor_id]) {
-        this.state[session_id][editor_id] = [];
-      }
-    } else {
-      if (!this.state[session_id][editor_id]) {
-        this.state[session_id][editor_id] = [];
-      }
+  onSelectedBreakpoints(session_id: string, type: SessionTypes) {
+    // this still not work
+
+    this.selectedType = type;
+    if (this.selectedType && this.selectedType !== type) {
+      this.clearSelectedBreakpoints();
     }
   }
 
@@ -81,18 +77,4 @@ export class BreakpointsService {
   }
 }
 
-// changeLines(lineInfo: LineInfo, sign: number) {
-//   const breakpoints = this.selectedBreakpoints.map(ele => {
-//     if (
-//       ele.line > lineInfo.line ||
-//       (lineInfo.text === '' && lineInfo.line === ele.line)
-//     ) {
-//       ele.line = ele.line + sign;
-//     }
-//     if (ele.line > 0) {
-//       return ele;
-//     }
-//   });
-//   this.selectedBreakpoints = [...breakpoints];
-//   this.selectedBreakpointsChanged.emit(this.selectedBreakpoints);
-// }
+export type SessionTypes = 'console' | 'notebook';

+ 24 - 17
src/cellManeger/index.ts

@@ -4,32 +4,42 @@
 import { CodeCell } from '@jupyterlab/cells';
 import { CodeMirrorEditor } from '@jupyterlab/codemirror';
 import { Editor, Doc } from 'codemirror';
-import { BreakpointsService } from '../breakpointsService';
+import { BreakpointsService, SessionTypes } from '../breakpointsService';
+import { DebugSession } from '../session';
 
 export class CellManager {
   constructor(options: CellManager.IOptions) {
     this.breakpointService = options.breakpointService;
     this.activeCell = options.activeCell;
-    this.debuggerSessionId = options.sessionId;
+    this.debuggerSession = options.session;
     this.onActiveCellChanged();
   }
 
-  previousCell: CodeCell;
+  private _previousCell: CodeCell;
   previousLineCount: number;
-  private _debuggerSessionId: string;
+  private _debuggerSession: DebugSession;
   breakpointService: BreakpointsService;
   private _activeCell: CodeCell;
 
-  set debuggerSessionId(id: string) {
-    this._debuggerSessionId = id;
+  set previousCell(cell: CodeCell) {
+    this._previousCell = cell;
   }
 
-  get debuggerSessionId() {
-    return this._debuggerSessionId;
+  get previousCell() {
+    return this._previousCell;
+  }
+
+  set debuggerSession(session: DebugSession) {
+    this._debuggerSession = session;
+  }
+
+  get debuggerSession() {
+    return this._debuggerSession;
   }
 
   set activeCell(cell: CodeCell) {
     this._activeCell = cell;
+    this.onActiveCellChanged();
   }
 
   get activeCell(): CodeCell {
@@ -46,12 +56,9 @@ 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,
-        this.getEditorId()
-      );
+    if (this.activeCell && this.activeCell.editor && this.debuggerSession) {
+      this.breakpointService.onSelectedBreakpoints(this.debuggerSession.id, this
+        .debuggerSession.type as SessionTypes);
       if (this.previousCell && !this.previousCell.isDisposed) {
         this.removeListner(this.previousCell);
         this.clearGutter(this.previousCell);
@@ -105,13 +112,13 @@ export class CellManager {
     const isRemoveGutter = !!info.gutterMarkers;
     if (isRemoveGutter) {
       this.breakpointService.removeBreakpoint(
-        this.debuggerSessionId,
+        this.debuggerSession,
         this.getEditorId,
         info as LineInfo
       );
     } else {
       this.breakpointService.addBreakpoint(
-        this.debuggerSessionId,
+        this.debuggerSession.id,
         this.getEditorId(),
         info as LineInfo
       );
@@ -153,7 +160,7 @@ export class CellManager {
 
 export namespace CellManager {
   export interface IOptions {
-    sessionId: string;
+    session: DebugSession;
     breakpointService: BreakpointsService;
     activeCell?: CodeCell;
   }

+ 93 - 0
src/consoleTracker/index.ts

@@ -0,0 +1,93 @@
+// 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
+  ) {
+    this.consoleSession = consolePanel ? consolePanel.session : false;
+    if (this.debuggerSession && this.consoleSession) {
+      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.debuggerSession = this.debuggerSession;
+          this.cellManager.previousCell = this.cellManager.activeCell;
+          this.cellManager.activeCell = consolePanel.console.promptCell;
+        }
+      }
+    } else if (this.consoleSession) {
+      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,
+        session: this.debuggerSession
+      });
+    }
+  }
+}
+
+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 });

+ 16 - 12
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,35 +31,38 @@ 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;
+        this.cellManager.debuggerSession = this.debuggerSession;
         this.cellManager.activeCell = codeCell;
         this.cellManager.onActiveCellChanged();
       } else {
         const options: CellManager.IOptions = {
           breakpointService: this.breakpointService,
           activeCell: codeCell,
-          sessionId: this.debuggerSession.id
+          session: this.debuggerSession
         };
         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);
+    }
   }
 }
 

+ 4 - 0
src/session.ts

@@ -32,6 +32,10 @@ export class DebugSession implements IDebugger.ISession {
     return this.client.name;
   }
 
+  get type() {
+    return this.client.type;
+  }
+
   get client(): IClientSession {
     return this._client;
   }