textModelEvents.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. /**
  6. * An event describing that a model has been reset to a new value.
  7. * @internal
  8. */
  9. export class ModelRawFlush {
  10. constructor() {
  11. this.changeType = 1 /* Flush */;
  12. }
  13. }
  14. /**
  15. * Represents text injected on a line
  16. * @internal
  17. */
  18. export class LineInjectedText {
  19. constructor(ownerId, lineNumber, column, options, order) {
  20. this.ownerId = ownerId;
  21. this.lineNumber = lineNumber;
  22. this.column = column;
  23. this.options = options;
  24. this.order = order;
  25. }
  26. static applyInjectedText(lineText, injectedTexts) {
  27. if (!injectedTexts || injectedTexts.length === 0) {
  28. return lineText;
  29. }
  30. let result = '';
  31. let lastOriginalOffset = 0;
  32. for (const injectedText of injectedTexts) {
  33. result += lineText.substring(lastOriginalOffset, injectedText.column - 1);
  34. lastOriginalOffset = injectedText.column - 1;
  35. result += injectedText.options.content;
  36. }
  37. result += lineText.substring(lastOriginalOffset);
  38. return result;
  39. }
  40. static fromDecorations(decorations) {
  41. const result = [];
  42. for (const decoration of decorations) {
  43. if (decoration.options.before && decoration.options.before.content.length > 0) {
  44. result.push(new LineInjectedText(decoration.ownerId, decoration.range.startLineNumber, decoration.range.startColumn, decoration.options.before, 0));
  45. }
  46. if (decoration.options.after && decoration.options.after.content.length > 0) {
  47. result.push(new LineInjectedText(decoration.ownerId, decoration.range.endLineNumber, decoration.range.endColumn, decoration.options.after, 1));
  48. }
  49. }
  50. result.sort((a, b) => {
  51. if (a.lineNumber === b.lineNumber) {
  52. if (a.column === b.column) {
  53. return a.order - b.order;
  54. }
  55. return a.column - b.column;
  56. }
  57. return a.lineNumber - b.lineNumber;
  58. });
  59. return result;
  60. }
  61. }
  62. /**
  63. * An event describing that a line has changed in a model.
  64. * @internal
  65. */
  66. export class ModelRawLineChanged {
  67. constructor(lineNumber, detail, injectedText) {
  68. this.changeType = 2 /* LineChanged */;
  69. this.lineNumber = lineNumber;
  70. this.detail = detail;
  71. this.injectedText = injectedText;
  72. }
  73. }
  74. /**
  75. * An event describing that line(s) have been deleted in a model.
  76. * @internal
  77. */
  78. export class ModelRawLinesDeleted {
  79. constructor(fromLineNumber, toLineNumber) {
  80. this.changeType = 3 /* LinesDeleted */;
  81. this.fromLineNumber = fromLineNumber;
  82. this.toLineNumber = toLineNumber;
  83. }
  84. }
  85. /**
  86. * An event describing that line(s) have been inserted in a model.
  87. * @internal
  88. */
  89. export class ModelRawLinesInserted {
  90. constructor(fromLineNumber, toLineNumber, detail, injectedTexts) {
  91. this.changeType = 4 /* LinesInserted */;
  92. this.injectedTexts = injectedTexts;
  93. this.fromLineNumber = fromLineNumber;
  94. this.toLineNumber = toLineNumber;
  95. this.detail = detail;
  96. }
  97. }
  98. /**
  99. * An event describing that a model has had its EOL changed.
  100. * @internal
  101. */
  102. export class ModelRawEOLChanged {
  103. constructor() {
  104. this.changeType = 5 /* EOLChanged */;
  105. }
  106. }
  107. /**
  108. * An event describing a change in the text of a model.
  109. * @internal
  110. */
  111. export class ModelRawContentChangedEvent {
  112. constructor(changes, versionId, isUndoing, isRedoing) {
  113. this.changes = changes;
  114. this.versionId = versionId;
  115. this.isUndoing = isUndoing;
  116. this.isRedoing = isRedoing;
  117. this.resultingSelection = null;
  118. }
  119. containsEvent(type) {
  120. for (let i = 0, len = this.changes.length; i < len; i++) {
  121. const change = this.changes[i];
  122. if (change.changeType === type) {
  123. return true;
  124. }
  125. }
  126. return false;
  127. }
  128. static merge(a, b) {
  129. const changes = [].concat(a.changes).concat(b.changes);
  130. const versionId = b.versionId;
  131. const isUndoing = (a.isUndoing || b.isUndoing);
  132. const isRedoing = (a.isRedoing || b.isRedoing);
  133. return new ModelRawContentChangedEvent(changes, versionId, isUndoing, isRedoing);
  134. }
  135. }
  136. /**
  137. * An event describing a change in injected text.
  138. * @internal
  139. */
  140. export class ModelInjectedTextChangedEvent {
  141. constructor(changes) {
  142. this.changes = changes;
  143. }
  144. }
  145. /**
  146. * @internal
  147. */
  148. export class InternalModelContentChangeEvent {
  149. constructor(rawContentChangedEvent, contentChangedEvent) {
  150. this.rawContentChangedEvent = rawContentChangedEvent;
  151. this.contentChangedEvent = contentChangedEvent;
  152. }
  153. merge(other) {
  154. const rawContentChangedEvent = ModelRawContentChangedEvent.merge(this.rawContentChangedEvent, other.rawContentChangedEvent);
  155. const contentChangedEvent = InternalModelContentChangeEvent._mergeChangeEvents(this.contentChangedEvent, other.contentChangedEvent);
  156. return new InternalModelContentChangeEvent(rawContentChangedEvent, contentChangedEvent);
  157. }
  158. static _mergeChangeEvents(a, b) {
  159. const changes = [].concat(a.changes).concat(b.changes);
  160. const eol = b.eol;
  161. const versionId = b.versionId;
  162. const isUndoing = (a.isUndoing || b.isUndoing);
  163. const isRedoing = (a.isRedoing || b.isRedoing);
  164. const isFlush = (a.isFlush || b.isFlush);
  165. return {
  166. changes: changes,
  167. eol: eol,
  168. versionId: versionId,
  169. isUndoing: isUndoing,
  170. isRedoing: isRedoing,
  171. isFlush: isFlush
  172. };
  173. }
  174. }