src/vs/editor/common/services/languagesRegistry.ts
367 LOC · 313 covered · 54 uncovered · 98 ranges · 1430 concepts · 27 introducers · 699 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.
/*---------------------------------------------------------------------------------------------
languagesRegistry.ts ×26
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Emitter, Event } from '../../../base/common/event.js';
import { Disposable, IDisposable } from '../../../base/common/lifecycle.js';
import { compareIgnoreCase, regExpLeadsToEndlessLoop } from '../../../base/common/strings.js';
import { clearPlatformLanguageAssociations, getLanguageIds, registerPlatformLanguageAssociation } from './languagesAssociations.js';
import { URI } from '../../../base/common/uri.js';
import { ILanguageIdCodec } from '../languages.js';
import { LanguageId } from '../encodedTokenAttributes.js';
import { ModesRegistry, PLAINTEXT_LANGUAGE_ID } from '../languages/modesRegistry.js';
import { ILanguageExtensionPoint, ILanguageNameIdPair, ILanguageIcon } from '../languages/language.js';
import { Extensions, IConfigurationRegistry } from '../../../platform/configuration/common/configurationRegistry.js';
import { Registry } from '../../../platform/registry/common/platform.js';
const hasOwnProperty = Object.prototype.hasOwnProperty;
const NULL_LANGUAGE_ID = 'vs.editor.nullLanguage';
interface IResolvedLanguage {
identifier: string;
name: string | null;
mimetypes: string[];
aliases: string[];
extensions: string[];
filenames: string[];
configurationFiles: URI[];
icons: ILanguageIcon[];
}
export class LanguageIdCodec implements ILanguageIdCodec {
private _nextLanguageId: number;
private readonly _languageIdToLanguage: string[] = [];
private readonly _languageToLanguageId = new Map<string, number>();
constructor() {
this._register(PLAINTEXT_LANGUAGE_ID, LanguageId.PlainText);
this._nextLanguageId = 2;
}
private _register(language: string, languageId: LanguageId): void {
this._languageToLanguageId.set(language, languageId);
}
public register(language: string): void {
}
this._register(language, languageId);
public encodeLanguageId(languageId: string): LanguageId {
}
public decodeLanguageId(languageId: LanguageId): string {
return this._languageIdToLanguage[languageId] || NULL_LANGUAGE_ID;
encodedTokenAttributes.ts ×1
}
export class LanguagesRegistry extends Disposable {
static instanceCount = 0;
private readonly _onDidChange: Emitter<void> = this._register(new Emitter<void>());
public readonly onDidChange: Event<void> = this._onDidChange.event;
private readonly _warnOnOverwrite: boolean;
public readonly languageIdCodec: LanguageIdCodec;
private _dynamicLanguages: ILanguageExtensionPoint[];
private _languages: { [id: string]: IResolvedLanguage };
private _mimeTypesMap: { [mimeType: string]: string };
private _nameMap: { [name: string]: string };
private _lowercaseNameMap: { [name: string]: string };
constructor(useModesRegistry = true, warnOnOverwrite = false) {
LanguagesRegistry.instanceCount++;
this._warnOnOverwrite = warnOnOverwrite;
this.languageIdCodec = new LanguageIdCodec();
this._dynamicLanguages = [];
this._languages = {};
this._mimeTypesMap = {};
this._nameMap = {};
this._lowercaseNameMap = {};
if (useModesRegistry) {
this._register(ModesRegistry.onDidChangeLanguages((m) => {
}
override dispose() {
super.dispose();
}
public setDynamicLanguages(def: ILanguageExtensionPoint[]): void {
this._dynamicLanguages = def;
this._initializeFromRegistry();
}
private _initializeFromRegistry(): void {
this._mimeTypesMap = {};
this._nameMap = {};
this._lowercaseNameMap = {};
clearPlatformLanguageAssociations();
const desc = (<ILanguageExtensionPoint[]>[]).concat(ModesRegistry.getLanguages()).concat(this._dynamicLanguages);
this._registerLanguages(desc);
}
registerLanguage(desc: ILanguageExtensionPoint): IDisposable {
}
_registerLanguages(desc: ILanguageExtensionPoint[]): void {
for (const d of desc) {
this._registerLanguage(d);
}
// Rebuild fast path maps
this._mimeTypesMap = {};
this._nameMap = {};
this._lowercaseNameMap = {};
Object.keys(this._languages).forEach((langId) => {
const language = this._languages[langId];
if (language.name) {
}
this._lowercaseNameMap[alias.toLowerCase()] = language.identifier;
});
language.mimetypes.forEach((mimetype) => {
this._mimeTypesMap[mimetype] = language.identifier;
});
});
Registry.as<IConfigurationRegistry>(Extensions.Configuration).registerOverrideIdentifiers(this.getRegisteredLanguageIds());
this._onDidChange.fire();
}
private _registerLanguage(lang: ILanguageExtensionPoint): void {
let resolvedLanguage: IResolvedLanguage;
if (hasOwnProperty.call(this._languages, langId)) {
this.languageIdCodec.register(langId);
resolvedLanguage = {
identifier: langId,
name: null,
mimetypes: [],
aliases: [],
extensions: [],
filenames: [],
configurationFiles: [],
icons: []
};
this._languages[langId] = resolvedLanguage;
}
this._mergeLanguage(resolvedLanguage, lang);
}
private _mergeLanguage(resolvedLanguage: IResolvedLanguage, lang: ILanguageExtensionPoint): void {
let primaryMime: string | null = null;
if (Array.isArray(lang.mimetypes) && lang.mimetypes.length > 0) {
primaryMime = lang.mimetypes[0];
}
if (!primaryMime) {
resolvedLanguage.mimetypes.push(primaryMime);
}
if (Array.isArray(lang.extensions)) {
// insert first as this appears to be the 'primary' language definition
languagesRegistry.ts ×1
resolvedLanguage.extensions = lang.extensions.concat(resolvedLanguage.extensions);
resolvedLanguage.extensions = resolvedLanguage.extensions.concat(lang.extensions);
}
for (const extension of lang.extensions) {
registerPlatformLanguageAssociation({ id: langId, mime: primaryMime, extension: extension }, this._warnOnOverwrite);
languagesRegistry.ts ×1
}
if (Array.isArray(lang.filenames)) {
registerPlatformLanguageAssociation({ id: langId, mime: primaryMime, filename: filename }, this._warnOnOverwrite);
resolvedLanguage.filenames.push(filename);
}
}
if (Array.isArray(lang.filenamePatterns)) {
for (const filenamePattern of lang.filenamePatterns) {
registerPlatformLanguageAssociation({ id: langId, mime: primaryMime, filepattern: filenamePattern }, this._warnOnOverwrite);
}
}
if (typeof lang.firstLine === 'string' && lang.firstLine.length > 0) {
let firstLineRegexStr = lang.firstLine;
if (firstLineRegexStr.charAt(0) !== '^') {
firstLineRegexStr = '^' + firstLineRegexStr;
}
try {
const firstLineRegex = new RegExp(firstLineRegexStr);
if (!regExpLeadsToEndlessLoop(firstLineRegex)) {
registerPlatformLanguageAssociation({ id: langId, mime: primaryMime, firstline: firstLineRegex }, this._warnOnOverwrite);
}
} catch (err) {
// Most likely, the regex was bad
console.warn(`[${lang.id}]: Invalid regular expression \`${firstLineRegexStr}\`: `, err);
}
}
resolvedLanguage.aliases.push(langId);
let langAliases: Array<string | null> | null = null;
if (typeof lang.aliases !== 'undefined' && Array.isArray(lang.aliases)) {
langAliases = [null];
}
if (langAliases !== null) {
if (!langAlias || langAlias.length === 0) {
}
}
const containsAliases = (langAliases !== null && langAliases.length > 0);
if (containsAliases && langAliases![0] === null) {
const bestName = (containsAliases ? langAliases![0] : null) || langId;
languagesRegistry.ts ×2
if (containsAliases || !resolvedLanguage.name) {
resolvedLanguage.name = bestName;
}
}
if (lang.configuration) {
}
if (lang.icon) {
resolvedLanguage.icons.push(lang.icon);
}
public isRegisteredLanguageId(languageId: string | null | undefined): boolean {
return false;
}
}
public getRegisteredLanguageIds(): string[] {
}
public getSortedRegisteredLanguageNames(): ILanguageNameIdPair[] {
for (const languageName in this._nameMap) {
result.push({
languageName: languageName,
languageId: this._nameMap[languageName]
});
}
}
result.sort((a, b) => compareIgnoreCase(a.languageName, b.languageName));
languagesRegistry.ts ×2
return result;
}
public getLanguageName(languageId: string): string | null {
return null;
}
}
public getMimeType(languageId: string): string | null {
return null;
}
return (language.mimetypes[0] || null);
}
public getExtensions(languageId: string): ReadonlyArray<string> {
return [];
}
}
public getFilenames(languageId: string): ReadonlyArray<string> {
return [];
}
}
public getIcon(languageId: string): ILanguageIcon | null {
if (!hasOwnProperty.call(this._languages, languageId)) {
return null;
}
const language = this._languages[languageId];
return (language.icons[0] || null);
}
public getConfigurationFiles(languageId: string): ReadonlyArray<URI> {
return [];
}
return this._languages[languageId].configurationFiles || [];
}
public getLanguageIdByLanguageName(languageName: string): string | null {
if (!hasOwnProperty.call(this._lowercaseNameMap, languageNameLower)) {
return null;
}
}
public getLanguageIdByMimeType(mimeType: string | null | undefined): string | null {
if (!mimeType) {
return null;
}
if (hasOwnProperty.call(this._mimeTypesMap, mimeType)) {
return this._mimeTypesMap[mimeType];
}
return null;
}
public guessLanguageIdByFilepathOrFirstLine(resource: URI | null, firstLine?: string): string[] {
if (!resource && !firstLine) {
return [];
}
return getLanguageIds(resource, firstLine);
}