debugger.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { CodeEditor } from '@jupyterlab/codeeditor';
  4. import { DebugService } from './service';
  5. import { DebugSession } from './session';
  6. import { DebuggerEditors } from './editors';
  7. import { DebuggerSidebar } from './sidebar';
  8. import { ReadonlyJSONValue } from '@phosphor/coreutils';
  9. import { IClientSession } from '@jupyterlab/apputils';
  10. import { IDisposable } from '@phosphor/disposable';
  11. import { Message } from '@phosphor/messaging';
  12. import { ISignal, Signal } from '@phosphor/signaling';
  13. import { SplitPanel } from '@phosphor/widgets';
  14. import { IObservableString } from '@jupyterlab/observables';
  15. import { IDebugger } from './tokens';
  16. import { IDataConnector } from '@jupyterlab/coreutils';
  17. export class Debugger extends SplitPanel {
  18. constructor(options: Debugger.IOptions) {
  19. super({ orientation: 'horizontal' });
  20. this.title.label = 'Debugger';
  21. this.title.iconClass = 'jp-BugIcon';
  22. this.model = new Debugger.Model(options);
  23. this.sidebar = new DebuggerSidebar(this.model);
  24. this.model.sidebar = this.sidebar;
  25. const { editorFactory } = options;
  26. this.editors = new DebuggerEditors({ editorFactory });
  27. this.addWidget(this.editors);
  28. this.addClass('jp-Debugger');
  29. }
  30. readonly editors: DebuggerEditors;
  31. readonly model: Debugger.Model;
  32. readonly sidebar: DebuggerSidebar;
  33. dispose(): void {
  34. if (this.isDisposed) {
  35. return;
  36. }
  37. this.model.dispose();
  38. super.dispose();
  39. }
  40. protected onAfterAttach(msg: Message) {
  41. this.addWidget(this.sidebar);
  42. this.sidebar.show();
  43. }
  44. }
  45. /**
  46. * A namespace for `Debugger` statics.
  47. */
  48. export namespace Debugger {
  49. export interface IOptions {
  50. editorFactory: CodeEditor.Factory;
  51. connector?: IDataConnector<ReadonlyJSONValue>;
  52. id?: string;
  53. session?: IClientSession;
  54. }
  55. export class Model implements IDisposable {
  56. constructor(options: Debugger.Model.IOptions) {
  57. this.connector = options.connector || null;
  58. // Avoids setting session with invalid client
  59. // session should be set only when a notebook or
  60. // a console get the focus.
  61. // TODO: also checks that the notebook or console
  62. // runs a kernel with debugging ability
  63. this.session = null;
  64. this.id = options.id;
  65. void this._populate();
  66. }
  67. readonly connector: IDataConnector<ReadonlyJSONValue> | null;
  68. readonly id: string;
  69. get mode(): IDebugger.Mode {
  70. return this._mode;
  71. }
  72. set mode(mode: IDebugger.Mode) {
  73. if (this._mode === mode) {
  74. return;
  75. }
  76. this._mode = mode;
  77. this._modeChanged.emit(mode);
  78. }
  79. get sidebar() {
  80. return this._sidebar;
  81. }
  82. set sidebar(sidebar: DebuggerSidebar) {
  83. this._sidebar = sidebar;
  84. }
  85. get modeChanged(): ISignal<this, IDebugger.Mode> {
  86. return this._modeChanged;
  87. }
  88. get session(): IDebugger.ISession {
  89. return this._session;
  90. }
  91. set session(session: IDebugger.ISession | null) {
  92. if (this._session === session) {
  93. return;
  94. }
  95. if (this._session) {
  96. this._session.dispose();
  97. }
  98. this._session = session;
  99. this._service.session = session as DebugSession;
  100. this._sessionChanged.emit(undefined);
  101. }
  102. get service(): DebugService {
  103. return this._service;
  104. }
  105. get sessionChanged(): ISignal<this, void> {
  106. return this._sessionChanged;
  107. }
  108. get isDisposed(): boolean {
  109. return this._isDisposed;
  110. }
  111. get codeValue() {
  112. return this._codeValue;
  113. }
  114. set codeValue(observableString: IObservableString) {
  115. this._codeValue = observableString;
  116. }
  117. dispose(): void {
  118. this._isDisposed = true;
  119. }
  120. private async _populate(): Promise<void> {
  121. const { connector } = this;
  122. if (!connector) {
  123. return;
  124. }
  125. }
  126. private _codeValue: IObservableString;
  127. private _sidebar: DebuggerSidebar;
  128. private _isDisposed = false;
  129. private _mode: IDebugger.Mode;
  130. private _modeChanged = new Signal<this, IDebugger.Mode>(this);
  131. private _session: IDebugger.ISession | null;
  132. private _sessionChanged = new Signal<this, void>(this);
  133. private _service = new DebugService(null, this);
  134. }
  135. export namespace Model {
  136. export interface IOptions extends Debugger.IOptions {}
  137. }
  138. }