files.ts 9.8 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';
E
Erich Gamma 已提交
11 12 13 14
import {guessMimeTypes} from 'vs/base/common/mime';
import {IModel, IEditorOptions} from 'vs/editor/common/editorCommon';
import {IDisposable} from 'vs/base/common/lifecycle';
import {EncodingMode, EditorInput, IFileEditorInput} from 'vs/workbench/common/editor';
B
Benjamin Pasero 已提交
15
import {IFileStat, IFilesConfiguration} from 'vs/platform/files/common/files';
E
Erich Gamma 已提交
16
import {createDecorator, ServiceIdentifier} from 'vs/platform/instantiation/common/instantiation';
17
import {FileStat} from 'vs/workbench/parts/files/common/viewModel';
E
Erich Gamma 已提交
18 19 20 21 22 23 24 25 26 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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

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

/**
 * 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';

/**
 * Marker ID for model entries.
 */
export const WORKING_FILES_MODEL_ENTRY_CLASS_ID = 'workbench.workingFiles.model.entry.class';

/**
 * 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;

	public abstract setEncoding(encoding: string, mode: EncodingMode): void;

	public abstract getEncoding(): string;
}

export interface IFilesConfiguration extends IFilesConfiguration {
	explorer: {
		workingFiles: {
			maxVisible: number;
			dynamicHeight: boolean;
		};
	};
	editor: IEditorOptions;
}

export interface IWorkingFileModelChangeEvent {
	added?: IWorkingFileEntry[];
	removed?: IWorkingFileEntry[];
}

export interface IWorkingFilesModel {

81
	onModelChange: Event<IWorkingFileModelChangeEvent>;
E
Erich Gamma 已提交
82

83
	onWorkingFileChange: Event<IWorkingFileEntry>;
E
Erich Gamma 已提交
84 85 86 87 88 89 90 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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199

	getEntries(excludeOutOfContext?: boolean): IWorkingFileEntry[];

	first(): IWorkingFileEntry;
	last(): IWorkingFileEntry;
	next(start?: URI): IWorkingFileEntry;
	previous(start?: URI): IWorkingFileEntry;

	getOutOfWorkspaceContextEntries(): IWorkingFileEntry[];

	count(): number;

	addEntry(resource: URI): IWorkingFileEntry;
	addEntry(stat: IFileStat): IWorkingFileEntry;
	addEntry(entry: IWorkingFileEntry): IWorkingFileEntry;
	addEntry(arg1: IFileStat | IWorkingFileEntry | URI): IWorkingFileEntry;

	moveEntry(oldResource: URI, newResource: URI): void;

	removeEntry(resource: URI): IWorkingFileEntry;
	removeEntry(entry: IWorkingFileEntry): IWorkingFileEntry;
	removeEntry(arg1: IWorkingFileEntry | URI): IWorkingFileEntry;

	reorder(source: IWorkingFileEntry, target: IWorkingFileEntry): void;

	hasEntry(resource: URI): boolean;
	findEntry(resource: URI): IWorkingFileEntry;

	clear(): void;
}

export interface IWorkingFileEntry {
	resource: URI;
	index: number;
	dirty: boolean;
	CLASS_ID: string;
	isFile: boolean;
	isUntitled: boolean;

	setIndex(index: number): void;
	setDirty(dirty: boolean): void;
}

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) {
		let stat = <FileStat>obj;

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

	if (obj && (<IWorkingFileEntry>obj).CLASS_ID === WORKING_FILES_MODEL_ENTRY_CLASS_ID) {
		let entry = <IWorkingFileEntry>obj;
		if (entry.isFile) {
			return {
				resource: entry.resource,
				mimes: guessMimeTypes(entry.resource.fsPath),
				isDirectory: false
			};
		}
	}

	return null;
}

/**
 * List of event types from files.
 */
export const EventType = {

	/**
	 * Indicates that a file content has changed but not yet saved.
	 */
	FILE_DIRTY: 'files:fileDirty',

	/**
	 * Indicates that a file is being saved.
	 */
	FILE_SAVING: 'files:fileSaving',

	/**
	 * Indicates that a file save resulted in an error.
	 */
	FILE_SAVE_ERROR: 'files:fileSaveError',

	/**
	 * Indicates that a file content has been saved to the disk.
	 */
	FILE_SAVED: 'files:fileSaved',

	/**
	 * Indicates that a file content has been reverted to the state
	 * on disk.
	 */
	FILE_REVERTED: 'files:fileReverted'
};

/**
 * 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 {

200
	constructor(before?: IFileStat, after?: IFileStat, originalEvent?: BaseEvent) {
E
Erich Gamma 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
		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;
	}
}

/**
 * Text file change events are emitted when files are saved or reverted.
 */
export class TextFileChangeEvent extends LocalFileChangeEvent {
	private _model: IModel;
252
	private _isAutoSaved: boolean;
E
Erich Gamma 已提交
253

254
	constructor(model: IModel, before: IFileStat, after: IFileStat = before, originalEvent?: BaseEvent) {
E
Erich Gamma 已提交
255 256 257 258 259 260 261 262
		super(before, after, originalEvent);

		this._model = model;
	}

	public get model(): IModel {
		return this._model;
	}
263 264 265 266 267 268 269 270

	public setAutoSaved(autoSaved: boolean): void {
		this._isAutoSaved = autoSaved;
	}

	public get isAutoSaved(): boolean {
		return this._isAutoSaved;
	}
E
Erich Gamma 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
}

export const TEXT_FILE_SERVICE_ID = 'textFileService';

export enum ConfirmResult {
	SAVE,
	DONT_SAVE,
	CANCEL
}

export interface ITextFileOperationResult {
	results: IResult[];
}

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

291
export interface IAutoSaveConfiguration {
292 293
	autoSaveDelay: number;
	autoSaveFocusChange: boolean;
294 295
}

E
Erich Gamma 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308 309
export var ITextFileService = createDecorator<ITextFileService>(TEXT_FILE_SERVICE_ID);

export interface ITextFileService extends IDisposable {
	serviceId: ServiceIdentifier<any>;

	/**
	 * 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;

	/**
310
	 * Returns all resources that are currently dirty matching the provided resource or all dirty resources.
E
Erich Gamma 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
	 *
	 * @param resource the resource to check for being dirty. If it is not specified, will check for
	 * all dirty resources.
	 */
	getDirty(resource?: URI): URI[];

	/**
	 * Saves the resource.
	 *
	 * @param resource the resource to save
	 * @return true iff the resource was saved.
	 */
	save(resource: URI): TPromise<boolean>;

	/**
	 * 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>;

	/**
358
	 * Brings up the confirm dialog to either save, don't save or cancel.
E
Erich Gamma 已提交
359 360 361 362 363 364 365 366 367
	 *
	 * @param resource the resource of the file to ask for confirmation.
	 */
	confirmSave(resource?: URI): ConfirmResult;

	/**
	 * Provides access to the list of working files.
	 */
	getWorkingFilesModel(): IWorkingFilesModel;
368 369 370 371 372 373 374 375 376 377

	/**
	 * Checks if the user configured auto save to be enabled or not
	 */
	isAutoSaveEnabled(): boolean;

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