Browse Source

Refactor editor finder

Afshin T. Darian 4 years ago
parent
commit
dcbad2672a
5 changed files with 112 additions and 95 deletions
  1. 34 70
      src/editor-finder.ts
  2. 9 9
      src/handlers/tracker.ts
  3. 8 8
      src/index.ts
  4. 6 8
      src/service.ts
  5. 55 0
      src/tokens.ts

+ 34 - 70
src/editor-finder.ts

@@ -19,8 +19,6 @@ import { INotebookTracker } from '@jupyterlab/notebook';
 
 import { chain, each, IIterator } from '@lumino/algorithm';
 
-import { Token } from '@lumino/coreutils';
-
 import { IDisposable } from '@lumino/disposable';
 
 import { Signal } from '@lumino/signaling';
@@ -30,7 +28,7 @@ import { IDebugger } from './tokens';
 /**
  * A class to find instances of code editors across notebook, console and files widgets
  */
-export class EditorFinder implements IDisposable, IDebuggerEditorFinder {
+export class EditorFinder implements IDisposable, IDebugger.IEditorFinder {
   /**
    * Instantiate a new EditorFinder.
    *
@@ -64,31 +62,33 @@ export class EditorFinder implements IDisposable, IDebuggerEditorFinder {
   }
 
   /**
-   * Find the editor for a source matching the current debug session
-   * by iterating through all the widgets in each of the notebook,
-   * console, file editor, and read-only file editor trackers.
+   * Returns an iterator of editors for a source matching the current debug
+   * session by iterating through all the widgets in each of the supported
+   * debugger types (i.e., consoles, files, notebooks).
    *
-   * @param findParams - Unified parameters for a source matching
+   * @param params - The editor search parameters.
    */
-  find(findParams: IFindParameters): IIterator<CodeEditor.IEditor> {
+  find(params: IDebugger.IEditorFinder.Params): IIterator<CodeEditor.IEditor> {
     return chain(
-      this._findInNotebooks(findParams),
-      this._findInConsoles(findParams),
-      this._findInEditors(findParams),
-      this._findInReadOnlyEditors(findParams)
+      this._findInConsoles(params),
+      this._findInEditors(params),
+      this._findInNotebooks(params),
+      this._findInReadOnlyEditors(params)
     );
   }
 
   /**
-   * Find the editor for a source matching the current debug session
+   * Find relevant editors matching the search params in the notebook tracker.
    *
-   * @param findParams - Unified parameters for a source matching
+   * @param params - The editor search parameters.
    */
-  private _findInNotebooks(findParams: IFindParameters): CodeEditor.IEditor[] {
+  private _findInNotebooks(
+    params: IDebugger.IEditorFinder.Params
+  ): CodeEditor.IEditor[] {
     if (!this._notebookTracker) {
       return [];
     }
-    const { focus, kernel, path, source } = findParams;
+    const { focus, kernel, path, source } = params;
 
     const editors: CodeEditor.IEditor[] = [];
     this._notebookTracker.forEach(notebookPanel => {
@@ -113,7 +113,8 @@ export class EditorFinder implements IDisposable, IDebuggerEditorFinder {
         }
         if (focus) {
           notebook.activeCellIndex = i;
-          const rect = notebook.activeCell.inputArea.node.getBoundingClientRect();
+          const { node } = notebook.activeCell.inputArea;
+          const rect = node.getBoundingClientRect();
           notebook.scrollToPosition(rect.bottom, 45);
           this._shell.activateById(notebookPanel.id);
         }
@@ -124,15 +125,17 @@ export class EditorFinder implements IDisposable, IDebuggerEditorFinder {
   }
 
   /**
-   * Find the editor for a source matching the current debug session
+   * Find relevant editors matching the search params in the console tracker.
    *
-   * @param findParams - Unified parameters for a source matching
+   * @param params - The editor search parameters.
    */
-  private _findInConsoles(findParams: IFindParameters): CodeEditor.IEditor[] {
+  private _findInConsoles(
+    params: IDebugger.IEditorFinder.Params
+  ): CodeEditor.IEditor[] {
     if (!this._consoleTracker) {
       return [];
     }
-    const { focus, kernel, path, source } = findParams;
+    const { focus, kernel, path, source } = params;
 
     const editors: CodeEditor.IEditor[] = [];
     this._consoleTracker.forEach(consoleWidget => {
@@ -159,16 +162,17 @@ export class EditorFinder implements IDisposable, IDebuggerEditorFinder {
   }
 
   /**
-   * Find the editor for a source matching the current debug session
-   * from the editor tracker.
+   * Find relevant editors matching the search params in the editor tracker.
    *
-   * @param findParams - Unified parameters for a source matching
+   * @param params - The editor search parameters.
    */
-  private _findInEditors(findParams: IFindParameters): CodeEditor.IEditor[] {
+  private _findInEditors(
+    params: IDebugger.IEditorFinder.Params
+  ): CodeEditor.IEditor[] {
     if (!this._editorTracker) {
       return;
     }
-    const { focus, kernel, path, source } = findParams;
+    const { focus, kernel, path, source } = params;
 
     const editors: CodeEditor.IEditor[] = [];
     this._editorTracker.forEach(doc => {
@@ -196,14 +200,14 @@ export class EditorFinder implements IDisposable, IDebuggerEditorFinder {
   }
 
   /**
-   * Find an editor for a source from the read-only editor tracker.
+   * Find relevant editors matching the search params in the read-only tracker.
    *
-   * @param findParams - Unified parameters for a source matching
+   * @param params - The editor search parameters.
    */
   private _findInReadOnlyEditors(
-    findParams: IFindParameters
+    params: IDebugger.IEditorFinder.Params
   ): CodeEditor.IEditor[] {
-    const { focus, kernel, source } = findParams;
+    const { focus, kernel, source } = params;
 
     const editors: CodeEditor.IEditor[] = [];
     this._readOnlyEditorTracker.forEach(widget => {
@@ -273,43 +277,3 @@ export namespace EditorFinder {
     shell: JupyterFrontEnd.IShell;
   }
 }
-
-/**
- * Unified parameters for find method
- */
-interface IFindParameters {
-  /**
-   * Extra flag prevent disable focus.
-   */
-  focus: boolean;
-
-  /**
-   * Name of current kernel.
-   */
-  kernel: string;
-
-  /**
-   * Path of session connection.
-   */
-  path: string;
-
-  /**
-   * Source path
-   */
-  source: 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(findParams: IFindParameters): IIterator<CodeEditor.IEditor>;
-}

+ 9 - 9
src/handlers/tracker.ts

@@ -26,8 +26,6 @@ import { IDisposable } from '@lumino/disposable';
 
 import { Signal } from '@lumino/signaling';
 
-import { IDebuggerEditorFinder } from '../editor-finder';
-
 import { EditorHandler } from './editor';
 
 import { CallstackModel } from '../callstack/model';
@@ -188,14 +186,15 @@ export class TrackerHandler implements IDisposable {
       EditorHandler.showCurrentLine(editor, frame.line);
     }
   }
-  private _debuggerService: IDebugger;
+
   private _debuggerModel: DebuggerModel;
-  private _shell: JupyterFrontEnd.IShell;
+  private _debuggerService: IDebugger;
+  private _editorFinder: IDebugger.IEditorFinder | null;
   private _readOnlyEditorFactory: ReadOnlyEditorFactory;
   private _readOnlyEditorTracker: WidgetTracker<
     MainAreaWidget<CodeEditorWrapper>
   >;
-  private _editorFinder: IDebuggerEditorFinder | null;
+  private _shell: JupyterFrontEnd.IShell;
 }
 
 /**
@@ -211,6 +210,11 @@ export namespace TrackerHandler {
      */
     debuggerService: IDebugger;
 
+    /**
+     * The editor finder.
+     */
+    editorFinder: IDebugger.IEditorFinder;
+
     /**
      * The editor services.
      */
@@ -220,10 +224,6 @@ export namespace TrackerHandler {
      * The application shell.
      */
     shell: JupyterFrontEnd.IShell;
-    /**
-     * The editor finder.
-     */
-    editorFinder: IDebuggerEditorFinder;
   }
 
   // TODO: move the interface and token below to token.ts?

+ 8 - 8
src/index.ts

@@ -29,7 +29,7 @@ import { Session } from '@jupyterlab/services';
 
 import { ISettingRegistry } from '@jupyterlab/settingregistry';
 
-import { EditorFinder, IDebuggerEditorFinder } from './editor-finder';
+import { EditorFinder } from './editor-finder';
 
 import {
   continueIcon,
@@ -48,10 +48,10 @@ import { DebuggerService } from './service';
 
 import { DebuggerHandler } from './handler';
 
-import { IDebugger, IDebuggerConfig } from './tokens';
-
 import { DebuggerModel } from './model';
 
+import { IDebugger, IDebuggerConfig, IDebuggerEditorFinder } from './tokens';
+
 import { VariablesBodyGrid } from './variables/grid';
 
 /**
@@ -82,7 +82,7 @@ const consoles: JupyterFrontEndPlugin<void> = {
   activate: (
     app: JupyterFrontEnd,
     debug: IDebugger,
-    editorFinder: IDebuggerEditorFinder,
+    editorFinder: IDebugger.IEditorFinder,
     consoleTracker: IConsoleTracker,
     labShell: ILabShell
   ) => {
@@ -254,7 +254,7 @@ const tracker: JupyterFrontEndPlugin<void> = {
     app: JupyterFrontEnd,
     debug: IDebugger,
     editorServices: IEditorServices,
-    editorFinder: IDebuggerEditorFinder
+    editorFinder: IDebugger.IEditorFinder
   ) => {
     new TrackerHandler({
       shell: app.shell,
@@ -276,7 +276,7 @@ const service: JupyterFrontEndPlugin<IDebugger> = {
   activate: (
     app: JupyterFrontEnd,
     config: IDebugger.IConfig,
-    editorFinder: IDebuggerEditorFinder
+    editorFinder: IDebugger.IEditorFinder
   ) =>
     new DebuggerService({
       config,
@@ -298,7 +298,7 @@ const configuration: JupyterFrontEndPlugin<IDebugger.IConfig> = {
 /**
  * A plugin that tracks editors, console and file editors used for debugging.
  */
-const finder: JupyterFrontEndPlugin<IDebuggerEditorFinder> = {
+const finder: JupyterFrontEndPlugin<IDebugger.IEditorFinder> = {
   id: '@jupyterlab/debugger:editor-finder',
   autoStart: true,
   provides: IDebuggerEditorFinder,
@@ -311,7 +311,7 @@ const finder: JupyterFrontEndPlugin<IDebuggerEditorFinder> = {
     notebookTracker: INotebookTracker | null,
     consoleTracker: IConsoleTracker | null,
     editorTracker: IEditorTracker | null
-  ): IDebuggerEditorFinder => {
+  ): IDebugger.IEditorFinder => {
     return new EditorFinder({
       config,
       shell: app.shell,

+ 6 - 8
src/service.ts

@@ -13,8 +13,6 @@ import { DebugProtocol } from 'vscode-debugprotocol';
 
 import { CallstackModel } from './callstack/model';
 
-import { IDebuggerEditorFinder } from './editor-finder';
-
 import { DebuggerModel } from './model';
 
 import { IDebugger } from './tokens';
@@ -658,7 +656,7 @@ export class DebuggerService implements IDebugger, IDisposable {
   }
 
   private _config: IDebugger.IConfig;
-  private _editorFinder: IDebuggerEditorFinder | null;
+  private _editorFinder: IDebugger.IEditorFinder | null;
   private _eventMessage = new Signal<IDebugger, IDebugger.ISession.Event>(this);
   private _isDisposed = false;
   private _model: DebuggerModel;
@@ -677,18 +675,18 @@ export namespace DebuggerService {
    */
   export interface IOptions {
     /**
-     * The kernel specs manager.
+     * The configuration instance with hash method.
      */
-    specsManager: KernelSpec.IManager;
+    config: IDebugger.IConfig;
 
     /**
      * The editor finder instance.
      */
-    editorFinder?: IDebuggerEditorFinder;
+    editorFinder?: IDebugger.IEditorFinder;
 
     /**
-     * The configuration instance with hash method.
+     * The kernel specs manager.
      */
-    config: IDebugger.IConfig;
+    specsManager: KernelSpec.IManager;
   }
 }

+ 55 - 0
src/tokens.ts

@@ -1,8 +1,12 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
+import { CodeEditor } from '@jupyterlab/codeeditor';
+
 import { KernelMessage, Session } from '@jupyterlab/services';
 
+import { IIterator } from '@lumino/algorithm';
+
 import { Token } from '@lumino/coreutils';
 
 import { IObservableDisposable } from '@lumino/disposable';
@@ -271,6 +275,50 @@ export namespace IDebugger {
     };
   }
 
+  /**
+   * A utility to find text editors used by the debugger.
+   */
+  export interface IEditorFinder {
+    /**
+     * Returns an iterator of editors for a source matching the current debug
+     * session by iterating through all the widgets in each of the supported
+     * debugger types (i.e., consoles, files, notebooks).
+     *
+     * @param params - The editor search parameters.
+     */
+    find(params: IEditorFinder.Params): IIterator<CodeEditor.IEditor>;
+  }
+
+  /**
+   * A utility to find text editors used by the debugger.
+   */
+  export namespace IEditorFinder {
+    /**
+     * Unified parameters for find method
+     */
+    export type Params = {
+      /**
+       * Extra flag prevent disable focus.
+       */
+      focus: boolean;
+
+      /**
+       * Name of current kernel.
+       */
+      kernel: string;
+
+      /**
+       * Path of session connection.
+       */
+      path: string;
+
+      /**
+       * Source path
+       */
+      source: string;
+    };
+  }
+
   /**
    * The interface for a source file.
    */
@@ -454,3 +502,10 @@ export const IDebugger = new Token<IDebugger>('@jupyterlab/debugger');
 export const IDebuggerConfig = new Token<IDebugger.IConfig>(
   '@jupyterlab/debugger:config'
 );
+
+/**
+ * The debugger editor finder utility token.
+ */
+export const IDebuggerEditorFinder = new Token<IDebugger.IEditorFinder>(
+  '@jupyterlab/debugger:editor-finder'
+);