tokenizationRegistry.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 { Emitter } from '../../../base/common/event.js';
  6. import { toDisposable } from '../../../base/common/lifecycle.js';
  7. export class TokenizationRegistryImpl {
  8. constructor() {
  9. this._map = new Map();
  10. this._promises = new Map();
  11. this._onDidChange = new Emitter();
  12. this.onDidChange = this._onDidChange.event;
  13. this._colorMap = null;
  14. }
  15. fire(languages) {
  16. this._onDidChange.fire({
  17. changedLanguages: languages,
  18. changedColorMap: false
  19. });
  20. }
  21. register(language, support) {
  22. this._map.set(language, support);
  23. this.fire([language]);
  24. return toDisposable(() => {
  25. if (this._map.get(language) !== support) {
  26. return;
  27. }
  28. this._map.delete(language);
  29. this.fire([language]);
  30. });
  31. }
  32. registerPromise(language, supportPromise) {
  33. let registration = null;
  34. let isDisposed = false;
  35. this._promises.set(language, supportPromise.then(support => {
  36. this._promises.delete(language);
  37. if (isDisposed || !support) {
  38. return;
  39. }
  40. registration = this.register(language, support);
  41. }));
  42. return toDisposable(() => {
  43. isDisposed = true;
  44. if (registration) {
  45. registration.dispose();
  46. }
  47. });
  48. }
  49. getPromise(language) {
  50. const support = this.get(language);
  51. if (support) {
  52. return Promise.resolve(support);
  53. }
  54. const promise = this._promises.get(language);
  55. if (promise) {
  56. return promise.then(_ => this.get(language));
  57. }
  58. return null;
  59. }
  60. get(language) {
  61. return (this._map.get(language) || null);
  62. }
  63. setColorMap(colorMap) {
  64. this._colorMap = colorMap;
  65. this._onDidChange.fire({
  66. changedLanguages: Array.from(this._map.keys()),
  67. changedColorMap: true
  68. });
  69. }
  70. getColorMap() {
  71. return this._colorMap;
  72. }
  73. getDefaultBackground() {
  74. if (this._colorMap && this._colorMap.length > 2 /* DefaultBackground */) {
  75. return this._colorMap[2 /* DefaultBackground */];
  76. }
  77. return null;
  78. }
  79. }