tokens.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { CodeEditor, CodeEditorWrapper } from '@jupyterlab/codeeditor';
  4. import { KernelMessage, Session } from '@jupyterlab/services';
  5. import { Token } from '@lumino/coreutils';
  6. import { IObservableDisposable } from '@lumino/disposable';
  7. import { ISignal, Signal } from '@lumino/signaling';
  8. import { DebugProtocol } from 'vscode-debugprotocol';
  9. /**
  10. * An interface describing an application's visual debugger.
  11. */
  12. export interface IDebugger {
  13. /**
  14. * Signal emitted for debug event messages.
  15. */
  16. readonly eventMessage: ISignal<IDebugger, IDebugger.ISession.Event>;
  17. /**
  18. * Whether the current debugger is started.
  19. */
  20. readonly isStarted: boolean;
  21. /**
  22. * The debugger service's model.
  23. */
  24. readonly model: IDebugger.Model.IService;
  25. /**
  26. * The current debugger session.
  27. */
  28. session: IDebugger.ISession;
  29. /**
  30. * Signal emitted upon session changed.
  31. */
  32. readonly sessionChanged: ISignal<IDebugger, IDebugger.ISession>;
  33. /**
  34. * Removes all the breakpoints from the current notebook or console
  35. */
  36. clearBreakpoints(): Promise<void>;
  37. /**
  38. * Continues the execution of the current thread.
  39. */
  40. continue(): Promise<void>;
  41. /**
  42. * Computes an id based on the given code.
  43. */
  44. getCodeId(code: string): string;
  45. /**
  46. * Retrieve the content of a source file.
  47. *
  48. * @param source The source object containing the path to the file.
  49. */
  50. getSource(source: DebugProtocol.Source): Promise<IDebugger.Source>;
  51. /**
  52. * Whether there exist a thread in stopped state.
  53. */
  54. hasStoppedThreads(): boolean;
  55. /**
  56. * Request variables for a given variable reference.
  57. *
  58. * @param variablesReference The variable reference to request.
  59. */
  60. inspectVariable(
  61. variablesReference: number
  62. ): Promise<DebugProtocol.Variable[]>;
  63. /**
  64. * Request whether debugging is available for the given session connection.
  65. *
  66. * @param connection The session connection.
  67. */
  68. isAvailable(connection: Session.ISessionConnection): Promise<boolean>;
  69. /**
  70. * Makes the current thread run again for one step.
  71. */
  72. next(): Promise<void>;
  73. /**
  74. * Restart the debugger.
  75. * Precondition: isStarted
  76. */
  77. restart(): Promise<void>;
  78. /**
  79. * Restore the state of a debug session.
  80. *
  81. * @param autoStart - when true, starts the debugger
  82. * if it has not been started yet.
  83. */
  84. restoreState(autoStart: boolean): Promise<void>;
  85. /**
  86. * Starts a debugger.
  87. * Precondition: !isStarted
  88. */
  89. start(): Promise<void>;
  90. /**
  91. * Makes the current thread step in a function / method if possible.
  92. */
  93. stepIn(): Promise<void>;
  94. /**
  95. * Makes the current thread step out a function / method if possible.
  96. */
  97. stepOut(): Promise<void>;
  98. /**
  99. * Stops the debugger.
  100. * Precondition: isStarted
  101. */
  102. stop(): Promise<void>;
  103. /**
  104. * Update all breakpoints of a cell at once.
  105. *
  106. * @param code - The code in the cell where the breakpoints are set.
  107. * @param breakpoints - The list of breakpoints to set.
  108. * @param path - Optional path to the file where to set the breakpoints.
  109. */
  110. updateBreakpoints(
  111. code: string,
  112. breakpoints: IDebugger.IBreakpoint[],
  113. path?: string
  114. ): Promise<void>;
  115. }
  116. /**
  117. * A namespace for visual debugger types.
  118. */
  119. export namespace IDebugger {
  120. /**
  121. * The type for a source file.
  122. */
  123. export type Source = {
  124. /**
  125. * The content of the source.
  126. */
  127. content: string;
  128. /**
  129. * The mimeType of the source.
  130. */
  131. mimeType?: string;
  132. /**
  133. * The path of the source.
  134. */
  135. path: string;
  136. };
  137. /**
  138. * Single breakpoint in an editor.
  139. */
  140. export interface IBreakpoint extends DebugProtocol.Breakpoint {
  141. active: boolean;
  142. }
  143. /**
  144. * Debugger file and hashing configuration.
  145. */
  146. export interface IConfig {
  147. /**
  148. * Returns an id based on the given code.
  149. *
  150. * @param code The source code.
  151. * @param kernel The kernel name from current session.
  152. */
  153. getCodeId(code: string, kernel: string): string;
  154. /**
  155. * Sets the hash parameters for a kernel.
  156. *
  157. * @param params - Hashing parameters for a kernel.
  158. */
  159. setHashParams(params: IConfig.HashParams): void;
  160. /**
  161. * Sets the parameters used for the temp files (e.g. cells) for a kernel.
  162. *
  163. * @param params - Temporary file prefix and suffix for a kernel.
  164. */
  165. setTmpFileParams(params: IConfig.FileParams): void;
  166. }
  167. /**
  168. * An interface for a scope.
  169. */
  170. export interface IScope {
  171. /**
  172. * The name of the scope.
  173. */
  174. name: string;
  175. /**
  176. * The list of variables.
  177. */
  178. variables: IVariable[];
  179. }
  180. /**
  181. * A visual debugger session.
  182. */
  183. export interface ISession extends IObservableDisposable {
  184. /**
  185. * The API session connection to connect to a debugger.
  186. */
  187. connection: Session.ISessionConnection;
  188. /**
  189. * Whether the debug session is started
  190. */
  191. readonly isStarted: boolean;
  192. /**
  193. * Signal emitted for debug event messages.
  194. */
  195. readonly eventMessage: ISignal<
  196. IDebugger.ISession,
  197. IDebugger.ISession.Event
  198. >;
  199. /**
  200. * Restore the state of a debug session.
  201. */
  202. restoreState(): Promise<IDebugger.ISession.Response['debugInfo']>;
  203. /**
  204. * Send a debug request to the kernel.
  205. */
  206. sendRequest<K extends keyof IDebugger.ISession.Request>(
  207. command: K,
  208. args: IDebugger.ISession.Request[K]
  209. ): Promise<IDebugger.ISession.Response[K]>;
  210. /**
  211. * Start a new debug session.
  212. */
  213. start(): Promise<void>;
  214. /**
  215. * Stop a running debug session.
  216. */
  217. stop(): Promise<void>;
  218. }
  219. /**
  220. * A utility to find text editors used by the debugger.
  221. */
  222. export interface ISources {
  223. /**
  224. * Returns an array of editors for a source matching the current debug
  225. * session by iterating through all the widgets in each of the supported
  226. * debugger types (i.e., consoles, files, notebooks).
  227. *
  228. * @param params - The editor find parameters.
  229. */
  230. find(params: ISources.FindParams): CodeEditor.IEditor[];
  231. /**
  232. * Open a read-only editor in the main area.
  233. *
  234. * @param params - The editor open parameters.
  235. */
  236. open(params: ISources.OpenParams): void;
  237. }
  238. /**
  239. * The type for a stack frame
  240. */
  241. export interface IStackFrame extends DebugProtocol.StackFrame {}
  242. /**
  243. * An interface for a variable.
  244. */
  245. export interface IVariable extends DebugProtocol.Variable {
  246. /**
  247. * Whether the variable is expanded.
  248. */
  249. expanded?: boolean;
  250. }
  251. /**
  252. * Debugger file and hashing configuration.
  253. */
  254. export namespace IConfig {
  255. /**
  256. * Temporary file prefix and suffix for a kernel.
  257. */
  258. export type FileParams = {
  259. /**
  260. * The kernel name.
  261. */
  262. kernel: string;
  263. /**
  264. * Prefix added to temporary files created by the kernel per cell.
  265. */
  266. prefix: string;
  267. /**
  268. * Suffix added temporary files created by the kernel per cell.
  269. */
  270. suffix: string;
  271. };
  272. /**
  273. * Hashing parameters for a kernel.
  274. */
  275. export type HashParams = {
  276. /**
  277. * The kernel name.
  278. */
  279. kernel: string;
  280. /**
  281. * The hashing method.
  282. */
  283. method: string;
  284. /**
  285. * An optional hashing seed provided by the kernel.
  286. */
  287. seed?: any;
  288. };
  289. }
  290. export namespace ISession {
  291. /**
  292. * A generic debug event.
  293. */
  294. export type Event = DebugProtocol.Event;
  295. /**
  296. * Expose all the debug requests types.
  297. */
  298. export type Request = {
  299. attach: DebugProtocol.AttachRequestArguments;
  300. completions: DebugProtocol.CompletionsArguments;
  301. configurationDone: DebugProtocol.ConfigurationDoneArguments;
  302. continue: DebugProtocol.ContinueArguments;
  303. debugInfo: {};
  304. disconnect: DebugProtocol.DisconnectArguments;
  305. dumpCell: IDumpCellArguments;
  306. evaluate: DebugProtocol.EvaluateArguments;
  307. exceptionInfo: DebugProtocol.ExceptionInfoArguments;
  308. goto: DebugProtocol.GotoArguments;
  309. gotoTargets: DebugProtocol.GotoTargetsArguments;
  310. initialize: DebugProtocol.InitializeRequestArguments;
  311. launch: DebugProtocol.LaunchRequestArguments;
  312. loadedSources: DebugProtocol.LoadedSourcesArguments;
  313. modules: DebugProtocol.ModulesArguments;
  314. next: DebugProtocol.NextArguments;
  315. pause: DebugProtocol.PauseArguments;
  316. restart: DebugProtocol.RestartArguments;
  317. restartFrame: DebugProtocol.RestartFrameArguments;
  318. reverseContinue: DebugProtocol.ReverseContinueArguments;
  319. scopes: DebugProtocol.ScopesArguments;
  320. setBreakpoints: DebugProtocol.SetBreakpointsArguments;
  321. setExceptionBreakpoints: DebugProtocol.SetExceptionBreakpointsArguments;
  322. setExpression: DebugProtocol.SetExpressionArguments;
  323. setFunctionBreakpoints: DebugProtocol.SetFunctionBreakpointsArguments;
  324. setVariable: DebugProtocol.SetVariableArguments;
  325. source: DebugProtocol.SourceArguments;
  326. stackTrace: DebugProtocol.StackTraceArguments;
  327. stepBack: DebugProtocol.StepBackArguments;
  328. stepIn: DebugProtocol.StepInArguments;
  329. stepInTargets: DebugProtocol.StepInTargetsArguments;
  330. stepOut: DebugProtocol.StepOutArguments;
  331. terminate: DebugProtocol.TerminateArguments;
  332. terminateThreads: DebugProtocol.TerminateThreadsArguments;
  333. threads: {};
  334. variables: DebugProtocol.VariablesArguments;
  335. };
  336. /**
  337. * Expose all the debug response types.
  338. */
  339. export type Response = {
  340. attach: DebugProtocol.AttachResponse;
  341. completions: DebugProtocol.CompletionsResponse;
  342. configurationDone: DebugProtocol.ConfigurationDoneResponse;
  343. continue: DebugProtocol.ContinueResponse;
  344. debugInfo: IDebugInfoResponse;
  345. disconnect: DebugProtocol.DisconnectResponse;
  346. dumpCell: IDumpCellResponse;
  347. evaluate: DebugProtocol.EvaluateResponse;
  348. exceptionInfo: DebugProtocol.ExceptionInfoResponse;
  349. goto: DebugProtocol.GotoResponse;
  350. gotoTargets: DebugProtocol.GotoTargetsResponse;
  351. initialize: DebugProtocol.InitializeResponse;
  352. launch: DebugProtocol.LaunchResponse;
  353. loadedSources: DebugProtocol.LoadedSourcesResponse;
  354. modules: DebugProtocol.ModulesResponse;
  355. next: DebugProtocol.NextResponse;
  356. pause: DebugProtocol.PauseResponse;
  357. restart: DebugProtocol.RestartResponse;
  358. restartFrame: DebugProtocol.RestartFrameResponse;
  359. reverseContinue: DebugProtocol.ReverseContinueResponse;
  360. scopes: DebugProtocol.ScopesResponse;
  361. setBreakpoints: DebugProtocol.SetBreakpointsResponse;
  362. setExceptionBreakpoints: DebugProtocol.SetExceptionBreakpointsResponse;
  363. setExpression: DebugProtocol.SetExpressionResponse;
  364. setFunctionBreakpoints: DebugProtocol.SetFunctionBreakpointsResponse;
  365. setVariable: DebugProtocol.SetVariableResponse;
  366. source: DebugProtocol.SourceResponse;
  367. stackTrace: DebugProtocol.StackTraceResponse;
  368. stepBack: DebugProtocol.StepBackResponse;
  369. stepIn: DebugProtocol.StepInResponse;
  370. stepInTargets: DebugProtocol.StepInTargetsResponse;
  371. stepOut: DebugProtocol.StepOutResponse;
  372. terminate: DebugProtocol.TerminateResponse;
  373. terminateThreads: DebugProtocol.TerminateThreadsResponse;
  374. threads: DebugProtocol.ThreadsResponse;
  375. variables: DebugProtocol.VariablesResponse;
  376. };
  377. /**
  378. * List of breakpoints in a source file.
  379. */
  380. export interface IDebugInfoBreakpoints {
  381. source: string;
  382. breakpoints: DebugProtocol.Breakpoint[];
  383. }
  384. /**
  385. * Response to 'debugInfo' request.
  386. * This is an addition to the Debug Adapter Protocol to be able
  387. * to retrieve the debugger state when restoring a session.
  388. */
  389. export interface IDebugInfoResponse extends DebugProtocol.Response {
  390. body: {
  391. isStarted: boolean;
  392. hashMethod: string;
  393. hashSeed: number;
  394. breakpoints: IDebugInfoBreakpoints[];
  395. tmpFilePrefix: string;
  396. tmpFileSuffix: string;
  397. stoppedThreads: number[];
  398. };
  399. }
  400. /**
  401. * Arguments for 'dumpCell' request.
  402. * This is an addition to the Debug Adapter Protocol to support
  403. * setting breakpoints for cells.
  404. */
  405. export interface IDumpCellArguments {
  406. code: string;
  407. }
  408. /**
  409. * Response to 'dumpCell' request.
  410. * This is an addition to the Debug Adapter Protocol to support
  411. * setting breakpoints for cells.
  412. */
  413. export interface IDumpCellResponse extends DebugProtocol.Response {
  414. body: {
  415. sourcePath: string;
  416. };
  417. }
  418. /**
  419. * Response to the 'kernel_info_request' request.
  420. * This interface extends the IInfoReply by adding the `debugger` key
  421. * that isn't part of the protocol yet.
  422. * See this pull request for more info: https://github.com/jupyter/jupyter_client/pull/486
  423. */
  424. export interface IInfoReply extends KernelMessage.IInfoReply {
  425. debugger: boolean;
  426. }
  427. }
  428. /**
  429. * A utility to find text editors used by the debugger.
  430. */
  431. export namespace ISources {
  432. /**
  433. * Unified parameters for the find method
  434. */
  435. export type FindParams = {
  436. /**
  437. * Extra flag to focus on the parent widget of the editor.
  438. */
  439. focus: boolean;
  440. /**
  441. * Name of current kernel.
  442. */
  443. kernel: string;
  444. /**
  445. * Path of session connection.
  446. */
  447. path: string;
  448. /**
  449. * Source path
  450. */
  451. source: string;
  452. };
  453. /**
  454. * Unified parameters for the open method
  455. */
  456. export type OpenParams = {
  457. /**
  458. * The caption for the read-only editor.
  459. */
  460. caption: string;
  461. /**
  462. * The code editor wrapper to add to the main area.
  463. */
  464. editorWrapper: CodeEditorWrapper;
  465. /**
  466. * The label for the read-only editor.
  467. */
  468. label: string;
  469. };
  470. }
  471. /**
  472. * A namespace for UI model definitions.
  473. */
  474. export namespace Model {
  475. /**
  476. * The breakpoints UI model.
  477. */
  478. export interface IBreakpoints {
  479. /**
  480. * Get all the breakpoints.
  481. */
  482. readonly breakpoints: Map<string, IDebugger.IBreakpoint[]>;
  483. /**
  484. * Signal emitted when the model changes.
  485. */
  486. readonly changed: ISignal<this, IDebugger.IBreakpoint[]>;
  487. /**
  488. * Signal emitted when a breakpoint is clicked.
  489. */
  490. readonly clicked: Signal<this, IDebugger.IBreakpoint>;
  491. /**
  492. * Signal emitted when the breakpoints are restored.
  493. */
  494. readonly restored: ISignal<this, void>;
  495. /**
  496. * Get the breakpoints for a given id (path).
  497. *
  498. * @param id The code id (path).
  499. */
  500. getBreakpoints(id: string): IBreakpoint[];
  501. /**
  502. * Restore a map of breakpoints.
  503. *
  504. * @param breakpoints The map of breakpoints
  505. */
  506. restoreBreakpoints(breakpoints: Map<string, IBreakpoint[]>): void;
  507. /**
  508. * Set the breakpoints for a given id (path).
  509. *
  510. * @param id The code id (path).
  511. * @param breakpoints The list of breakpoints.
  512. */
  513. setBreakpoints(id: string, breakpoints: IBreakpoint[]): void;
  514. }
  515. /**
  516. * The callstack UI model.
  517. */
  518. export interface ICallstack {
  519. /**
  520. * Signal emitted when the current frame has changed.
  521. */
  522. readonly currentFrameChanged: ISignal<this, IDebugger.IStackFrame>;
  523. /**
  524. * The current frame.
  525. */
  526. frame: IDebugger.IStackFrame;
  527. /**
  528. * The frames for the callstack.
  529. */
  530. frames: IDebugger.IStackFrame[];
  531. /**
  532. * Signal emitted when the frames have changed.
  533. */
  534. readonly framesChanged: ISignal<this, IDebugger.IStackFrame[]>;
  535. }
  536. /**
  537. * The data model for the debugger service.
  538. */
  539. export interface IService {
  540. /**
  541. * The breakpoints UI model.
  542. */
  543. readonly breakpoints: IBreakpoints;
  544. /**
  545. * The callstack UI model.
  546. */
  547. readonly callstack: ICallstack;
  548. /**
  549. * The variables UI model.
  550. */
  551. readonly variables: IVariables;
  552. /**
  553. * The sources UI model.
  554. */
  555. readonly sources: ISources;
  556. /**
  557. * The set of threads in stopped state.
  558. */
  559. stoppedThreads: Set<number>;
  560. /**
  561. * The current debugger title.
  562. */
  563. title: string;
  564. /**
  565. * A signal emitted when the title changes.
  566. */
  567. titleChanged: ISignal<this, string>;
  568. /**
  569. * Clear the model.
  570. */
  571. clear(): void;
  572. }
  573. /**
  574. * The sources UI model.
  575. */
  576. export interface ISources {
  577. /**
  578. * Signal emitted when the current frame changes.
  579. */
  580. readonly currentFrameChanged: ISignal<
  581. IDebugger.Model.ICallstack,
  582. IDebugger.IStackFrame
  583. >;
  584. /**
  585. * Return the current source.
  586. */
  587. currentSource: IDebugger.Source;
  588. /**
  589. * Signal emitted when the current source changes.
  590. */
  591. readonly currentSourceChanged: ISignal<
  592. IDebugger.Model.ISources,
  593. IDebugger.Source
  594. >;
  595. /**
  596. * Signal emitted when a source should be open in the main area.
  597. */
  598. readonly currentSourceOpened: ISignal<
  599. IDebugger.Model.ISources,
  600. IDebugger.Source
  601. >;
  602. /**
  603. * Open a source in the main area.
  604. */
  605. open(): void;
  606. }
  607. /**
  608. * The variables UI model.
  609. */
  610. export interface IVariables {
  611. /**
  612. * Signal emitted when the current variable has changed.
  613. */
  614. readonly changed: ISignal<this, void>;
  615. /**
  616. * The variable scopes.
  617. */
  618. scopes: IDebugger.IScope[];
  619. /**
  620. * Signal emitted when the current variable has been expanded.
  621. */
  622. readonly variableExpanded: ISignal<this, IDebugger.IVariable>;
  623. /**
  624. * Expand a variable.
  625. *
  626. * @param variable The variable to expand.
  627. */
  628. expandVariable(variable: IDebugger.IVariable): void;
  629. }
  630. }
  631. }
  632. /**
  633. * The visual debugger token.
  634. */
  635. export const IDebugger = new Token<IDebugger>('@jupyterlab/debugger');
  636. /**
  637. * The debugger configuration token.
  638. */
  639. export const IDebuggerConfig = new Token<IDebugger.IConfig>(
  640. '@jupyterlab/debugger:config'
  641. );
  642. /**
  643. * The debugger sources utility token.
  644. */
  645. export const IDebuggerSources = new Token<IDebugger.ISources>(
  646. '@jupyterlab/debugger:sources'
  647. );