src/vs/platform/theme/common/tokenClassificationRegistry.ts

632 LOC · 539 covered · 93 uncovered · 44 ranges · 26 concepts · 7 introducers · 11 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.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the filesrc/vs/platform/extensionManagement/common/extensionGalleryManifestService.ts · 228 LOCcommon/extensionGalleryM…src/vs/platform/extensionResourceLoader/common/extensionResourceLoader.ts · 171 LOCcommon/extensionResource…src/vs/platform/extensionResourceLoader/common/extensionResourceLoaderService.ts · 46 LOCcommon/extensionResource…src/vs/workbench/services/themes/common/colorThemeData.ts · 1083 LOCcommon/colorThemeData.tssrc/vs/workbench/services/themes/common/plistParser.ts · 492 LOCcommon/plistParser.tssrc/vs/workbench/services/themes/common/textMateScopeMatcher.ts · 134 LOCcommon/textMateScopeMatc…src/vs/workbench/services/themes/common/themeCompatibility.ts · 76 LOCcommon/themeCompatibilit…src/vs/workbench/services/themes/common/workbenchThemeService.ts · 541 LOCcommon/workbenchThemeSer…colorThemeData.ts ×16 · 64 introduced LOCcolorThemeData.ts ×16tokenClassificationRegistry.ts ×1 · 3 introduced LOCtokenClassificationRegis…tokenClassificationRegistry.ts ×2 · 7 introduced LOCtokenClassificationRegis…tokenStyleResolving.test|title=Themes - TokenStyleResolving language|occurrence=1 · 0 introduced LOCtokenStyleResolving.test…colorThemeData.ts ×1 · 2 introduced LOCcolorThemeData.ts ×1colorThemeData.ts ×12 · 57 introduced LOCcolorThemeData.ts ×12tokenClassificationRegistry.ts ×2 · 6 introduced LOCtokenClassificationRegis…colorThemeData.ts ×5 · 21 introduced LOCcolorThemeData.ts ×5colorThemeData.ts ×1 · 6 introduced LOCcolorThemeData.ts ×1colorThemeData.ts ×33 · 242 introduced LOCcolorThemeData.ts ×33colorThemeData.ts ×2 · 5 introduced LOCcolorThemeData.ts ×2tokenStyleResolving.test|title=Themes - TokenStyleResolving resolveScopes - match most specific|occurrence=1 · 0 introduced LOCtokenStyleResolving.test…colorThemeData.ts ×5 · 33 introduced LOCcolorThemeData.ts ×5colorThemeData.ts ×1 · 4 introduced LOCcolorThemeData.ts ×1tokenClassificationRegistry.ts ×1 · 16 introduced LOCtokenClassificationRegis…colorThemeData.ts ×1 · 1 introduced LOCcolorThemeData.ts ×1colorThemeData.ts ×13 · 114 introduced LOCcolorThemeData.ts ×13colorThemeData.ts ×1 · 1 introduced LOCcolorThemeData.ts ×1colorThemeData.ts ×4 · 15 introduced LOCcolorThemeData.ts ×4colorThemeData.ts ×57 · 483 introduced LOCcolorThemeData.ts ×57themeConfiguration.ts ×5 · 13 introduced LOCthemeConfiguration.ts ×5workbenchThemeService.ts ×2 · 2 introduced LOCworkbenchThemeService.ts…workbenchThemeService.ts ×1 · 2 introduced LOCworkbenchThemeService.ts…workbenchThemeService.ts ×3 · 10 introduced LOCworkbenchThemeService.ts…themeConfiguration.ts ×22 · 550 introduced LOCthemeConfiguration.ts ×2…tokenClassificationRegistry.ts ×28 · 983 introduced LOCtokenClassificationRegis…workbenchThemeService.test|title=WorkbenchThemeService migrateThemeSettingsId migrates Default-prefixed theme IDs|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/services/themes/test/common/workbenchThemeService.test|title=WorkbenchThemeService migrateThemeSettingsId migrates Default-prefixed theme IDs|occurrence=1workbenchThemeService.te…workbenchThemeService.test|title=WorkbenchThemeService migrateThemeSettingsId migrates Experimental theme IDs to VS Code themes|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/services/themes/test/common/workbenchThemeService.test|title=WorkbenchThemeService migrateThemeSettingsId migrates Experimental theme IDs to VS Code themes|occurrence=1workbenchThemeService.te…workbenchThemeService.test|title=WorkbenchThemeService migrateThemeSettingsId returns unknown IDs unchanged|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/services/themes/test/common/workbenchThemeService.test|title=WorkbenchThemeService migrateThemeSettingsId returns unknown IDs unchanged|occurrence=1workbenchThemeService.te…workbenchThemeService.test|title=WorkbenchThemeService restores only for preferred color scheme changes|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/services/themes/test/common/workbenchThemeService.test|title=WorkbenchThemeService restores only for preferred color scheme changes|occurrence=1workbenchThemeService.te…tokenStyleResolving.test|title=Themes - TokenStyleResolving color defaults|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/services/themes/test/node/tokenStyleResolving.test|title=Themes - TokenStyleResolving color defaults|occurrence=1tokenStyleResolving.test…tokenStyleResolving.test|title=Themes - TokenStyleResolving language - scope resolving|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/services/themes/test/node/tokenStyleResolving.test|title=Themes - TokenStyleResolving language - scope resolving|occurrence=1tokenStyleResolving.test…tokenStyleResolving.test|title=Themes - TokenStyleResolving language|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/services/themes/test/node/tokenStyleResolving.test|title=Themes - TokenStyleResolving language|occurrence=1tokenStyleResolving.test…tokenStyleResolving.test|title=Themes - TokenStyleResolving resolveScopes - match most specific|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/services/themes/test/node/tokenStyleResolving.test|title=Themes - TokenStyleResolving resolveScopes - match most specific|occurrence=1tokenStyleResolving.test…tokenStyleResolving.test|title=Themes - TokenStyleResolving resolveScopes|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/services/themes/test/node/tokenStyleResolving.test|title=Themes - TokenStyleResolving resolveScopes|occurrence=1tokenStyleResolving.test…tokenStyleResolving.test|title=Themes - TokenStyleResolving rule matching|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/services/themes/test/node/tokenStyleResolving.test|title=Themes - TokenStyleResolving rule matching|occurrence=1tokenStyleResolving.test…tokenStyleResolving.test|title=Themes - TokenStyleResolving super type|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/workbench/services/themes/test/node/tokenStyleResolving.test|title=Themes - TokenStyleResolving super type|occurrence=1tokenStyleResolving.test…Focused file · src/vs/platform/theme/common/tokenClassificationRegistry.ts · 632 LOCcommon/tokenClassificati…

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 > /*--------------------------------------------------------------------------------------------- tokenClassificationRegistry.ts ×28
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 { RunOnceScheduler } from '../../../base/common/async.js';
7 > import { Color } from '../../../base/common/color.js';
8 > import { Emitter, Event } from '../../../base/common/event.js';
9 > import { IJSONSchema, IJSONSchemaMap } from '../../../base/common/jsonSchema.js';
10 > import { Disposable } from '../../../base/common/lifecycle.js';
11 > import * as nls from '../../../nls.js';
12 > import { Extensions as JSONExtensions, IJSONContributionRegistry } from '../../jsonschemas/common/jsonContributionRegistry.js';
13 > import * as platform from '../../registry/common/platform.js';
14 > import { IColorTheme } from './themeService.js';
15 >
16 > const TOKEN_TYPE_WILDCARD = '*';
17 > const TOKEN_CLASSIFIER_LANGUAGE_SEPARATOR = ':';
18 > const CLASSIFIER_MODIFIER_SEPARATOR = '.';
19 >
20 > // qualified string [type|*](.modifier)*(/language)!
21 > type TokenClassificationString = string;
22 >
23 > const idPattern = '\\w+[-_\\w+]*';
24 > export const typeAndModifierIdPattern = `^${idPattern}$`;
25 >
26 > const selectorPattern = `^(${idPattern}|\\*)(\\${CLASSIFIER_MODIFIER_SEPARATOR}${idPattern})*(${TOKEN_CLASSIFIER_LANGUAGE_SEPARATOR}${idPattern})?$`;
27 >
28 > const fontStylePattern = '^(\\s*(italic|bold|underline|strikethrough))*\\s*$';
29 >
30 > export interface TokenSelector {
31 > match(type: string, modifiers: string[], language: string): number;
32 > readonly id: string;
33 > }
34 >
35 > export interface TokenTypeOrModifierContribution {
36 > readonly num: number;
37 > readonly id: string;
38 > readonly superType?: string;
39 > readonly description: string;
40 > readonly deprecationMessage?: string;
41 > }
42 >
43 >
44 > export interface TokenStyleData {
45 > foreground: Color | undefined;
46 > bold: boolean | undefined;
47 > underline: boolean | undefined;
48 > strikethrough: boolean | undefined;
49 > italic: boolean | undefined;
50 > }
51 >
52 > export class TokenStyle implements Readonly<TokenStyleData> {
53 > constructor(
54 > public readonly foreground: Color | undefined, colorThemeData.ts ×57
55 > public readonly bold: boolean | undefined,
56 > public readonly underline: boolean | undefined,
57 > public readonly strikethrough: boolean | undefined,
58 > public readonly italic: boolean | undefined,
59 > ) {
60 > }
62 >
63 > export namespace TokenStyle {
64 > export function toJSONObject(style: TokenStyle): any {
65 return {
66 _foreground: style.foreground === undefined ? null : Color.Format.CSS.formatHexA(style.foreground, true),
67 _bold: style.bold === undefined ? null : style.bold,
68 _underline: style.underline === undefined ? null : style.underline,
69 _italic: style.italic === undefined ? null : style.italic,
70 _strikethrough: style.strikethrough === undefined ? null : style.strikethrough,
71 };
72 }
73 > export function fromJSONObject(obj: any): TokenStyle | undefined { tokenClassificationRegistry.ts ×28
74 if (obj) {
75 const boolOrUndef = (b: any) => (typeof b === 'boolean') ? b : undefined;
76 const colorOrUndef = (s: any) => (typeof s === 'string') ? Color.fromHex(s) : undefined;
77 return new TokenStyle(
78 colorOrUndef(obj._foreground),
79 boolOrUndef(obj._bold),
80 boolOrUndef(obj._underline),
81 boolOrUndef(obj._strikethrough),
82 boolOrUndef(obj._italic)
83 );
84 }
85 return undefined;
86 }
87 > export function equals(s1: any, s2: any): boolean { tokenClassificationRegistry.ts ×28
88 if (s1 === s2) {
89 return true;
90 }
91 return s1 !== undefined && s2 !== undefined
92 && (s1.foreground instanceof Color ? s1.foreground.equals(s2.foreground) : s2.foreground === undefined)
93 && s1.bold === s2.bold
94 && s1.underline === s2.underline
95 && s1.strikethrough === s2.strikethrough
96 && s1.italic === s2.italic;
97 }
98 > export function is(s: any): s is TokenStyle { tokenClassificationRegistry.ts ×28
99 return s instanceof TokenStyle;
100 }
101 > export function fromData(data: { foreground: Color | undefined; bold: boolean | undefined; underline: boolean | undefined; strikethrough: boolean | undefined; italic: boolean | undefined }): TokenStyle { tokenClassificationRegistry.ts ×28
102 > return new TokenStyle(data.foreground, data.bold, data.underline, data.strikethrough, data.italic); colorThemeData.ts ×33
103 > }
104 > export function fromSettings(foreground: string | undefined, fontStyle: string | undefined): TokenStyle; tokenClassificationRegistry.ts ×28
105 > export function fromSettings(foreground: string | undefined, fontStyle: string | undefined, bold: boolean | undefined, underline: boolean | undefined, strikethrough: boolean | undefined, italic: boolean | undefined): TokenStyle;
106 > export function fromSettings(foreground: string | undefined, fontStyle: string | undefined, bold?: boolean, underline?: boolean, strikethrough?: boolean, italic?: boolean): TokenStyle {
107 > let foregroundColor = undefined; colorThemeData.ts ×57
108 > if (foreground !== undefined) {
109 > foregroundColor = Color.fromHex(foreground);
110 > }
111 > if (fontStyle !== undefined) {
112 > bold = italic = underline = strikethrough = false; tokenClassificationRegistry.ts ×1
113 > const expression = /italic|bold|underline|strikethrough/g;
114 > let match;
115 > while ((match = expression.exec(fontStyle))) {
116 > switch (match[0]) {
117 > case 'bold': bold = true; break;
118 > case 'italic': italic = true; break;
119 > case 'underline': underline = true; break;
120 > case 'strikethrough': strikethrough = true; break;
121 > }
122 > }
123 > }
124 > return new TokenStyle(foregroundColor, bold, underline, strikethrough, italic); colorThemeData.ts ×57
125 > }
127 >
128 > export type ProbeScope = string[];
129 >
130 > export interface TokenStyleFunction {
131 > (theme: IColorTheme): TokenStyle | undefined;
132 > }
133 >
134 > export interface TokenStyleDefaults {
135 > scopesToProbe?: ProbeScope[];
136 > light?: TokenStyleValue;
137 > dark?: TokenStyleValue;
138 > hcDark?: TokenStyleValue;
139 > hcLight?: TokenStyleValue;
140 > }
141 >
142 > export interface SemanticTokenDefaultRule {
143 > selector: TokenSelector;
144 > defaults: TokenStyleDefaults;
145 > }
146 >
147 > export interface SemanticTokenRule {
148 > style: TokenStyle;
149 > selector: TokenSelector;
150 > }
151 >
152 > export namespace SemanticTokenRule {
153 > export function fromJSONObject(registry: ITokenClassificationRegistry, o: any): SemanticTokenRule | undefined {
154 if (o && typeof o._selector === 'string' && o._style) {
155 const style = TokenStyle.fromJSONObject(o._style);
156 if (style) {
157 try {
158 return { selector: registry.parseTokenSelector(o._selector), style };
159 } catch (_ignore) {
160 }
161 }
162 }
163 return undefined;
164 }
165 > export function toJSONObject(rule: SemanticTokenRule): any { tokenClassificationRegistry.ts ×28
166 return {
167 _selector: rule.selector.id,
168 _style: TokenStyle.toJSONObject(rule.style)
169 };
170 }
171 > export function equals(r1: SemanticTokenRule | undefined, r2: SemanticTokenRule | undefined) { tokenClassificationRegistry.ts ×28
172 if (r1 === r2) {
173 return true;
174 }
175 return r1 !== undefined && r2 !== undefined
176 && r1.selector && r2.selector && r1.selector.id === r2.selector.id
177 && TokenStyle.equals(r1.style, r2.style);
178 }
179 > export function is(r: any): r is SemanticTokenRule { tokenClassificationRegistry.ts ×28
180 return r && r.selector && typeof r.selector.id === 'string' && TokenStyle.is(r.style);
181 }
183 >
184 > /**
185 > * A TokenStyle Value is either a token style literal, or a TokenClassificationString
186 > */
187 > export type TokenStyleValue = TokenStyle | TokenClassificationString;
188 >
189 > // TokenStyle registry
190 > const Extensions = {
191 > TokenClassificationContribution: 'base.contributions.tokenClassification'
192 > };
193 >
194 > export interface ITokenClassificationRegistry {
195 >
196 > readonly onDidChangeSchema: Event<void>;
197 >
198 > /**
199 > * Register a token type to the registry.
200 > * @param id The TokenType id as used in theme description files
201 > * @param description the description
202 > */
203 > registerTokenType(id: string, description: string, superType?: string, deprecationMessage?: string): void;
204 >
205 > /**
206 > * Register a token modifier to the registry.
207 > * @param id The TokenModifier id as used in theme description files
208 > * @param description the description
209 > */
210 > registerTokenModifier(id: string, description: string): void;
211 >
212 > /**
213 > * Parses a token selector from a selector string.
214 > * @param selectorString selector string in the form (*|type)(.modifier)*
215 > * @param language language to which the selector applies or undefined if the selector is for all languafe
216 > * @returns the parsesd selector
217 > * @throws an error if the string is not a valid selector
218 > */
219 > parseTokenSelector(selectorString: string, language?: string): TokenSelector;
220 >
221 > /**
222 > * Register a TokenStyle default to the registry.
223 > * @param selector The rule selector
224 > * @param defaults The default values
225 > */
226 > registerTokenStyleDefault(selector: TokenSelector, defaults: TokenStyleDefaults): void;
227 >
228 > /**
229 > * Deregister a TokenStyle default to the registry.
230 > * @param selector The rule selector
231 > */
232 > deregisterTokenStyleDefault(selector: TokenSelector): void;
233 >
234 > /**
235 > * Deregister a TokenType from the registry.
236 > */
237 > deregisterTokenType(id: string): void;
238 >
239 > /**
240 > * Deregister a TokenModifier from the registry.
241 > */
242 > deregisterTokenModifier(id: string): void;
243 >
244 > /**
245 > * Get all TokenType contributions
246 > */
247 > getTokenTypes(): TokenTypeOrModifierContribution[];
248 >
249 > /**
250 > * Get all TokenModifier contributions
251 > */
252 > getTokenModifiers(): TokenTypeOrModifierContribution[];
253 >
254 > /**
255 > * The styling rules to used when a schema does not define any styling rules.
256 > */
257 > getTokenStylingDefaultRules(): SemanticTokenDefaultRule[];
258 >
259 > /**
260 > * JSON schema for an object to assign styling to token classifications
261 > */
262 > getTokenStylingSchema(): IJSONSchema;
263 > }
264 >
265 > class TokenClassificationRegistry extends Disposable implements ITokenClassificationRegistry {
266 >
267 > private readonly _onDidChangeSchema = this._register(new Emitter<void>());
268 > readonly onDidChangeSchema: Event<void> = this._onDidChangeSchema.event;
269 >
270 > private currentTypeNumber = 0;
271 > private currentModifierBit = 1;
272 >
273 > private tokenTypeById: { [key: string]: TokenTypeOrModifierContribution };
274 > private tokenModifierById: { [key: string]: TokenTypeOrModifierContribution };
275 >
276 > private tokenStylingDefaultRules: SemanticTokenDefaultRule[] = [];
277 >
278 > private typeHierarchy: { [id: string]: string[] };
279 >
280 > private tokenStylingSchema: IJSONSchema & { properties: IJSONSchemaMap; patternProperties: IJSONSchemaMap } = {
281 > type: 'object',
282 > properties: {},
283 > patternProperties: {
284 > [selectorPattern]: getStylingSchemeEntry()
285 > },
286 > //errorMessage: nls.localize('schema.token.errors', 'Valid token selectors have the form (*|tokenType)(.tokenModifier)*(:tokenLanguage)?.'),
287 > additionalProperties: false,
288 > definitions: {
289 > style: {
290 > type: 'object',
291 > description: nls.localize('schema.token.settings', 'Colors and styles for the token.'),
292 > properties: {
293 > foreground: {
294 > type: 'string',
295 > description: nls.localize('schema.token.foreground', 'Foreground color for the token.'),
296 > format: 'color-hex',
297 > default: '#ff0000'
298 > },
299 > background: {
300 > type: 'string',
301 > deprecationMessage: nls.localize('schema.token.background.warning', 'Token background colors are currently not supported.')
302 > },
303 > fontStyle: {
304 > type: 'string',
305 > description: nls.localize('schema.token.fontStyle', 'Sets the all font styles of the rule: \'italic\', \'bold\', \'underline\' or \'strikethrough\' or a combination. All styles that are not listed are unset. The empty string unsets all styles.'),
306 > pattern: fontStylePattern,
307 > patternErrorMessage: nls.localize('schema.fontStyle.error', 'Font style must be \'italic\', \'bold\', \'underline\' or \'strikethrough\' or a combination. The empty string unsets all styles.'),
308 > defaultSnippets: [
309 > { label: nls.localize('schema.token.fontStyle.none', 'None (clear inherited style)'), bodyText: '""' },
310 > { body: 'italic' },
311 > { body: 'bold' },
312 > { body: 'underline' },
313 > { body: 'strikethrough' },
314 > { body: 'italic bold' },
315 > { body: 'italic underline' },
316 > { body: 'italic strikethrough' },
317 > { body: 'bold underline' },
318 > { body: 'bold strikethrough' },
319 > { body: 'underline strikethrough' },
320 > { body: 'italic bold underline' },
321 > { body: 'italic bold strikethrough' },
322 > { body: 'italic underline strikethrough' },
323 > { body: 'bold underline strikethrough' },
324 > { body: 'italic bold underline strikethrough' }
325 > ]
326 > },
327 > bold: {
328 > type: 'boolean',
329 > description: nls.localize('schema.token.bold', 'Sets or unsets the font style to bold. Note, the presence of \'fontStyle\' overrides this setting.'),
330 > },
331 > italic: {
332 > type: 'boolean',
333 > description: nls.localize('schema.token.italic', 'Sets or unsets the font style to italic. Note, the presence of \'fontStyle\' overrides this setting.'),
334 > },
335 > underline: {
336 > type: 'boolean',
337 > description: nls.localize('schema.token.underline', 'Sets or unsets the font style to underline. Note, the presence of \'fontStyle\' overrides this setting.'),
338 > },
339 > strikethrough: {
340 > type: 'boolean',
341 > description: nls.localize('schema.token.strikethrough', 'Sets or unsets the font style to strikethrough. Note, the presence of \'fontStyle\' overrides this setting.'),
342 > }
343 >
344 > },
345 > defaultSnippets: [{ body: { foreground: '${1:#FF0000}', fontStyle: '${2:bold}' } }]
346 > }
347 > }
348 > };
349 >
350 > constructor() {
351 > super();
352 > this.tokenTypeById = Object.create(null);
353 > this.tokenModifierById = Object.create(null);
354 > this.typeHierarchy = Object.create(null);
355 > }
356 >
357 > public registerTokenType(id: string, description: string, superType?: string, deprecationMessage?: string): void {
358 > if (!id.match(typeAndModifierIdPattern)) {
359 throw new Error('Invalid token type id.');
360 }
361 > if (superType && !superType.match(typeAndModifierIdPattern)) { tokenClassificationRegistry.ts ×28
362 throw new Error('Invalid token super type id.');
363 }
365 > const num = this.currentTypeNumber++;
366 > const tokenStyleContribution: TokenTypeOrModifierContribution = { num, id, superType, description, deprecationMessage };
367 > this.tokenTypeById[id] = tokenStyleContribution;
368 >
369 > const stylingSchemeEntry = getStylingSchemeEntry(description, deprecationMessage);
370 > this.tokenStylingSchema.properties[id] = stylingSchemeEntry;
371 > this.typeHierarchy = Object.create(null);
372 > }
373 >
374 > public registerTokenModifier(id: string, description: string, deprecationMessage?: string): void {
375 > if (!id.match(typeAndModifierIdPattern)) {
376 throw new Error('Invalid token modifier id.');
377 }
379 > const num = this.currentModifierBit;
380 > this.currentModifierBit = this.currentModifierBit * 2;
381 > const tokenStyleContribution: TokenTypeOrModifierContribution = { num, id, description, deprecationMessage };
382 > this.tokenModifierById[id] = tokenStyleContribution;
383 >
384 > this.tokenStylingSchema.properties[`*.${id}`] = getStylingSchemeEntry(description, deprecationMessage);
385 > }
386 >
387 > public parseTokenSelector(selectorString: string, language?: string): TokenSelector {
388 > const selector = parseClassifierString(selectorString, language);
389 >
390 > if (!selector.type) {
391 return {
392 match: () => -1,
393 id: '$invalid'
394 };
395 }
397 > return {
398 > match: (type: string, modifiers: string[], language: string) => {
399 > let score = 0; colorThemeData.ts ×33
400 > if (selector.language !== undefined) {
401 > if (selector.language !== language) { tokenClassificationRegistry.ts ×2
402 > return -1;
403 > }
404 > score += 10;
405 > }
406 > if (selector.type !== TOKEN_TYPE_WILDCARD) { colorThemeData.ts ×33
407 > const hierarchy = this.getTypeHierarchy(type);
408 > const level = hierarchy.indexOf(selector.type);
409 > if (level === -1) {
410 > return -1;
411 > }
412 > score += (100 - level);
413 > }
414 > // all selector modifiers must be present
415 > for (const selectorModifier of selector.modifiers) {
416 > if (modifiers.indexOf(selectorModifier) === -1) {
417 > return -1;
418 > }
419 > }
420 > return score + selector.modifiers.length * 100;
421 > },
422 > id: `${[selector.type, ...selector.modifiers.sort()].join('.')}${selector.language !== undefined ? ':' + selector.language : ''}` tokenClassificationRegistry.ts ×28
423 > };
424 > }
425 >
426 > public registerTokenStyleDefault(selector: TokenSelector, defaults: TokenStyleDefaults): void {
427 > this.tokenStylingDefaultRules.push({ selector, defaults });
428 > }
429 >
430 > public deregisterTokenStyleDefault(selector: TokenSelector): void {
431 > const selectorString = selector.id; tokenClassificationRegistry.ts ×1
432 > this.tokenStylingDefaultRules = this.tokenStylingDefaultRules.filter(r => r.selector.id !== selectorString);
433 > }
435 > public deregisterTokenType(id: string): void {
436 > delete this.tokenTypeById[id]; tokenClassificationRegistry.ts ×2
437 > delete this.tokenStylingSchema.properties[id];
438 > this.typeHierarchy = Object.create(null);
439 > }
441 > public deregisterTokenModifier(id: string): void {
442 delete this.tokenModifierById[id];
443 delete this.tokenStylingSchema.properties[`*.${id}`];
444 }
446 > public getTokenTypes(): TokenTypeOrModifierContribution[] {
447 return Object.keys(this.tokenTypeById).map(id => this.tokenTypeById[id]);
448 }
450 > public getTokenModifiers(): TokenTypeOrModifierContribution[] {
451 return Object.keys(this.tokenModifierById).map(id => this.tokenModifierById[id]);
452 }
454 > public getTokenStylingSchema(): IJSONSchema {
455 > return this.tokenStylingSchema;
456 > }
457 >
458 > public getTokenStylingDefaultRules(): SemanticTokenDefaultRule[] {
459 > return this.tokenStylingDefaultRules; colorThemeData.ts ×33
460 > }
462 > private getTypeHierarchy(typeId: string): string[] {
463 > let hierarchy = this.typeHierarchy[typeId]; colorThemeData.ts ×33
464 > if (!hierarchy) {
465 > this.typeHierarchy[typeId] = hierarchy = [typeId];
466 > let type = this.tokenTypeById[typeId];
467 > while (type && type.superType) {
468 > hierarchy.push(type.superType); tokenClassificationRegistry.ts ×2
469 > type = this.tokenTypeById[type.superType];
470 > }
472 > return hierarchy;
473 > }
475 >
476 > public override toString() {
477 const sorter = (a: string, b: string) => {
478 const cat1 = a.indexOf('.') === -1 ? 0 : 1;
479 const cat2 = b.indexOf('.') === -1 ? 0 : 1;
480 if (cat1 !== cat2) {
481 return cat1 - cat2;
482 }
483 return a.localeCompare(b);
484 };
485
486 return Object.keys(this.tokenTypeById).sort(sorter).map(k => `- \`${k}\`: ${this.tokenTypeById[k].description}`).join('\n');
487 }
489 > }
490 >
491 > const CHAR_LANGUAGE = TOKEN_CLASSIFIER_LANGUAGE_SEPARATOR.charCodeAt(0);
492 > const CHAR_MODIFIER = CLASSIFIER_MODIFIER_SEPARATOR.charCodeAt(0);
493 >
494 > export function parseClassifierString(s: string, defaultLanguage: string): { type: string; modifiers: string[]; language: string };
495 > export function parseClassifierString(s: string, defaultLanguage?: string): { type: string; modifiers: string[]; language: string | undefined };
496 > export function parseClassifierString(s: string, defaultLanguage: string | undefined): { type: string; modifiers: string[]; language: string | undefined } {
497 > let k = s.length;
498 > let language: string | undefined = defaultLanguage;
499 > const modifiers = [];
500 >
501 > for (let i = k - 1; i >= 0; i--) {
502 > const ch = s.charCodeAt(i);
503 > if (ch === CHAR_LANGUAGE || ch === CHAR_MODIFIER) {
504 > const segment = s.substring(i + 1, k);
505 > k = i;
506 > if (ch === CHAR_LANGUAGE) {
507 > language = segment; tokenClassificationRegistry.ts ×2
509 > modifiers.push(segment);
510 > }
511 > }
512 > }
513 > const type = s.substring(0, k);
514 > return { type, modifiers, language };
515 > }
516 >
517 >
518 > const tokenClassificationRegistry = createDefaultTokenClassificationRegistry();
519 > platform.Registry.add(Extensions.TokenClassificationContribution, tokenClassificationRegistry);
520 >
521 >
522 > function createDefaultTokenClassificationRegistry(): TokenClassificationRegistry {
523 >
524 > const registry = new TokenClassificationRegistry();
525 >
526 > function registerTokenType(id: string, description: string, scopesToProbe: ProbeScope[] = [], superType?: string, deprecationMessage?: string): string {
527 > registry.registerTokenType(id, description, superType, deprecationMessage);
528 > if (scopesToProbe) {
529 > registerTokenStyleDefault(id, scopesToProbe);
530 > }
531 > return id;
532 > }
533 >
534 > function registerTokenStyleDefault(selectorString: string, scopesToProbe: ProbeScope[]) {
535 > try {
536 > const selector = registry.parseTokenSelector(selectorString);
537 > registry.registerTokenStyleDefault(selector, { scopesToProbe });
538 > } catch (e) {
539 console.log(e);
540 }
542 >
543 > // default token types
544 >
545 > registerTokenType('comment', nls.localize('comment', "Style for comments."), [['comment']]);
546 > registerTokenType('string', nls.localize('string', "Style for strings."), [['string']]);
547 > registerTokenType('keyword', nls.localize('keyword', "Style for keywords."), [['keyword.control']]);
548 > registerTokenType('number', nls.localize('number', "Style for numbers."), [['constant.numeric']]);
549 > registerTokenType('regexp', nls.localize('regexp', "Style for expressions."), [['constant.regexp']]);
550 > registerTokenType('operator', nls.localize('operator', "Style for operators."), [['keyword.operator']]);
551 >
552 > registerTokenType('namespace', nls.localize('namespace', "Style for namespaces."), [['entity.name.namespace']]);
553 >
554 > registerTokenType('type', nls.localize('type', "Style for types."), [['entity.name.type'], ['support.type']]);
555 > registerTokenType('struct', nls.localize('struct', "Style for structs."), [['entity.name.type.struct']]);
556 > registerTokenType('class', nls.localize('class', "Style for classes."), [['entity.name.type.class'], ['support.class']]);
557 > registerTokenType('interface', nls.localize('interface', "Style for interfaces."), [['entity.name.type.interface']]);
558 > registerTokenType('enum', nls.localize('enum', "Style for enums."), [['entity.name.type.enum']]);
559 > registerTokenType('typeParameter', nls.localize('typeParameter', "Style for type parameters."), [['entity.name.type.parameter']]);
560 >
561 > registerTokenType('function', nls.localize('function', "Style for functions"), [['entity.name.function'], ['support.function']]);
562 > registerTokenType('member', nls.localize('member', "Style for member functions"), [], 'method', 'Deprecated use `method` instead');
563 > registerTokenType('method', nls.localize('method', "Style for method (member functions)"), [['entity.name.function.member'], ['support.function']]);
564 > registerTokenType('macro', nls.localize('macro', "Style for macros."), [['entity.name.function.preprocessor']]);
565 >
566 > registerTokenType('variable', nls.localize('variable', "Style for variables."), [['variable.other.readwrite'], ['entity.name.variable']]);
567 > registerTokenType('parameter', nls.localize('parameter', "Style for parameters."), [['variable.parameter']]);
568 > registerTokenType('property', nls.localize('property', "Style for properties."), [['variable.other.property']]);
569 > registerTokenType('enumMember', nls.localize('enumMember', "Style for enum members."), [['variable.other.enummember']]);
570 > registerTokenType('event', nls.localize('event', "Style for events."), [['variable.other.event']]);
571 > registerTokenType('decorator', nls.localize('decorator', "Style for decorators & annotations."), [['entity.name.decorator'], ['entity.name.function']]);
572 >
573 > registerTokenType('label', nls.localize('labels', "Style for labels. "), undefined);
574 >
575 > // default token modifiers
576 >
577 > registry.registerTokenModifier('declaration', nls.localize('declaration', "Style for all symbol declarations."), undefined);
578 > registry.registerTokenModifier('documentation', nls.localize('documentation', "Style to use for references in documentation."), undefined);
579 > registry.registerTokenModifier('static', nls.localize('static', "Style to use for symbols that are static."), undefined);
580 > registry.registerTokenModifier('abstract', nls.localize('abstract', "Style to use for symbols that are abstract."), undefined);
581 > registry.registerTokenModifier('deprecated', nls.localize('deprecated', "Style to use for symbols that are deprecated."), undefined);
582 > registry.registerTokenModifier('modification', nls.localize('modification', "Style to use for write accesses."), undefined);
583 > registry.registerTokenModifier('async', nls.localize('async', "Style to use for symbols that are async."), undefined);
584 > registry.registerTokenModifier('readonly', nls.localize('readonly', "Style to use for symbols that are read-only."), undefined);
585 >
586 >
587 > registerTokenStyleDefault('variable.readonly', [['variable.other.constant']]);
588 > registerTokenStyleDefault('property.readonly', [['variable.other.constant.property']]);
589 > registerTokenStyleDefault('type.defaultLibrary', [['support.type']]);
590 > registerTokenStyleDefault('class.defaultLibrary', [['support.class']]);
591 > registerTokenStyleDefault('interface.defaultLibrary', [['support.class']]);
592 > registerTokenStyleDefault('variable.defaultLibrary', [['support.variable'], ['support.other.variable']]);
593 > registerTokenStyleDefault('variable.defaultLibrary.readonly', [['support.constant']]);
594 > registerTokenStyleDefault('property.defaultLibrary', [['support.variable.property']]);
595 > registerTokenStyleDefault('property.defaultLibrary.readonly', [['support.constant.property']]);
596 > registerTokenStyleDefault('function.defaultLibrary', [['support.function']]);
597 > registerTokenStyleDefault('member.defaultLibrary', [['support.function']]);
598 > return registry;
599 > }
600 >
601 > export function getTokenClassificationRegistry(): ITokenClassificationRegistry {
602 > return tokenClassificationRegistry; colorThemeData.ts ×57
603 > }
605 > function getStylingSchemeEntry(description?: string, deprecationMessage?: string): IJSONSchema {
606 > return {
607 > description,
608 > deprecationMessage,
609 > defaultSnippets: [{ body: '${1:#ff0000}' }],
610 > anyOf: [
611 > {
612 > type: 'string',
613 > format: 'color-hex'
614 > },
615 > {
616 > $ref: '#/definitions/style'
617 > }
618 > ]
619 > };
620 > }
621 >
622 > export const tokenStylingSchemaId = 'vscode://schemas/token-styling';
623 >
624 > const schemaRegistry = platform.Registry.as<IJSONContributionRegistry>(JSONExtensions.JSONContribution);
625 > schemaRegistry.registerSchema(tokenStylingSchemaId, tokenClassificationRegistry.getTokenStylingSchema());
626 >
627 > const delayer = new RunOnceScheduler(() => schemaRegistry.notifySchemaChanged(tokenStylingSchemaId), 200);
628 > tokenClassificationRegistry.onDidChangeSchema(() => {
629 if (!delayer.isScheduled()) {
630 delayer.schedule();
631 }
632 });