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

import { IDisposable } from 'vs/base/common/lifecycle';
import Event, { Emitter } from 'vs/base/common/event';
import { ITokenizationRegistry, ITokenizationSupport, ITokenizationSupportChangedEvent } from 'vs/editor/common/modes';
10
import { Color } from 'vs/base/common/color';
11 12 13 14 15 16 17 18

export class TokenizationRegistryImpl implements ITokenizationRegistry {

	private _map: { [language: string]: ITokenizationSupport };

	private _onDidChange: Emitter<ITokenizationSupportChangedEvent> = new Emitter<ITokenizationSupportChangedEvent>();
	public onDidChange: Event<ITokenizationSupportChangedEvent> = this._onDidChange.event;

19
	private _colorMap: Color[];
20 21 22 23 24 25 26

	constructor() {
		this._map = Object.create(null);
		this._colorMap = null;
	}

	public fire(languages: string[]): void {
A
Alex Dima 已提交
27 28 29 30
		this._onDidChange.fire({
			changedLanguages: languages,
			changedColorMap: false
		});
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
	}

	public register(language: string, support: ITokenizationSupport): IDisposable {
		this._map[language] = support;
		this.fire([language]);
		return {
			dispose: () => {
				if (this._map[language] !== support) {
					return;
				}
				delete this._map[language];
				this.fire([language]);
			}
		};
	}

	public get(language: string): ITokenizationSupport {
		return (this._map[language] || null);
	}

51
	public setColorMap(colorMap: Color[]): void {
52
		this._colorMap = colorMap;
A
Alex Dima 已提交
53 54
		this._onDidChange.fire({
			changedLanguages: Object.keys(this._map),
55
			changedColorMap: true
A
Alex Dima 已提交
56
		});
57 58
	}

59
	public getColorMap(): Color[] {
60 61 62
		return this._colorMap;
	}
}