浏览代码

add cellManager

Borys Palka 5 年之前
父节点
当前提交
a3995cbf8c
共有 3 个文件被更改,包括 205 次插入154 次删除
  1. 1 1
      src/breakpointsService.ts
  2. 173 0
      src/cellManeger/index.ts
  3. 31 153
      src/notebookTracker/index.ts

+ 1 - 1
src/breakpointsService.ts

@@ -3,7 +3,7 @@
 
 
 import { Breakpoints } from './breakpoints';
 import { Breakpoints } from './breakpoints';
 import { Signal } from '@phosphor/signaling';
 import { Signal } from '@phosphor/signaling';
-import { LineInfo } from './notebookTracker';
+import { LineInfo } from './cellManeger';
 
 
 export class BreakpointsService {
 export class BreakpointsService {
   constructor() {}
   constructor() {}

+ 173 - 0
src/cellManeger/index.ts

@@ -0,0 +1,173 @@
+// Copyright (c) Jupyter Development Team.
+// Distributed under the terms of the Modified BSD License.
+
+import { CodeCell } from '@jupyterlab/cells';
+import { CodeMirrorEditor } from '@jupyterlab/codemirror';
+import { Editor, Doc } from 'codemirror';
+import { BreakpointsService } from '../breakpointsService';
+
+export class CellManager {
+  constructor(options: CellManager.IOptions) {
+    this.breakpointService = options.breakpointService;
+    this.activeCell = options.activeCell;
+    this.debuggerSessionId = options.sessionId;
+    this.onActiveCellChanged();
+  }
+
+  previousCell: CodeCell;
+  previousLineCount: number;
+  private _debuggerSessionId: string;
+  breakpointService: BreakpointsService;
+  private _activeCell: CodeCell;
+
+  set debuggerSessionId(id: string) {
+    this._debuggerSessionId = id;
+  }
+
+  get debuggerSessionId() {
+    return this._debuggerSessionId;
+  }
+
+  set activeCell(cell: CodeCell) {
+    this._activeCell = cell;
+  }
+
+  get activeCell(): CodeCell {
+    return this._activeCell;
+  }
+
+  protected clearGutter(cell: CodeCell) {
+    const editor = cell.editor as CodeMirrorEditor;
+    editor.doc.eachLine(line => {
+      if ((line as LineInfo).gutterMarkers) {
+        editor.editor.setGutterMarker(line, 'breakpoints', null);
+      }
+    });
+  }
+
+  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.previousCell && !this.previousCell.isDisposed) {
+        this.removeListner(this.previousCell);
+        this.clearGutter(this.previousCell);
+        this.breakpointService.clearSelectedBreakpoints();
+      }
+      this.previousCell = this.activeCell;
+      this.setEditor(this.activeCell);
+    }
+  }
+
+  protected setEditor(cell: CodeCell) {
+    if (!cell || !cell.editor) {
+      return;
+    }
+
+    const editor = cell.editor as CodeMirrorEditor;
+
+    this.previousLineCount = editor.lineCount;
+
+    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.activeCell as CodeCell;
+  }
+
+  protected getEditorId(): string {
+    return this.getCell().editor.uuid;
+  }
+
+  protected onGutterClick = (editor: Editor, lineNumber: number) => {
+    const info = editor.lineInfo(lineNumber);
+    if (!info) {
+      return;
+    }
+
+    const isRemoveGutter = !!info.gutterMarkers;
+    if (isRemoveGutter) {
+      this.breakpointService.removeBreakpoint(
+        this.debuggerSessionId,
+        this.getEditorId,
+        info as LineInfo
+      );
+    } else {
+      this.breakpointService.addBreakpoint(
+        this.debuggerSessionId,
+        this.getEditorId(),
+        info as LineInfo
+      );
+    }
+
+    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) {
+      var lines: LineInfo[] = [];
+      doc.eachLine(line => {
+        if ((line as LineInfo).gutterMarkers) {
+          lines.push(editor.lineInfo(line));
+        }
+      });
+      this.breakpointService.changeLines(lines);
+      this.previousLineCount = linesNumber;
+    }
+  };
+
+  private createMarkerNode() {
+    var marker = document.createElement('div');
+    marker.className = 'jp-breakpoint-marker';
+    marker.innerHTML = '●';
+    return marker;
+  }
+}
+
+export namespace CellManager {
+  export interface IOptions {
+    sessionId: string;
+    breakpointService: BreakpointsService;
+    activeCell?: CodeCell;
+  }
+}
+
+export interface LineInfo {
+  line: any;
+  handle: any;
+  text: string;
+  /** Object mapping gutter IDs to marker elements. */
+  gutterMarkers: any;
+  textClass: string;
+  bgClass: string;
+  wrapClass: string;
+  /** Array of line widgets attached to this line. */
+  widgets: any;
+}

+ 31 - 153
src/notebookTracker/index.ts

@@ -1,173 +1,64 @@
 // Copyright (c) Jupyter Development Team.
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 // 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 {
+  INotebookTracker,
+  NotebookPanel,
+  NotebookTracker
+} from '@jupyterlab/notebook';
+// import { CodeCell } from '@jupyterlab/cells';
 import { DebugSession } from './../session';
 import { DebugSession } from './../session';
 import { IClientSession } from '@jupyterlab/apputils';
 import { IClientSession } from '@jupyterlab/apputils';
 import { BreakpointsService } from '../breakpointsService';
 import { BreakpointsService } from '../breakpointsService';
+import { CodeCell } from '@jupyterlab/cells';
+import { CellManager } from '../cellManeger';
 
 
 export class DebuggerNotebookTracker {
 export class DebuggerNotebookTracker {
   constructor(options: DebuggerNotebookTracker.IOptions) {
   constructor(options: DebuggerNotebookTracker.IOptions) {
     this.breakpointService = options.breakpointService;
     this.breakpointService = options.breakpointService;
     this.notebookTracker = options.notebookTracker;
     this.notebookTracker = options.notebookTracker;
-    this.notebookTracker.widgetAdded.connect(
-      (sender, notePanel: NotebookPanel) => {
-        this.newDebuggerSession(notePanel.session);
-      }
-    );
 
 
     this.notebookTracker.currentChanged.connect(
     this.notebookTracker.currentChanged.connect(
       (sender, notePanel: NotebookPanel) => {
       (sender, notePanel: NotebookPanel) => {
-        this.newDebuggerSession(notePanel.session);
-      }
-    );
-
-    this.breakpointService.selectedBreakpointsChanged.connect(
-      (sender, update) => {
-        if (update && update.length === 0) {
-          // this.clearGutter(this.getCell());
-        }
+        this.newDebuggerSession(notePanel.session, sender);
       }
       }
     );
     );
   }
   }
 
 
   notebookTracker: INotebookTracker;
   notebookTracker: INotebookTracker;
-  previousCell: CodeCell;
-  previousLineCount: number;
   debuggerSession: DebugSession;
   debuggerSession: DebugSession;
   breakpointService: BreakpointsService;
   breakpointService: BreakpointsService;
+  cellManager: CellManager;
+
+  protected test(noteTracker: NotebookTracker, codeCell: CodeCell) {
+    setTimeout(() => {
+      if (this.cellManager) {
+        this.cellManager.debuggerSessionId = this.debuggerSession.id;
+        this.cellManager.activeCell = codeCell;
+        this.cellManager.onActiveCellChanged();
+      } else {
+        const options: CellManager.IOptions = {
+          breakpointService: this.breakpointService,
+          activeCell: codeCell,
+          sessionId: this.debuggerSession.id
+        };
+        this.cellManager = new CellManager(options);
+      }
+      // console.log('active cell changed', this);
+    });
+  }
 
 
-  protected async newDebuggerSession(client: IClientSession) {
+  protected newDebuggerSession(client: IClientSession, note: INotebookTracker) {
     if (this.debuggerSession) {
     if (this.debuggerSession) {
       this.debuggerSession.dispose();
       this.debuggerSession.dispose();
+      note.activeCellChanged.disconnect(this.test, this);
     }
     }
-
     // create new session. Just changing client make sometimes that kernel is not attach to note
     // create new session. Just changing client make sometimes that kernel is not attach to note
     this.debuggerSession = new DebugSession({
     this.debuggerSession = new DebugSession({
       client: client
       client: client
     });
     });
-    await this.notebookTracker.activeCellChanged.connect(
-      this.onActiveCellChanged,
-      this
-    );
-  }
-
-  protected clearGutter(cell: CodeCell) {
-    const editor = cell.editor as CodeMirrorEditor;
-    editor.doc.eachLine(line => {
-      if ((line as LineInfo).gutterMarkers) {
-        editor.editor.setGutterMarker(line, 'breakpoints', null);
-      }
-    });
-  }
-
-  protected async onActiveCellChanged() {
-    const activeCell = this.getCell();
-    // this run before change note, consider how to resolve this
-    if (activeCell && activeCell.editor && this.debuggerSession) {
-      this.breakpointService.onSelectedBreakpoints(
-        this.debuggerSession.id,
-        this.getEditorId()
-      );
-      if (this.previousCell && !this.previousCell.isDisposed) {
-        this.removeListner(this.previousCell);
-        this.clearGutter(this.previousCell);
-        this.breakpointService.clearSelectedBreakpoints();
-      }
-      this.previousCell = activeCell;
-      this.setEditor(activeCell);
-    }
-  }
-
-  protected setEditor(cell: CodeCell) {
-    if (!cell || !cell.editor) {
-      return;
-    }
-
-    const editor = cell.editor as CodeMirrorEditor;
-
-    this.previousLineCount = editor.lineCount;
-
-    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;
-  }
-
-  getEditorId(): string {
-    return this.getCell().editor.uuid;
-  }
-
-  protected onGutterClick = (editor: Editor, lineNumber: number) => {
-    const info = editor.lineInfo(lineNumber);
-    if (!info) {
-      return;
-    }
-
-    const isRemoveGutter = !!info.gutterMarkers;
-    if (isRemoveGutter) {
-      this.breakpointService.removeBreakpoint(
-        this.debuggerSession.id,
-        this.getEditorId,
-        info as LineInfo
-      );
-    } else {
-      this.breakpointService.addBreakpoint(
-        this.debuggerSession.id,
-        this.getEditorId(),
-        info as LineInfo
-      );
-    }
-
-    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) {
-      var lines: LineInfo[] = [];
-      doc.eachLine(line => {
-        if ((line as LineInfo).gutterMarkers) {
-          lines.push(editor.lineInfo(line));
-        }
-      });
-      this.breakpointService.changeLines(lines);
-      this.previousLineCount = linesNumber;
-    }
-  };
-
-  private createMarkerNode() {
-    var marker = document.createElement('div');
-    marker.className = 'jp-breakpoint-marker';
-    marker.innerHTML = '●';
-    return marker;
+    note.activeCellChanged.connect(this.test, this);
   }
   }
 }
 }
 
 
@@ -177,16 +68,3 @@ export namespace DebuggerNotebookTracker {
     breakpointService: BreakpointsService;
     breakpointService: BreakpointsService;
   }
   }
 }
 }
-
-export interface LineInfo {
-  line: any;
-  handle: any;
-  text: string;
-  /** Object mapping gutter IDs to marker elements. */
-  gutterMarkers: any;
-  textClass: string;
-  bgClass: string;
-  wrapClass: string;
-  /** Array of line widgets attached to this line. */
-  widgets: any;
-}