untitledEditorModel.ts 6.9 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';

J
Johannes Rieken 已提交
7 8 9 10
import { IDisposable } from 'vs/base/common/lifecycle';
import { TPromise } from 'vs/base/common/winjs.base';
import { EditorModel, IEncodingSupport } from 'vs/workbench/common/editor';
import { StringEditorModel } from 'vs/workbench/common/editor/stringEditorModel';
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 } from 'vs/platform/files/common/files';
J
Johannes Rieken 已提交
15 16 17 18 19
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
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';
20
import { RunOnceScheduler } from 'vs/base/common/async';
21
import { IBackupFileService, BACKUP_FILE_RESOLVE_OPTIONS } from 'vs/workbench/services/backup/common/backup';
22
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
E
Erich Gamma 已提交
23 24

export class UntitledEditorModel extends StringEditorModel implements IEncodingSupport {
25 26 27

	public static DEFAULT_CONTENT_CHANGE_BUFFER_DELAY = 1000;

A
Alex Dima 已提交
28
	private textModelChangeListener: IDisposable;
29
	private configurationChangeListener: IDisposable;
E
Erich Gamma 已提交
30 31

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

36 37
	private contentChangeEventScheduler: RunOnceScheduler;

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

41 42
	private hasAssociatedFilePath: boolean;

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

55
		this.hasAssociatedFilePath = hasAssociatedFilePath;
56
		this.dirty = false;
E
Erich Gamma 已提交
57

58
		this._onDidChangeContent = new Emitter<void>();
59
		this._onDidChangeDirty = new Emitter<void>();
60
		this._onDidChangeEncoding = new Emitter<void>();
61

62 63
		this.contentChangeEventScheduler = new RunOnceScheduler(() => this._onDidChangeContent.fire(), UntitledEditorModel.DEFAULT_CONTENT_CHANGE_BUFFER_DELAY);

D
Daniel Imms 已提交
64
		this.registerListeners();
65 66
	}

67 68 69 70
	public get onDidChangeContent(): Event<void> {
		return this._onDidChangeContent.event;
	}

71 72 73 74
	public get onDidChangeDirty(): Event<void> {
		return this._onDidChangeDirty.event;
	}

75 76 77 78
	public get onDidChangeEncoding(): Event<void> {
		return this._onDidChangeEncoding.event;
	}

79 80 81
	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
82 83
		}

84
		return super.getOrCreateMode(modeService, modeId, firstLineText);
85 86
	}

E
Erich Gamma 已提交
87 88 89
	private registerListeners(): void {

		// Config Changes
90
		this.configurationChangeListener = this.configurationService.onDidUpdateConfiguration(e => this.onConfigurationChange(e.config));
E
Erich Gamma 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
	}

	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 {
118
		const oldEncoding = this.getEncoding();
E
Erich Gamma 已提交
119 120 121 122
		this.preferredEncoding = encoding;

		// Emit if it changed
		if (oldEncoding !== this.preferredEncoding) {
123
			this._onDidChangeEncoding.fire();
E
Erich Gamma 已提交
124 125 126 127 128 129 130
		}
	}

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

131 132 133 134 135 136 137 138 139
	private setDirty(dirty: boolean): void {
		if (this.dirty === dirty) {
			return;
		}

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

D
Daniel Imms 已提交
140 141 142 143
	public getResource(): URI {
		return this.resource;
	}

144
	public revert(): void {
145
		this.setDirty(false);
146 147 148

		// Handle content change event buffered
		this.contentChangeEventScheduler.schedule();
149 150
	}

E
Erich Gamma 已提交
151 152
	public load(): TPromise<EditorModel> {

153 154 155
		// Check for backups first
		return this.backupFileService.hasBackup(this.resource).then(hasBackup => {
			if (hasBackup) {
156
				return this.textFileService.resolveTextContent(this.backupFileService.getBackupResource(this.resource), BACKUP_FILE_RESOLVE_OPTIONS).then(rawTextContent => rawTextContent.value.lines.join('\n'));
157 158 159 160 161 162 163
			}

			return null;
		}).then(backupContent => {
			if (backupContent) {
				this.setValue(backupContent);
			}
E
Erich Gamma 已提交
164

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

167 168
			return super.load().then(model => {
				const configuration = this.configurationService.getConfiguration<IFilesConfiguration>();
E
Erich Gamma 已提交
169

170 171
				// Encoding
				this.configuredEncoding = configuration && configuration.files && configuration.files.encoding;
E
Erich Gamma 已提交
172

173 174 175 176 177
				// Listen to content changes
				this.textModelChangeListener = this.textEditorModel.onDidChangeContent(e => this.onModelContentChanged());

				return model;
			});
E
Erich Gamma 已提交
178 179 180
		});
	}

B
Benjamin Pasero 已提交
181
	private onModelContentChanged(): void {
182 183

		// mark the untitled editor as non-dirty once its content becomes empty and we do
B
Benjamin Pasero 已提交
184 185
		// 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) === '') {
186
			this.setDirty(false);
E
Erich Gamma 已提交
187
		}
188

189 190 191
		// turn dirty otherwise
		else {
			this.setDirty(true);
192
		}
193

194 195
		// Handle content change event buffered
		this.contentChangeEventScheduler.schedule();
E
Erich Gamma 已提交
196 197 198 199 200 201
	}

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

		if (this.textModelChangeListener) {
A
Alex Dima 已提交
202
			this.textModelChangeListener.dispose();
E
Erich Gamma 已提交
203 204 205
			this.textModelChangeListener = null;
		}

206 207 208
		if (this.configurationChangeListener) {
			this.configurationChangeListener.dispose();
			this.configurationChangeListener = null;
E
Erich Gamma 已提交
209
		}
210

211 212
		this.contentChangeEventScheduler.dispose();

213
		this._onDidChangeContent.dispose();
214 215
		this._onDidChangeDirty.dispose();
		this._onDidChangeEncoding.dispose();
E
Erich Gamma 已提交
216 217
	}
}