EditorDebuggingHook.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. // Based upon the TypeScript language services example at https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#incremental-build-support-using-the-language-services
  23. import HostInteropType from "../../../interop";
  24. import ClientExtensionEventNames from "../../ClientExtensionEventNames";
  25. import BreakpointDecoratorManager from "../../../debugger/BreakpointDecoratorManager";
  26. import * as debuggerProxy from "../../../debugger/HostDebuggerExtensionProxy";
  27. /**
  28. * Resource extension that handles compiling or transpling typescript on file save.
  29. */
  30. export default class EditorDebuggingHook implements Editor.ClientExtensions.WebViewServiceEventListener {
  31. name = "EditorDebuggingHook";
  32. description = "This extension adds debugger functionality to the current editor";
  33. /**
  34. * current filename
  35. * @type {string}
  36. */
  37. filename: string;
  38. /**
  39. * Is this instance of the extension active? Only true if the current editor
  40. * is a typescript file.
  41. * @type {Boolean}
  42. */
  43. private active = false;
  44. private serviceLocator: Editor.ClientExtensions.ClientServiceLocator;
  45. private editor: monaco.editor.IStandaloneCodeEditor;
  46. private breakpointDecorator: BreakpointDecoratorManager;
  47. /**
  48. * Inject this language service into the registry
  49. * @param {Editor.ClientExtensions.ClientServiceLocator} serviceLocator
  50. */
  51. initialize(serviceLocator: Editor.ClientExtensions.ClientServiceLocator) {
  52. // initialize the language service
  53. this.serviceLocator = serviceLocator;
  54. serviceLocator.clientServices.register(this);
  55. }
  56. /**
  57. * Determines if the file name/path provided is something we care about
  58. * @param {string} path
  59. * @return {boolean}
  60. */
  61. private isValidFiletype(path: string): boolean {
  62. // For now, only allow JS files
  63. let ext = path.split(".").pop();
  64. return ext == "js";
  65. }
  66. /**
  67. * Grab the list of breakpoints from the host
  68. */
  69. async retrieveBreakpoints() {
  70. let s = await debuggerProxy.getBreakpoints();
  71. // If the filename starts with "Resources" then trim it off since the module
  72. // name won't have that, but the editor uses it
  73. s.forEach(b => {
  74. if (b.fileName == this.filename) {
  75. this.breakpointDecorator.addBreakpointDecoration(b.fileName, b.lineNumber);
  76. }
  77. });
  78. }
  79. registerDebuggerFunctions() {
  80. // Register the callback functions
  81. const interop = HostInteropType.getInstance();
  82. interop.addCustomHostRoutine(
  83. debuggerProxy.debuggerHostKeys.toggleBreakpoint,
  84. (filename, linenumber) => {
  85. if (filename == this.filename || "Resources/" + filename == this.filename) {
  86. this.breakpointDecorator.toggleBreakpoint(this.filename, linenumber);
  87. }
  88. });
  89. interop.addCustomHostRoutine(
  90. debuggerProxy.debuggerHostKeys.addBreakpoint,
  91. (filename, linenumber) => {
  92. if (filename == this.filename || "Resources/" + filename == this.filename) {
  93. this.breakpointDecorator.addBreakpointDecoration(this.filename, linenumber);
  94. }
  95. });
  96. interop.addCustomHostRoutine(
  97. debuggerProxy.debuggerHostKeys.removeBreakpoint,
  98. (filename, linenumber) => {
  99. if (filename == this.filename || "Resources/" + filename == this.filename) {
  100. this.breakpointDecorator.removeBreakpointDecoration(this.filename, linenumber);
  101. }
  102. });
  103. debuggerProxy.registerDebuggerListener(this.filename);
  104. }
  105. /**
  106. * Called when the editor needs to be configured for a particular file
  107. * @param {Editor.ClientExtensions.EditorFileEvent} ev
  108. */
  109. configureEditor(ev: Editor.ClientExtensions.EditorFileEvent) {
  110. if (this.isValidFiletype(ev.filename)) {
  111. let editor = ev.editor as monaco.editor.IStandaloneCodeEditor;
  112. this.editor = editor; // cache this so that we can reference it later
  113. this.active = true;
  114. this.filename = ev.filename;
  115. // for now, we just want to set breakpoints in JS files
  116. editor.updateOptions({
  117. glyphMargin: true
  118. });
  119. this.breakpointDecorator = new BreakpointDecoratorManager(editor);
  120. this.breakpointDecorator.setCurrentFileName(ev.filename);
  121. this.registerDebuggerFunctions();
  122. this.retrieveBreakpoints();
  123. let marginTimeout = 0;
  124. editor.onMouseMove((e) => {
  125. var targetZone = e.target.toString();
  126. if (targetZone.indexOf("GUTTER_GLYPH_MARGIN") != -1) {
  127. clearTimeout(marginTimeout);
  128. var line = e.target.position.lineNumber;
  129. this.breakpointDecorator.updateMarginHover(line);
  130. marginTimeout = setTimeout(() => this.breakpointDecorator.removeMarginHover(), 500);
  131. } else {
  132. clearTimeout(marginTimeout);
  133. this.breakpointDecorator.removeMarginHover();
  134. }
  135. });
  136. editor.onMouseDown((e) => {
  137. var targetZone = e.target.toString();
  138. if (targetZone.indexOf("GUTTER_GLYPH_MARGIN") != -1) {
  139. var line = e.target.position.lineNumber;
  140. this.breakpointDecorator.toggleBreakpoint(ev.filename, line);
  141. debuggerProxy.toggleBreakpoint(ev.filename, line);
  142. }
  143. });
  144. }
  145. }
  146. }