untitledEditorModel.ts 7.7 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, dispose } from 'vs/base/common/lifecycle';
J
Johannes Rieken 已提交
8
import { TPromise } from 'vs/base/common/winjs.base';
B
Benjamin Pasero 已提交
9
import { IEncodingSupport } from 'vs/workbench/common/editor';
10
import { BaseTextEditorModel } from 'vs/workbench/common/editor/textEditorModel';
E
Erich Gamma 已提交
11
import URI from 'vs/base/common/uri';
J
Johannes Rieken 已提交
12 13
import { PLAINTEXT_MODE_ID } from 'vs/editor/common/modes/modesRegistry';
import { EndOfLinePreference } from 'vs/editor/common/editorCommon';
14
import { IFilesConfiguration, CONTENT_CHANGE_EVENT_BUFFER_DELAY } from 'vs/platform/files/common/files';
J
Johannes Rieken 已提交
15 16 17 18
import { IModeService } from 'vs/editor/common/services/modeService';
import { IModelService } from 'vs/editor/common/services/modelService';
import { IMode } from 'vs/editor/common/modes';
import Event, { Emitter } from 'vs/base/common/event';
19
import { RunOnceScheduler } from 'vs/base/common/async';
20
import { IBackupFileService, BACKUP_FILE_RESOLVE_OPTIONS } from 'vs/workbench/services/backup/common/backup';
21
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
22
import { ITextResourceConfigurationService } from 'vs/editor/common/services/resourceConfiguration';
E
Erich Gamma 已提交
23

24
export class UntitledEditorModel extends BaseTextEditorModel implements IEncodingSupport {
25

26
	public static DEFAULT_CONTENT_CHANGE_BUFFER_DELAY = CONTENT_CHANGE_EVENT_BUFFER_DELAY;
27

28
	private toDispose: IDisposable[];
E
Erich Gamma 已提交
29 30

	private dirty: boolean;
31
	private _onDidChangeContent: Emitter<void>;
32
	private _onDidChangeDirty: Emitter<void>;
33
	private _onDidChangeEncoding: Emitter<void>;
34

35 36
	private versionId: number;

37 38
	private contentChangeEventScheduler: RunOnceScheduler;

E
Erich Gamma 已提交
39
	private configuredEncoding: string;
40

E
Erich Gamma 已提交
41
	constructor(
42 43
		private modeId: string,
		private resource: URI,
44 45 46
		private hasAssociatedFilePath: boolean,
		private initialValue: string,
		private preferredEncoding: string,
E
Erich Gamma 已提交
47 48
		@IModeService modeService: IModeService,
		@IModelService modelService: IModelService,
49 50
		@IBackupFileService private backupFileService: IBackupFileService,
		@ITextFileService private textFileService: ITextFileService,
51
		@ITextResourceConfigurationService private configurationService: ITextResourceConfigurationService
E
Erich Gamma 已提交
52
	) {
53
		super(modelService, modeService);
E
Erich Gamma 已提交
54

55
		this.dirty = false;
56
		this.versionId = 0;
57
		this.toDispose = [];
E
Erich Gamma 已提交
58

59
		this._onDidChangeContent = new Emitter<void>();
60 61
		this.toDispose.push(this._onDidChangeContent);

62
		this._onDidChangeDirty = new Emitter<void>();
63 64
		this.toDispose.push(this._onDidChangeDirty);

65
		this._onDidChangeEncoding = new Emitter<void>();
66
		this.toDispose.push(this._onDidChangeEncoding);
67

68
		this.contentChangeEventScheduler = new RunOnceScheduler(() => this._onDidChangeContent.fire(), UntitledEditorModel.DEFAULT_CONTENT_CHANGE_BUFFER_DELAY);
69
		this.toDispose.push(this.contentChangeEventScheduler);
70

D
Daniel Imms 已提交
71
		this.registerListeners();
72 73
	}

74 75 76 77
	public get onDidChangeContent(): Event<void> {
		return this._onDidChangeContent.event;
	}

78 79 80 81
	public get onDidChangeDirty(): Event<void> {
		return this._onDidChangeDirty.event;
	}

82 83 84 85
	public get onDidChangeEncoding(): Event<void> {
		return this._onDidChangeEncoding.event;
	}

86 87 88
	protected getOrCreateMode(modeService: IModeService, modeId: string, firstLineText?: string): TPromise<IMode> {
		if (!modeId || modeId === PLAINTEXT_MODE_ID) {
			return modeService.getOrCreateModeByFilenameOrFirstLine(this.resource.fsPath, firstLineText); // lookup mode via resource path if the provided modeId is unspecific
89 90
		}

91
		return super.getOrCreateMode(modeService, modeId, firstLineText);
92 93
	}

E
Erich Gamma 已提交
94 95 96
	private registerListeners(): void {

		// Config Changes
97
		this.toDispose.push(this.configurationService.onDidUpdateConfiguration(e => this.onConfigurationChange()));
E
Erich Gamma 已提交
98 99
	}

100 101 102 103 104 105 106 107 108 109 110
	private onConfigurationChange(): void {
		const configuration = this.configurationService.getConfiguration<IFilesConfiguration>(this.resource);
		const configuredEncoding = configuration && configuration.files && configuration.files.encoding;

		if (this.configuredEncoding !== configuredEncoding) {
			this.configuredEncoding = configuredEncoding;

			if (!this.preferredEncoding) {
				this._onDidChangeEncoding.fire(); // do not fire event if we have a preferred encoding set
			}
		}
E
Erich Gamma 已提交
111 112
	}

113 114 115 116
	public getVersionId(): number {
		return this.versionId;
	}

E
Erich Gamma 已提交
117 118 119 120 121 122 123 124 125 126
	public getValue(): string {
		if (this.textEditorModel) {
			return this.textEditorModel.getValue(EndOfLinePreference.TextDefined, true /* Preserve BOM */);
		}

		return null;
	}

	public getModeId(): string {
		if (this.textEditorModel) {
A
Alex Dima 已提交
127
			return this.textEditorModel.getLanguageIdentifier().language;
E
Erich Gamma 已提交
128 129 130 131 132 133 134 135 136 137
		}

		return null;
	}

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

	public setEncoding(encoding: string): void {
138
		const oldEncoding = this.getEncoding();
E
Erich Gamma 已提交
139 140 141 142
		this.preferredEncoding = encoding;

		// Emit if it changed
		if (oldEncoding !== this.preferredEncoding) {
143
			this._onDidChangeEncoding.fire();
E
Erich Gamma 已提交
144 145 146 147 148 149 150
		}
	}

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

151 152 153 154 155 156 157 158 159
	private setDirty(dirty: boolean): void {
		if (this.dirty === dirty) {
			return;
		}

		this.dirty = dirty;
		this._onDidChangeDirty.fire();
	}

D
Daniel Imms 已提交
160 161 162 163
	public getResource(): URI {
		return this.resource;
	}

164
	public revert(): void {
165
		this.setDirty(false);
166 167 168

		// Handle content change event buffered
		this.contentChangeEventScheduler.schedule();
169 170
	}

B
Benjamin Pasero 已提交
171
	public load(): TPromise<UntitledEditorModel> {
E
Erich Gamma 已提交
172

173
		// Check for backups first
B
Benjamin Pasero 已提交
174 175
		return this.backupFileService.loadBackupResource(this.resource).then(backupResource => {
			if (backupResource) {
176
				return this.textFileService.resolveTextContent(backupResource, BACKUP_FILE_RESOLVE_OPTIONS).then(rawTextContent => {
177
					return this.backupFileService.parseBackupContent(rawTextContent.value);
178
				});
179 180 181 182
			}

			return null;
		}).then(backupContent => {
E
Erich Gamma 已提交
183

184 185
			// untitled associated to file path are dirty right away as well as untitled with content
			this.setDirty(this.hasAssociatedFilePath || !!backupContent);
E
Erich Gamma 已提交
186

187
			return this.doLoad(backupContent || this.initialValue || '').then(model => {
188
				const configuration = this.configurationService.getConfiguration<IFilesConfiguration>(this.resource);
E
Erich Gamma 已提交
189

190 191
				// Encoding
				this.configuredEncoding = configuration && configuration.files && configuration.files.encoding;
E
Erich Gamma 已提交
192

193
				// Listen to content changes
194 195 196
				this.toDispose.push(this.textEditorModel.onDidChangeContent(() => this.onModelContentChanged()));

				// Listen to mode changes
B
Benjamin Pasero 已提交
197
				this.toDispose.push(this.textEditorModel.onDidChangeLanguage(() => this.onConfigurationChange())); // mode change can have impact on config
198 199 200

				return model;
			});
E
Erich Gamma 已提交
201 202
		});
	}
203

B
Benjamin Pasero 已提交
204
	private doLoad(content: string): TPromise<UntitledEditorModel> {
205 206 207

		// Create text editor model if not yet done
		if (!this.textEditorModel) {
B
Benjamin Pasero 已提交
208
			return this.createTextEditorModel(content, this.resource, this.modeId).then(model => this);
209 210 211 212 213 214 215
		}

		// Otherwise update
		else {
			this.updateTextEditorModel(content);
		}

B
Benjamin Pasero 已提交
216
		return TPromise.as<UntitledEditorModel>(this);
217
	}
E
Erich Gamma 已提交
218

B
Benjamin Pasero 已提交
219
	private onModelContentChanged(): void {
220
		this.versionId++;
221 222

		// mark the untitled editor as non-dirty once its content becomes empty and we do
B
Benjamin Pasero 已提交
223 224
		// not have an associated path set. we never want dirty indicator in that case.
		if (!this.hasAssociatedFilePath && this.textEditorModel.getLineCount() === 1 && this.textEditorModel.getLineContent(1) === '') {
225
			this.setDirty(false);
E
Erich Gamma 已提交
226
		}
227

228 229 230
		// turn dirty otherwise
		else {
			this.setDirty(true);
231
		}
232

233 234
		// Handle content change event buffered
		this.contentChangeEventScheduler.schedule();
E
Erich Gamma 已提交
235 236 237 238 239
	}

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

240
		this.toDispose = dispose(this.toDispose);
E
Erich Gamma 已提交
241 242
	}
}