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

import {TPromise} from 'vs/base/common/winjs.base';
8
import {Event as BaseEvent, PropertyChangeEvent} from 'vs/base/common/events';
E
Erich Gamma 已提交
9
import URI from 'vs/base/common/uri';
B
Benjamin Pasero 已提交
10
import Event from 'vs/base/common/event';
B
Benjamin Pasero 已提交
11
import {IEditorOptions, IRawText} from 'vs/editor/common/editorCommon';
E
Erich Gamma 已提交
12
import {IDisposable} from 'vs/base/common/lifecycle';
13
import {IEncodingSupport, EncodingMode, EditorInput, IFileEditorInput, ConfirmResult, IWorkbenchEditorConfiguration, IEditorDescriptor} from 'vs/workbench/common/editor';
A
Alex Dima 已提交
14
import {IFileStat, IFilesConfiguration, IBaseStat, IResolveContentOptions} from 'vs/platform/files/common/files';
15
import {createDecorator} from 'vs/platform/instantiation/common/instantiation';
16
import {FileStat} from 'vs/workbench/parts/files/common/explorerViewModel';
A
Alex Dima 已提交
17
import {RawContextKey} from 'vs/platform/contextkey/common/contextkey';
18
import {ITextEditorModel} from 'vs/platform/editor/common/editor';
E
Erich Gamma 已提交
19 20 21 22 23 24

/**
 * Explorer viewlet id.
 */
export const VIEWLET_ID = 'workbench.view.explorer';

A
Alex Dima 已提交
25
export const ExplorerViewletVisible = new RawContextKey<boolean>('explorerViewletVisible', true);
A
Alex Dima 已提交
26

E
Erich Gamma 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
/**
 * File editor input id.
 */
export const FILE_EDITOR_INPUT_ID = 'workbench.editors.files.fileEditorInput';

/**
 * Text file editor id.
 */
export const TEXT_FILE_EDITOR_ID = 'workbench.editors.files.textFileEditor';

/**
 * Binary file editor id.
 */
export const BINARY_FILE_EDITOR_ID = 'workbench.editors.files.binaryFileEditor';

/**
 * API class to denote file editor inputs. Internal implementation is provided.
 *
 * Note: This class is not intended to be instantiated.
 */
export abstract class FileEditorInput extends EditorInput implements IFileEditorInput {

	public abstract setResource(resource: URI): void;

	public abstract getResource(): URI;

	public abstract setMime(mime: string): void;

	public abstract getMime(): string;

57 58
	public abstract setPreferredEncoding(encoding: string): void;

E
Erich Gamma 已提交
59 60 61 62 63
	public abstract setEncoding(encoding: string, mode: EncodingMode): void;

	public abstract getEncoding(): string;
}

64
export interface IFilesConfiguration extends IFilesConfiguration, IWorkbenchEditorConfiguration {
E
Erich Gamma 已提交
65
	explorer: {
I
isidor 已提交
66
		openEditors: {
67
			visible: number;
E
Erich Gamma 已提交
68 69
			dynamicHeight: boolean;
		};
70
		autoReveal: boolean;
71
		enableDragAndDrop: boolean;
E
Erich Gamma 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
	};
	editor: IEditorOptions;
}

export interface IFileResource {
	resource: URI;
	isDirectory: boolean;
	mimes: string[];
}

/**
 * Helper to get a file resource from an object.
 */
export function asFileResource(obj: any): IFileResource {
	if (obj instanceof FileStat) {
B
Benjamin Pasero 已提交
87
		const stat = <FileStat>obj;
E
Erich Gamma 已提交
88 89 90 91 92 93 94 95 96 97 98

		return {
			resource: stat.resource,
			mimes: stat.mime ? stat.mime.split(', ') : [],
			isDirectory: stat.isDirectory
		};
	}

	return null;
}

99 100 101 102 103 104 105 106 107 108 109
/**
 * The save error handler can be installed on the text text file editor model to install code that executes when save errors occur.
 */
export interface ISaveErrorHandler {

	/**
	 * Called whenever a save fails.
	 */
	onSaveError(error: any, model: ITextFileEditorModel): void;
}

B
Benjamin Pasero 已提交
110
export interface ISaveParticipant {
E
Erich Gamma 已提交
111 112

	/**
B
Benjamin Pasero 已提交
113
	 * Participate in a save of a model. Allows to change the model before it is being saved to disk.
E
Erich Gamma 已提交
114
	 */
J
Johannes Rieken 已提交
115
	participate(model: ITextFileEditorModel, env: { isAutoSaved: boolean }): TPromise<any>;
B
Benjamin Pasero 已提交
116
}
E
Erich Gamma 已提交
117

B
Benjamin Pasero 已提交
118 119 120 121 122 123 124 125 126 127 128
/**
 * States the text text file editor model can be in.
 */
export enum ModelState {
	SAVED,
	DIRTY,
	PENDING_SAVE,
	CONFLICT,
	ERROR
}

E
Erich Gamma 已提交
129 130 131 132 133 134 135
/**
 * Local file change events are being emitted when a file is added, removed, moved or its contents got updated. These events
 * are being emitted from within the workbench and are not reflecting the truth on the disk file system. For that, please
 * use FileChangesEvent instead.
 */
export class LocalFileChangeEvent extends PropertyChangeEvent {

136
	constructor(before?: IFileStat, after?: IFileStat, originalEvent?: BaseEvent) {
E
Erich Gamma 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
		super(null, before, after, originalEvent);
	}

	/**
	 * Returns the meta information of the file before the event occurred or null if the file is new.
	 */
	public getBefore(): IFileStat {
		return this.oldValue;
	}

	/**
	 * Returns the meta information of the file after the event occurred or null if the file got deleted.
	 */
	public getAfter(): IFileStat {
		return this.newValue;
	}

	/**
	 * Indicates if the file was added as a new file.
	 */
	public gotAdded(): boolean {
		return !this.oldValue && !!this.newValue;
	}

	/**
	 * Indicates if the file was moved to a different path.
	 */
	public gotMoved(): boolean {
		return !!this.oldValue && !!this.newValue && this.oldValue.resource.toString() !== this.newValue.resource.toString();
	}

	/**
	 * Indicates if the files metadata was updated.
	 */
	public gotUpdated(): boolean {
		return !!this.oldValue && !!this.newValue && !this.gotMoved() && this.oldValue !== this.newValue;
	}

	/**
	 * Indicates if the file was deleted.
	 */
	public gotDeleted(): boolean {
		return !!this.oldValue && !this.newValue;
	}
}

183 184 185 186 187
export enum StateChange {
	DIRTY,
	SAVING,
	SAVE_ERROR,
	SAVED,
188 189
	REVERTED,
	ENCODING
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
}

export class TextFileModelChangeEvent {
	private _resource: URI;
	private _kind: StateChange;

	constructor(model: ITextFileEditorModel, kind: StateChange) {
		this._resource = model.getResource();
		this._kind = kind;
	}

	public get resource(): URI {
		return this._resource;
	}

	public get kind(): StateChange {
		return this._kind;
	}
}

E
Erich Gamma 已提交
210 211 212 213 214 215 216 217 218 219 220 221
export const TEXT_FILE_SERVICE_ID = 'textFileService';

export interface ITextFileOperationResult {
	results: IResult[];
}

export interface IResult {
	source: URI;
	target?: URI;
	success?: boolean;
}

222
export interface IAutoSaveConfiguration {
223 224
	autoSaveDelay: number;
	autoSaveFocusChange: boolean;
225
	autoSaveApplicationChange: boolean;
226 227
}

228 229 230 231
export enum AutoSaveMode {
	OFF,
	AFTER_SHORT_DELAY,
	AFTER_LONG_DELAY,
232 233
	ON_FOCUS_CHANGE,
	ON_WINDOW_CHANGE
234 235
}

B
Benjamin Pasero 已提交
236 237 238 239 240 241 242
export enum SaveReason {
	EXPLICIT,
	AUTO,
	FOCUS_CHANGE,
	WINDOW_CHANGE
}

243 244
export interface IFileEditorDescriptor extends IEditorDescriptor {
	getMimeTypes(): string[];
245 246
}

B
Benjamin Pasero 已提交
247
export const ITextFileService = createDecorator<ITextFileService>(TEXT_FILE_SERVICE_ID);
E
Erich Gamma 已提交
248

A
Alex Dima 已提交
249
export interface IRawTextContent extends IBaseStat {
250

A
Alex Dima 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
	/**
	 * The line grouped content of a text file.
	 */
	value: IRawText;

	/**
	 * The line grouped logical hash of a text file.
	 */
	valueLogicalHash: string;

	/**
	 * The encoding of the content if known.
	 */
	encoding: string;
}

267 268
export interface ITextFileEditorModelManager {

269 270 271 272
	onModelDirty: Event<TextFileModelChangeEvent>;
	onModelSaveError: Event<TextFileModelChangeEvent>;
	onModelSaved: Event<TextFileModelChangeEvent>;
	onModelReverted: Event<TextFileModelChangeEvent>;
273
	onModelEncodingChanged: Event<TextFileModelChangeEvent>;
274

275 276 277 278
	get(resource: URI): ITextFileEditorModel;

	getAll(resource?: URI): ITextFileEditorModel[];

279
	loadOrCreate(resource: URI, preferredEncoding: string, refresh?: boolean): TPromise<ITextEditorModel>;
280 281
}

B
Benjamin Pasero 已提交
282 283 284 285 286 287
export interface IModelSaveOptions {
	reason?: SaveReason;
	overwriteReadonly?: boolean;
	overwriteEncoding?: boolean;
}

288 289
export interface ITextFileEditorModel extends ITextEditorModel, IEncodingSupport {

290 291
	onDidStateChange: Event<StateChange>;

292 293 294 295 296 297 298 299
	getResource(): URI;

	getLastSaveAttemptTime(): number;

	getLastModifiedTime(): number;

	getState(): ModelState;

300 301
	updatePreferredEncoding(encoding: string): void;

B
Benjamin Pasero 已提交
302
	save(options?: IModelSaveOptions): TPromise<void>;
303 304 305 306 307 308 309

	revert(): TPromise<void>;

	setConflictResolutionMode();

	getValue(): string;

310 311 312 313 314 315 316
	isDirty(): boolean;

	isResolved(): boolean;

	isDisposed(): boolean;
}

317 318 319 320 321 322 323 324 325
export interface ISaveOptions {

	/**
	 * Save the file on disk even if not dirty. If the file is not dirty, it will be touched
	 * so that mtime and atime are updated. This helps to trigger external file watchers.
	 */
	force: boolean;
}

E
Erich Gamma 已提交
326
export interface ITextFileService extends IDisposable {
327
	_serviceBrand: any;
328 329
	onAutoSaveConfigurationChange: Event<IAutoSaveConfiguration>;
	onFilesAssociationChange: Event<void>;
E
Erich Gamma 已提交
330

331 332 333 334 335
	/**
	 * Access to the manager of text file editor models providing further methods to work with them.
	 */
	models: ITextFileEditorModelManager;

A
Alex Dima 已提交
336 337 338 339 340
	/**
	 * Resolve the contents of a file identified by the resource.
	 */
	resolveTextContent(resource: URI, options?: IResolveContentOptions): TPromise<IRawTextContent>;

E
Erich Gamma 已提交
341 342 343 344 345 346 347 348 349
	/**
	 * A resource is dirty if it has unsaved changes or is an untitled file not yet saved.
	 *
	 * @param resource the resource to check for being dirty. If it is not specified, will check for
	 * all dirty resources.
	 */
	isDirty(resource?: URI): boolean;

	/**
350
	 * Returns all resources that are currently dirty matching the provided resources or all dirty resources.
E
Erich Gamma 已提交
351
	 *
352
	 * @param resources the resources to check for being dirty. If it is not specified, will check for
E
Erich Gamma 已提交
353 354
	 * all dirty resources.
	 */
355
	getDirty(resources?: URI[]): URI[];
E
Erich Gamma 已提交
356 357 358 359 360 361 362

	/**
	 * Saves the resource.
	 *
	 * @param resource the resource to save
	 * @return true iff the resource was saved.
	 */
363
	save(resource: URI, options?: ISaveOptions): TPromise<boolean>;
E
Erich Gamma 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397

	/**
	 * Saves the provided resource asking the user for a file name.
	 *
	 * @param resource the resource to save as.
	 * @return true iff the file was saved.
	 */
	saveAs(resource: URI, targetResource?: URI): TPromise<URI>;

	/**
	 * Saves the set of resources and returns a promise with the operation result.
	 *
	 * @param resources can be null to save all.
	 * @param includeUntitled to save all resources and optionally exclude untitled ones.
	 */
	saveAll(includeUntitled?: boolean): TPromise<ITextFileOperationResult>;
	saveAll(resources: URI[]): TPromise<ITextFileOperationResult>;

	/**
	 * Reverts the provided resource.
	 *
	 * @param resource the resource of the file to revert.
	 * @param force to force revert even when the file is not dirty
	 */
	revert(resource: URI, force?: boolean): TPromise<boolean>;

	/**
	 * Reverts all the provided resources and returns a promise with the operation result.
	 *
	 * @param force to force revert even when the file is not dirty
	 */
	revertAll(resources?: URI[], force?: boolean): TPromise<ITextFileOperationResult>;

	/**
398
	 * Brings up the confirm dialog to either save, don't save or cancel.
E
Erich Gamma 已提交
399
	 *
400 401
	 * @param resources the resources of the files to ask for confirmation or null if
	 * confirming for all dirty resources.
E
Erich Gamma 已提交
402
	 */
403
	confirmSave(resources?: URI[]): ConfirmResult;
E
Erich Gamma 已提交
404

405
	/**
406
	 * Convinient fast access to the current auto save mode.
407
	 */
408
	getAutoSaveMode(): AutoSaveMode;
409 410

	/**
411
	 * Convinient fast access to the raw configured auto save settings.
412 413
	 */
	getAutoSaveConfiguration(): IAutoSaveConfiguration;
E
Erich Gamma 已提交
414
}