files.ts 15.4 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
import { TPromise } from 'vs/base/common/winjs.base';
E
Erich Gamma 已提交
8 9 10 11
import paths = require('vs/base/common/paths');
import URI from 'vs/base/common/uri';
import glob = require('vs/base/common/glob');
import events = require('vs/base/common/events');
12
import { isLinux } from 'vs/base/common/platform';
J
Johannes Rieken 已提交
13
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
E
Erich Gamma 已提交
14

B
Benjamin Pasero 已提交
15
export const IFileService = createDecorator<IFileService>('fileService');
E
Erich Gamma 已提交
16 17

export interface IFileService {
18
	_serviceBrand: any;
E
Erich Gamma 已提交
19 20 21 22 23 24 25 26 27 28 29 30

	/**
	 * Resolve the properties of a file identified by the resource.
	 *
	 * If the optional parameter "resolveTo" is specified in options, the stat service is asked
	 * to provide a stat object that should contain the full graph of folders up to all of the
	 * target resources.
	 *
	 * If the optional parameter "resolveSingleChildDescendants" is specified in options,
	 * the stat service is asked to automatically resolve child folders that only
	 * contain a single element.
	 */
B
Benjamin Pasero 已提交
31
	resolveFile(resource: URI, options?: IResolveFileOptions): TPromise<IFileStat>;
E
Erich Gamma 已提交
32

33 34 35
	/**
	 *Finds out if a file identified by the resource exists.
	 */
B
Benjamin Pasero 已提交
36
	existsFile(resource: URI): TPromise<boolean>;
37

E
Erich Gamma 已提交
38 39 40 41 42
	/**
	 * Resolve the contents of a file identified by the resource.
	 *
	 * The returned object contains properties of the file and the full value as string.
	 */
B
Benjamin Pasero 已提交
43
	resolveContent(resource: URI, options?: IResolveContentOptions): TPromise<IContent>;
E
Erich Gamma 已提交
44

A
Alex Dima 已提交
45 46 47 48 49
	/**
	 * Resolve the contents of a file identified by the resource.
	 *
	 * The returned object contains properties of the file and the value as a readable stream.
	 */
B
Benjamin Pasero 已提交
50
	resolveStreamContent(resource: URI, options?: IResolveContentOptions): TPromise<IStreamContent>;
A
Alex Dima 已提交
51

E
Erich Gamma 已提交
52 53 54
	/**
	 * Returns the contents of all files by the given array of file resources.
	 */
B
Benjamin Pasero 已提交
55
	resolveContents(resources: URI[]): TPromise<IContent[]>;
E
Erich Gamma 已提交
56 57 58 59

	/**
	 * Updates the content replacing its previous value.
	 */
B
Benjamin Pasero 已提交
60
	updateContent(resource: URI, value: string, options?: IUpdateContentOptions): TPromise<IFileStat>;
E
Erich Gamma 已提交
61 62 63 64 65 66

	/**
	 * Moves the file to a new path identified by the resource.
	 *
	 * The optional parameter overwrite can be set to replace an existing file at the location.
	 */
B
Benjamin Pasero 已提交
67
	moveFile(source: URI, target: URI, overwrite?: boolean): TPromise<IFileStat>;
E
Erich Gamma 已提交
68 69 70 71 72 73

	/**
	 * Copies the file to a path identified by the resource.
	 *
	 * The optional parameter overwrite can be set to replace an existing file at the location.
	 */
B
Benjamin Pasero 已提交
74
	copyFile(source: URI, target: URI, overwrite?: boolean): TPromise<IFileStat>;
E
Erich Gamma 已提交
75 76 77 78 79 80 81

	/**
	 * Creates a new file with the given path. The returned promise
	 * will have the stat model object as a result.
	 *
	 * The optional parameter content can be used as value to fill into the new file.
	 */
B
Benjamin Pasero 已提交
82
	createFile(resource: URI, content?: string): TPromise<IFileStat>;
E
Erich Gamma 已提交
83 84 85 86 87

	/**
	 * Creates a new folder with the given path. The returned promise
	 * will have the stat model object as a result.
	 */
B
Benjamin Pasero 已提交
88
	createFolder(resource: URI): TPromise<IFileStat>;
E
Erich Gamma 已提交
89 90 91 92 93

	/**
	 * Renames the provided file to use the new name. The returned promise
	 * will have the stat model object as a result.
	 */
B
Benjamin Pasero 已提交
94
	rename(resource: URI, newName: string): TPromise<IFileStat>;
E
Erich Gamma 已提交
95

96 97 98 99 100 101
	/**
	 * Creates a new empty file if the given path does not exist and otherwise
	 * will set the mtime and atime of the file to the current date.
	 */
	touchFile(resource: URI): TPromise<IFileStat>;

E
Erich Gamma 已提交
102 103 104 105
	/**
	 * Deletes the provided file.  The optional useTrash parameter allows to
	 * move the file to trash.
	 */
B
Benjamin Pasero 已提交
106
	del(resource: URI, useTrash?: boolean): TPromise<void>;
E
Erich Gamma 已提交
107 108 109 110

	/**
	 * Imports the file to the parent identified by the resource.
	 */
B
Benjamin Pasero 已提交
111
	importFile(source: URI, targetFolder: URI): TPromise<IImportResult>;
E
Erich Gamma 已提交
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

	/**
	 * Allows to start a watcher that reports file change events on the provided resource.
	 */
	watchFileChanges(resource: URI): void;

	/**
	 * Allows to stop a watcher on the provided resource or absolute fs path.
	 */
	unwatchFileChanges(resource: URI): void;
	unwatchFileChanges(fsPath: string): void;

	/**
	 * Configures the file service with the provided options.
	 */
	updateOptions(options: any): void;

	/**
	 * Frees up any resources occupied by this service.
	 */
	dispose(): void;
}


/**
 * Possible changes that can occur to a file.
 */
export enum FileChangeType {
	UPDATED = 0,
	ADDED = 1,
	DELETED = 2
}

/**
 * Possible events to subscribe to
 */
B
Benjamin Pasero 已提交
148
export const EventType = {
E
Erich Gamma 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

	/**
	* Send on file changes.
	*/
	FILE_CHANGES: 'files:fileChanges'
};

/**
 * Identifies a single change in a file.
 */
export interface IFileChange {

	/**
	 * The type of change that occured to the file.
	 */
	type: FileChangeType;

	/**
	 * The unified resource identifier of the file that changed.
	 */
	resource: URI;
B
Benjamin Pasero 已提交
170
}
E
Erich Gamma 已提交
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 200 201

export class FileChangesEvent extends events.Event {
	private _changes: IFileChange[];

	constructor(changes: IFileChange[]) {
		super();

		this._changes = changes;
	}

	public get changes() {
		return this._changes;
	}

	/**
	 * Returns true if this change event contains the provided file with the given change type. In case of
	 * type DELETED, this method will also return true if a folder got deleted that is the parent of the
	 * provided file path.
	 */
	public contains(resource: URI, type: FileChangeType): boolean {
		if (!resource) {
			return false;
		}

		return this._changes.some((change) => {
			if (change.type !== type) {
				return false;
			}

			// For deleted also return true when deleted folder is parent of target path
			if (type === FileChangeType.DELETED) {
202
				return isEqual(resource.fsPath, change.resource.fsPath) || isParent(resource.fsPath, change.resource.fsPath);
E
Erich Gamma 已提交
203 204
			}

205
			return isEqual(resource.fsPath, change.resource.fsPath);
E
Erich Gamma 已提交
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 252 253 254 255 256 257 258 259 260 261
		});
	}

	/**
	 * Returns the changes that describe added files.
	 */
	public getAdded(): IFileChange[] {
		return this.getOfType(FileChangeType.ADDED);
	}

	/**
	 * Returns if this event contains added files.
	 */
	public gotAdded(): boolean {
		return this.hasType(FileChangeType.ADDED);
	}

	/**
	 * Returns the changes that describe deleted files.
	 */
	public getDeleted(): IFileChange[] {
		return this.getOfType(FileChangeType.DELETED);
	}

	/**
	 * Returns if this event contains deleted files.
	 */
	public gotDeleted(): boolean {
		return this.hasType(FileChangeType.DELETED);
	}

	/**
	 * Returns the changes that describe updated files.
	 */
	public getUpdated(): IFileChange[] {
		return this.getOfType(FileChangeType.UPDATED);
	}

	/**
	 * Returns if this event contains updated files.
	 */
	public gotUpdated(): boolean {
		return this.hasType(FileChangeType.UPDATED);
	}

	private getOfType(type: FileChangeType): IFileChange[] {
		return this._changes.filter((change) => change.type === type);
	}

	private hasType(type: FileChangeType): boolean {
		return this._changes.some((change) => {
			return change.type === type;
		});
	}
}

262 263 264 265 266 267 268 269 270 271 272 273 274
export function isEqual(path1: string, path2: string) {
	const identityEquals = (path1 === path2);
	if (isLinux || identityEquals) {
		return identityEquals;
	}

	return path1.toLowerCase() === path2.toLowerCase();
}

export function isParent(path: string, candidate: string): boolean {
	return path.indexOf(candidate + paths.nativeSep) === 0;
}

E
Erich Gamma 已提交
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
export interface IBaseStat {

	/**
	 * The unified resource identifier of this file or folder.
	 */
	resource: URI;

	/**
	 * The name which is the last segement
	 * of the {{path}}.
	 */
	name: string;

	/**
	 * The last modifictaion date represented
	 * as millis from unix epoch.
	 */
	mtime: number;

	/**
	 * A unique identifier thet represents the
	 * current state of the file or directory.
	 */
	etag: string;
}

/**
 * A file resource with meta information.
 */
export interface IFileStat extends IBaseStat {

	/**
	 * The resource is a directory. Iff {{true}}
308
	 * {{encoding}} has no meaning.
E
Erich Gamma 已提交
309 310 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
	 */
	isDirectory: boolean;

	/**
	 * Return {{true}} when this is a directory
	 * that is not empty.
	 */
	hasChildren: boolean;

	/**
	 * The children of the file stat or undefined if none.
	 */
	children?: IFileStat[];

	/**
	 * The size of the file if known.
	 */
	size?: number;
}

/**
 * Content and meta information of a file.
 */
export interface IContent extends IBaseStat {

	/**
	 * The content of a text file.
	 */
	value: string;

	/**
B
Benjamin Pasero 已提交
340
	 * The encoding of the content if known.
E
Erich Gamma 已提交
341
	 */
B
Benjamin Pasero 已提交
342
	encoding: string;
E
Erich Gamma 已提交
343 344
}

A
Alex Dima 已提交
345 346 347 348
/**
 * A Stream emitting strings.
 */
export interface IStringStream {
B
Benjamin Pasero 已提交
349 350 351 352
	on(event: 'data', callback: (chunk: string) => void): void;
	on(event: 'error', callback: (err: any) => void): void;
	on(event: 'end', callback: () => void): void;
	on(event: string, callback: any): void;
A
Alex Dima 已提交
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
}

/**
 * Streamable content and meta information of a file.
 */
export interface IStreamContent extends IBaseStat {

	/**
	 * The streamable content of a text file.
	 */
	value: IStringStream;

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

E
Erich Gamma 已提交
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
export interface IResolveContentOptions {

	/**
	 * The optional acceptTextOnly parameter allows to fail this request early if the file
	 * contents are not textual.
	 */
	acceptTextOnly?: boolean;

	/**
	 * The optional etag parameter allows to return a 304 (Not Modified) if the etag matches
	 * with the remote resource. It is the task of the caller to makes sure to handle this
	 * error case from the promise.
	 */
	etag?: string;

	/**
	 * The optional encoding parameter allows to specify the desired encoding when resolving
	 * the contents of the file.
	 */
	encoding?: string;
}

export interface IUpdateContentOptions {

	/**
B
Benjamin Pasero 已提交
396
	 * The encoding to use when updating a file.
E
Erich Gamma 已提交
397
	 */
B
Benjamin Pasero 已提交
398
	encoding?: string;
E
Erich Gamma 已提交
399

B
wip  
Benjamin Pasero 已提交
400
	/**
B
Benjamin Pasero 已提交
401
	 * If set to true, will enforce the selected encoding and not perform any detection using BOMs.
B
wip  
Benjamin Pasero 已提交
402 403 404
	 */
	overwriteEncoding?: boolean;

E
Erich Gamma 已提交
405
	/**
P
Pascal Borreli 已提交
406
	 * Whether to overwrite a file even if it is readonly.
E
Erich Gamma 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
	 */
	overwriteReadonly?: boolean;

	/**
	 * The last known modification time of the file. This can be used to prevent dirty writes.
	 */
	mtime?: number;

	/**
	 * The etag of the file. This can be used to prevent dirty writes.
	 */
	etag?: string;
}

export interface IResolveFileOptions {
	resolveTo?: URI[];
	resolveSingleChildDescendants?: boolean;
}

export interface IImportResult {
	stat: IFileStat;
	isNew: boolean;
}

export interface IFileOperationResult {
	message: string;
	fileOperationResult: FileOperationResult;
}

export enum FileOperationResult {
	FILE_IS_BINARY,
	FILE_IS_DIRECTORY,
	FILE_NOT_FOUND,
	FILE_NOT_MODIFIED_SINCE,
	FILE_MODIFIED_SINCE,
	FILE_MOVE_CONFLICT,
	FILE_READ_ONLY,
B
Benjamin Pasero 已提交
444 445
	FILE_TOO_LARGE,
	FILE_INVALID_PATH
E
Erich Gamma 已提交
446 447
}

448 449
export const MAX_FILE_SIZE = 50 * 1024 * 1024;

450
export const AutoSaveConfiguration = {
451 452
	OFF: 'off',
	AFTER_DELAY: 'afterDelay',
453 454
	ON_FOCUS_CHANGE: 'onFocusChange',
	ON_WINDOW_CHANGE: 'onWindowChange'
B
Benjamin Pasero 已提交
455
};
456

E
Erich Gamma 已提交
457 458
export interface IFilesConfiguration {
	files: {
459
		associations: { [filepattern: string]: string };
E
Erich Gamma 已提交
460
		exclude: glob.IExpression;
461
		watcherExclude: { [filepattern: string]: boolean };
E
Erich Gamma 已提交
462 463
		encoding: string;
		trimTrailingWhitespace: boolean;
464
		autoSave: string;
465
		autoSaveDelay: number;
466
		eol: string;
E
Erich Gamma 已提交
467
	};
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
}

export const SUPPORTED_ENCODINGS: { [encoding: string]: { labelLong: string; labelShort: string; order: number; encodeOnly?: boolean; alias?: string } } = {
	utf8: {
		labelLong: 'UTF-8',
		labelShort: 'UTF-8',
		order: 1,
		alias: 'utf8bom'
	},
	utf8bom: {
		labelLong: 'UTF-8 with BOM',
		labelShort: 'UTF-8 with BOM',
		encodeOnly: true,
		order: 2,
		alias: 'utf8'
	},
	utf16le: {
		labelLong: 'UTF-16 LE',
		labelShort: 'UTF-16 LE',
		order: 3
	},
	utf16be: {
		labelLong: 'UTF-16 BE',
		labelShort: 'UTF-16 BE',
		order: 4
	},
	windows1252: {
		labelLong: 'Western (Windows 1252)',
		labelShort: 'Windows 1252',
		order: 5
	},
	iso88591: {
		labelLong: 'Western (ISO 8859-1)',
		labelShort: 'ISO 8859-1',
		order: 6
	},
	iso88593: {
		labelLong: 'Western (ISO 8859-3)',
		labelShort: 'ISO 8859-3',
		order: 7
	},
	iso885915: {
		labelLong: 'Western (ISO 8859-15)',
		labelShort: 'ISO 8859-15',
		order: 8
	},
	macroman: {
		labelLong: 'Western (Mac Roman)',
		labelShort: 'Mac Roman',
		order: 9
	},
	cp437: {
		labelLong: 'DOS (CP 437)',
		labelShort: 'CP437',
		order: 10
	},
	windows1256: {
		labelLong: 'Arabic (Windows 1256)',
		labelShort: 'Windows 1256',
		order: 11
	},
	iso88596: {
		labelLong: 'Arabic (ISO 8859-6)',
		labelShort: 'ISO 8859-6',
		order: 12
	},
	windows1257: {
		labelLong: 'Baltic (Windows 1257)',
		labelShort: 'Windows 1257',
		order: 13
	},
	iso88594: {
		labelLong: 'Baltic (ISO 8859-4)',
		labelShort: 'ISO 8859-4',
		order: 14
	},
	iso885914: {
		labelLong: 'Celtic (ISO 8859-14)',
		labelShort: 'ISO 8859-14',
		order: 15
	},
	windows1250: {
		labelLong: 'Central European (Windows 1250)',
		labelShort: 'Windows 1250',
		order: 16
	},
	iso88592: {
		labelLong: 'Central European (ISO 8859-2)',
		labelShort: 'ISO 8859-2',
		order: 17
	},
559 560 561 562 563
	cp852: {
		labelLong: 'Central European (CP 852)',
		labelShort: 'CP 852',
		order: 18
	},
564 565 566
	windows1251: {
		labelLong: 'Cyrillic (Windows 1251)',
		labelShort: 'Windows 1251',
567
		order: 19
568 569 570 571
	},
	cp866: {
		labelLong: 'Cyrillic (CP 866)',
		labelShort: 'CP 866',
572
		order: 20
573 574 575 576
	},
	iso88595: {
		labelLong: 'Cyrillic (ISO 8859-5)',
		labelShort: 'ISO 8859-5',
577
		order: 21
578 579 580 581
	},
	koi8r: {
		labelLong: 'Cyrillic (KOI8-R)',
		labelShort: 'KOI8-R',
582
		order: 22
583 584 585 586
	},
	koi8u: {
		labelLong: 'Cyrillic (KOI8-U)',
		labelShort: 'KOI8-U',
587
		order: 23
588 589 590 591
	},
	iso885913: {
		labelLong: 'Estonian (ISO 8859-13)',
		labelShort: 'ISO 8859-13',
592
		order: 24
593 594 595 596
	},
	windows1253: {
		labelLong: 'Greek (Windows 1253)',
		labelShort: 'Windows 1253',
597
		order: 25
598 599 600 601
	},
	iso88597: {
		labelLong: 'Greek (ISO 8859-7)',
		labelShort: 'ISO 8859-7',
602
		order: 26
603 604 605 606
	},
	windows1255: {
		labelLong: 'Hebrew (Windows 1255)',
		labelShort: 'Windows 1255',
607
		order: 27
608 609 610 611
	},
	iso88598: {
		labelLong: 'Hebrew (ISO 8859-8)',
		labelShort: 'ISO 8859-8',
612
		order: 28
613 614 615 616
	},
	iso885910: {
		labelLong: 'Nordic (ISO 8859-10)',
		labelShort: 'ISO 8859-10',
617
		order: 29
618 619 620 621
	},
	iso885916: {
		labelLong: 'Romanian (ISO 8859-16)',
		labelShort: 'ISO 8859-16',
622
		order: 30
623 624 625 626
	},
	windows1254: {
		labelLong: 'Turkish (Windows 1254)',
		labelShort: 'Windows 1254',
627
		order: 31
628 629 630 631
	},
	iso88599: {
		labelLong: 'Turkish (ISO 8859-9)',
		labelShort: 'ISO 8859-9',
632
		order: 32
633 634 635 636
	},
	windows1258: {
		labelLong: 'Vietnamese (Windows 1258)',
		labelShort: 'Windows 1258',
637
		order: 33
638 639 640 641
	},
	gbk: {
		labelLong: 'Chinese (GBK)',
		labelShort: 'GBK',
642
		order: 34
643 644 645 646
	},
	gb18030: {
		labelLong: 'Chinese (GB18030)',
		labelShort: 'GB18030',
647
		order: 35
648 649 650 651
	},
	cp950: {
		labelLong: 'Traditional Chinese (Big5)',
		labelShort: 'Big5',
652
		order: 36
653 654 655 656
	},
	big5hkscs: {
		labelLong: 'Traditional Chinese (Big5-HKSCS)',
		labelShort: 'Big5-HKSCS',
657
		order: 37
658 659 660 661
	},
	shiftjis: {
		labelLong: 'Japanese (Shift JIS)',
		labelShort: 'Shift JIS',
662
		order: 38
663 664 665 666
	},
	eucjp: {
		labelLong: 'Japanese (EUC-JP)',
		labelShort: 'EUC-JP',
667
		order: 39
668 669 670 671
	},
	euckr: {
		labelLong: 'Korean (EUC-KR)',
		labelShort: 'EUC-KR',
672
		order: 40
673 674 675 676
	},
	windows874: {
		labelLong: 'Thai (Windows 874)',
		labelShort: 'Windows 874',
677 678 679
		order: 41
	},
	iso885911: {
680 681
		labelLong: 'Latin/Thai (ISO 8859-11)',
		labelShort: 'ISO 8859-11',
682
		order: 42
683 684 685 686
	},
	'koi8-ru': {
		labelLong: 'Cyrillic (KOI8-RU)',
		labelShort: 'KOI8-RU',
687
		order: 43
688 689 690 691
	},
	'koi8-t': {
		labelLong: 'Tajik (KOI8-T)',
		labelShort: 'KOI8-T',
692
		order: 44
693 694 695 696
	},
	GB2312: {
		labelLong: 'Simplified Chinese (GB 2312)',
		labelShort: 'GB 2312',
697
		order: 45
698
	}
699
};