Преглед изворни кода

Some clean up and refactoring

Afshin T. Darian пре 4 година
родитељ
комит
1681f752f2
6 измењених фајлова са 174 додато и 199 уклоњено
  1. 0 137
      src/debugger-configuration.ts
  2. 69 0
      src/debugger.ts
  3. 14 15
      src/editor-finder.ts
  4. 14 19
      src/index.ts
  5. 18 24
      src/service.ts
  6. 59 4
      src/tokens.ts

+ 0 - 137
src/debugger-configuration.ts

@@ -1,137 +0,0 @@
-// Copyright (c) Jupyter Development Team.
-// Distributed under the terms of the Modified BSD License.
-
-import { Token } from '@lumino/coreutils';
-
-import { Signal } from '@lumino/signaling';
-
-import { murmur2 } from 'murmurhash-js';
-
-/**
- * A class that holds debugger configuration for a kernel.
- */
-export class DebuggerConfiguration implements IDebuggerConfig {
-  /**
-   * Whether the handler is disposed.
-   */
-  isDisposed: boolean;
-
-  /**
-   * Dispose the objects.
-   */
-  dispose(): void {
-    if (this.isDisposed) {
-      return;
-    }
-    this.isDisposed = true;
-    Signal.clearData(this);
-  }
-
-  /**
-   * Computes an id based on the given code.
-   *
-   * @param code The source code.
-   * @param kernelName The kernel name from current session.
-   */
-  public getCodeId(code: string, kernelName: string): string {
-    return (
-      this._tmpFileAssociatedWithKernel.get(kernelName).tmpFilePrefix +
-      this._hashMethod(code) +
-      this._tmpFileAssociatedWithKernel.get(kernelName).tmpFileSuffix
-    );
-  }
-
-  /**
-   * Set the hash parameters for the current session.
-   *
-   * @param hashParams - Unified parameters for hash method
-   */
-  public setHashParameters(hashParams: IHashParameters): void {
-    const { kernelName, hashMethod, hashSeed } = hashParams;
-    if (kernelName === 'xpython') {
-      if (hashMethod === 'Murmur2') {
-        this._hashMethod = (code: string): string => {
-          return murmur2(code, hashSeed).toString();
-        };
-      } else {
-        throw new Error('hash method not supported ' + hashMethod);
-      }
-    } else {
-      throw new Error('Kernel not supported ' + kernelName);
-    }
-  }
-
-  /**
-   * Set the parameters used for the temporary files (e.g. cells).
-   *
-   * @param fileParams - Unified parameters for mapping
-   */
-  public setTmpFileParameters(fileParams: ITmpFileParameters): void {
-    const { kernelName, tmpFilePrefix, tmpFileSuffix } = fileParams;
-    this._tmpFileAssociatedWithKernel.set(kernelName, {
-      tmpFilePrefix,
-      tmpFileSuffix
-    });
-  }
-
-  private _hashMethod: (code: string) => string;
-  private _tmpFileAssociatedWithKernel = new Map<string, IFileParameters>();
-}
-
-/**
- * Interface with unified parameters of method for mapping temporary file.
- *
- */
-interface ITmpFileParameters extends IFileParameters {
-  /**
-   * Name of current kernel.
-   */
-  kernelName: string;
-}
-
-/**
- * Interface with prefix and suffix for map.
- */
-interface IFileParameters {
-  /**
-   * Prefix of temporary file.
-   */
-  tmpFilePrefix: string;
-
-  /**
-   * Suffix of temporary file.
-   */
-  tmpFileSuffix: string;
-}
-
-/**
- * Interface with unified parameters of hashing method.
- */
-interface IHashParameters {
-  /**
-   * Type of hash method.
-   */
-  hashMethod: string;
-
-  /**
-   * Hash seed
-   */
-  hashSeed: number;
-
-  /**
-   * Name of current kernel.
-   */
-  kernelName: string;
-}
-
-export const IDebuggerConfig = new Token<IDebuggerConfig>(
-  '@jupyterlab/debugger:configuration'
-);
-/**
- * Interface for configuration plugin
- */
-export interface IDebuggerConfig {
-  setHashParameters(hashParams: IHashParameters): void;
-  setTmpFileParameters(fileParams: ITmpFileParameters): void;
-  getCodeId(code: string, kernelName: string): string;
-}

+ 69 - 0
src/debugger.ts

@@ -5,8 +5,12 @@ import { IEditorServices } from '@jupyterlab/codeeditor';
 
 import { bugIcon } from '@jupyterlab/ui-components';
 
+import { Signal } from '@lumino/signaling';
+
 import { Panel, SplitPanel, Widget } from '@lumino/widgets';
 
+import { murmur2 } from 'murmurhash-js';
+
 import { Breakpoints } from './breakpoints';
 
 import { Callstack } from './callstack';
@@ -25,6 +29,71 @@ import { Variables } from './variables';
  * A namespace for `Debugger` statics.
  */
 export namespace Debugger {
+  /**
+   * A class that holds debugger configuration for a kernel.
+   */
+  export class Config implements IDebugger.IConfig {
+    /**
+     * Whether the handler is disposed.
+     */
+    isDisposed: boolean;
+
+    /**
+     * Dispose the objects.
+     */
+    dispose(): void {
+      if (this.isDisposed) {
+        return;
+      }
+      this.isDisposed = true;
+      Signal.clearData(this);
+    }
+
+    /**
+     * Computes an id based on the given code.
+     *
+     * @param code The source code.
+     * @param kernel The kernel name from current session.
+     */
+    getCodeId(code: string, kernel: string): string {
+      const { prefix, suffix } = this._fileParams.get(kernel);
+      return `${prefix}${this._hashMethod(code)}${suffix}`;
+    }
+
+    /**
+     * Set the hash parameters for a kernel.
+     *
+     * @param params - Hashing parameters for a kernel.
+     */
+    public setHashParams(params: IDebugger.IConfig.HashParams): void {
+      const { kernel, method, seed } = params;
+      if (kernel === 'xpython') {
+        if (method === 'Murmur2') {
+          this._hashMethod = (code: string): string => {
+            return murmur2(code, seed).toString();
+          };
+        } else {
+          throw new Error('hash method not supported ' + method);
+        }
+      } else {
+        throw new Error('Kernel not supported ' + kernel);
+      }
+    }
+
+    /**
+     * Set the parameters used for the temporary files (e.g. cells).
+     *
+     * @param params - Temporary file prefix and suffix for a kernel.
+     */
+    public setTmpFileParams(params: IDebugger.IConfig.FileParams): void {
+      const { kernel, prefix, suffix } = params;
+      this._fileParams.set(kernel, { kernel, prefix, suffix });
+    }
+
+    private _fileParams = new Map<string, IDebugger.IConfig.FileParams>();
+    private _hashMethod: (code: string) => string;
+  }
+
   /**
    * A debugger sidebar.
    */

+ 14 - 15
src/editor-finder.ts

@@ -25,7 +25,7 @@ import { IDisposable } from '@lumino/disposable';
 
 import { Signal } from '@lumino/signaling';
 
-import { IDebuggerConfig } from './debugger-configuration';
+import { IDebugger } from './tokens';
 
 /**
  * A class to find instances of code editors across notebook, console and files widgets
@@ -37,16 +37,14 @@ export class EditorFinder implements IDisposable, IDebuggerEditorFinder {
    * @param options The instantiation options for a EditorFinder.
    */
   constructor(options: EditorFinder.IOptions) {
+    this._config = options.config;
     this._shell = options.shell;
     this._notebookTracker = options.notebookTracker;
     this._consoleTracker = options.consoleTracker;
     this._editorTracker = options.editorTracker;
-    this._debuggerConfiguration = options.debuggerConfiguration;
     this._readOnlyEditorTracker = new WidgetTracker<
       MainAreaWidget<CodeEditorWrapper>
-    >({
-      namespace: '@jupyterlab/debugger'
-    });
+    >({ namespace: '@jupyterlab/debugger' });
   }
 
   /**
@@ -109,7 +107,7 @@ export class EditorFinder implements IDisposable, IDebuggerEditorFinder {
       cells.forEach((cell, i) => {
         // check the event is for the correct cell
         const code = cell.model.value.text;
-        const cellId = this._debuggerConfiguration.getCodeId(code, kernelName);
+        const cellId = this._config.getCodeId(code, kernelName);
         if (source !== cellId) {
           return;
         }
@@ -147,7 +145,7 @@ export class EditorFinder implements IDisposable, IDebuggerEditorFinder {
       const cells = consoleWidget.console.cells;
       each(cells, cell => {
         const code = cell.model.value.text;
-        const codeId = this._debuggerConfiguration.getCodeId(code, kernelName);
+        const codeId = this._config.getCodeId(code, kernelName);
         if (source !== codeId) {
           return;
         }
@@ -185,7 +183,7 @@ export class EditorFinder implements IDisposable, IDebuggerEditorFinder {
       }
 
       const code = editor.model.value.text;
-      const codeId = this._debuggerConfiguration.getCodeId(code, kernelName);
+      const codeId = this._config.getCodeId(code, kernelName);
       if (source !== codeId) {
         return;
       }
@@ -215,7 +213,7 @@ export class EditorFinder implements IDisposable, IDebuggerEditorFinder {
       }
 
       const code = editor.model.value.text;
-      const codeId = this._debuggerConfiguration.getCodeId(code, kernelName);
+      const codeId = this._config.getCodeId(code, kernelName);
       if (widget.title.caption !== source && source !== codeId) {
         return;
       }
@@ -230,10 +228,11 @@ export class EditorFinder implements IDisposable, IDebuggerEditorFinder {
   private _readOnlyEditorTracker: WidgetTracker<
     MainAreaWidget<CodeEditorWrapper>
   >;
+
+  private _config: IDebugger.IConfig;
   private _notebookTracker: INotebookTracker | null;
   private _consoleTracker: IConsoleTracker | null;
   private _editorTracker: IEditorTracker | null;
-  private _debuggerConfiguration: IDebuggerConfig;
 }
 /**
  * A namespace for editor finder statics.
@@ -243,6 +242,11 @@ export namespace EditorFinder {
    * The options used to initialize a EditorFinder object.
    */
   export interface IOptions {
+    /**
+     * The instance of configuration with hash method.
+     */
+    config: IDebugger.IConfig;
+
     /**
      * An optional editor finder for consoles.
      */
@@ -267,11 +271,6 @@ export namespace EditorFinder {
      * The application shell.
      */
     shell: JupyterFrontEnd.IShell;
-
-    /**
-     * The instance of configuration with hash method.
-     */
-    debuggerConfiguration: IDebuggerConfig;
   }
 }
 

+ 14 - 19
src/index.ts

@@ -19,8 +19,6 @@ import { IEditorServices } from '@jupyterlab/codeeditor';
 
 import { ConsolePanel, IConsoleTracker } from '@jupyterlab/console';
 
-import { ISettingRegistry } from '@jupyterlab/settingregistry';
-
 import { DocumentWidget } from '@jupyterlab/docregistry';
 
 import { FileEditor, IEditorTracker } from '@jupyterlab/fileeditor';
@@ -29,6 +27,8 @@ import { INotebookTracker, NotebookPanel } from '@jupyterlab/notebook';
 
 import { Session } from '@jupyterlab/services';
 
+import { ISettingRegistry } from '@jupyterlab/settingregistry';
+
 import { EditorFinder, IDebuggerEditorFinder } from './editor-finder';
 
 import {
@@ -48,17 +48,12 @@ import { DebuggerService } from './service';
 
 import { DebuggerHandler } from './handler';
 
-import { IDebugger } from './tokens';
+import { IDebugger, IDebuggerConfig } from './tokens';
 
 import { DebuggerModel } from './model';
 
 import { VariablesBodyGrid } from './variables/grid';
 
-import {
-  IDebuggerConfig,
-  DebuggerConfiguration
-} from './debugger-configuration';
-
 /**
  * The command IDs used by the debugger plugin.
  */
@@ -277,27 +272,27 @@ const service: JupyterFrontEndPlugin<IDebugger> = {
   id: '@jupyterlab/debugger:service',
   autoStart: true,
   provides: IDebugger,
-  requires: [IDebuggerEditorFinder, IDebuggerConfig],
+  requires: [IDebuggerConfig, IDebuggerEditorFinder],
   activate: (
     app: JupyterFrontEnd,
-    editorFinder: IDebuggerEditorFinder,
-    debuggerConfiguration: IDebuggerConfig
+    config: IDebugger.IConfig,
+    editorFinder: IDebuggerEditorFinder
   ) =>
     new DebuggerService({
-      specsManager: app.serviceManager.kernelspecs,
+      config,
       editorFinder,
-      debuggerConfiguration
+      specsManager: app.serviceManager.kernelspecs
     })
 };
 
 /**
  * A plugin that provides a configuration with hash method.
  */
-const configuration: JupyterFrontEndPlugin<IDebuggerConfig> = {
-  id: '@jupyterlab/debugger:configuration',
+const configuration: JupyterFrontEndPlugin<IDebugger.IConfig> = {
+  id: '@jupyterlab/debugger:config',
   provides: IDebuggerConfig,
   autoStart: true,
-  activate: (): IDebuggerConfig => new DebuggerConfiguration()
+  activate: () => new Debugger.Config()
 };
 
 /**
@@ -307,20 +302,20 @@ const finder: JupyterFrontEndPlugin<IDebuggerEditorFinder> = {
   id: '@jupyterlab/debugger:editor-finder',
   autoStart: true,
   provides: IDebuggerEditorFinder,
-  requires: [IEditorServices, IDebuggerConfig],
+  requires: [IDebuggerConfig, IEditorServices],
   optional: [INotebookTracker, IConsoleTracker, IEditorTracker],
   activate: (
     app: JupyterFrontEnd,
+    config: IDebugger.IConfig,
     editorServices: IEditorServices,
-    debuggerConfiguration: IDebuggerConfig,
     notebookTracker: INotebookTracker | null,
     consoleTracker: IConsoleTracker | null,
     editorTracker: IEditorTracker | null
   ): IDebuggerEditorFinder => {
     return new EditorFinder({
+      config,
       shell: app.shell,
       editorServices,
-      debuggerConfiguration,
       notebookTracker,
       consoleTracker,
       editorTracker

+ 18 - 24
src/service.ts

@@ -13,16 +13,14 @@ import { DebugProtocol } from 'vscode-debugprotocol';
 
 import { CallstackModel } from './callstack/model';
 
+import { IDebuggerEditorFinder } from './editor-finder';
+
 import { DebuggerModel } from './model';
 
 import { IDebugger } from './tokens';
 
-import { IDebuggerEditorFinder } from './editor-finder';
-
 import { VariablesModel } from './variables/model';
 
-import { IDebuggerConfig } from './debugger-configuration';
-
 /**
  * A concrete implementation of IDebugger.
  */
@@ -33,6 +31,7 @@ export class DebuggerService implements IDebugger, IDisposable {
    * @param options The instantiation options for a DebuggerService.
    */
   constructor(options: DebuggerService.IOptions) {
+    this._config = options.config;
     // Avoids setting session with invalid client
     // session should be set only when a notebook or
     // a console get the focus.
@@ -42,7 +41,6 @@ export class DebuggerService implements IDebugger, IDisposable {
     this._specsManager = options.specsManager;
     this._model = new DebuggerModel();
     this._editorFinder = options.editorFinder;
-    this._debuggerConfiguration = options.debuggerConfiguration;
   }
 
   /**
@@ -166,10 +164,7 @@ export class DebuggerService implements IDebugger, IDisposable {
    * @param code The source code.
    */
   getCodeId(code: string): string {
-    return this._debuggerConfiguration.getCodeId(
-      code,
-      this.session.connection.kernel.name
-    );
+    return this._config.getCodeId(code, this.session.connection.kernel.name);
   }
 
   /**
@@ -231,15 +226,15 @@ export class DebuggerService implements IDebugger, IDisposable {
     const breakpoints = this._mapBreakpoints(reply.body.breakpoints);
     const stoppedThreads = new Set(reply.body.stoppedThreads);
     const kernelName = this.session.connection.kernel.name;
-    this._debuggerConfiguration.setHashParameters({
-      hashMethod,
-      hashSeed,
-      kernelName
+    this._config.setHashParams({
+      kernel: kernelName,
+      method: hashMethod,
+      seed: hashSeed
     });
-    this._debuggerConfiguration.setTmpFileParameters({
-      tmpFilePrefix,
-      tmpFileSuffix,
-      kernelName
+    this._config.setTmpFileParams({
+      kernel: kernelName,
+      prefix: tmpFilePrefix,
+      suffix: tmpFileSuffix
     });
 
     this._model.stoppedThreads = stoppedThreads;
@@ -664,16 +659,15 @@ export class DebuggerService implements IDebugger, IDisposable {
     return 1;
   }
 
+  private _config: IDebugger.IConfig;
+  private _editorFinder: IDebuggerEditorFinder | null;
+  private _eventMessage = new Signal<IDebugger, IDebugger.ISession.Event>(this);
   private _isDisposed = false;
-  private _session: IDebugger.ISession;
   private _model: DebuggerModel;
-  private _sessionChanged = new Signal<IDebugger, IDebugger.ISession>(this);
   private _modelChanged = new Signal<IDebugger, IDebugger.IModel>(this);
-  private _eventMessage = new Signal<IDebugger, IDebugger.ISession.Event>(this);
-
+  private _session: IDebugger.ISession;
+  private _sessionChanged = new Signal<IDebugger, IDebugger.ISession>(this);
   private _specsManager: KernelSpec.IManager;
-  private _editorFinder: IDebuggerEditorFinder | null;
-  private _debuggerConfiguration: IDebuggerConfig;
 }
 
 /**
@@ -697,6 +691,6 @@ export namespace DebuggerService {
     /**
      * The configuration instance with hash method.
      */
-    debuggerConfiguration: IDebuggerConfig;
+    config: IDebugger.IConfig;
   }
 }

+ 59 - 4
src/tokens.ts

@@ -64,10 +64,8 @@ export interface IDebugger {
 
   /**
    * Starts a debugger.
-   * Precondition: !isStarted
-   */
-  start(): Promise<void>;
-
+   * Precondition: !  start(): Promise<void>;
+   
   /**
    * Stops the debugger.
    * Precondition: isStarted
@@ -199,6 +197,56 @@ export namespace IDebugger {
   export interface IBreakpoint extends DebugProtocol.Breakpoint {
     active: boolean;
   }
+  /**
+   * Interface for debugger file and hashing configuration.
+   */
+  export interface IConfig {
+    getCodeId(code: string, kernelName: string): string;
+    setHashParams(params: IConfig.HashParams): void;
+    setTmpFileParams(params: IConfig.FileParams): void;
+  }
+
+  export namespace IConfig {
+    /**
+     * Temporary file prefix and suffix for a kernel.
+     */
+    export type FileParams = {
+      /**
+       * The kernel name.
+       */
+      kernel: string;
+
+      /**
+       * Prefix added to temporary files created by the kernel per cell.
+       */
+      prefix: string;
+
+      /**
+       * Suffix added temporary files created by the kernel per cell.
+       */
+      suffix: string;
+    };
+
+    /**
+     * Hashing parameters for a kernel.
+     */
+    export type HashParams = {
+      /**
+       * The kernel name.
+       */
+      kernel: string;
+
+      /**
+       * The hash method.
+       */
+      method: string;
+
+      /**
+       * The hash seed.
+       */
+      seed: number;
+    };
+  }
 
   /**
    * The interface for a source file.
@@ -376,3 +424,10 @@ export namespace IDebugger {
  * A token for a tracker for an application's visual debugger instances.
  */
 export const IDebugger = new Token<IDebugger>('@jupyterlab/debugger');
+
+/**
+ * The debugger configuration token.
+ */
+export const IDebuggerConfig = new Token<IDebugger.IConfig>(
+  '@jupyterlab/debugger:config'
+);