tokenizationRegistry.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. import { Emitter } from '../../base/common/event.js';
  15. import { Disposable, toDisposable } from '../../base/common/lifecycle.js';
  16. export class TokenizationRegistry {
  17. constructor() {
  18. this._tokenizationSupports = new Map();
  19. this._factories = new Map();
  20. this._onDidChange = new Emitter();
  21. this.onDidChange = this._onDidChange.event;
  22. this._colorMap = null;
  23. }
  24. handleChange(languageIds) {
  25. this._onDidChange.fire({
  26. changedLanguages: languageIds,
  27. changedColorMap: false
  28. });
  29. }
  30. register(languageId, support) {
  31. this._tokenizationSupports.set(languageId, support);
  32. this.handleChange([languageId]);
  33. return toDisposable(() => {
  34. if (this._tokenizationSupports.get(languageId) !== support) {
  35. return;
  36. }
  37. this._tokenizationSupports.delete(languageId);
  38. this.handleChange([languageId]);
  39. });
  40. }
  41. get(languageId) {
  42. return this._tokenizationSupports.get(languageId) || null;
  43. }
  44. registerFactory(languageId, factory) {
  45. var _a;
  46. (_a = this._factories.get(languageId)) === null || _a === void 0 ? void 0 : _a.dispose();
  47. const myData = new TokenizationSupportFactoryData(this, languageId, factory);
  48. this._factories.set(languageId, myData);
  49. return toDisposable(() => {
  50. const v = this._factories.get(languageId);
  51. if (!v || v !== myData) {
  52. return;
  53. }
  54. this._factories.delete(languageId);
  55. v.dispose();
  56. });
  57. }
  58. getOrCreate(languageId) {
  59. return __awaiter(this, void 0, void 0, function* () {
  60. // check first if the support is already set
  61. const tokenizationSupport = this.get(languageId);
  62. if (tokenizationSupport) {
  63. return tokenizationSupport;
  64. }
  65. const factory = this._factories.get(languageId);
  66. if (!factory || factory.isResolved) {
  67. // no factory or factory.resolve already finished
  68. return null;
  69. }
  70. yield factory.resolve();
  71. return this.get(languageId);
  72. });
  73. }
  74. isResolved(languageId) {
  75. const tokenizationSupport = this.get(languageId);
  76. if (tokenizationSupport) {
  77. return true;
  78. }
  79. const factory = this._factories.get(languageId);
  80. if (!factory || factory.isResolved) {
  81. return true;
  82. }
  83. return false;
  84. }
  85. setColorMap(colorMap) {
  86. this._colorMap = colorMap;
  87. this._onDidChange.fire({
  88. changedLanguages: Array.from(this._tokenizationSupports.keys()),
  89. changedColorMap: true
  90. });
  91. }
  92. getColorMap() {
  93. return this._colorMap;
  94. }
  95. getDefaultBackground() {
  96. if (this._colorMap && this._colorMap.length > 2 /* ColorId.DefaultBackground */) {
  97. return this._colorMap[2 /* ColorId.DefaultBackground */];
  98. }
  99. return null;
  100. }
  101. }
  102. class TokenizationSupportFactoryData extends Disposable {
  103. get isResolved() {
  104. return this._isResolved;
  105. }
  106. constructor(_registry, _languageId, _factory) {
  107. super();
  108. this._registry = _registry;
  109. this._languageId = _languageId;
  110. this._factory = _factory;
  111. this._isDisposed = false;
  112. this._resolvePromise = null;
  113. this._isResolved = false;
  114. }
  115. dispose() {
  116. this._isDisposed = true;
  117. super.dispose();
  118. }
  119. resolve() {
  120. return __awaiter(this, void 0, void 0, function* () {
  121. if (!this._resolvePromise) {
  122. this._resolvePromise = this._create();
  123. }
  124. return this._resolvePromise;
  125. });
  126. }
  127. _create() {
  128. return __awaiter(this, void 0, void 0, function* () {
  129. const value = yield this._factory.tokenizationSupport;
  130. this._isResolved = true;
  131. if (value && !this._isDisposed) {
  132. this._register(this._registry.register(this._languageId, value));
  133. }
  134. });
  135. }
  136. }