浏览代码

add service instance

Borys Palka 5 年之前
父节点
当前提交
47d0bb33a0
共有 7 个文件被更改,包括 104 次插入55 次删除
  1. 11 1
      src/breakpoints/index.ts
  2. 31 4
      src/breakpointsService.ts
  3. 7 9
      src/debugger.ts
  4. 8 2
      src/index.ts
  5. 43 30
      src/notebookTracker/index.ts
  6. 1 8
      src/session.ts
  7. 3 1
      src/sidebar.ts

+ 11 - 1
src/breakpoints/index.ts

@@ -7,11 +7,18 @@ import { Widget, Panel, PanelLayout } from '@phosphor/widgets';
 import { DebugProtocol } from 'vscode-debugprotocol';
 import { Body } from './body';
 import { Signal, ISignal } from '@phosphor/signaling';
+import { BreakpointsService } from '../breakpointsService';
 
 export class Breakpoints extends Panel {
   constructor(options: Breakpoints.IOptions) {
     super();
 
+    this.service = options.service;
+
+    this.service.testSignal.connect((sender, update) => {
+      console.log('test');
+    });
+
     this.model = new Breakpoints.IModel([]);
     this.addClass('jp-DebuggerBreakpoints');
     this.title.label = 'Breakpoints';
@@ -52,6 +59,7 @@ export class Breakpoints extends Panel {
   private isAllActive = true;
   readonly body: Widget;
   readonly model: Breakpoints.IModel;
+  service: BreakpointsService;
 }
 class BreakpointsHeader extends Widget {
   constructor(title: string) {
@@ -126,5 +134,7 @@ export namespace Breakpoints {
   /**
    * Instantiation options for `Breakpoints`;
    */
-  export interface IOptions extends Panel.IOptions {}
+  export interface IOptions extends Panel.IOptions {
+    service?: BreakpointsService;
+  }
 }

+ 31 - 4
src/breakpointsService.ts

@@ -1,14 +1,41 @@
+import { Breakpoints } from './breakpoints';
+import { Signal } from '@phosphor/signaling';
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
 export class BreakpointsService {
   constructor() {}
 
-  addBreakpoint() {}
+  state: any = {};
 
-  removeBreakpoint() {}
+  testSignal = new Signal<this, any>(this);
 
-  getBreakpointState() {}
+  addBreakpoint(session_id: any, editor_id: any, lineInfo: any) {
+    console.log('adding');
+    this.testSignal.emit('test');
+  }
 
-  setBreakpointState() {}
+  removeBreakpoint(session_id: any, editor_id: any, lineInfo: any) {}
+
+  getBreakpointState(session_id: any, editor_id: any, lineInfo: any) {}
+
+  setBreakpointState(session_id: any, editor_id: any, lineInfo: any) {}
+
+  newLine(session_id: any, editor_id: any, lineInfo: any) {}
+
+  protected changeLines(
+    breakpoints: Breakpoints.IBreakpoint[],
+    lineInfo: any,
+    sign: number
+  ) {
+    breakpoints.map(ele => {
+      if (
+        ele.line > lineInfo.line ||
+        (lineInfo.text === '' && lineInfo.line === ele.line)
+      ) {
+        ele.line = ele.line + sign;
+      }
+      return ele;
+    });
+  }
 }

+ 7 - 9
src/debugger.ts

@@ -11,13 +11,11 @@ import { IDisposable } from '@phosphor/disposable';
 
 import { DebuggerSidebar } from './sidebar';
 import { INotebookTracker } from '@jupyterlab/notebook';
-import { CodeCell } from '@jupyterlab/cells';
-import { IEditorTracker } from '@jupyterlab/fileeditor';
+import { BreakpointsService } from './breakpointsService';
 
 export class Debugger extends BoxPanel {
   constructor(options: Debugger.IOptions) {
     super({ direction: 'left-to-right' });
-
     this.model = new Debugger.Model(options);
     this.sidebar = new DebuggerSidebar(this.model);
     this.title.label = 'Debugger';
@@ -30,8 +28,6 @@ export class Debugger extends BoxPanel {
 
   readonly sidebar: DebuggerSidebar;
 
-  previousCell: CodeCell;
-
   dispose(): void {
     if (this.isDisposed) {
       return;
@@ -47,16 +43,15 @@ export class Debugger extends BoxPanel {
 export namespace Debugger {
   export interface IOptions {
     connector?: IDataConnector<ReadonlyJSONValue>;
-    noteTracker?: INotebookTracker;
-    editorTracker?: IEditorTracker;
     id?: string;
+    breakpointsService?: BreakpointsService;
   }
 
   export class Model implements IDisposable {
     constructor(options: Debugger.Model.IOptions) {
       this.connector = options.connector || null;
+      this.breakpointsService = options.breakpointsService;
       this.id = options.id || UUID.uuid4();
-      this._notebook = options.noteTracker;
       void this._populate();
     }
 
@@ -86,9 +81,12 @@ export namespace Debugger {
 
     private _isDisposed = false;
     private _notebook: INotebookTracker;
+    breakpointsService: BreakpointsService;
   }
 
   export namespace Model {
-    export interface IOptions extends Debugger.IOptions {}
+    export interface IOptions extends Debugger.IOptions {
+      breakpointsService?: BreakpointsService;
+    }
   }
 }

+ 8 - 2
src/index.ts

@@ -25,6 +25,7 @@ import { Debugger } from './debugger';
 
 import { IDebugger, IDebuggerSidebar } from './tokens';
 import { DebuggerNotebookTracker } from './notebookTracker';
+import { BreakpointsService } from './breakpointsService';
 
 // import { ClientSession, IClientSession } from '@jupyterlab/apputils';
 
@@ -43,6 +44,9 @@ export namespace CommandIDs {
   export const debugNotebook = 'debugger:debug-notebook';
 }
 
+// Service for controll state of breakpoints in extensione
+const service = new BreakpointsService();
+
 /**
  * A plugin that provides visual debugging support for consoles.
  */
@@ -98,8 +102,10 @@ const notebooks: JupyterFrontEndPlugin<void> = {
     palette: ICommandPalette
   ) => {
     new DebuggerNotebookTracker({
-      notebookTracker: notebook
+      notebookTracker: notebook,
+      breakpointService: service
     });
+
     // console.log(debugetNoteTracker);
     // this exist only for my test in futre will be removed
     const command: string = CommandIDs.debugNotebook;
@@ -126,7 +132,7 @@ const sidebar: JupyterFrontEndPlugin<Debugger> = {
     const { shell } = app;
     const label = 'Environment';
     const namespace = 'jp-debugger-sidebar';
-    const sidebar = new Debugger({});
+    const sidebar = new Debugger({ breakpointsService: service });
     sidebar.id = namespace;
     sidebar.title.label = label;
     shell.add(sidebar, 'right', { activate: false });

+ 43 - 30
src/notebookTracker/index.ts

@@ -7,50 +7,54 @@ import { CodeMirrorEditor } from '@jupyterlab/codemirror';
 import { Editor, Doc } from 'codemirror';
 import { DebugSession } from './../session';
 import { Breakpoints } from '../breakpoints';
+import { IClientSession } from '@jupyterlab/apputils';
+import { BreakpointsService } from '../breakpointsService';
 
 export class DebuggerNotebookTracker {
   constructor(options: DebuggerNotebookTracker.IOptions) {
+    this.breakpointService = options.breakpointService;
     this.notebookTracker = options.notebookTracker;
     this.notebookTracker.widgetAdded.connect(
       (sender, notePanel: NotebookPanel) => {
-        console.log('first');
-        if (!this.debuggerSession) {
-          this.debuggerSession = new DebugSession({
-            client: notePanel.session
-          });
-        }
-        this.debuggerSession.client = notePanel.session;
+        this.newDebuggerSession(notePanel.session);
       }
     );
 
-    // this.notebookTracker.currentChanged.connect(
-    //   (sender, notePanel: NotebookPanel) => {
-
-    //   }
-    // );
-
-    this.notebookTracker.activeCellChanged.connect(
-      this.onActiveCellChanged,
-      this
+    this.notebookTracker.currentChanged.connect(
+      (sender, notePanel: NotebookPanel) => {
+        this.newDebuggerSession(notePanel.session);
+        this.notebookTracker.activeCellChanged.connect(
+          this.onActiveCellChanged,
+          this
+        );
+      }
     );
   }
+
   notebookTracker: INotebookTracker;
   previousCell: CodeCell;
   previousLineCount: number;
   debuggerSession: DebugSession;
+  breakpointService: BreakpointsService;
+
+  protected newDebuggerSession(client: IClientSession) {
+    if (this.debuggerSession) {
+      this.debuggerSession.dispose();
+    }
+    this.debuggerSession = new DebugSession({
+      client: client
+    });
+  }
 
   protected onActiveCellChanged() {
-    console.log('second');
     const activeCell = this.getCell();
     if (activeCell && activeCell.editor && this.debuggerSession) {
       if (this.previousCell && !this.previousCell.isDisposed) {
         this.removeListner(this.previousCell);
       }
       this.previousCell = activeCell;
-      // console.log(activeCell.editor.uuid);
       this.setEditor(activeCell);
     }
-    // console.log(this.debuggerSession);
   }
 
   protected setEditor(cell: CodeCell) {
@@ -83,14 +87,10 @@ export class DebuggerNotebookTracker {
     return this.notebookTracker.activeCell as CodeCell;
   }
 
-  protected getEditorId(): string {
+  get getEditorId(): string {
     return this.getCell().editor.uuid;
   }
 
-  protected getCurrentNoteBook(): NotebookPanel {
-    return this.notebookTracker.currentWidget;
-  }
-
   protected onGutterClick = (editor: Editor, lineNumber: number) => {
     const info = editor.lineInfo(lineNumber);
     if (!info) {
@@ -99,9 +99,12 @@ export class DebuggerNotebookTracker {
 
     const isRemoveGutter = !!info.gutterMarkers;
     if (isRemoveGutter) {
-      this.removeBreakpoint(lineNumber);
     } else {
-      this.addBreakpoint(lineNumber);
+      this.breakpointService.addBreakpoint(
+        this.debuggerSession.id,
+        this.getEditorId,
+        info as LineInfo
+      );
     }
 
     editor.setGutterMarker(
@@ -111,10 +114,6 @@ export class DebuggerNotebookTracker {
     );
   };
 
-  protected removeBreakpoint(lineNumber: number) {}
-
-  protected addBreakpoint(lineNumber: number) {}
-
   protected onNewRenderLine = (editor: Editor, line: any) => {
     const lineInfo = editor.lineInfo(line);
     if (lineInfo.handle && lineInfo.handle.order === false) {
@@ -163,5 +162,19 @@ export class DebuggerNotebookTracker {
 export namespace DebuggerNotebookTracker {
   export interface IOptions {
     notebookTracker: INotebookTracker;
+    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;
+}

+ 1 - 8
src/session.ts

@@ -21,21 +21,15 @@ export class DebugSession implements IDebugger.ISession {
    */
   constructor(options: DebugSession.IOptions) {
     this.client = options.client;
-    this.id = this.client.name;
   }
 
   /**
    * The client session to connect to a debugger.
    */
   private _client: IClientSession;
-  private _id: string;
 
   get id() {
-    return this._id;
-  }
-
-  set id(id: string) {
-    this._id = id;
+    return this.client.name;
   }
 
   get client(): IClientSession {
@@ -52,7 +46,6 @@ export class DebugSession implements IDebugger.ISession {
     }
 
     this._client = client;
-    this._id = client.name;
     this._client.iopubMessage.connect(this._handleEvent, this);
   }
 

+ 3 - 1
src/sidebar.ts

@@ -20,7 +20,9 @@ export class DebuggerSidebar extends SplitPanel {
 
     this.variables = new Variables();
     this.callstack = new Callstack();
-    this.breakpoints = new Breakpoints({});
+    this.breakpoints = new Breakpoints({
+      service: this.model.breakpointsService
+    });
 
     this.addWidget(this.variables);
     this.addWidget(this.callstack);