import { JupyterFrontEnd } from '@jupyterlab/application' ; import { MainAreaWidget , WidgetTracker , DOMUtils } from '@jupyterlab/apputils' ; import { CodeEditor , CodeEditorWrapper , IEditorServices } from '@jupyterlab/codeeditor' ; import { IConsoleTracker } from '@jupyterlab/console' ; import { IEditorTracker } from '@jupyterlab/fileeditor' ; import { INotebookTracker } from '@jupyterlab/notebook' ; import { textEditorIcon } from '@jupyterlab/ui-components' ; import { each } from '@lumino/algorithm' ; import { IDebugger } from './tokens' ; /**
 * The source and editor manager for a debugger instance.
 */ export class DebuggerSources implements IDebugger . ISources { param options constructor ( options ) { this . _config = options . config ; this . _shell = options . shell ; this . _notebookTracker = options . notebookTracker ; this . _consoleTracker = options . consoleTracker ; this . _editorTracker = options . editorTracker ; this . _readOnlyEditorTracker = new WidgetTracker < MainAreaWidget < CodeEditorWrapper > > ( { namespace : '@jupyterlab/debugger' } ) ; } param params find ( params ) { return [ ... this . _findInConsoles ( params ) , ... this . _findInEditors ( params ) , ... this . _findInNotebooks ( params ) , ... this . _findInReadOnlyEditors ( params ) ] ; } param params open ( params ) { const { editorWrapper , label , caption } = params ; const widget = new MainAreaWidget < CodeEditorWrapper > ( { content : editorWrapper } ) ; widget . id = DOMUtils . createDomID ( ) ; widget . title . label = label ; widget . title . closable = true ; widget . title . caption = caption ; widget . title . icon = textEditorIcon ; this . _shell . add ( widget , 'main' ) ; void this . _readOnlyEditorTracker . add ( widget ) ; } param params private _findInNotebooks ( params ) { if ( ! this . _notebookTracker ) { return [ ] ; } const { focus , kernel , path , source } = params ; const editors = [ ] ; this . _notebookTracker . forEach ( notebookPanel => { const sessionContext = notebookPanel . sessionContext ; if ( path !== sessionContext . path ) { return ; } const notebook = notebookPanel . content ; if ( focus ) { notebook . mode = 'command' ; } const cells = notebookPanel . content . widgets ; cells . forEach ( ( cell , i ) => { const code = cell . model . value . text ; const codeId = this . _getCodeId ( code , kernel ) ; if ( ! codeId ) { return ; } if ( source !== codeId ) { return ; } if ( focus ) { notebook . activeCellIndex = i ; const { node } = notebook . activeCell . inputArea ; const rect = node . getBoundingClientRect ( ) ; notebook . scrollToPosition ( rect . bottom , 45 ) ; this . _shell . activateById ( notebookPanel . id ) ; } editors . push ( cell . editor ) ; } ) ; } ) ; return editors ; } param params private _findInConsoles ( params ) { if ( ! this . _consoleTracker ) { return [ ] ; } const { focus , kernel , path , source } = params ; const editors = [ ] ; this . _consoleTracker . forEach ( consoleWidget => { const sessionContext = consoleWidget . sessionContext ; if ( path !== sessionContext . path ) { return ; } const cells = consoleWidget . console . cells ; each ( cells , cell => { const code = cell . model . value . text ; const codeId = this . _getCodeId ( code , kernel ) ; if ( ! codeId ) { return ; } if ( source !== codeId ) { return ; } editors . push ( cell . editor ) ; if ( focus ) { this . _shell . activateById ( consoleWidget . id ) ; } } ) ; } ) ; return editors ; } param params private _findInEditors ( params ) { if ( ! this . _editorTracker ) { return ; } const { focus , kernel , path , source } = params ; const editors = [ ] ; this . _editorTracker . forEach ( doc => { const fileEditor = doc . content ; if ( path !== fileEditor . context . path ) { return ; } const editor = fileEditor . editor ; if ( ! editor ) { return ; } const code = editor . model . value . text ; const codeId = this . _getCodeId ( code , kernel ) ; if ( ! codeId ) { return ; } if ( source !== codeId ) { return ; } editors . push ( editor ) ; if ( focus ) { this . _shell . activateById ( doc . id ) ; } } ) ; return editors ; } param params private _findInReadOnlyEditors ( params ) { const { focus , kernel , source } = params ; const editors = [ ] ; this . _readOnlyEditorTracker . forEach ( widget => { const editor = widget . content ?. editor ; if ( ! editor ) { return ; } const code = editor . model . value . text ; const codeId = this . _getCodeId ( code , kernel ) ; if ( ! codeId ) { return ; } if ( widget . title . caption !== source && source !== codeId ) { return ; } editors . push ( editor ) ; if ( focus ) { this . _shell . activateById ( widget . id ) ; } } ) ; return editors ; } param code param kernel private _getCodeId ( code , kernel ) { try { return this . _config . getCodeId ( code , kernel ) ; } catch { return '' ; } } private _shell : JupyterFrontEnd . IShell ; private _readOnlyEditorTracker : WidgetTracker < MainAreaWidget < CodeEditorWrapper > > ; private _config : IDebugger . IConfig ; private _notebookTracker : INotebookTracker | null ; private _consoleTracker : IConsoleTracker | null ; private _editorTracker : IEditorTracker | null ; } /**
 * A namespace for `DebuggerSources` statics.
 */ export namespace DebuggerSources { /**
     * The options used to initialize a DebuggerSources object.
     */ export interface IOptions { /**
         * The instance of configuration with hash method.
         */ config : IDebugger . IConfig ; /**
         * The editor services.
         */ editorServices : IEditorServices ; /**
         * The application shell.
         */ shell : JupyterFrontEnd . IShell ; /**
         * An optional console tracker.
         */ consoleTracker ? : IConsoleTracker ; /**
         * An optional file editor tracker.
         */ editorTracker ? : IEditorTracker ; /**
         * An optional notebook tracker.
         */ notebookTracker ? : INotebookTracker ; } }