f35937b14c518218057f13d2015f5b163e41d7637b2a87d942c12f128b91ef9710a535140789bd53fc5b5e1ca380a26c7ec3eca3b4495841052cc5d1cfaf5c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  6. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  7. return new (P || (P = Promise))(function (resolve, reject) {
  8. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  9. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  10. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  11. step((generator = generator.apply(thisArg, _arguments || [])).next());
  12. });
  13. };
  14. var _a;
  15. import * as strings from '../../../base/common/strings.js';
  16. import { LineTokens } from '../../common/tokens/lineTokens.js';
  17. import { TokenizationRegistry } from '../../common/languages.js';
  18. import { RenderLineInput, renderViewLine2 as renderViewLine } from '../../common/viewLayout/viewLineRenderer.js';
  19. import { ViewLineRenderingData } from '../../common/viewModel.js';
  20. import { MonarchTokenizer } from '../common/monarch/monarchLexer.js';
  21. const ttPolicy = (_a = window.trustedTypes) === null || _a === void 0 ? void 0 : _a.createPolicy('standaloneColorizer', { createHTML: value => value });
  22. export class Colorizer {
  23. static colorizeElement(themeService, languageService, domNode, options) {
  24. options = options || {};
  25. const theme = options.theme || 'vs';
  26. const mimeType = options.mimeType || domNode.getAttribute('lang') || domNode.getAttribute('data-lang');
  27. if (!mimeType) {
  28. console.error('Mode not detected');
  29. return Promise.resolve();
  30. }
  31. const languageId = languageService.getLanguageIdByMimeType(mimeType) || mimeType;
  32. themeService.setTheme(theme);
  33. const text = domNode.firstChild ? domNode.firstChild.nodeValue : '';
  34. domNode.className += ' ' + theme;
  35. const render = (str) => {
  36. var _a;
  37. const trustedhtml = (_a = ttPolicy === null || ttPolicy === void 0 ? void 0 : ttPolicy.createHTML(str)) !== null && _a !== void 0 ? _a : str;
  38. domNode.innerHTML = trustedhtml;
  39. };
  40. return this.colorize(languageService, text || '', languageId, options).then(render, (err) => console.error(err));
  41. }
  42. static colorize(languageService, text, languageId, options) {
  43. return __awaiter(this, void 0, void 0, function* () {
  44. const languageIdCodec = languageService.languageIdCodec;
  45. let tabSize = 4;
  46. if (options && typeof options.tabSize === 'number') {
  47. tabSize = options.tabSize;
  48. }
  49. if (strings.startsWithUTF8BOM(text)) {
  50. text = text.substr(1);
  51. }
  52. const lines = strings.splitLines(text);
  53. if (!languageService.isRegisteredLanguageId(languageId)) {
  54. return _fakeColorize(lines, tabSize, languageIdCodec);
  55. }
  56. const tokenizationSupport = yield TokenizationRegistry.getOrCreate(languageId);
  57. if (tokenizationSupport) {
  58. return _colorize(lines, tabSize, tokenizationSupport, languageIdCodec);
  59. }
  60. return _fakeColorize(lines, tabSize, languageIdCodec);
  61. });
  62. }
  63. static colorizeLine(line, mightContainNonBasicASCII, mightContainRTL, tokens, tabSize = 4) {
  64. const isBasicASCII = ViewLineRenderingData.isBasicASCII(line, mightContainNonBasicASCII);
  65. const containsRTL = ViewLineRenderingData.containsRTL(line, isBasicASCII, mightContainRTL);
  66. const renderResult = renderViewLine(new RenderLineInput(false, true, line, false, isBasicASCII, containsRTL, 0, tokens, [], tabSize, 0, 0, 0, 0, -1, 'none', false, false, null));
  67. return renderResult.html;
  68. }
  69. static colorizeModelLine(model, lineNumber, tabSize = 4) {
  70. const content = model.getLineContent(lineNumber);
  71. model.tokenization.forceTokenization(lineNumber);
  72. const tokens = model.tokenization.getLineTokens(lineNumber);
  73. const inflatedTokens = tokens.inflate();
  74. return this.colorizeLine(content, model.mightContainNonBasicASCII(), model.mightContainRTL(), inflatedTokens, tabSize);
  75. }
  76. }
  77. function _colorize(lines, tabSize, tokenizationSupport, languageIdCodec) {
  78. return new Promise((c, e) => {
  79. const execute = () => {
  80. const result = _actualColorize(lines, tabSize, tokenizationSupport, languageIdCodec);
  81. if (tokenizationSupport instanceof MonarchTokenizer) {
  82. const status = tokenizationSupport.getLoadStatus();
  83. if (status.loaded === false) {
  84. status.promise.then(execute, e);
  85. return;
  86. }
  87. }
  88. c(result);
  89. };
  90. execute();
  91. });
  92. }
  93. function _fakeColorize(lines, tabSize, languageIdCodec) {
  94. let html = [];
  95. const defaultMetadata = ((0 /* FontStyle.None */ << 11 /* MetadataConsts.FONT_STYLE_OFFSET */)
  96. | (1 /* ColorId.DefaultForeground */ << 15 /* MetadataConsts.FOREGROUND_OFFSET */)
  97. | (2 /* ColorId.DefaultBackground */ << 24 /* MetadataConsts.BACKGROUND_OFFSET */)) >>> 0;
  98. const tokens = new Uint32Array(2);
  99. tokens[0] = 0;
  100. tokens[1] = defaultMetadata;
  101. for (let i = 0, length = lines.length; i < length; i++) {
  102. const line = lines[i];
  103. tokens[0] = line.length;
  104. const lineTokens = new LineTokens(tokens, line, languageIdCodec);
  105. const isBasicASCII = ViewLineRenderingData.isBasicASCII(line, /* check for basic ASCII */ true);
  106. const containsRTL = ViewLineRenderingData.containsRTL(line, isBasicASCII, /* check for RTL */ true);
  107. const renderResult = renderViewLine(new RenderLineInput(false, true, line, false, isBasicASCII, containsRTL, 0, lineTokens, [], tabSize, 0, 0, 0, 0, -1, 'none', false, false, null));
  108. html = html.concat(renderResult.html);
  109. html.push('<br/>');
  110. }
  111. return html.join('');
  112. }
  113. function _actualColorize(lines, tabSize, tokenizationSupport, languageIdCodec) {
  114. let html = [];
  115. let state = tokenizationSupport.getInitialState();
  116. for (let i = 0, length = lines.length; i < length; i++) {
  117. const line = lines[i];
  118. const tokenizeResult = tokenizationSupport.tokenizeEncoded(line, true, state);
  119. LineTokens.convertToEndOffset(tokenizeResult.tokens, line.length);
  120. const lineTokens = new LineTokens(tokenizeResult.tokens, line, languageIdCodec);
  121. const isBasicASCII = ViewLineRenderingData.isBasicASCII(line, /* check for basic ASCII */ true);
  122. const containsRTL = ViewLineRenderingData.containsRTL(line, isBasicASCII, /* check for RTL */ true);
  123. const renderResult = renderViewLine(new RenderLineInput(false, true, line, false, isBasicASCII, containsRTL, 0, lineTokens.inflate(), [], tabSize, 0, 0, 0, 0, -1, 'none', false, false, null));
  124. html = html.concat(renderResult.html);
  125. html.push('<br/>');
  126. state = tokenizeResult.endState;
  127. }
  128. return html.join('');
  129. }