浏览代码

add DebugerNotebookTracker

Borys Palka 5 年之前
父节点
当前提交
061feff525
共有 5 个文件被更改,包括 162 次插入178 次删除
  1. 1 163
      src/breakpoints/index.ts
  2. 11 10
      src/index.ts
  3. 127 0
      src/notebookTracker/index.ts
  4. 22 2
      src/session.ts
  5. 1 3
      src/sidebar.ts

+ 1 - 163
src/breakpoints/index.ts

@@ -7,10 +7,6 @@ import { Widget, Panel, PanelLayout } from '@phosphor/widgets';
 import { DebugProtocol } from 'vscode-debugprotocol';
 import { Body } from './body';
 import { Signal, ISignal } from '@phosphor/signaling';
-import { INotebookTracker } from '@jupyterlab/notebook';
-import { CodeMirrorEditor } from '@jupyterlab/codemirror';
-import { Editor, Doc } from 'codemirror';
-import { CodeCell } from '@jupyterlab/cells';
 
 export class Breakpoints extends Panel {
   constructor(options: Breakpoints.IOptions) {
@@ -47,159 +43,15 @@ export class Breakpoints extends Panel {
         iconClassName: 'jp-CloseAllIcon',
         onClick: () => {
           this.model.breakpoints = [];
-          this.cellsBreakpoints[this.getCell().id] = [];
-          this.removeAllGutterBreakpoints(this.getCell());
         },
         tooltip: 'Remove All Breakpoints'
       })
     );
-
-    this.noteTracker = options.noteTracker;
-    if (this.noteTracker) {
-      this.noteTracker.activeCellChanged.connect(
-        this.onActiveCellChanged,
-        this
-      );
-    }
   }
 
   private isAllActive = true;
   readonly body: Widget;
   readonly model: Breakpoints.IModel;
-  noteTracker: INotebookTracker;
-  previousCell: CodeCell;
-  previousLineCount: number;
-  cellsBreakpoints: { [id: string]: Breakpoints.IBreakpoint[] } = {};
-
-  protected onActiveCellChanged() {
-    const activeCell = this.getCell();
-    if (this.model && activeCell) {
-      if (this.previousCell && !this.previousCell.isDisposed) {
-        this.removeListner(this.previousCell);
-      }
-      this.previousCell = activeCell;
-      const id: string = activeCell.model.id;
-      if (id && !this.cellsBreakpoints[id]) {
-        this.cellsBreakpoints[id] = [];
-      }
-      this.model.breakpoints = this.cellsBreakpoints[id];
-      this.setEditor(activeCell);
-    }
-  }
-
-  protected getCell(): CodeCell {
-    return this.noteTracker.activeCell as CodeCell;
-  }
-
-  protected removeAllGutterBreakpoints(cell: CodeCell) {
-    const editor = cell.editor as CodeMirrorEditor;
-    editor.editor.getDoc().eachLine(line => {
-      editor.editor.setGutterMarker(line, 'breakpoints', null);
-    });
-  }
-
-  removeListner(cell: CodeCell) {
-    const editor = cell.editor as CodeMirrorEditor;
-    this.cellsBreakpoints[cell.model.id] = this.model.breakpoints;
-    this.model.breakpoints = [];
-    editor.setOption('lineNumbers', false);
-    editor.editor.off('gutterClick', this.onGutterClick);
-    editor.editor.off('renderLine', this.onNewRenderLine);
-  }
-
-  setEditor(cell: CodeCell) {
-    if (!cell || !cell.editor) {
-      return;
-    }
-
-    const editor = cell.editor as CodeMirrorEditor;
-    editor.setOption('lineNumbers', true);
-    editor.editor.setOption('gutters', [
-      'CodeMirror-linenumbers',
-      'breakpoints'
-    ]);
-
-    editor.editor.on('gutterClick', this.onGutterClick);
-    editor.editor.on('renderLine', this.onNewRenderLine);
-  }
-
-  protected onNewRenderLine = (editor: Editor, line: any) => {
-    const lineInfo = editor.lineInfo(line);
-    if (
-      !this.model.breakpoints &&
-      this.model.breakpoints.length < 1 &&
-      lineInfo.handle &&
-      lineInfo.handle.order === false
-    ) {
-      return;
-    }
-
-    const doc: Doc = editor.getDoc();
-    const linesNumber = doc.lineCount();
-
-    if (this.previousLineCount !== linesNumber) {
-      if (this.previousLineCount > linesNumber) {
-        this.model.changeLines(lineInfo.line, -1);
-      }
-      if (this.previousLineCount < linesNumber) {
-        this.model.changeLines(lineInfo.line, +1);
-      }
-      this.previousLineCount = linesNumber;
-    }
-    // eage case for backspace line 2
-    if (lineInfo.line === 0) {
-      const breakpoint: Breakpoints.IBreakpoint = this.model.getBreakpointByLineNumber(
-        -1
-      );
-      if (breakpoint) {
-        this.model.removeBreakpoint(breakpoint);
-      }
-    }
-  };
-
-  private addBreakpoint(line: number) {
-    this.model.breakpoint = {
-      id: this.model.breakpoints.length + 1,
-      active: true,
-      verified: true,
-      source: {
-        // TODO: need get filename
-        name: 'untitled.py'
-      },
-      line: line
-    };
-  }
-
-  protected onGutterClick = (editor: Editor, lineNumber: number) => {
-    const info = editor.lineInfo(lineNumber);
-    if (!info) {
-      return;
-    }
-    const isRemoveGutter = !!info.gutterMarkers;
-
-    const breakpoint: Breakpoints.IBreakpoint = this.model.getBreakpointByLineNumber(
-      lineNumber
-    );
-
-    if (!breakpoint && !isRemoveGutter) {
-      this.addBreakpoint(lineNumber);
-    } else if (isRemoveGutter) {
-      this.model.removeBreakpoint(breakpoint);
-    }
-
-    editor.setGutterMarker(
-      lineNumber,
-      'breakpoints',
-      isRemoveGutter ? null : this.createMarkerNode()
-    );
-  };
-
-  createMarkerNode() {
-    var marker = document.createElement('div');
-    marker.className = 'jp-breakpoint-marker';
-    marker.innerHTML = '●';
-    return marker;
-  }
 }
 class BreakpointsHeader extends Widget {
   constructor(title: string) {
@@ -266,18 +118,6 @@ export namespace Breakpoints {
       this.breakpoints = breakpoints;
     }
 
-    changeLines(lineEnter: number, howMany: number) {
-      const breakpoints = this.breakpoints.map(ele => {
-        ele.line = lineEnter <= ele.line ? ele.line + howMany : ele.line;
-        return ele;
-      });
-      this.breakpoints = breakpoints;
-    }
-
-    getBreakpointByLineNumber(lineNumber: number) {
-      return this.breakpoints.find(ele => ele.line === lineNumber);
-    }
-
     private _state: IBreakpoint[];
     private _breakpointsChanged = new Signal<this, IBreakpoint[]>(this);
     private _breakpointChanged = new Signal<this, IBreakpoint>(this);
@@ -286,7 +126,5 @@ export namespace Breakpoints {
   /**
    * Instantiation options for `Breakpoints`;
    */
-  export interface IOptions extends Panel.IOptions {
-    noteTracker?: INotebookTracker;
-  }
+  export interface IOptions extends Panel.IOptions {}
 }

+ 11 - 10
src/index.ts

@@ -17,13 +17,14 @@ import { IStateDB } from '@jupyterlab/coreutils';
 
 import { IEditorTracker } from '@jupyterlab/fileeditor';
 
-import { INotebookTracker, NotebookPanel } from '@jupyterlab/notebook';
+import { INotebookTracker } from '@jupyterlab/notebook';
 
 import { Debugger } from './debugger';
 
 // import { DebuggerSidebar } from './sidebar';
 
 import { IDebugger, IDebuggerSidebar } from './tokens';
+import { DebuggerNotebookTracker } from './notebookTracker';
 
 // import { ClientSession, IClientSession } from '@jupyterlab/apputils';
 
@@ -96,8 +97,10 @@ const notebooks: JupyterFrontEndPlugin<void> = {
     notebook: INotebookTracker,
     palette: ICommandPalette
   ) => {
-    notebook.widgetAdded.connect((sender, notePanel: NotebookPanel) => {});
-
+    new DebuggerNotebookTracker({
+      notebookTracker: notebook
+    });
+    // console.log(debugetNoteTracker);
     // this exist only for my test in futre will be removed
     const command: string = CommandIDs.debugNotebook;
     app.commands.addCommand(command, {
@@ -118,16 +121,12 @@ const sidebar: JupyterFrontEndPlugin<Debugger> = {
   autoStart: true,
   activate: (
     app: JupyterFrontEnd,
-    restorer: ILayoutRestorer | null,
-    notebookTracker: INotebookTracker
+    restorer: ILayoutRestorer | null
   ): Debugger => {
     const { shell } = app;
     const label = 'Environment';
     const namespace = 'jp-debugger-sidebar';
-    const sidebar = new Debugger({
-      noteTracker: notebookTracker
-    });
-
+    const sidebar = new Debugger({});
     sidebar.id = namespace;
     sidebar.title.label = label;
     shell.add(sidebar, 'right', { activate: false });
@@ -158,7 +157,9 @@ const tracker: JupyterFrontEndPlugin<IDebugger> = {
     const tracker = new WidgetTracker<MainAreaWidget<Debugger>>({
       namespace: 'debugger'
     });
-
+    tracker.widgetUpdated.connect((_, upadete) => {
+      upadete;
+    });
     app.commands.addCommand(CommandIDs.create, {
       execute: args => {
         const id = (args.id as string) || '';

+ 127 - 0
src/notebookTracker/index.ts

@@ -0,0 +1,127 @@
+// Copyright (c) Jupyter Development Team.
+// Distributed under the terms of the Modified BSD License.
+
+import { INotebookTracker, NotebookPanel } from '@jupyterlab/notebook';
+import { CodeCell } from '@jupyterlab/cells';
+import { CodeMirrorEditor } from '@jupyterlab/codemirror';
+import { Editor, Doc } from 'codemirror';
+import { DebugSession } from './../session';
+
+export class DebuggerNotebookTracker {
+  constructor(options: DebuggerNotebookTracker.IOptions) {
+    this.notebookTracker = options.notebookTracker;
+    this.notebookTracker.widgetAdded.connect(
+      (sender, notePanel: NotebookPanel) => {}
+    );
+
+    this.notebookTracker.currentChanged.connect(
+      (sender, notePanel: NotebookPanel) => {
+        if (!this.debuggerSession) {
+          this.debuggerSession = new DebugSession({
+            client: notePanel.session
+          });
+        }
+        this.debuggerSession.client = notePanel.session;
+      }
+    );
+
+    this.notebookTracker.activeCellChanged.connect(
+      this.onActiveCellChanged,
+      this
+    );
+  }
+  notebookTracker: INotebookTracker;
+  previousCell: CodeCell;
+  previousLineCount: number;
+  debuggerSession: DebugSession;
+
+  protected onActiveCellChanged() {
+    const activeCell = this.getCell();
+    if (activeCell) {
+      if (this.previousCell && !this.previousCell.isDisposed) {
+        this.removeListner(this.previousCell);
+      }
+      this.previousCell = activeCell;
+      this.setEditor(activeCell);
+    }
+  }
+
+  protected setEditor(cell: CodeCell) {
+    if (!cell || !cell.editor) {
+      return;
+    }
+
+    const editor = cell.editor as CodeMirrorEditor;
+    editor.setOption('lineNumbers', true);
+    editor.editor.setOption('gutters', [
+      'CodeMirror-linenumbers',
+      'breakpoints'
+    ]);
+
+    editor.editor.on('gutterClick', this.onGutterClick);
+    // editor.editor.on('renderLine', this.onNewRenderLine);
+  }
+
+  protected removeListner(cell: CodeCell) {
+    const editor = cell.editor as CodeMirrorEditor;
+    editor.setOption('lineNumbers', false);
+    editor.editor.off('gutterClick', this.onGutterClick);
+    // editor.editor.off('renderLine', this.onNewRenderLine);
+  }
+
+  protected getCell(): CodeCell {
+    return this.notebookTracker.activeCell as CodeCell;
+  }
+
+  protected getCurrentNoteBook(): NotebookPanel {
+    return this.notebookTracker.currentWidget;
+  }
+
+  protected onGutterClick = (editor: Editor, lineNumber: number) => {
+    const info = editor.lineInfo(lineNumber);
+    if (!info) {
+      return;
+    }
+    const isRemoveGutter = !!info.gutterMarkers;
+
+    editor.setGutterMarker(
+      lineNumber,
+      'breakpoints',
+      isRemoveGutter ? null : this.createMarkerNode()
+    );
+  };
+
+  protected onNewRenderLine = (editor: Editor, line: any) => {
+    const lineInfo = editor.lineInfo(line);
+    if (lineInfo.handle && lineInfo.handle.order === false) {
+      return;
+    }
+
+    const doc: Doc = editor.getDoc();
+    const linesNumber = doc.lineCount();
+
+    if (this.previousLineCount !== linesNumber) {
+      if (this.previousLineCount > linesNumber) {
+      }
+      if (this.previousLineCount < linesNumber) {
+      }
+      this.previousLineCount = linesNumber;
+    }
+    // eage case for backspace line 2
+    if (lineInfo.line === 0) {
+    }
+  };
+
+  private createMarkerNode() {
+    var marker = document.createElement('div');
+    marker.className = 'jp-breakpoint-marker';
+    marker.innerHTML = '●';
+    return marker;
+  }
+}
+
+export namespace DebuggerNotebookTracker {
+  export interface IOptions {
+    notebookTracker: INotebookTracker;
+  }
+}

+ 22 - 2
src/session.ts

@@ -21,13 +21,33 @@ export class DebugSession implements IDebugger.ISession {
    */
   constructor(options: DebugSession.IOptions) {
     this.client = options.client;
-    this.client.iopubMessage.connect(this._handleEvent, this);
+    this.path = this.client.path || '';
+    this.type = this.client.type;
   }
 
   /**
    * The client session to connect to a debugger.
    */
-  client: IClientSession;
+  private _client: IClientSession;
+  type: string;
+  path?: string;
+
+  get client(): IClientSession {
+    return this._client;
+  }
+
+  set client(client: IClientSession | null) {
+    if (this._client === client) {
+      return;
+    }
+
+    if (this._client) {
+      Signal.clearData(this._client);
+    }
+
+    this._client = client;
+    this._client.iopubMessage.connect(this._handleEvent, this);
+  }
 
   /**
    * The code editors in a debugger session.

+ 1 - 3
src/sidebar.ts

@@ -18,11 +18,9 @@ export class DebuggerSidebar extends SplitPanel {
     this.orientation = 'vertical';
     this.addClass('jp-DebuggerSidebar');
 
-    const notebook = this.model.notebook;
-
     this.variables = new Variables();
     this.callstack = new Callstack();
-    this.breakpoints = new Breakpoints({ noteTracker: notebook });
+    this.breakpoints = new Breakpoints({});
 
     this.addWidget(this.variables);
     this.addWidget(this.callstack);