BreakpointDecoratorManager.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. export interface Breakpoint {
  2. fileName: string;
  3. lineNumber: number;
  4. }
  5. export default class BreakpointDecoratorManager {
  6. currEditorDecorations: monaco.editor.IModelDeltaDecoration[] = [];
  7. currEditorDecorationIds: string[] = [];
  8. currFileName: string;
  9. constructor(private editor: monaco.editor.IStandaloneCodeEditor) { }
  10. setCurrentFileName(fileName: string) {
  11. if (this.currFileName != fileName) {
  12. this.clearBreakpointDecorations();
  13. }
  14. this.currFileName = fileName;
  15. }
  16. clearBreakpointDecorations() {
  17. this.currEditorDecorations.length = 0;
  18. this.currEditorDecorationIds = this.editor.deltaDecorations(this.currEditorDecorationIds, this.currEditorDecorations);
  19. }
  20. removeBreakpointDecoration(fileName: string, lineNumber: number) {
  21. if (fileName != this.currFileName) {
  22. return;
  23. }
  24. const idx = this.currEditorDecorations.findIndex(d => d.range.startLineNumber == lineNumber);
  25. if (idx != -1) {
  26. this.currEditorDecorations.splice(idx);
  27. this.currEditorDecorationIds = this.editor.deltaDecorations(this.currEditorDecorationIds, this.currEditorDecorations);
  28. }
  29. }
  30. addBreakpointDecoration(fileName: string, lineNumber: number, pendingBreakpoint?: boolean) {
  31. if (fileName != this.currFileName) {
  32. return;
  33. }
  34. const requestedBreakpointClass = pendingBreakpoint ? "code-breakpoint-pending" : "code-breakpoint";
  35. let idx = this.currEditorDecorations.findIndex(d => d.range.startLineNumber == lineNumber);
  36. if (idx != -1 && this.currEditorDecorations[idx].options.glyphMarginClassName != requestedBreakpointClass) {
  37. this.removeBreakpointDecoration(fileName, lineNumber);
  38. idx = -1;
  39. }
  40. if (idx == -1) {
  41. this.currEditorDecorations.push({
  42. range: new monaco.Range(lineNumber, 1, lineNumber, 1),
  43. options: {
  44. glyphMarginClassName: pendingBreakpoint ? "code-breakpoint-pending" : "code-breakpoint"
  45. }
  46. });
  47. this.currEditorDecorationIds = this.editor.deltaDecorations(this.currEditorDecorationIds, this.currEditorDecorations);
  48. }
  49. }
  50. getBreakpointDecorator(fileName: string, lineNumber: number) {
  51. return this.currEditorDecorations.find(d => d.range.startLineNumber == lineNumber && d.options.glyphMarginClassName != "code-breakpoint-margin-hover");
  52. }
  53. toggleBreakpoint(fileName: string, lineNumber: number) {
  54. if (fileName != this.currFileName) {
  55. return;
  56. }
  57. let decoration = this.getBreakpointDecorator(fileName, lineNumber);
  58. if (decoration) {
  59. this.removeBreakpointDecoration(fileName, lineNumber);
  60. } else {
  61. this.addBreakpointDecoration(fileName, lineNumber, false);
  62. }
  63. }
  64. updateMarginHover(lineNumber: number) {
  65. let idx = this.currEditorDecorations.findIndex(d => d.options.glyphMarginClassName == "code-breakpoint-margin-hover");
  66. if (idx != -1 && this.currEditorDecorations[idx].range.startLineNumber == lineNumber) {
  67. return; // nothing to do
  68. }
  69. const hover = {
  70. range: new monaco.Range(lineNumber, 1, lineNumber, 1),
  71. options: {
  72. glyphMarginClassName: "code-breakpoint-margin-hover"
  73. }
  74. };
  75. if (idx == -1) {
  76. this.currEditorDecorations.push(hover);
  77. } else {
  78. this.currEditorDecorations[idx] = hover;
  79. }
  80. this.currEditorDecorationIds = this.editor.deltaDecorations(this.currEditorDecorationIds, this.currEditorDecorations);
  81. }
  82. removeMarginHover() {
  83. let idx = this.currEditorDecorations.findIndex(d => d.options.glyphMarginClassName == "code-breakpoint-margin-hover");
  84. if (idx != -1) {
  85. this.currEditorDecorations.splice(idx);
  86. this.currEditorDecorationIds = this.editor.deltaDecorations(this.currEditorDecorationIds, this.currEditorDecorations);
  87. }
  88. }
  89. }