src/vs/editor/common/tokenizationRegistry.ts

152 LOC · 89 covered · 63 uncovered · 24 ranges · 2653 concepts · 6 introducers · 1317 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- languages.ts ×27
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 >
6 > import { Color } from '../../base/common/color.js';
7 > import { Emitter, Event } from '../../base/common/event.js';
8 > import { Disposable, IDisposable, toDisposable } from '../../base/common/lifecycle.js';
9 > import { ITokenizationRegistry, ITokenizationSupportChangedEvent, ILazyTokenizationSupport } from './languages.js';
10 > import { ColorId } from './encodedTokenAttributes.js';
11 >
12 > export class TokenizationRegistry<TSupport> implements ITokenizationRegistry<TSupport> {
13 >
14 > private readonly _tokenizationSupports = new Map<string, TSupport>();
15 > private readonly _factories = new Map<string, TokenizationSupportFactoryData<TSupport>>();
16 >
17 > private readonly _onDidChange = new Emitter<ITokenizationSupportChangedEvent>();
18 > public readonly onDidChange: Event<ITokenizationSupportChangedEvent> = this._onDidChange.event;
19 >
20 > private _colorMap: Color[] | null;
21 >
22 > constructor() {
23 > this._colorMap = null;
24 > }
25 >
26 > public handleChange(languageIds: string[]): void {
27 > this._onDidChange.fire({ tokenizationRegistry.ts ×3
28 > changedLanguages: languageIds,
29 > changedColorMap: false
30 > });
31 > }
33 > public register(languageId: string, support: TSupport): IDisposable {
34 > this._tokenizationSupports.set(languageId, support); tokenizationRegistry.ts ×3
35 > this.handleChange([languageId]);
36 > return toDisposable(() => {
37 > if (this._tokenizationSupports.get(languageId) !== support) {
38 return;
39 }
40 > this._tokenizationSupports.delete(languageId); tokenizationRegistry.ts ×3
41 > this.handleChange([languageId]);
42 > });
43 > }
45 > public get(languageId: string): TSupport | null {
46 > return this._tokenizationSupports.get(languageId) || null; tokenizationRegistry.ts ×1
47 > }
49 > public registerFactory(languageId: string, factory: ILazyTokenizationSupport<TSupport>): IDisposable {
50 this._factories.get(languageId)?.dispose();
51 const myData = new TokenizationSupportFactoryData(this, languageId, factory);
52 this._factories.set(languageId, myData);
53 return toDisposable(() => {
54 const v = this._factories.get(languageId);
55 if (!v || v !== myData) {
56 return;
57 }
58 this._factories.delete(languageId);
59 v.dispose();
60 });
61 }
63 > public async getOrCreate(languageId: string): Promise<TSupport | null> {
64 > // check first if the support is already set textModel.ts ×20
65 > const tokenizationSupport = this.get(languageId);
66 > if (tokenizationSupport) {
67 > return tokenizationSupport; tokenizerSyntaxTokenBackend.ts ×11
68 > }
70 > const factory = this._factories.get(languageId);
71 > if (!factory || factory.isResolved) { textModel.ts ×20
72 > // no factory or factory.resolve already finished tokenizationRegistry.ts ×2
73 > return null;
74 > }
75
76 await factory.resolve();
77
78 return this.get(languageId);
81 > public isResolved(languageId: string): boolean {
82 const tokenizationSupport = this.get(languageId);
83 if (tokenizationSupport) {
84 return true;
85 }
86
87 const factory = this._factories.get(languageId);
88 if (!factory || factory.isResolved) {
89 return true;
90 }
91
92 return false;
93 }
95 > public setColorMap(colorMap: Color[]): void {
96 this._colorMap = colorMap;
97 this._onDidChange.fire({
98 changedLanguages: Array.from(this._tokenizationSupports.keys()),
99 changedColorMap: true
100 });
101 }
103 > public getColorMap(): Color[] | null {
104 return this._colorMap;
105 }
107 > public getDefaultBackground(): Color | null {
108 if (this._colorMap && this._colorMap.length > ColorId.DefaultBackground) {
109 return this._colorMap[ColorId.DefaultBackground];
110 }
111 return null;
112 }
114 >
115 > class TokenizationSupportFactoryData<TSupport> extends Disposable {
116 >
117 > private _isDisposed: boolean = false;
118 > private _resolvePromise: Promise<void> | null = null;
119 > private _isResolved: boolean = false;
120 >
121 > public get isResolved(): boolean {
122 > return this._isResolved;
123 > }
124 >
125 > constructor(
126 private readonly _registry: TokenizationRegistry<TSupport>,
127 private readonly _languageId: string,
128 private readonly _factory: ILazyTokenizationSupport<TSupport>,
129 ) {
130 super();
131 }
133 > public override dispose(): void {
134 this._isDisposed = true;
135 super.dispose();
136 }
138 > public async resolve(): Promise<void> {
139 if (!this._resolvePromise) {
140 this._resolvePromise = this._create();
141 }
142 return this._resolvePromise;
143 }
145 > private async _create(): Promise<void> {
146 const value = await this._factory.tokenizationSupport;
147 this._isResolved = true;
148 if (value && !this._isDisposed) {
149 this._register(this._registry.register(this._languageId, value));
150 }
151 }