TMScopeRegistry.ts 2.0 KB
Newer Older
1 2 3 4 5 6 7 8
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import * as resources from 'vs/base/common/resources';
import { URI } from 'vs/base/common/uri';
import { Disposable } from 'vs/base/common/lifecycle';
A
Alex Dima 已提交
9 10 11 12 13 14 15 16 17 18
import { StandardTokenType, LanguageId } from 'vs/editor/common/modes';

export interface IValidGrammarDefinition {
	location: URI;
	language?: LanguageId;
	scopeName: string;
	embeddedLanguages: IValidEmbeddedLanguagesMap;
	tokenTypes: IValidTokenTypeMap;
	injectTo?: string[];
}
19

A
Alex Dima 已提交
20
export interface IValidTokenTypeMap {
21 22 23
	[selector: string]: StandardTokenType;
}

A
Alex Dima 已提交
24 25 26 27
export interface IValidEmbeddedLanguagesMap {
	[scopeName: string]: LanguageId;
}

28 29
export class TMScopeRegistry extends Disposable {

A
Alex Dima 已提交
30
	private _scopeNameToLanguageRegistration: { [scopeName: string]: IValidGrammarDefinition; };
31 32 33

	constructor() {
		super();
A
Alex Dima 已提交
34
		this._scopeNameToLanguageRegistration = Object.create(null);
35 36 37 38 39 40
	}

	public reset(): void {
		this._scopeNameToLanguageRegistration = Object.create(null);
	}

A
Alex Dima 已提交
41 42 43 44
	public register(def: IValidGrammarDefinition): void {
		if (this._scopeNameToLanguageRegistration[def.scopeName]) {
			const existingRegistration = this._scopeNameToLanguageRegistration[def.scopeName];
			if (!resources.isEqual(existingRegistration.location, def.location)) {
45
				console.warn(
A
Alex Dima 已提交
46 47 48
					`Overwriting grammar scope name to file mapping for scope ${def.scopeName}.\n` +
					`Old grammar file: ${existingRegistration.location.toString()}.\n` +
					`New grammar file: ${def.location.toString()}`
49 50 51
				);
			}
		}
A
Alex Dima 已提交
52
		this._scopeNameToLanguageRegistration[def.scopeName] = def;
53 54
	}

A
Alex Dima 已提交
55
	public getGrammarDefinition(scopeName: string): IValidGrammarDefinition | null {
56 57 58
		return this._scopeNameToLanguageRegistration[scopeName] || null;
	}
}