monospaceLineBreaksComputer.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 strings from '../../../base/common/strings.js';
  6. import { CharacterClassifier } from '../core/characterClassifier.js';
  7. import { LineInjectedText } from '../model/textModelEvents.js';
  8. import { ModelLineProjectionData } from './modelLineProjectionData.js';
  9. export class MonospaceLineBreaksComputerFactory {
  10. constructor(breakBeforeChars, breakAfterChars) {
  11. this.classifier = new WrappingCharacterClassifier(breakBeforeChars, breakAfterChars);
  12. }
  13. static create(options) {
  14. return new MonospaceLineBreaksComputerFactory(options.get(119 /* wordWrapBreakBeforeCharacters */), options.get(118 /* wordWrapBreakAfterCharacters */));
  15. }
  16. createLineBreaksComputer(fontInfo, tabSize, wrappingColumn, wrappingIndent) {
  17. const requests = [];
  18. const injectedTexts = [];
  19. const previousBreakingData = [];
  20. return {
  21. addRequest: (lineText, injectedText, previousLineBreakData) => {
  22. requests.push(lineText);
  23. injectedTexts.push(injectedText);
  24. previousBreakingData.push(previousLineBreakData);
  25. },
  26. finalize: () => {
  27. const columnsForFullWidthChar = fontInfo.typicalFullwidthCharacterWidth / fontInfo.typicalHalfwidthCharacterWidth;
  28. let result = [];
  29. for (let i = 0, len = requests.length; i < len; i++) {
  30. const injectedText = injectedTexts[i];
  31. const previousLineBreakData = previousBreakingData[i];
  32. if (previousLineBreakData && !previousLineBreakData.injectionOptions && !injectedText) {
  33. result[i] = createLineBreaksFromPreviousLineBreaks(this.classifier, previousLineBreakData, requests[i], tabSize, wrappingColumn, columnsForFullWidthChar, wrappingIndent);
  34. }
  35. else {
  36. result[i] = createLineBreaks(this.classifier, requests[i], injectedText, tabSize, wrappingColumn, columnsForFullWidthChar, wrappingIndent);
  37. }
  38. }
  39. arrPool1.length = 0;
  40. arrPool2.length = 0;
  41. return result;
  42. }
  43. };
  44. }
  45. }
  46. class WrappingCharacterClassifier extends CharacterClassifier {
  47. constructor(BREAK_BEFORE, BREAK_AFTER) {
  48. super(0 /* NONE */);
  49. for (let i = 0; i < BREAK_BEFORE.length; i++) {
  50. this.set(BREAK_BEFORE.charCodeAt(i), 1 /* BREAK_BEFORE */);
  51. }
  52. for (let i = 0; i < BREAK_AFTER.length; i++) {
  53. this.set(BREAK_AFTER.charCodeAt(i), 2 /* BREAK_AFTER */);
  54. }
  55. }
  56. get(charCode) {
  57. if (charCode >= 0 && charCode < 256) {
  58. return this._asciiMap[charCode];
  59. }
  60. else {
  61. // Initialize CharacterClass.BREAK_IDEOGRAPHIC for these Unicode ranges:
  62. // 1. CJK Unified Ideographs (0x4E00 -- 0x9FFF)
  63. // 2. CJK Unified Ideographs Extension A (0x3400 -- 0x4DBF)
  64. // 3. Hiragana and Katakana (0x3040 -- 0x30FF)
  65. if ((charCode >= 0x3040 && charCode <= 0x30FF)
  66. || (charCode >= 0x3400 && charCode <= 0x4DBF)
  67. || (charCode >= 0x4E00 && charCode <= 0x9FFF)) {
  68. return 3 /* BREAK_IDEOGRAPHIC */;
  69. }
  70. return (this._map.get(charCode) || this._defaultValue);
  71. }
  72. }
  73. }
  74. let arrPool1 = [];
  75. let arrPool2 = [];
  76. function createLineBreaksFromPreviousLineBreaks(classifier, previousBreakingData, lineText, tabSize, firstLineBreakColumn, columnsForFullWidthChar, wrappingIndent) {
  77. if (firstLineBreakColumn === -1) {
  78. return null;
  79. }
  80. const len = lineText.length;
  81. if (len <= 1) {
  82. return null;
  83. }
  84. const prevBreakingOffsets = previousBreakingData.breakOffsets;
  85. const prevBreakingOffsetsVisibleColumn = previousBreakingData.breakOffsetsVisibleColumn;
  86. const wrappedTextIndentLength = computeWrappedTextIndentLength(lineText, tabSize, firstLineBreakColumn, columnsForFullWidthChar, wrappingIndent);
  87. const wrappedLineBreakColumn = firstLineBreakColumn - wrappedTextIndentLength;
  88. let breakingOffsets = arrPool1;
  89. let breakingOffsetsVisibleColumn = arrPool2;
  90. let breakingOffsetsCount = 0;
  91. let lastBreakingOffset = 0;
  92. let lastBreakingOffsetVisibleColumn = 0;
  93. let breakingColumn = firstLineBreakColumn;
  94. const prevLen = prevBreakingOffsets.length;
  95. let prevIndex = 0;
  96. if (prevIndex >= 0) {
  97. let bestDistance = Math.abs(prevBreakingOffsetsVisibleColumn[prevIndex] - breakingColumn);
  98. while (prevIndex + 1 < prevLen) {
  99. const distance = Math.abs(prevBreakingOffsetsVisibleColumn[prevIndex + 1] - breakingColumn);
  100. if (distance >= bestDistance) {
  101. break;
  102. }
  103. bestDistance = distance;
  104. prevIndex++;
  105. }
  106. }
  107. while (prevIndex < prevLen) {
  108. // Allow for prevIndex to be -1 (for the case where we hit a tab when walking backwards from the first break)
  109. let prevBreakOffset = prevIndex < 0 ? 0 : prevBreakingOffsets[prevIndex];
  110. let prevBreakOffsetVisibleColumn = prevIndex < 0 ? 0 : prevBreakingOffsetsVisibleColumn[prevIndex];
  111. if (lastBreakingOffset > prevBreakOffset) {
  112. prevBreakOffset = lastBreakingOffset;
  113. prevBreakOffsetVisibleColumn = lastBreakingOffsetVisibleColumn;
  114. }
  115. let breakOffset = 0;
  116. let breakOffsetVisibleColumn = 0;
  117. let forcedBreakOffset = 0;
  118. let forcedBreakOffsetVisibleColumn = 0;
  119. // initially, we search as much as possible to the right (if it fits)
  120. if (prevBreakOffsetVisibleColumn <= breakingColumn) {
  121. let visibleColumn = prevBreakOffsetVisibleColumn;
  122. let prevCharCode = prevBreakOffset === 0 ? 0 /* Null */ : lineText.charCodeAt(prevBreakOffset - 1);
  123. let prevCharCodeClass = prevBreakOffset === 0 ? 0 /* NONE */ : classifier.get(prevCharCode);
  124. let entireLineFits = true;
  125. for (let i = prevBreakOffset; i < len; i++) {
  126. const charStartOffset = i;
  127. const charCode = lineText.charCodeAt(i);
  128. let charCodeClass;
  129. let charWidth;
  130. if (strings.isHighSurrogate(charCode)) {
  131. // A surrogate pair must always be considered as a single unit, so it is never to be broken
  132. i++;
  133. charCodeClass = 0 /* NONE */;
  134. charWidth = 2;
  135. }
  136. else {
  137. charCodeClass = classifier.get(charCode);
  138. charWidth = computeCharWidth(charCode, visibleColumn, tabSize, columnsForFullWidthChar);
  139. }
  140. if (charStartOffset > lastBreakingOffset && canBreak(prevCharCode, prevCharCodeClass, charCode, charCodeClass)) {
  141. breakOffset = charStartOffset;
  142. breakOffsetVisibleColumn = visibleColumn;
  143. }
  144. visibleColumn += charWidth;
  145. // check if adding character at `i` will go over the breaking column
  146. if (visibleColumn > breakingColumn) {
  147. // We need to break at least before character at `i`:
  148. if (charStartOffset > lastBreakingOffset) {
  149. forcedBreakOffset = charStartOffset;
  150. forcedBreakOffsetVisibleColumn = visibleColumn - charWidth;
  151. }
  152. else {
  153. // we need to advance at least by one character
  154. forcedBreakOffset = i + 1;
  155. forcedBreakOffsetVisibleColumn = visibleColumn;
  156. }
  157. if (visibleColumn - breakOffsetVisibleColumn > wrappedLineBreakColumn) {
  158. // Cannot break at `breakOffset` => reset it if it was set
  159. breakOffset = 0;
  160. }
  161. entireLineFits = false;
  162. break;
  163. }
  164. prevCharCode = charCode;
  165. prevCharCodeClass = charCodeClass;
  166. }
  167. if (entireLineFits) {
  168. // there is no more need to break => stop the outer loop!
  169. if (breakingOffsetsCount > 0) {
  170. // Add last segment, no need to assign to `lastBreakingOffset` and `lastBreakingOffsetVisibleColumn`
  171. breakingOffsets[breakingOffsetsCount] = prevBreakingOffsets[prevBreakingOffsets.length - 1];
  172. breakingOffsetsVisibleColumn[breakingOffsetsCount] = prevBreakingOffsetsVisibleColumn[prevBreakingOffsets.length - 1];
  173. breakingOffsetsCount++;
  174. }
  175. break;
  176. }
  177. }
  178. if (breakOffset === 0) {
  179. // must search left
  180. let visibleColumn = prevBreakOffsetVisibleColumn;
  181. let charCode = lineText.charCodeAt(prevBreakOffset);
  182. let charCodeClass = classifier.get(charCode);
  183. let hitATabCharacter = false;
  184. for (let i = prevBreakOffset - 1; i >= lastBreakingOffset; i--) {
  185. const charStartOffset = i + 1;
  186. const prevCharCode = lineText.charCodeAt(i);
  187. if (prevCharCode === 9 /* Tab */) {
  188. // cannot determine the width of a tab when going backwards, so we must go forwards
  189. hitATabCharacter = true;
  190. break;
  191. }
  192. let prevCharCodeClass;
  193. let prevCharWidth;
  194. if (strings.isLowSurrogate(prevCharCode)) {
  195. // A surrogate pair must always be considered as a single unit, so it is never to be broken
  196. i--;
  197. prevCharCodeClass = 0 /* NONE */;
  198. prevCharWidth = 2;
  199. }
  200. else {
  201. prevCharCodeClass = classifier.get(prevCharCode);
  202. prevCharWidth = (strings.isFullWidthCharacter(prevCharCode) ? columnsForFullWidthChar : 1);
  203. }
  204. if (visibleColumn <= breakingColumn) {
  205. if (forcedBreakOffset === 0) {
  206. forcedBreakOffset = charStartOffset;
  207. forcedBreakOffsetVisibleColumn = visibleColumn;
  208. }
  209. if (visibleColumn <= breakingColumn - wrappedLineBreakColumn) {
  210. // went too far!
  211. break;
  212. }
  213. if (canBreak(prevCharCode, prevCharCodeClass, charCode, charCodeClass)) {
  214. breakOffset = charStartOffset;
  215. breakOffsetVisibleColumn = visibleColumn;
  216. break;
  217. }
  218. }
  219. visibleColumn -= prevCharWidth;
  220. charCode = prevCharCode;
  221. charCodeClass = prevCharCodeClass;
  222. }
  223. if (breakOffset !== 0) {
  224. const remainingWidthOfNextLine = wrappedLineBreakColumn - (forcedBreakOffsetVisibleColumn - breakOffsetVisibleColumn);
  225. if (remainingWidthOfNextLine <= tabSize) {
  226. const charCodeAtForcedBreakOffset = lineText.charCodeAt(forcedBreakOffset);
  227. let charWidth;
  228. if (strings.isHighSurrogate(charCodeAtForcedBreakOffset)) {
  229. // A surrogate pair must always be considered as a single unit, so it is never to be broken
  230. charWidth = 2;
  231. }
  232. else {
  233. charWidth = computeCharWidth(charCodeAtForcedBreakOffset, forcedBreakOffsetVisibleColumn, tabSize, columnsForFullWidthChar);
  234. }
  235. if (remainingWidthOfNextLine - charWidth < 0) {
  236. // it is not worth it to break at breakOffset, it just introduces an extra needless line!
  237. breakOffset = 0;
  238. }
  239. }
  240. }
  241. if (hitATabCharacter) {
  242. // cannot determine the width of a tab when going backwards, so we must go forwards from the previous break
  243. prevIndex--;
  244. continue;
  245. }
  246. }
  247. if (breakOffset === 0) {
  248. // Could not find a good breaking point
  249. breakOffset = forcedBreakOffset;
  250. breakOffsetVisibleColumn = forcedBreakOffsetVisibleColumn;
  251. }
  252. if (breakOffset <= lastBreakingOffset) {
  253. // Make sure that we are advancing (at least one character)
  254. const charCode = lineText.charCodeAt(lastBreakingOffset);
  255. if (strings.isHighSurrogate(charCode)) {
  256. // A surrogate pair must always be considered as a single unit, so it is never to be broken
  257. breakOffset = lastBreakingOffset + 2;
  258. breakOffsetVisibleColumn = lastBreakingOffsetVisibleColumn + 2;
  259. }
  260. else {
  261. breakOffset = lastBreakingOffset + 1;
  262. breakOffsetVisibleColumn = lastBreakingOffsetVisibleColumn + computeCharWidth(charCode, lastBreakingOffsetVisibleColumn, tabSize, columnsForFullWidthChar);
  263. }
  264. }
  265. lastBreakingOffset = breakOffset;
  266. breakingOffsets[breakingOffsetsCount] = breakOffset;
  267. lastBreakingOffsetVisibleColumn = breakOffsetVisibleColumn;
  268. breakingOffsetsVisibleColumn[breakingOffsetsCount] = breakOffsetVisibleColumn;
  269. breakingOffsetsCount++;
  270. breakingColumn = breakOffsetVisibleColumn + wrappedLineBreakColumn;
  271. while (prevIndex < 0 || (prevIndex < prevLen && prevBreakingOffsetsVisibleColumn[prevIndex] < breakOffsetVisibleColumn)) {
  272. prevIndex++;
  273. }
  274. let bestDistance = Math.abs(prevBreakingOffsetsVisibleColumn[prevIndex] - breakingColumn);
  275. while (prevIndex + 1 < prevLen) {
  276. const distance = Math.abs(prevBreakingOffsetsVisibleColumn[prevIndex + 1] - breakingColumn);
  277. if (distance >= bestDistance) {
  278. break;
  279. }
  280. bestDistance = distance;
  281. prevIndex++;
  282. }
  283. }
  284. if (breakingOffsetsCount === 0) {
  285. return null;
  286. }
  287. // Doing here some object reuse which ends up helping a huge deal with GC pauses!
  288. breakingOffsets.length = breakingOffsetsCount;
  289. breakingOffsetsVisibleColumn.length = breakingOffsetsCount;
  290. arrPool1 = previousBreakingData.breakOffsets;
  291. arrPool2 = previousBreakingData.breakOffsetsVisibleColumn;
  292. previousBreakingData.breakOffsets = breakingOffsets;
  293. previousBreakingData.breakOffsetsVisibleColumn = breakingOffsetsVisibleColumn;
  294. previousBreakingData.wrappedTextIndentLength = wrappedTextIndentLength;
  295. return previousBreakingData;
  296. }
  297. function createLineBreaks(classifier, _lineText, injectedTexts, tabSize, firstLineBreakColumn, columnsForFullWidthChar, wrappingIndent) {
  298. const lineText = LineInjectedText.applyInjectedText(_lineText, injectedTexts);
  299. let injectionOptions;
  300. let injectionOffsets;
  301. if (injectedTexts && injectedTexts.length > 0) {
  302. injectionOptions = injectedTexts.map(t => t.options);
  303. injectionOffsets = injectedTexts.map(text => text.column - 1);
  304. }
  305. else {
  306. injectionOptions = null;
  307. injectionOffsets = null;
  308. }
  309. if (firstLineBreakColumn === -1) {
  310. if (!injectionOptions) {
  311. return null;
  312. }
  313. // creating a `LineBreakData` with an invalid `breakOffsetsVisibleColumn` is OK
  314. // because `breakOffsetsVisibleColumn` will never be used because it contains injected text
  315. return new ModelLineProjectionData(injectionOffsets, injectionOptions, [lineText.length], [], 0);
  316. }
  317. const len = lineText.length;
  318. if (len <= 1) {
  319. if (!injectionOptions) {
  320. return null;
  321. }
  322. // creating a `LineBreakData` with an invalid `breakOffsetsVisibleColumn` is OK
  323. // because `breakOffsetsVisibleColumn` will never be used because it contains injected text
  324. return new ModelLineProjectionData(injectionOffsets, injectionOptions, [lineText.length], [], 0);
  325. }
  326. const wrappedTextIndentLength = computeWrappedTextIndentLength(lineText, tabSize, firstLineBreakColumn, columnsForFullWidthChar, wrappingIndent);
  327. const wrappedLineBreakColumn = firstLineBreakColumn - wrappedTextIndentLength;
  328. let breakingOffsets = [];
  329. let breakingOffsetsVisibleColumn = [];
  330. let breakingOffsetsCount = 0;
  331. let breakOffset = 0;
  332. let breakOffsetVisibleColumn = 0;
  333. let breakingColumn = firstLineBreakColumn;
  334. let prevCharCode = lineText.charCodeAt(0);
  335. let prevCharCodeClass = classifier.get(prevCharCode);
  336. let visibleColumn = computeCharWidth(prevCharCode, 0, tabSize, columnsForFullWidthChar);
  337. let startOffset = 1;
  338. if (strings.isHighSurrogate(prevCharCode)) {
  339. // A surrogate pair must always be considered as a single unit, so it is never to be broken
  340. visibleColumn += 1;
  341. prevCharCode = lineText.charCodeAt(1);
  342. prevCharCodeClass = classifier.get(prevCharCode);
  343. startOffset++;
  344. }
  345. for (let i = startOffset; i < len; i++) {
  346. const charStartOffset = i;
  347. const charCode = lineText.charCodeAt(i);
  348. let charCodeClass;
  349. let charWidth;
  350. if (strings.isHighSurrogate(charCode)) {
  351. // A surrogate pair must always be considered as a single unit, so it is never to be broken
  352. i++;
  353. charCodeClass = 0 /* NONE */;
  354. charWidth = 2;
  355. }
  356. else {
  357. charCodeClass = classifier.get(charCode);
  358. charWidth = computeCharWidth(charCode, visibleColumn, tabSize, columnsForFullWidthChar);
  359. }
  360. if (canBreak(prevCharCode, prevCharCodeClass, charCode, charCodeClass)) {
  361. breakOffset = charStartOffset;
  362. breakOffsetVisibleColumn = visibleColumn;
  363. }
  364. visibleColumn += charWidth;
  365. // check if adding character at `i` will go over the breaking column
  366. if (visibleColumn > breakingColumn) {
  367. // We need to break at least before character at `i`:
  368. if (breakOffset === 0 || visibleColumn - breakOffsetVisibleColumn > wrappedLineBreakColumn) {
  369. // Cannot break at `breakOffset`, must break at `i`
  370. breakOffset = charStartOffset;
  371. breakOffsetVisibleColumn = visibleColumn - charWidth;
  372. }
  373. breakingOffsets[breakingOffsetsCount] = breakOffset;
  374. breakingOffsetsVisibleColumn[breakingOffsetsCount] = breakOffsetVisibleColumn;
  375. breakingOffsetsCount++;
  376. breakingColumn = breakOffsetVisibleColumn + wrappedLineBreakColumn;
  377. breakOffset = 0;
  378. }
  379. prevCharCode = charCode;
  380. prevCharCodeClass = charCodeClass;
  381. }
  382. if (breakingOffsetsCount === 0 && (!injectedTexts || injectedTexts.length === 0)) {
  383. return null;
  384. }
  385. // Add last segment
  386. breakingOffsets[breakingOffsetsCount] = len;
  387. breakingOffsetsVisibleColumn[breakingOffsetsCount] = visibleColumn;
  388. return new ModelLineProjectionData(injectionOffsets, injectionOptions, breakingOffsets, breakingOffsetsVisibleColumn, wrappedTextIndentLength);
  389. }
  390. function computeCharWidth(charCode, visibleColumn, tabSize, columnsForFullWidthChar) {
  391. if (charCode === 9 /* Tab */) {
  392. return (tabSize - (visibleColumn % tabSize));
  393. }
  394. if (strings.isFullWidthCharacter(charCode)) {
  395. return columnsForFullWidthChar;
  396. }
  397. if (charCode < 32) {
  398. // when using `editor.renderControlCharacters`, the substitutions are often wide
  399. return columnsForFullWidthChar;
  400. }
  401. return 1;
  402. }
  403. function tabCharacterWidth(visibleColumn, tabSize) {
  404. return (tabSize - (visibleColumn % tabSize));
  405. }
  406. /**
  407. * Kinsoku Shori : Don't break after a leading character, like an open bracket
  408. * Kinsoku Shori : Don't break before a trailing character, like a period
  409. */
  410. function canBreak(prevCharCode, prevCharCodeClass, charCode, charCodeClass) {
  411. return (charCode !== 32 /* Space */
  412. && ((prevCharCodeClass === 2 /* BREAK_AFTER */)
  413. || (prevCharCodeClass === 3 /* BREAK_IDEOGRAPHIC */ && charCodeClass !== 2 /* BREAK_AFTER */)
  414. || (charCodeClass === 1 /* BREAK_BEFORE */)
  415. || (charCodeClass === 3 /* BREAK_IDEOGRAPHIC */ && prevCharCodeClass !== 1 /* BREAK_BEFORE */)));
  416. }
  417. function computeWrappedTextIndentLength(lineText, tabSize, firstLineBreakColumn, columnsForFullWidthChar, wrappingIndent) {
  418. let wrappedTextIndentLength = 0;
  419. if (wrappingIndent !== 0 /* None */) {
  420. const firstNonWhitespaceIndex = strings.firstNonWhitespaceIndex(lineText);
  421. if (firstNonWhitespaceIndex !== -1) {
  422. // Track existing indent
  423. for (let i = 0; i < firstNonWhitespaceIndex; i++) {
  424. const charWidth = (lineText.charCodeAt(i) === 9 /* Tab */ ? tabCharacterWidth(wrappedTextIndentLength, tabSize) : 1);
  425. wrappedTextIndentLength += charWidth;
  426. }
  427. // Increase indent of continuation lines, if desired
  428. const numberOfAdditionalTabs = (wrappingIndent === 3 /* DeepIndent */ ? 2 : wrappingIndent === 2 /* Indent */ ? 1 : 0);
  429. for (let i = 0; i < numberOfAdditionalTabs; i++) {
  430. const charWidth = tabCharacterWidth(wrappedTextIndentLength, tabSize);
  431. wrappedTextIndentLength += charWidth;
  432. }
  433. // Force sticking to beginning of line if no character would fit except for the indentation
  434. if (wrappedTextIndentLength + columnsForFullWidthChar > firstLineBreakColumn) {
  435. wrappedTextIndentLength = 0;
  436. }
  437. }
  438. }
  439. return wrappedTextIndentLength;
  440. }