untitledEditorModel.ts 5.3 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 {IDisposable} from 'vs/base/common/lifecycle';
E
Erich Gamma 已提交
8 9
import {TPromise} from 'vs/base/common/winjs.base';
import {EditorModel, IEncodingSupport} from 'vs/workbench/common/editor';
10
import {StringEditorModel} from 'vs/workbench/common/editor/stringEditorModel';
E
Erich Gamma 已提交
11
import URI from 'vs/base/common/uri';
12
import {EndOfLinePreference} from 'vs/editor/common/editorCommon';
E
Erich Gamma 已提交
13
import {IFilesConfiguration} from 'vs/platform/files/common/files';
14
import {IConfigurationService} from 'vs/platform/configuration/common/configuration';
E
Erich Gamma 已提交
15 16
import {IModeService} from 'vs/editor/common/services/modeService';
import {IModelService} from 'vs/editor/common/services/modelService';
17 18
import {IMode} from 'vs/editor/common/modes';
import {isUnspecific} from 'vs/base/common/mime';
19
import Event, {Emitter} from 'vs/base/common/event';
E
Erich Gamma 已提交
20 21

export class UntitledEditorModel extends StringEditorModel implements IEncodingSupport {
A
Alex Dima 已提交
22
	private textModelChangeListener: IDisposable;
23
	private configurationChangeListener: IDisposable;
E
Erich Gamma 已提交
24 25

	private dirty: boolean;
26
	private _onDidChangeDirty: Emitter<void>;
27
	private _onDidChangeEncoding: Emitter<void>;
28

E
Erich Gamma 已提交
29 30 31
	private configuredEncoding: string;
	private preferredEncoding: string;

32 33
	private hasAssociatedFilePath: boolean;

E
Erich Gamma 已提交
34 35
	constructor(
		value: string,
36
		mime: string,
E
Erich Gamma 已提交
37 38 39 40 41 42
		resource: URI,
		hasAssociatedFilePath: boolean,
		@IModeService modeService: IModeService,
		@IModelService modelService: IModelService,
		@IConfigurationService private configurationService: IConfigurationService
	) {
43
		super(value, mime, resource, modeService, modelService);
E
Erich Gamma 已提交
44

45
		this.hasAssociatedFilePath = hasAssociatedFilePath;
E
Erich Gamma 已提交
46 47
		this.dirty = hasAssociatedFilePath; // untitled associated to file path are dirty right away

48
		this._onDidChangeDirty = new Emitter<void>();
49
		this._onDidChangeEncoding = new Emitter<void>();
50

E
Erich Gamma 已提交
51 52 53
		this.registerListeners();
	}

54 55 56 57
	public get onDidChangeDirty(): Event<void> {
		return this._onDidChangeDirty.event;
	}

58 59 60 61
	public get onDidChangeEncoding(): Event<void> {
		return this._onDidChangeEncoding.event;
	}

62 63 64 65 66 67 68 69
	protected getOrCreateMode(modeService: IModeService, mime: string, firstLineText?: string): TPromise<IMode> {
		if (isUnspecific(mime)) {
			return modeService.getOrCreateModeByFilenameOrFirstLine(this.resource.fsPath, firstLineText); // lookup mode via resource path if the provided mime is unspecific
		}

		return super.getOrCreateMode(modeService, mime, firstLineText);
	}

E
Erich Gamma 已提交
70 71 72
	private registerListeners(): void {

		// Config Changes
73
		this.configurationChangeListener = this.configurationService.onDidUpdateConfiguration(e => this.onConfigurationChange(e.config));
E
Erich Gamma 已提交
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
	}

	private onConfigurationChange(configuration: IFilesConfiguration): void {
		this.configuredEncoding = configuration && configuration.files && configuration.files.encoding;
	}

	public getValue(): string {
		if (this.textEditorModel) {
			return this.textEditorModel.getValue(EndOfLinePreference.TextDefined, true /* Preserve BOM */);
		}

		return null;
	}

	public getModeId(): string {
		if (this.textEditorModel) {
			return this.textEditorModel.getModeId();
		}

		return null;
	}

	public getEncoding(): string {
		return this.preferredEncoding || this.configuredEncoding;
	}

	public setEncoding(encoding: string): void {
101
		const oldEncoding = this.getEncoding();
E
Erich Gamma 已提交
102 103 104 105
		this.preferredEncoding = encoding;

		// Emit if it changed
		if (oldEncoding !== this.preferredEncoding) {
106
			this._onDidChangeEncoding.fire();
E
Erich Gamma 已提交
107 108 109 110 111 112 113
		}
	}

	public isDirty(): boolean {
		return this.dirty;
	}

114 115 116
	public revert(): void {
		this.dirty = false;

117
		this._onDidChangeDirty.fire();
118 119
	}

E
Erich Gamma 已提交
120 121
	public load(): TPromise<EditorModel> {
		return super.load().then((model) => {
122
			const configuration = this.configurationService.getConfiguration<IFilesConfiguration>();
E
Erich Gamma 已提交
123

124 125
			// Encoding
			this.configuredEncoding = configuration && configuration.files && configuration.files.encoding;
E
Erich Gamma 已提交
126

127
			// Listen to content changes
B
Benjamin Pasero 已提交
128
			this.textModelChangeListener = this.textEditorModel.onDidChangeContent(e => this.onModelContentChanged());
E
Erich Gamma 已提交
129

130 131 132
			// Emit initial dirty event if we are
			if (this.dirty) {
				setTimeout(() => {
133
					this._onDidChangeDirty.fire();
134 135
				}, 0 /* prevent race condition between creating model and emitting dirty event */);
			}
E
Erich Gamma 已提交
136

137
			return model;
E
Erich Gamma 已提交
138 139 140
		});
	}

B
Benjamin Pasero 已提交
141
	private onModelContentChanged(): void {
142 143

		// turn dirty if we were not
E
Erich Gamma 已提交
144 145
		if (!this.dirty) {
			this.dirty = true;
146
			this._onDidChangeDirty.fire();
E
Erich Gamma 已提交
147
		}
148 149 150

		// mark the untitled editor as non-dirty once its content becomes empty and we do
		// not have an associated path set
B
Benjamin Pasero 已提交
151
		else if (!this.hasAssociatedFilePath && this.textEditorModel.getLineCount() === 1 && this.textEditorModel.getLineContent(1) === '') {
152 153 154
			this.dirty = false;
			this._onDidChangeDirty.fire();
		}
E
Erich Gamma 已提交
155 156 157 158 159 160
	}

	public dispose(): void {
		super.dispose();

		if (this.textModelChangeListener) {
A
Alex Dima 已提交
161
			this.textModelChangeListener.dispose();
E
Erich Gamma 已提交
162 163 164
			this.textModelChangeListener = null;
		}

165 166 167
		if (this.configurationChangeListener) {
			this.configurationChangeListener.dispose();
			this.configurationChangeListener = null;
E
Erich Gamma 已提交
168
		}
169 170 171

		this._onDidChangeDirty.dispose();
		this._onDidChangeEncoding.dispose();
E
Erich Gamma 已提交
172 173
	}
}