index.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. ILayoutRestorer,
  5. JupyterFrontEnd,
  6. JupyterFrontEndPlugin
  7. } from '@jupyterlab/application';
  8. import { WidgetTracker, MainAreaWidget } from '@jupyterlab/apputils';
  9. import { IConsoleTracker } from '@jupyterlab/console';
  10. import { IStateDB } from '@jupyterlab/coreutils';
  11. import { IEditorTracker } from '@jupyterlab/fileeditor';
  12. import { INotebookTracker } from '@jupyterlab/notebook';
  13. import { Debugger } from './debugger';
  14. import { DebuggerSidebar } from './sidebar';
  15. import { IDebugger } from './tokens';
  16. /**
  17. * The command IDs used by the debugger plugin.
  18. */
  19. export namespace CommandIDs {
  20. export const create = 'debugger:create';
  21. export const debugConsole = 'debugger:debug-console';
  22. export const debugFile = 'debugger:debug-file';
  23. export const debugNotebook = 'debugger:debug-notebook';
  24. }
  25. /**
  26. * A plugin that provides visual debugging support for consoles.
  27. */
  28. const consoles: JupyterFrontEndPlugin<void> = {
  29. id: '@jupyterlab/debugger:consoles',
  30. autoStart: true,
  31. requires: [IDebugger],
  32. optional: [IConsoleTracker],
  33. activate: (_, debug, tracker: IConsoleTracker | null) => {
  34. if (!tracker) {
  35. console.log(`${consoles.id} load failed. There is no console tracker.`);
  36. return;
  37. }
  38. console.log(`${consoles.id} has not been implemented.`, debug);
  39. }
  40. };
  41. /**
  42. * A plugin that provides visual debugging support for file editors.
  43. */
  44. const files: JupyterFrontEndPlugin<void> = {
  45. id: '@jupyterlab/debugger:files',
  46. autoStart: true,
  47. optional: [IEditorTracker],
  48. activate: (app: JupyterFrontEnd, tracker: IEditorTracker | null) => {
  49. app.commands.addCommand(CommandIDs.debugFile, {
  50. execute: async _ => {
  51. if (!tracker || !tracker.currentWidget) {
  52. return;
  53. }
  54. if (tracker.currentWidget) {
  55. // TODO: Find if the file is backed by a kernel or attach it to one.
  56. const widget = await app.commands.execute(CommandIDs.create);
  57. app.shell.add(widget, 'main');
  58. }
  59. }
  60. });
  61. }
  62. };
  63. /**
  64. * A plugin that provides visual debugging support for notebooks.
  65. */
  66. const notebooks: JupyterFrontEndPlugin<void> = {
  67. id: '@jupyterlab/debugger:notebooks',
  68. autoStart: true,
  69. requires: [IDebugger],
  70. optional: [INotebookTracker],
  71. activate: (_, debug, tracker: INotebookTracker | null) => {
  72. if (!tracker) {
  73. console.log(`${notebooks.id} load failed. There is no notebook tracker.`);
  74. return;
  75. }
  76. console.log(`${notebooks.id} has not been implemented.`, debug);
  77. }
  78. };
  79. /**
  80. * A plugin providing a condensed sidebar UI for debugging.
  81. */
  82. const sidebar: JupyterFrontEndPlugin<void> = {
  83. id: '@jupyterlab/debugger:sidebar',
  84. optional: [ILayoutRestorer],
  85. autoStart: true,
  86. activate: (app: JupyterFrontEnd, restorer: ILayoutRestorer | null) => {
  87. const { shell } = app;
  88. const label = 'Environment';
  89. const namespace = 'jp-debugger-sidebar';
  90. const sidebar = new DebuggerSidebar(null);
  91. sidebar.id = namespace;
  92. sidebar.title.label = label;
  93. shell.add(sidebar, 'right', { activate: false });
  94. if (restorer) {
  95. restorer.add(sidebar, sidebar.id);
  96. }
  97. }
  98. };
  99. /**
  100. * A plugin providing a tracker code debuggers.
  101. */
  102. const tracker: JupyterFrontEndPlugin<IDebugger> = {
  103. id: '@jupyterlab/debugger:tracker',
  104. optional: [ILayoutRestorer],
  105. requires: [IStateDB],
  106. provides: IDebugger,
  107. autoStart: true,
  108. activate: (
  109. app: JupyterFrontEnd,
  110. state: IStateDB,
  111. restorer: ILayoutRestorer | null
  112. ): IDebugger => {
  113. const tracker = new WidgetTracker<MainAreaWidget<Debugger>>({
  114. namespace: 'debugger'
  115. });
  116. app.commands.addCommand(CommandIDs.create, {
  117. execute: args => {
  118. const id = (args.id as string) || '';
  119. if (tracker.find(widget => id === widget.content.model.id)) {
  120. return;
  121. }
  122. const widget = new MainAreaWidget({
  123. content: new Debugger({ connector: state, id })
  124. });
  125. void tracker.add(widget);
  126. return widget;
  127. }
  128. });
  129. if (restorer) {
  130. // Handle state restoration.
  131. void restorer.restore(tracker, {
  132. command: CommandIDs.create,
  133. args: widget => ({ id: widget.content.model.id }),
  134. name: widget => widget.content.model.id
  135. });
  136. }
  137. return tracker;
  138. }
  139. };
  140. /**
  141. * Export the plugins as default.
  142. */
  143. const plugins: JupyterFrontEndPlugin<any>[] = [
  144. consoles,
  145. files,
  146. notebooks,
  147. sidebar,
  148. tracker
  149. ];
  150. export default plugins;