090fb38733c9d81a1716917e16af97fb974d04019dc95e12d578542eea96730a4df79d972d1dbfc8f8a9a84c5722894a0fbe8b97e0c0cf508728a12fe24720 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. import * as arrays from '../../../base/common/arrays.js';
  6. import { LineTokens } from './lineTokens.js';
  7. /**
  8. * Represents sparse tokens in a text model.
  9. */
  10. export class SparseTokensStore {
  11. constructor(languageIdCodec) {
  12. this._pieces = [];
  13. this._isComplete = false;
  14. this._languageIdCodec = languageIdCodec;
  15. }
  16. flush() {
  17. this._pieces = [];
  18. this._isComplete = false;
  19. }
  20. isEmpty() {
  21. return (this._pieces.length === 0);
  22. }
  23. set(pieces, isComplete) {
  24. this._pieces = pieces || [];
  25. this._isComplete = isComplete;
  26. }
  27. setPartial(_range, pieces) {
  28. // console.log(`setPartial ${_range} ${pieces.map(p => p.toString()).join(', ')}`);
  29. let range = _range;
  30. if (pieces.length > 0) {
  31. const _firstRange = pieces[0].getRange();
  32. const _lastRange = pieces[pieces.length - 1].getRange();
  33. if (!_firstRange || !_lastRange) {
  34. return _range;
  35. }
  36. range = _range.plusRange(_firstRange).plusRange(_lastRange);
  37. }
  38. let insertPosition = null;
  39. for (let i = 0, len = this._pieces.length; i < len; i++) {
  40. const piece = this._pieces[i];
  41. if (piece.endLineNumber < range.startLineNumber) {
  42. // this piece is before the range
  43. continue;
  44. }
  45. if (piece.startLineNumber > range.endLineNumber) {
  46. // this piece is after the range, so mark the spot before this piece
  47. // as a good insertion position and stop looping
  48. insertPosition = insertPosition || { index: i };
  49. break;
  50. }
  51. // this piece might intersect with the range
  52. piece.removeTokens(range);
  53. if (piece.isEmpty()) {
  54. // remove the piece if it became empty
  55. this._pieces.splice(i, 1);
  56. i--;
  57. len--;
  58. continue;
  59. }
  60. if (piece.endLineNumber < range.startLineNumber) {
  61. // after removal, this piece is before the range
  62. continue;
  63. }
  64. if (piece.startLineNumber > range.endLineNumber) {
  65. // after removal, this piece is after the range
  66. insertPosition = insertPosition || { index: i };
  67. continue;
  68. }
  69. // after removal, this piece contains the range
  70. const [a, b] = piece.split(range);
  71. if (a.isEmpty()) {
  72. // this piece is actually after the range
  73. insertPosition = insertPosition || { index: i };
  74. continue;
  75. }
  76. if (b.isEmpty()) {
  77. // this piece is actually before the range
  78. continue;
  79. }
  80. this._pieces.splice(i, 1, a, b);
  81. i++;
  82. len++;
  83. insertPosition = insertPosition || { index: i };
  84. }
  85. insertPosition = insertPosition || { index: this._pieces.length };
  86. if (pieces.length > 0) {
  87. this._pieces = arrays.arrayInsert(this._pieces, insertPosition.index, pieces);
  88. }
  89. // console.log(`I HAVE ${this._pieces.length} pieces`);
  90. // console.log(`${this._pieces.map(p => p.toString()).join('\n')}`);
  91. return range;
  92. }
  93. isComplete() {
  94. return this._isComplete;
  95. }
  96. addSparseTokens(lineNumber, aTokens) {
  97. if (aTokens.getLineContent().length === 0) {
  98. // Don't do anything for empty lines
  99. return aTokens;
  100. }
  101. const pieces = this._pieces;
  102. if (pieces.length === 0) {
  103. return aTokens;
  104. }
  105. const pieceIndex = SparseTokensStore._findFirstPieceWithLine(pieces, lineNumber);
  106. const bTokens = pieces[pieceIndex].getLineTokens(lineNumber);
  107. if (!bTokens) {
  108. return aTokens;
  109. }
  110. const aLen = aTokens.getCount();
  111. const bLen = bTokens.getCount();
  112. let aIndex = 0;
  113. const result = [];
  114. let resultLen = 0;
  115. let lastEndOffset = 0;
  116. const emitToken = (endOffset, metadata) => {
  117. if (endOffset === lastEndOffset) {
  118. return;
  119. }
  120. lastEndOffset = endOffset;
  121. result[resultLen++] = endOffset;
  122. result[resultLen++] = metadata;
  123. };
  124. for (let bIndex = 0; bIndex < bLen; bIndex++) {
  125. const bStartCharacter = bTokens.getStartCharacter(bIndex);
  126. const bEndCharacter = bTokens.getEndCharacter(bIndex);
  127. const bMetadata = bTokens.getMetadata(bIndex);
  128. const bMask = (((bMetadata & 1 /* MetadataConsts.SEMANTIC_USE_ITALIC */) ? 2048 /* MetadataConsts.ITALIC_MASK */ : 0)
  129. | ((bMetadata & 2 /* MetadataConsts.SEMANTIC_USE_BOLD */) ? 4096 /* MetadataConsts.BOLD_MASK */ : 0)
  130. | ((bMetadata & 4 /* MetadataConsts.SEMANTIC_USE_UNDERLINE */) ? 8192 /* MetadataConsts.UNDERLINE_MASK */ : 0)
  131. | ((bMetadata & 8 /* MetadataConsts.SEMANTIC_USE_STRIKETHROUGH */) ? 16384 /* MetadataConsts.STRIKETHROUGH_MASK */ : 0)
  132. | ((bMetadata & 16 /* MetadataConsts.SEMANTIC_USE_FOREGROUND */) ? 16744448 /* MetadataConsts.FOREGROUND_MASK */ : 0)
  133. | ((bMetadata & 32 /* MetadataConsts.SEMANTIC_USE_BACKGROUND */) ? 4278190080 /* MetadataConsts.BACKGROUND_MASK */ : 0)) >>> 0;
  134. const aMask = (~bMask) >>> 0;
  135. // push any token from `a` that is before `b`
  136. while (aIndex < aLen && aTokens.getEndOffset(aIndex) <= bStartCharacter) {
  137. emitToken(aTokens.getEndOffset(aIndex), aTokens.getMetadata(aIndex));
  138. aIndex++;
  139. }
  140. // push the token from `a` if it intersects the token from `b`
  141. if (aIndex < aLen && aTokens.getStartOffset(aIndex) < bStartCharacter) {
  142. emitToken(bStartCharacter, aTokens.getMetadata(aIndex));
  143. }
  144. // skip any tokens from `a` that are contained inside `b`
  145. while (aIndex < aLen && aTokens.getEndOffset(aIndex) < bEndCharacter) {
  146. emitToken(aTokens.getEndOffset(aIndex), (aTokens.getMetadata(aIndex) & aMask) | (bMetadata & bMask));
  147. aIndex++;
  148. }
  149. if (aIndex < aLen) {
  150. emitToken(bEndCharacter, (aTokens.getMetadata(aIndex) & aMask) | (bMetadata & bMask));
  151. if (aTokens.getEndOffset(aIndex) === bEndCharacter) {
  152. // `a` ends exactly at the same spot as `b`!
  153. aIndex++;
  154. }
  155. }
  156. else {
  157. const aMergeIndex = Math.min(Math.max(0, aIndex - 1), aLen - 1);
  158. // push the token from `b`
  159. emitToken(bEndCharacter, (aTokens.getMetadata(aMergeIndex) & aMask) | (bMetadata & bMask));
  160. }
  161. }
  162. // push the remaining tokens from `a`
  163. while (aIndex < aLen) {
  164. emitToken(aTokens.getEndOffset(aIndex), aTokens.getMetadata(aIndex));
  165. aIndex++;
  166. }
  167. return new LineTokens(new Uint32Array(result), aTokens.getLineContent(), this._languageIdCodec);
  168. }
  169. static _findFirstPieceWithLine(pieces, lineNumber) {
  170. let low = 0;
  171. let high = pieces.length - 1;
  172. while (low < high) {
  173. let mid = low + Math.floor((high - low) / 2);
  174. if (pieces[mid].endLineNumber < lineNumber) {
  175. low = mid + 1;
  176. }
  177. else if (pieces[mid].startLineNumber > lineNumber) {
  178. high = mid - 1;
  179. }
  180. else {
  181. while (mid > low && pieces[mid - 1].startLineNumber <= lineNumber && lineNumber <= pieces[mid - 1].endLineNumber) {
  182. mid--;
  183. }
  184. return mid;
  185. }
  186. }
  187. return low;
  188. }
  189. acceptEdit(range, eolCount, firstLineLength, lastLineLength, firstCharCode) {
  190. for (const piece of this._pieces) {
  191. piece.acceptEdit(range, eolCount, firstLineLength, lastLineLength, firstCharCode);
  192. }
  193. }
  194. }