modeServiceImpl.ts 20.0 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

7
import * as nls from 'vs/nls';
A
Alex Dima 已提交
8 9
import {onUnexpectedError} from 'vs/base/common/errors';
import Event, {Emitter} from 'vs/base/common/event';
10
import {IDisposable, empty as EmptyDisposable} from 'vs/base/common/lifecycle'; // TODO@Alex
A
Alex Dima 已提交
11 12
import * as objects from 'vs/base/common/objects';
import * as paths from 'vs/base/common/paths';
E
Erich Gamma 已提交
13
import {TPromise} from 'vs/base/common/winjs.base';
14 15
import mime = require('vs/base/common/mime');
import {IFilesConfiguration} from 'vs/platform/files/common/files';
J
Johannes Rieken 已提交
16
import {createAsyncDescriptor1} from 'vs/platform/instantiation/common/descriptors';
A
Alex Dima 已提交
17
import {IExtensionService} from 'vs/platform/extensions/common/extensions';
18
import {IExtensionPointUser, IExtensionMessageCollector, ExtensionsRegistry} from 'vs/platform/extensions/common/extensionsRegistry';
19
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
A
Alex Dima 已提交
20
import * as modes from 'vs/editor/common/modes';
E
Erich Gamma 已提交
21
import {FrankensteinMode} from 'vs/editor/common/modes/abstractMode';
22
import {ModesRegistry} from 'vs/editor/common/modes/modesRegistry';
A
Alex Dima 已提交
23
import {LanguagesRegistry} from 'vs/editor/common/services/languagesRegistry';
24
import {ILanguageExtensionPoint, IValidLanguageExtensionPoint, IModeLookupResult, IModeService} from 'vs/editor/common/services/modeService';
25
import {IConfigurationService} from 'vs/platform/configuration/common/configuration';
A
Alex Dima 已提交
26 27
import {AbstractState} from 'vs/editor/common/modes/abstractState';
import {Token} from 'vs/editor/common/modes/supports';
E
Erich Gamma 已提交
28 29 30

interface IModeConfigurationMap { [modeId: string]: any; }

31
let languagesExtPoint = ExtensionsRegistry.registerExtensionPoint<ILanguageExtensionPoint[]>('languages', {
32 33
	description: nls.localize('vscode.extension.contributes.languages', 'Contributes language declarations.'),
	type: 'array',
34
	defaultSnippets: [{ body: [{ id: '', aliases: [], extensions: [] }] }],
35 36
	items: {
		type: 'object',
37
		defaultSnippets: [{ body: { id: '', extensions: [] } }],
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
		properties: {
			id: {
				description: nls.localize('vscode.extension.contributes.languages.id', 'ID of the language.'),
				type: 'string'
			},
			aliases: {
				description: nls.localize('vscode.extension.contributes.languages.aliases', 'Name aliases for the language.'),
				type: 'array',
				items: {
					type: 'string'
				}
			},
			extensions: {
				description: nls.localize('vscode.extension.contributes.languages.extensions', 'File extensions associated to the language.'),
				default: ['.foo'],
				type: 'array',
				items: {
					type: 'string'
				}
			},
			filenames: {
				description: nls.localize('vscode.extension.contributes.languages.filenames', 'File names associated to the language.'),
				type: 'array',
				items: {
					type: 'string'
				}
			},
			filenamePatterns: {
				description: nls.localize('vscode.extension.contributes.languages.filenamePatterns', 'File name glob patterns associated to the language.'),
				type: 'array',
68
				items: {
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
					type: 'string'
				}
			},
			mimetypes: {
				description: nls.localize('vscode.extension.contributes.languages.mimetypes', 'Mime types associated to the language.'),
				type: 'array',
				items: {
					type: 'string'
				}
			},
			firstLine: {
				description: nls.localize('vscode.extension.contributes.languages.firstLine', 'A regular expression matching the first line of a file of the language.'),
				type: 'string'
			},
			configuration: {
				description: nls.localize('vscode.extension.contributes.languages.configuration', 'A relative path to a file containing configuration options for the language.'),
				type: 'string'
			}
		}
	}
});

function isUndefinedOrStringArray(value: string[]): boolean {
	if (typeof value === 'undefined') {
		return true;
	}
	if (!Array.isArray(value)) {
		return false;
	}
	return value.every(item => typeof item === 'string');
}

101
function isValidLanguageExtensionPoint(value:ILanguageExtensionPoint, collector:IExtensionMessageCollector): boolean {
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	if (!value) {
		collector.error(nls.localize('invalid.empty', "Empty value for `contributes.{0}`", languagesExtPoint.name));
		return false;
	}
	if (typeof value.id !== 'string') {
		collector.error(nls.localize('require.id', "property `{0}` is mandatory and must be of type `string`", 'id'));
		return false;
	}
	if (!isUndefinedOrStringArray(value.extensions)) {
		collector.error(nls.localize('opt.extensions', "property `{0}` can be omitted and must be of type `string[]`", 'extensions'));
		return false;
	}
	if (!isUndefinedOrStringArray(value.filenames)) {
		collector.error(nls.localize('opt.filenames', "property `{0}` can be omitted and must be of type `string[]`", 'filenames'));
		return false;
	}
	if (typeof value.firstLine !== 'undefined' && typeof value.firstLine !== 'string') {
		collector.error(nls.localize('opt.firstLine', "property `{0}` can be omitted and must be of type `string`", 'firstLine'));
		return false;
	}
	if (typeof value.configuration !== 'undefined' && typeof value.configuration !== 'string') {
		collector.error(nls.localize('opt.configuration', "property `{0}` can be omitted and must be of type `string`", 'configuration'));
		return false;
	}
	if (!isUndefinedOrStringArray(value.aliases)) {
		collector.error(nls.localize('opt.aliases', "property `{0}` can be omitted and must be of type `string[]`", 'aliases'));
		return false;
	}
	if (!isUndefinedOrStringArray(value.mimetypes)) {
		collector.error(nls.localize('opt.mimetypes', "property `{0}` can be omitted and must be of type `string[]`", 'mimetypes'));
		return false;
	}
	return true;
}

E
Erich Gamma 已提交
137
export class ModeServiceImpl implements IModeService {
138
	public _serviceBrand: any;
E
Erich Gamma 已提交
139

140
	private _instantiationService: IInstantiationService;
141 142
	protected _extensionService: IExtensionService;

A
Alex Dima 已提交
143 144
	private _activationPromises: { [modeId: string]: TPromise<modes.IMode>; };
	private _instantiatedModes: { [modeId: string]: modes.IMode; };
E
Erich Gamma 已提交
145 146
	private _config: IModeConfigurationMap;

147 148 149 150
	private _registry: LanguagesRegistry;

	private _onDidAddModes: Emitter<string[]> = new Emitter<string[]>();
	public onDidAddModes: Event<string[]> = this._onDidAddModes.event;
151

A
Alex Dima 已提交
152 153 154
	private _onDidCreateMode: Emitter<modes.IMode> = new Emitter<modes.IMode>();
	public onDidCreateMode: Event<modes.IMode> = this._onDidCreateMode.event;

155 156
	constructor(instantiationService:IInstantiationService, extensionService:IExtensionService) {
		this._instantiationService = instantiationService;
A
Alex Dima 已提交
157
		this._extensionService = extensionService;
158

E
Erich Gamma 已提交
159 160 161
		this._activationPromises = {};
		this._instantiatedModes = {};
		this._config = {};
162

163 164
		this._registry = new LanguagesRegistry();
		this._registry.onDidAddModes((modes) => this._onDidAddModes.fire(modes));
E
Erich Gamma 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
	}

	public getConfigurationForMode(modeId:string): any {
		return this._config[modeId] || {};
	}

	public configureMode(mimetype: string, options: any): void {
		var modeId = this.getModeId(mimetype);
		if (modeId) {
			this.configureModeById(modeId, options);
		}
	}

	public configureModeById(modeId:string, options:any):void {
		var previousOptions = this._config[modeId] || {};
A
Alex Dima 已提交
180
		var newOptions = objects.mixin(objects.clone(previousOptions), options);
E
Erich Gamma 已提交
181

A
Alex Dima 已提交
182
		if (objects.equals(previousOptions, newOptions)) {
E
Erich Gamma 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
			// This configure call is a no-op
			return;
		}

		this._config[modeId] = newOptions;

		var mode = this.getMode(modeId);
		if (mode && mode.configSupport) {
			mode.configSupport.configure(this.getConfigurationForMode(modeId));
		}
	}

	public configureAllModes(config:any): void {
		if (!config) {
			return;
		}
199
		var modes = this._registry.getRegisteredModes();
E
Erich Gamma 已提交
200 201 202 203 204 205
		modes.forEach((modeIdentifier) => {
			var configuration = config[modeIdentifier];
			this.configureModeById(modeIdentifier, configuration);
		});
	}

206
	public isRegisteredMode(mimetypeOrModeId: string): boolean {
207
		return this._registry.isRegisteredMode(mimetypeOrModeId);
208 209
	}

210 211 212 213 214
	public isCompatMode(modeId:string): boolean {
		let compatModeData = this._registry.getCompatMode(modeId);
		return (compatModeData ? true : false);
	}

215
	public getRegisteredModes(): string[] {
216
		return this._registry.getRegisteredModes();
217 218
	}

219
	public getRegisteredLanguageNames(): string[] {
220
		return this._registry.getRegisteredLanguageNames();
221 222 223
	}

	public getExtensions(alias: string): string[] {
224
		return this._registry.getExtensions(alias);
225 226 227
	}

	public getMimeForMode(modeId: string): string {
228
		return this._registry.getMimeForMode(modeId);
229 230 231
	}

	public getLanguageName(modeId: string): string {
232
		return this._registry.getLanguageName(modeId);
233 234 235
	}

	public getModeIdForLanguageName(alias:string): string {
236
		return this._registry.getModeIdForLanguageNameLowercase(alias);
237 238 239
	}

	public getModeId(commaSeparatedMimetypesOrCommaSeparatedIds: string): string {
240
		var modeIds = this._registry.extractModeIds(commaSeparatedMimetypesOrCommaSeparatedIds);
241 242 243 244 245 246 247 248

		if (modeIds.length > 0) {
			return modeIds[0];
		}

		return null;
	}

249
	public getConfigurationFiles(modeId: string): string[] {
250
		return this._registry.getConfigurationFiles(modeId);
251 252
	}

E
Erich Gamma 已提交
253 254 255 256
	// --- instantiation

	public lookup(commaSeparatedMimetypesOrCommaSeparatedIds: string): IModeLookupResult[]{
		var r: IModeLookupResult[] = [];
257
		var modeIds = this._registry.extractModeIds(commaSeparatedMimetypesOrCommaSeparatedIds);
E
Erich Gamma 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270

		for (var i = 0; i < modeIds.length; i++) {
			var modeId = modeIds[i];

			r.push({
				modeId: modeId,
				isInstantiated: this._instantiatedModes.hasOwnProperty(modeId)
			});
		}

		return r;
	}

A
Alex Dima 已提交
271
	public getMode(commaSeparatedMimetypesOrCommaSeparatedIds: string): modes.IMode {
272
		var modeIds = this._registry.extractModeIds(commaSeparatedMimetypesOrCommaSeparatedIds);
E
Erich Gamma 已提交
273 274 275 276 277 278 279 280 281 282 283

		var isPlainText = false;
		for (var i = 0; i < modeIds.length; i++) {
			if (this._instantiatedModes.hasOwnProperty(modeIds[i])) {
				return this._instantiatedModes[modeIds[i]];
			}
			isPlainText = isPlainText || (modeIds[i] === 'plaintext');
		}

		if (isPlainText) {
			// Try to do it synchronously
A
Alex Dima 已提交
284
			var r: modes.IMode = null;
E
Erich Gamma 已提交
285 286
			this.getOrCreateMode(commaSeparatedMimetypesOrCommaSeparatedIds).then((mode) => {
				r = mode;
A
Alex Dima 已提交
287
			}).done(null, onUnexpectedError);
E
Erich Gamma 已提交
288 289 290 291 292
			return r;
		}
	}

	public getModeIdByLanguageName(languageName: string): string {
293
		var modeIds = this._registry.getModeIdsFromLanguageName(languageName);
E
Erich Gamma 已提交
294 295 296 297 298 299 300 301 302

		if (modeIds.length > 0) {
			return modeIds[0];
		}

		return null;
	}

	public getModeIdByFilenameOrFirstLine(filename: string, firstLine?:string): string {
303
		var modeIds = this._registry.getModeIdsFromFilenameOrFirstLine(filename, firstLine);
E
Erich Gamma 已提交
304 305 306 307 308 309 310 311

		if (modeIds.length > 0) {
			return modeIds[0];
		}

		return null;
	}

312 313 314 315
	public onReady(): TPromise<boolean> {
		return this._extensionService.onReady();
	}

A
Alex Dima 已提交
316
	public getOrCreateMode(commaSeparatedMimetypesOrCommaSeparatedIds: string): TPromise<modes.IMode> {
317
		return this.onReady().then(() => {
E
Erich Gamma 已提交
318 319 320 321 322 323
			var modeId = this.getModeId(commaSeparatedMimetypesOrCommaSeparatedIds);
			// Fall back to plain text if no mode was found
			return this._getOrCreateMode(modeId || 'plaintext');
		});
	}

A
Alex Dima 已提交
324
	public getOrCreateModeByLanguageName(languageName: string): TPromise<modes.IMode> {
325
		return this.onReady().then(() => {
E
Erich Gamma 已提交
326 327 328 329 330 331
			var modeId = this.getModeIdByLanguageName(languageName);
			// Fall back to plain text if no mode was found
			return this._getOrCreateMode(modeId || 'plaintext');
		});
	}

A
Alex Dima 已提交
332
	public getOrCreateModeByFilenameOrFirstLine(filename: string, firstLine?:string): TPromise<modes.IMode> {
333
		return this.onReady().then(() => {
E
Erich Gamma 已提交
334 335 336 337 338 339
			var modeId = this.getModeIdByFilenameOrFirstLine(filename, firstLine);
			// Fall back to plain text if no mode was found
			return this._getOrCreateMode(modeId || 'plaintext');
		});
	}

A
Alex Dima 已提交
340
	private _getOrCreateMode(modeId: string): TPromise<modes.IMode> {
E
Erich Gamma 已提交
341 342 343 344 345 346 347
		if (this._instantiatedModes.hasOwnProperty(modeId)) {
			return TPromise.as(this._instantiatedModes[modeId]);
		}

		if (this._activationPromises.hasOwnProperty(modeId)) {
			return this._activationPromises[modeId];
		}
348 349 350
		var c, e;
		var promise = new TPromise((cc,ee,pp) => { c = cc; e = ee; });
		this._activationPromises[modeId] = promise;
J
Joao Moreno 已提交
351

352
		this._createMode(modeId).then((mode) => {
E
Erich Gamma 已提交
353 354
			this._instantiatedModes[modeId] = mode;
			delete this._activationPromises[modeId];
A
Alex Dima 已提交
355 356 357

			this._onDidCreateMode.fire(mode);

A
Alex Dima 已提交
358
			this._extensionService.activateByEvent(`onLanguage:${modeId}`).done(null, onUnexpectedError);
A
Alex Dima 已提交
359

E
Erich Gamma 已提交
360
			return this._instantiatedModes[modeId];
361 362 363
		}).then(c, e);

		return promise;
E
Erich Gamma 已提交
364 365
	}

366
	private _createMode(modeId:string): TPromise<modes.IMode> {
A
Alex Dima 已提交
367
		let modeDescriptor = this._createModeDescriptor(modeId);
E
Erich Gamma 已提交
368

369
		let compatModeData = this._registry.getCompatMode(modeId);
370
		if (compatModeData) {
A
Alex Dima 已提交
371
			// This is a compatibility mode
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387

			let resolvedDeps: TPromise<modes.IMode[]> = null;
			if (Array.isArray(compatModeData.deps)) {
				resolvedDeps = TPromise.join(compatModeData.deps.map(dep => this.getOrCreateMode(dep)));
			} else {
				resolvedDeps = TPromise.as<modes.IMode[]>(null);
			}

			return resolvedDeps.then(_ => {
				let compatModeAsyncDescriptor = createAsyncDescriptor1<modes.IModeDescriptor, modes.IMode>(compatModeData.moduleId, compatModeData.ctorName);
				return this._instantiationService.createInstance(compatModeAsyncDescriptor, modeDescriptor).then((compatMode) => {
					if (compatMode.configSupport) {
						compatMode.configSupport.configure(this.getConfigurationForMode(modeId));
					}
					return compatMode;
				});
A
Alex Dima 已提交
388
			});
E
Erich Gamma 已提交
389 390
		}

391
		return TPromise.as<modes.IMode>(this._instantiationService.createInstance(FrankensteinMode, modeDescriptor));
E
Erich Gamma 已提交
392 393
	}

A
Alex Dima 已提交
394
	private _createModeDescriptor(modeId:string): modes.IModeDescriptor {
E
Erich Gamma 已提交
395
		return {
J
Johannes Rieken 已提交
396
			id: modeId
E
Erich Gamma 已提交
397 398 399
		};
	}

400 401 402
	private _registerTokenizationSupport<T>(mode:modes.IMode, callback: (mode: modes.IMode) => T): IDisposable {
		if (mode.setTokenizationSupport) {
			return mode.setTokenizationSupport(callback);
403
		} else {
404
			console.warn('Cannot register tokenizationSupport on mode ' + mode.getId() + ' because it does not support it.');
405 406 407 408
			return EmptyDisposable;
		}
	}

409
	private registerModeSupport<T>(modeId: string, callback: (mode: modes.IMode) => T): IDisposable {
410
		if (this._instantiatedModes.hasOwnProperty(modeId)) {
411
			return this._registerTokenizationSupport(this._instantiatedModes[modeId], callback);
412 413 414 415 416 417 418 419
		}

		let cc: (disposable:IDisposable)=>void;
		let promise = new TPromise<IDisposable>((c, e) => { cc = c; });

		let disposable = this.onDidCreateMode((mode) => {
			if (mode.getId() !== modeId) {
				return;
E
Erich Gamma 已提交
420
			}
421

422
			cc(this._registerTokenizationSupport(mode, callback));
423
			disposable.dispose();
E
Erich Gamma 已提交
424
		});
425

E
Erich Gamma 已提交
426 427 428 429
		return {
			dispose: () => {
				promise.done(disposable => disposable.dispose(), null);
			}
A
tslint  
Alex Dima 已提交
430
		};
E
Erich Gamma 已提交
431 432
	}

A
Alex Dima 已提交
433
	public registerTokenizationSupport(modeId: string, callback: (mode: modes.IMode) => modes.ITokenizationSupport): IDisposable {
434
		return this.registerModeSupport(modeId, callback);
E
Erich Gamma 已提交
435
	}
A
Alex Dima 已提交
436

437
	public registerTokenizationSupport2(modeId: string, support: modes.TokensProvider): IDisposable {
438
		return this.registerModeSupport(modeId, (mode) => {
A
Alex Dima 已提交
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
			return new TokenizationSupport2Adapter(mode, support);
		});
	}
}

export class TokenizationState2Adapter implements modes.IState {

	private _mode: modes.IMode;
	private _actual: modes.IState2;
	private _stateData: modes.IState;

	constructor(mode: modes.IMode, actual: modes.IState2, stateData: modes.IState) {
		this._mode = mode;
		this._actual = actual;
		this._stateData = stateData;
	}

	public get actual(): modes.IState2 { return this._actual; }

	public clone(): TokenizationState2Adapter {
		return new TokenizationState2Adapter(this._mode, this._actual.clone(), AbstractState.safeClone(this._stateData));
	}

	public equals(other:modes.IState): boolean {
		if (other instanceof TokenizationState2Adapter) {
			if (!this._actual.equals(other._actual)) {
				return false;
			}
			return AbstractState.safeEquals(this._stateData, other._stateData);
		}
		return false;
	}

	public getMode(): modes.IMode {
		return this._mode;
	}

	public tokenize(stream:any): any {
		throw new Error('Unexpected tokenize call!');
	}

	public getStateData(): modes.IState {
		return this._stateData;
	}

	public setStateData(stateData:modes.IState): void {
		this._stateData = stateData;
	}
}

export class TokenizationSupport2Adapter implements modes.ITokenizationSupport {

	private _mode: modes.IMode;
492
	private _actual: modes.TokensProvider;
A
Alex Dima 已提交
493

494
	constructor(mode: modes.IMode, actual: modes.TokensProvider) {
A
Alex Dima 已提交
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
		this._mode = mode;
		this._actual = actual;
	}

	public getInitialState(): modes.IState {
		return new TokenizationState2Adapter(this._mode, this._actual.getInitialState(), null);
	}

	public tokenize(line:string, state:modes.IState, offsetDelta: number = 0, stopAtOffset?: number): modes.ILineTokens {
		if (state instanceof TokenizationState2Adapter) {
			let actualResult = this._actual.tokenize(line, state.actual);
			let tokens: modes.IToken[] = [];
			actualResult.tokens.forEach((t) => {
				if (typeof t.scopes === 'string') {
					tokens.push(new Token(t.startIndex + offsetDelta, <string>t.scopes));
				} else if (Array.isArray(t.scopes) && t.scopes.length === 1) {
					tokens.push(new Token(t.startIndex + offsetDelta, t.scopes[0]));
				} else {
					throw new Error('Only token scopes as strings or of precisely 1 length are supported at this time!');
				}
			});
			return {
				tokens: tokens,
				actualStopOffset: offsetDelta + line.length,
				endState: new TokenizationState2Adapter(state.getMode(), actualResult.endState, state.getStateData()),
				modeTransitions: [{ startIndex: offsetDelta, mode: state.getMode() }],
			};
		}
		throw new Error('Unexpected state to tokenize with!');
	}

E
Erich Gamma 已提交
526 527 528
}

export class MainThreadModeServiceImpl extends ModeServiceImpl {
529 530
	private _configurationService: IConfigurationService;
	private _onReadyPromise: TPromise<boolean>;
E
Erich Gamma 已提交
531

532
	constructor(
533
		@IInstantiationService instantiationService:IInstantiationService,
534 535
		@IExtensionService extensionService:IExtensionService,
		@IConfigurationService configurationService:IConfigurationService
536
	) {
537
		super(instantiationService, extensionService);
538
		this._configurationService = configurationService;
539 540

		languagesExtPoint.setHandler((extensions:IExtensionPointUser<ILanguageExtensionPoint[]>[]) => {
541
			let allValidLanguages: IValidLanguageExtensionPoint[] = [];
542 543 544 545 546 547 548 549 550 551

			for (let i = 0, len = extensions.length; i < len; i++) {
				let extension = extensions[i];

				if (!Array.isArray(extension.value)) {
					extension.collector.error(nls.localize('invalid', "Invalid `contributes.{0}`. Expected an array.", languagesExtPoint.name));
					continue;
				}

				for (let j = 0, lenJ = extension.value.length; j < lenJ; j++) {
552 553 554
					let ext = extension.value[j];
					if (isValidLanguageExtensionPoint(ext, extension.collector)) {
						let configuration = (ext.configuration ? paths.join(extension.description.extensionFolderPath, ext.configuration) : ext.configuration);
555
						allValidLanguages.push({
556 557 558 559 560 561 562 563
							id: ext.id,
							extensions: ext.extensions,
							filenames: ext.filenames,
							filenamePatterns: ext.filenamePatterns,
							firstLine: ext.firstLine,
							aliases: ext.aliases,
							mimetypes: ext.mimetypes,
							configuration: configuration
564 565 566 567 568 569 570 571
						});
					}
				}
			}

			ModesRegistry.registerLanguages(allValidLanguages);

		});
572

573
		this._configurationService.onDidUpdateConfiguration(e => this.onConfigurationChange(e.config));
574 575 576 577
	}

	public onReady(): TPromise<boolean> {
		if (!this._onReadyPromise) {
578 579 580
			const configuration = this._configurationService.getConfiguration<IFilesConfiguration>();
			this._onReadyPromise = this._extensionService.onReady().then(() => {
				this.onConfigurationChange(configuration);
581

582
				return true;
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
			});
		}

		return this._onReadyPromise;
	}

	private onConfigurationChange(configuration: IFilesConfiguration): void {

		// Clear user configured mime associations
		mime.clearTextMimes(true /* user configured */);

		// Register based on settings
		if (configuration.files && configuration.files.associations) {
			Object.keys(configuration.files.associations).forEach(pattern => {
				mime.registerTextMime({ mime: this.getMimeForMode(configuration.files.associations[pattern]), filepattern: pattern, userConfigured: true });
			});
		}
E
Erich Gamma 已提交
600
	}
601
}