Procházet zdrojové kódy

Refactor of parameters for method

krzysztof.sikora před 4 roky
rodič
revize
96561f56c6
4 změnil soubory, kde provedl 139 přidání a 107 odebrání
  1. 65 30
      src/debugger-configuration.ts
  2. 52 60
      src/editor-finder.ts
  3. 10 5
      src/handlers/tracker.ts
  4. 12 12
      src/service.ts

+ 65 - 30
src/debugger-configuration.ts

@@ -8,7 +8,7 @@ import { Signal } from '@lumino/signaling';
 import { murmur2 } from 'murmurhash-js';
 
 /**
- * A class to hash code.
+ * A class that holds debugger configuration for a kernel.
  */
 export class DebuggerConfiguration implements IDebuggerConfig {
   /**
@@ -35,31 +35,26 @@ export class DebuggerConfiguration implements IDebuggerConfig {
    */
   public getCodeId(code: string, kernelName: string): string {
     return (
-      this._tmpFileAssociatedWithKernel.get(kernelName)[0] +
+      this._tmpFileAssociatedWithKernel.get(kernelName).tmpFilePrefix +
       this._hashMethod(code) +
-      this._tmpFileAssociatedWithKernel.get(kernelName)[1]
+      this._tmpFileAssociatedWithKernel.get(kernelName).tmpFileSuffix
     );
   }
 
   /**
    * Set the hash parameters for the current session.
    *
-   * @param method The hash method.
-   * @param seed The seed for the hash method.
-   * @param kernelName Kernel name for algorithm selection
+   * @param hashParams - Unified parameters for hash method
    */
-  public setHashParameters(
-    method: string,
-    seed: number,
-    kernelName: string
-  ): void {
+  public setHashParameters(hashParams: IHashParameters): void {
+    const { kernelName, hashMethod, hashSeed } = hashParams;
     if (kernelName === 'xpython') {
-      if (method === 'Murmur2') {
+      if (hashMethod === 'Murmur2') {
         this._hashMethod = (code: string): string => {
-          return murmur2(code, seed).toString();
+          return murmur2(code, hashSeed).toString();
         };
       } else {
-        throw new Error('hash method not supported ' + method);
+        throw new Error('hash method not supported ' + hashMethod);
       }
     } else {
       throw new Error('Kernel not supported ' + kernelName);
@@ -69,20 +64,64 @@ export class DebuggerConfiguration implements IDebuggerConfig {
   /**
    * Set the parameters used for the temporary files (e.g. cells).
    *
-   * @param prefix The prefix used for the temporary files.
-   * @param suffix The suffix used for the temporary files.
-   * @param kernelName The kernel name from current session.
+   * @param fileParams - Unified parameters for mapping
    */
-  public setTmpFileParameters(
-    prefix: string,
-    suffix: string,
-    kernelName: string
-  ): void {
-    this._tmpFileAssociatedWithKernel.set(kernelName, [prefix, suffix]);
+  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, [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>(
@@ -92,11 +131,7 @@ export const IDebuggerConfig = new Token<IDebuggerConfig>(
  * Interface for configuration plugin
  */
 export interface IDebuggerConfig {
-  setHashParameters(method: string, seed: number, kernelName: string): void;
-  setTmpFileParameters(
-    prefix: string,
-    suffix: string,
-    kernelName: string
-  ): void;
+  setHashParameters(hashParams: IHashParameters): void;
+  setTmpFileParameters(fileParams: ITmpFileParameters): void;
   getCodeId(code: string, kernelName: string): string;
 }

+ 52 - 60
src/editor-finder.ts

@@ -70,42 +70,28 @@ export class EditorFinder implements IDisposable, IDebuggerEditorFinder {
    * by iterating through all the widgets in each of the notebook,
    * console, file editor, and read-only file editor trackers.
    *
-   * @param debugSessionPath The path for the current debug session.
-   * @param source The source to find.
-   * @param focus - Set to true to focus on the relevant cell. Default to false.
-   * @param kernelName The kernel name of current session.
+   * @param findParams - Unified parameters for a source matching
    */
-  find(
-    debugSessionPath: string,
-    source: string,
-    focus: boolean,
-    kernelName: string
-  ): IIterator<CodeEditor.IEditor> {
+  find(findParams: IFindParameters): IIterator<CodeEditor.IEditor> {
     return chain(
-      this._findInNotebooks(debugSessionPath, source, focus, kernelName),
-      this._findInConsoles(debugSessionPath, source, focus, kernelName),
-      this._findInEditors(debugSessionPath, source, focus, kernelName),
-      this._findInReadOnlyEditors(debugSessionPath, source, focus, kernelName)
+      this._findInNotebooks(findParams),
+      this._findInConsoles(findParams),
+      this._findInEditors(findParams),
+      this._findInReadOnlyEditors(findParams)
     );
   }
 
   /**
    * Find the editor for a source matching the current debug session
    *
-   * @param debugSessionPath The path for the current debug session.
-   * @param source The source to find.
-   * @param focus - Set to true to focus on the relevant cell. Default to false.
-   * @param kernelName The kernel name from current session.
+   * @param findParams - Unified parameters for a source matching
    */
-  private _findInNotebooks(
-    debugSessionPath: string,
-    source: string,
-    focus: boolean,
-    kernelName: string
-  ): CodeEditor.IEditor[] {
+  private _findInNotebooks(findParams: IFindParameters): CodeEditor.IEditor[] {
     if (!this._notebookTracker) {
       return [];
     }
+    const { debugSessionPath, source, focus, kernelName } = findParams;
+
     const editors: CodeEditor.IEditor[] = [];
     this._notebookTracker.forEach(notebookPanel => {
       const sessionContext = notebookPanel.sessionContext;
@@ -142,20 +128,14 @@ export class EditorFinder implements IDisposable, IDebuggerEditorFinder {
   /**
    * Find the editor for a source matching the current debug session
    *
-   * @param debugSessionPath The path for the current debug session.
-   * @param source The source to find.
-   * @param focus - Set to true to focus on the relevant cell. Default to false.
-   * @param kernelName The kernel name from current session.
+   * @param findParams - Unified parameters for a source matching
    */
-  private _findInConsoles(
-    debugSessionPath: string,
-    source: string,
-    focus: boolean,
-    kernelName: string
-  ): CodeEditor.IEditor[] {
+  private _findInConsoles(findParams: IFindParameters): CodeEditor.IEditor[] {
     if (!this._consoleTracker) {
       return [];
     }
+    const { debugSessionPath, source, focus, kernelName } = findParams;
+
     const editors: CodeEditor.IEditor[] = [];
     this._consoleTracker.forEach(consoleWidget => {
       const sessionContext = consoleWidget.sessionContext;
@@ -184,20 +164,14 @@ export class EditorFinder implements IDisposable, IDebuggerEditorFinder {
    * Find the editor for a source matching the current debug session
    * from the editor tracker.
    *
-   * @param debugSessionPath The path for the current debug session.
-   * @param source The source to find.
-   * @param focus - Set to true to focus on the relevant cell. Default to false.
-   * @param kernelName The kernel name from current session.
+   * @param findParams - Unified parameters for a source matching
    */
-  private _findInEditors(
-    debugSessionPath: string,
-    source: string,
-    focus: boolean,
-    kernelName: string
-  ): CodeEditor.IEditor[] {
+  private _findInEditors(findParams: IFindParameters): CodeEditor.IEditor[] {
     if (!this._editorTracker) {
       return;
     }
+    const { debugSessionPath, source, focus, kernelName } = findParams;
+
     const editors: CodeEditor.IEditor[] = [];
     this._editorTracker.forEach(doc => {
       const fileEditor = doc.content;
@@ -226,17 +200,13 @@ export class EditorFinder implements IDisposable, IDebuggerEditorFinder {
   /**
    * Find an editor for a source from the read-only editor tracker.
    *
-   * @param debugSessionPath The path for the current debug session.
-   * @param source The source to find.
-   * @param focus Set to true to focus on the relevant cell. Default to false.
-   * @param kernelName The kernel name from current session.
+   * @param findParams - Unified parameters for a source matching
    */
   private _findInReadOnlyEditors(
-    debugSessionPath: string,
-    source: string,
-    focus: boolean,
-    kernelName: string
+    findParams: IFindParameters
   ): CodeEditor.IEditor[] {
+    const { source, focus, kernelName } = findParams;
+
     const editors: CodeEditor.IEditor[] = [];
     this._readOnlyEditorTracker.forEach(widget => {
       const editor = widget.content?.editor;
@@ -303,22 +273,44 @@ export namespace EditorFinder {
      */
     debuggerConfiguration: IDebuggerConfig;
   }
+}
+
+/**
+ * Unified parameters for find method
+ */
+interface IFindParameters {
   /**
-   * A token for a editor finder handler find method plugin
-   *
+   * Path of session connection.
    */
+  debugSessionPath: string;
+
+  /**
+   * Source path
+   */
+  source: string;
+
+  /**
+   * Extra flag prevent disable focus.
+   */
+  focus: boolean;
+
+  /**
+   * Name of current kernel.
+   */
+  kernelName: string;
 }
+
+/**
+ * A token for a editor finder handler find method plugin
+ *
+ */
 export const IDebuggerEditorFinder = new Token<IDebuggerEditorFinder>(
   '@jupyterlab/debugger:editor-finder'
 );
+
 /**
  * Interface for separated find method from editor finder plugin
  */
 export interface IDebuggerEditorFinder {
-  find(
-    debugSessionPath: string,
-    source: string,
-    focus: boolean,
-    kernelName: string
-  ): IIterator<CodeEditor.IEditor>;
+  find(findParams: IFindParameters): IIterator<CodeEditor.IEditor>;
 }

+ 10 - 5
src/handlers/tracker.ts

@@ -126,7 +126,12 @@ export class TrackerHandler implements IDisposable {
     const source = frame?.source.path ?? null;
     const kernelName = this._debuggerService.session.connection.kernel.name;
     each(
-      this._editorFinder.find(debugSessionPath, source, true, kernelName),
+      this._editorFinder.find({
+        debugSessionPath,
+        source,
+        focus: true,
+        kernelName
+      }),
       editor => {
         requestAnimationFrame(() => {
           EditorHandler.showCurrentLine(editor, frame.line);
@@ -151,12 +156,12 @@ export class TrackerHandler implements IDisposable {
     const debugSessionPath = this._debuggerService.session.connection.path;
     const { content, mimeType, path } = source;
     const kernelName = this._debuggerService.session.connection.kernel.name;
-    const results = this._editorFinder.find(
+    const results = this._editorFinder.find({
       debugSessionPath,
-      path,
-      false,
+      source: path,
+      focus: false,
       kernelName
-    );
+    });
     if (results.next()) {
       return;
     }

+ 12 - 12
src/service.ts

@@ -231,17 +231,16 @@ 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(
+    this._debuggerConfiguration.setHashParameters({
       hashMethod,
       hashSeed,
       kernelName
-    );
-    this._debuggerConfiguration.setTmpFileParameters(
+    });
+    this._debuggerConfiguration.setTmpFileParameters({
       tmpFilePrefix,
       tmpFileSuffix,
       kernelName
-    );
+    });
 
     this._model.stoppedThreads = stoppedThreads;
 
@@ -428,18 +427,19 @@ export class DebuggerService implements IDebugger, IDisposable {
   private _filterBreakpoints(
     breakpoints: Map<string, IDebugger.IBreakpoint[]>
   ): Map<string, IDebugger.IBreakpoint[]> {
-    const path = this._session.connection.path;
+    const debugSessionPath = this._session.connection.path;
+    const kernelName = this.session.connection.kernel.name;
     let bpMapForRestore = new Map<string, IDebugger.IBreakpoint[]>();
     for (let collection of breakpoints) {
       const [id, list] = collection;
       list.forEach(() => {
         each(
-          this._editorFinder.find(
-            path,
-            id,
-            false,
-            this.session.connection.kernel.name
-          ),
+          this._editorFinder.find({
+            debugSessionPath,
+            source: id,
+            focus: false,
+            kernelName
+          }),
           () => {
             if (list.length > 0) {
               bpMapForRestore.set(id, list);