diff --git a/src/vs/workbench/common/editor/textEditorModel.ts b/src/vs/workbench/common/editor/textEditorModel.ts index f37c2fd37c4..a879a77de77 100644 --- a/src/vs/workbench/common/editor/textEditorModel.ts +++ b/src/vs/workbench/common/editor/textEditorModel.ts @@ -153,6 +153,20 @@ export abstract class BaseTextEditorModel extends EditorModel implements ITextEd this.textEditorModel.setValueFromRawText(rawText); } + /** + * Updates the text editor model mode based on the settings and configuration. + */ + protected updateTextEditorModelMode(mime?: string): void { + if (!this.textEditorModel) { + return; + } + + const firstLineText = this.getFirstLineText(this.textEditorModel.getValue()); + const mode = this.getOrCreateMode(this.modeService, mime, firstLineText); + + this.textEditorModel.setMode(mode); + } + /** * Returns the textual value of this editor model or null if it has not yet been created. */ diff --git a/src/vs/workbench/parts/files/common/editors/textFileEditorModel.ts b/src/vs/workbench/parts/files/common/editors/textFileEditorModel.ts index 4fe9dbb1c89..d57c484c600 100644 --- a/src/vs/workbench/parts/files/common/editors/textFileEditorModel.ts +++ b/src/vs/workbench/parts/files/common/editors/textFileEditorModel.ts @@ -91,6 +91,7 @@ export class TextFileEditorModel extends BaseTextEditorModel implements ITextFil private registerListeners(): void { this.toDispose.push(this.textFileService.onAutoSaveConfigurationChange(config => this.updateAutoSaveConfiguration(config))); + this.toDispose.push(this.textFileService.onFilesAssociationChange(e => this.onFilesAssociationChange())); } private updateAutoSaveConfiguration(config: IAutoSaveConfiguration): void { @@ -103,6 +104,10 @@ export class TextFileEditorModel extends BaseTextEditorModel implements ITextFil } } + private onFilesAssociationChange(): void { + this.updateTextEditorModelMode(); + } + public get onDidStateChange(): Event { return this._onDidStateChange.event; } diff --git a/src/vs/workbench/parts/files/common/files.ts b/src/vs/workbench/parts/files/common/files.ts index 44ac8de74e5..2826e979506 100644 --- a/src/vs/workbench/parts/files/common/files.ts +++ b/src/vs/workbench/parts/files/common/files.ts @@ -303,6 +303,8 @@ export interface ITextFileEditorModel extends ITextEditorModel, IEncodingSupport export interface ITextFileService extends IDisposable { _serviceBrand: any; + onAutoSaveConfigurationChange: Event; + onFilesAssociationChange: Event; /** * Access to the manager of text file editor models providing further methods to work with them. @@ -387,9 +389,4 @@ export interface ITextFileService extends IDisposable { * Convinient fast access to the raw configured auto save settings. */ getAutoSaveConfiguration(): IAutoSaveConfiguration; - - /** - * Event is fired with the auto save configuration whenever it changes. - */ - onAutoSaveConfigurationChange: Event; } \ No newline at end of file diff --git a/src/vs/workbench/parts/files/common/textFileService.ts b/src/vs/workbench/parts/files/common/textFileService.ts index 3ada394c220..d237f058268 100644 --- a/src/vs/workbench/parts/files/common/textFileService.ts +++ b/src/vs/workbench/parts/files/common/textFileService.ts @@ -7,7 +7,9 @@ import {TPromise} from 'vs/base/common/winjs.base'; import URI from 'vs/base/common/uri'; import paths = require('vs/base/common/paths'); +import DOM = require('vs/base/browser/dom'); import errors = require('vs/base/common/errors'); +import objects = require('vs/base/common/objects'); import Event, {Emitter} from 'vs/base/common/event'; import {IResult, ITextFileOperationResult, ITextFileService, IRawTextContent, IAutoSaveConfiguration, AutoSaveMode, ITextFileEditorModelManager, ITextFileEditorModel} from 'vs/workbench/parts/files/common/files'; import {ConfirmResult} from 'vs/workbench/common/editor'; @@ -37,6 +39,9 @@ export abstract class TextFileService implements ITextFileService { private toUnbind: IDisposable[]; private _models: TextFileEditorModelManager; + private _onFilesAssociationChange: Emitter; + private currentFilesAssociationConfig: { [key: string]: string; }; + private _onAutoSaveConfigurationChange: Emitter; private configuredAutoSaveDelay: number; private configuredAutoSaveOnFocusChange: boolean; @@ -54,10 +59,18 @@ export abstract class TextFileService implements ITextFileService { @IInstantiationService private instantiationService: IInstantiationService ) { this.toUnbind = []; + this._onAutoSaveConfigurationChange = new Emitter(); + this.toUnbind.push(this._onAutoSaveConfigurationChange); + + this._onFilesAssociationChange = new Emitter(); + this.toUnbind.push(this._onFilesAssociationChange); + this._models = this.instantiationService.createInstance(TextFileEditorModelManager); const configuration = this.configurationService.getConfiguration(); + this.currentFilesAssociationConfig = configuration && configuration.files && configuration.files.associations; + this.onConfigurationChange(configuration); this.telemetryService.publicLog('autoSave', this.getAutoSaveConfiguration()); @@ -79,6 +92,10 @@ export abstract class TextFileService implements ITextFileService { return this._onAutoSaveConfigurationChange.event; } + public get onFilesAssociationChange(): Event { + return this._onFilesAssociationChange.event; + } + private registerListeners(): void { // Lifecycle @@ -89,8 +106,8 @@ export abstract class TextFileService implements ITextFileService { this.toUnbind.push(this.configurationService.onDidUpdateConfiguration(e => this.onConfigurationChange(e.config))); // Application & Editor focus change - window.addEventListener('blur', () => this.onWindowFocusLost()); - window.addEventListener('blur', () => this.onEditorFocusChanged(), true); + this.toUnbind.push(DOM.addDisposableListener(window, 'blur', () => this.onWindowFocusLost())); + this.toUnbind.push(DOM.addDisposableListener(window, 'blur', () => this.onEditorFocusChanged(), true)); this.toUnbind.push(this.editorGroupService.onEditorsChanged(() => this.onEditorFocusChanged())); } @@ -191,6 +208,13 @@ export abstract class TextFileService implements ITextFileService { if (!wasAutoSaveEnabled && this.getAutoSaveMode() !== AutoSaveMode.OFF) { this.saveAll().done(null, errors.onUnexpectedError); } + + // Check for change in files associations + const filesAssociation = configuration && configuration.files && configuration.files.associations; + if (!objects.equals(this.currentFilesAssociationConfig, filesAssociation)) { + this.currentFilesAssociationConfig = filesAssociation; + this._onFilesAssociationChange.fire(); + } } public getDirty(resources?: URI[]): URI[] {