Ver código fonte

Merge pull request #199 from jtpio/run-button

Initial support for handling messages for other cells
Johan Mabille 5 anos atrás
pai
commit
f81e4d2710
3 arquivos alterados com 76 adições e 24 exclusões
  1. 11 0
      examples/index.ipynb
  2. 30 24
      src/handlers/cell.ts
  3. 35 0
      src/handlers/notebook.ts

+ 11 - 0
examples/index.ipynb

@@ -13,6 +13,17 @@
     "result = add(1, 2)\n",
     "print(result)"
    ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "i = 0\n",
+    "i += 1\n",
+    "print(i)"
+   ]
   }
  ],
  "metadata": {

+ 30 - 24
src/handlers/cell.ts

@@ -1,7 +1,7 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
-import { CodeCell, ICellModel } from '@jupyterlab/cells';
+import { Cell, CodeCell, ICellModel } from '@jupyterlab/cells';
 
 import { CodeMirrorEditor } from '@jupyterlab/codemirror';
 
@@ -15,6 +15,8 @@ import { Editor } from 'codemirror';
 
 import { Breakpoints, SessionTypes } from '../breakpoints';
 
+import { Callstack } from '../callstack';
+
 import { Debugger } from '../debugger';
 
 import { IDebugger } from '../tokens';
@@ -43,11 +45,10 @@ export class CellManager implements IDisposable {
 
     this._debuggerModel.callstackModel.currentFrameChanged.connect(
       (_, frame) => {
-        this.cleanupHighlight();
+        CellManager.cleanupHighlight(this.activeCell);
         if (!frame) {
           return;
         }
-        this.showCurrentLine(frame.line);
       }
     );
 
@@ -74,26 +75,6 @@ export class CellManager implements IDisposable {
     }
   }
 
-  private showCurrentLine(lineNumber: number) {
-    if (!this.activeCell) {
-      return;
-    }
-    const editor = this.activeCell.editor as CodeMirrorEditor;
-    this.cleanupHighlight();
-    editor.editor.addLineClass(lineNumber - 1, 'wrap', LINE_HIGHLIGHT_CLASS);
-  }
-
-  // TODO: call when the debugger stops
-  private cleanupHighlight() {
-    if (!this.activeCell || this.activeCell.isDisposed) {
-      return;
-    }
-    const editor = this.activeCell.editor as CodeMirrorEditor;
-    editor.doc.eachLine(line => {
-      editor.editor.removeLineClass(line, 'wrap', LINE_HIGHLIGHT_CLASS);
-    });
-  }
-
   dispose(): void {
     if (this.isDisposed) {
       return;
@@ -105,7 +86,7 @@ export class CellManager implements IDisposable {
       this._cellMonitor.dispose();
     }
     this.removeListener(this.activeCell);
-    this.cleanupHighlight();
+    CellManager.cleanupHighlight(this.activeCell);
     Signal.clearData(this);
   }
 
@@ -291,6 +272,31 @@ export namespace CellManager {
     activeCell?: CodeCell;
     type: SessionTypes;
   }
+
+  /**
+   * Highlight the current line of the frame in the given cell.
+   * @param cell The cell to highlight.
+   * @param frame The frame with the current line number.
+   */
+  export function showCurrentLine(cell: Cell, frame: Callstack.IFrame) {
+    const editor = cell.editor as CodeMirrorEditor;
+    cleanupHighlight(cell);
+    editor.editor.addLineClass(frame.line - 1, 'wrap', LINE_HIGHLIGHT_CLASS);
+  }
+
+  /**
+   * Remove all line highlighting indicators for the given cell.
+   * @param cell The cell to cleanup.
+   */
+  export function cleanupHighlight(cell: Cell) {
+    if (!cell || cell.isDisposed) {
+      return;
+    }
+    const editor = cell.editor as CodeMirrorEditor;
+    editor.doc.eachLine(line => {
+      editor.editor.removeLineClass(line, 'wrap', LINE_HIGHLIGHT_CLASS);
+    });
+  }
 }
 
 export interface ILineInfo {

+ 35 - 0
src/handlers/notebook.ts

@@ -15,6 +15,8 @@ import { Debugger } from '../debugger';
 
 import { IDebugger } from '../tokens';
 
+import { Callstack } from '../callstack';
+
 import { CellManager } from './cell';
 
 export class NotebookHandler implements IDisposable {
@@ -37,6 +39,11 @@ export class NotebookHandler implements IDisposable {
       this.onActiveCellChanged,
       this
     );
+
+    this.debuggerModel.callstackModel.currentFrameChanged.connect(
+      this.onCurrentFrameChanged,
+      this
+    );
   }
 
   isDisposed: boolean;
@@ -63,6 +70,34 @@ export class NotebookHandler implements IDisposable {
     });
   }
 
+  private onCurrentFrameChanged(
+    callstackModel: Callstack.Model,
+    frame: Callstack.IFrame
+  ) {
+    const notebook = this.notebookTracker.currentWidget;
+    if (!notebook) {
+      return;
+    }
+
+    const cells = notebook.content.widgets;
+    cells.forEach(cell => CellManager.cleanupHighlight(cell));
+
+    if (!frame) {
+      return;
+    }
+
+    cells.forEach((cell, i) => {
+      // check the event is for the correct cell
+      const code = cell.model.value.text;
+      const cellId = this.debuggerService.getCellId(code);
+      if (frame.source.path !== cellId) {
+        return;
+      }
+      notebook.content.activeCellIndex = i;
+      CellManager.showCurrentLine(cell, frame);
+    });
+  }
+
   private notebookTracker: INotebookTracker;
   private debuggerModel: Debugger.Model;
   private debuggerService: IDebugger;