explorerView.ts 28.6 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 nls = require('vs/nls');
J
Johannes Rieken 已提交
8 9
import { TPromise } from 'vs/base/common/winjs.base';
import { Builder, $ } from 'vs/base/browser/builder';
E
Erich Gamma 已提交
10
import URI from 'vs/base/common/uri';
J
Johannes Rieken 已提交
11
import { ThrottledDelayer } from 'vs/base/common/async';
E
Erich Gamma 已提交
12
import errors = require('vs/base/common/errors');
13
import labels = require('vs/base/common/labels');
E
Erich Gamma 已提交
14
import paths = require('vs/base/common/paths');
J
Johannes Rieken 已提交
15 16 17 18 19
import { Action, IActionRunner, IAction } from 'vs/base/common/actions';
import { prepareActions } from 'vs/workbench/browser/actionBarRegistry';
import { ITree } from 'vs/base/parts/tree/browser/tree';
import { Tree } from 'vs/base/parts/tree/browser/treeImpl';
import { IFilesConfiguration } from 'vs/workbench/parts/files/common/files';
20 21
import { FileOperation, FileOperationEvent, IResolveFileOptions, FileChangeType, FileChangesEvent, IFileChange, IFileService } from 'vs/platform/files/common/files';
import { RefreshViewExplorerAction, NewFolderAction, NewFileAction } from 'vs/workbench/parts/files/browser/fileActions';
J
Johannes Rieken 已提交
22
import { FileDragAndDrop, FileFilter, FileSorter, FileController, FileRenderer, FileDataSource, FileViewletState, FileAccessibilityProvider } from 'vs/workbench/parts/files/browser/views/explorerViewer';
E
Erich Gamma 已提交
23
import lifecycle = require('vs/base/common/lifecycle');
24
import { toResource } from 'vs/workbench/common/editor';
25
import { DiffEditorInput } from 'vs/workbench/common/editor/diffEditorInput';
J
Johannes Rieken 已提交
26 27
import { UntitledEditorInput } from 'vs/workbench/common/editor/untitledEditorInput';
import { IEditorGroupService } from 'vs/workbench/services/group/common/groupService';
M
Maxime Quandalle 已提交
28
import * as DOM from 'vs/base/browser/dom';
J
Johannes Rieken 已提交
29 30 31 32
import { CollapseAction, CollapsibleViewletView } from 'vs/workbench/browser/viewlet';
import { FileStat } from 'vs/workbench/parts/files/common/explorerViewModel';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IPartService } from 'vs/workbench/services/part/common/partService';
33
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
J
Johannes Rieken 已提交
34 35 36 37 38 39 40
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IProgressService } from 'vs/platform/progress/common/progress';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { IMessageService, Severity } from 'vs/platform/message/common/message';
import { RawContextKey, IContextKeyService, IContextKey } from 'vs/platform/contextkey/common/contextkey';
J
Johannes Rieken 已提交
41
import { ResourceContextKey } from 'vs/workbench/common/resourceContextKey';
E
Erich Gamma 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55

export class ExplorerView extends CollapsibleViewletView {

	private static EXPLORER_FILE_CHANGES_REACT_DELAY = 500; // delay in ms to react to file changes to give our internal events a chance to react first
	private static EXPLORER_FILE_CHANGES_REFRESH_DELAY = 100; // delay in ms to refresh the explorer from disk file changes
	private static EXPLORER_IMPORT_REFRESH_DELAY = 300; // delay in ms to refresh the explorer from imports

	private static MEMENTO_LAST_ACTIVE_FILE_RESOURCE = 'explorer.memento.lastActiveFileResource';
	private static MEMENTO_EXPANDED_FOLDER_RESOURCES = 'explorer.memento.expandedFolderResources';

	private explorerViewer: ITree;
	private filter: FileFilter;
	private viewletState: FileViewletState;

J
Joao Moreno 已提交
56 57
	private explorerRefreshDelayer: ThrottledDelayer<void>;
	private explorerImportDelayer: ThrottledDelayer<void>;
E
Erich Gamma 已提交
58

59
	private resourceContext: ResourceContextKey;
60
	private folderContext: IContextKey<boolean>;
61

E
Erich Gamma 已提交
62 63
	private shouldRefresh: boolean;

64 65
	private autoReveal: boolean;

E
Erich Gamma 已提交
66 67 68 69 70 71
	private settings: any;

	constructor(
		viewletState: FileViewletState,
		actionRunner: IActionRunner,
		settings: any,
I
isidor 已提交
72
		headerSize: number,
E
Erich Gamma 已提交
73 74 75
		@IMessageService messageService: IMessageService,
		@IContextMenuService contextMenuService: IContextMenuService,
		@IInstantiationService private instantiationService: IInstantiationService,
76
		@IEditorGroupService private editorGroupService: IEditorGroupService,
E
Erich Gamma 已提交
77 78 79 80 81
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
		@IProgressService private progressService: IProgressService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
		@IFileService private fileService: IFileService,
		@IPartService private partService: IPartService,
B
Benjamin Pasero 已提交
82
		@IKeybindingService keybindingService: IKeybindingService,
83
		@IContextKeyService contextKeyService: IContextKeyService,
E
Erich Gamma 已提交
84 85
		@IConfigurationService private configurationService: IConfigurationService
	) {
I
isidor 已提交
86
		super(actionRunner, false, nls.localize('explorerSection', "Files Explorer Section"), messageService, keybindingService, contextMenuService, headerSize);
E
Erich Gamma 已提交
87 88 89 90

		this.settings = settings;
		this.viewletState = viewletState;
		this.actionRunner = actionRunner;
91
		this.autoReveal = true;
E
Erich Gamma 已提交
92

J
Joao Moreno 已提交
93 94
		this.explorerRefreshDelayer = new ThrottledDelayer<void>(ExplorerView.EXPLORER_FILE_CHANGES_REFRESH_DELAY);
		this.explorerImportDelayer = new ThrottledDelayer<void>(ExplorerView.EXPLORER_IMPORT_REFRESH_DELAY);
95 96

		this.resourceContext = instantiationService.createInstance(ResourceContextKey);
97
		this.folderContext = new RawContextKey<boolean>('explorerResourceIsFolder', undefined).bindTo(contextKeyService);
E
Erich Gamma 已提交
98 99 100
	}

	public renderHeader(container: HTMLElement): void {
101
		const titleDiv = $('div.title').appendTo(container);
102
		$('span').text(this.contextService.getWorkspace().name).title(labels.getPathLabel(this.contextService.getWorkspace().resource.fsPath)).appendTo(titleDiv);
I
isidor 已提交
103 104

		super.renderHeader(container);
E
Erich Gamma 已提交
105 106 107 108 109
	}

	public renderBody(container: HTMLElement): void {
		this.treeContainer = super.renderViewTree(container);
		DOM.addClass(this.treeContainer, 'explorer-folders-view');
110
		DOM.addClass(this.treeContainer, 'show-file-icons');
E
Erich Gamma 已提交
111 112 113

		this.tree = this.createViewer($(this.treeContainer));

I
isidor 已提交
114 115 116
		if (this.toolBar) {
			this.toolBar.setActions(prepareActions(this.getActions()), [])();
		}
E
Erich Gamma 已提交
117 118
	}

I
isidor 已提交
119 120
	public getActions(): IAction[] {
		const actions: Action[] = [];
E
Erich Gamma 已提交
121

B
polish  
Benjamin Pasero 已提交
122
		actions.push(this.instantiationService.createInstance(NewFileAction, this.getViewer(), null));
E
Erich Gamma 已提交
123 124 125 126 127 128
		actions.push(this.instantiationService.createInstance(NewFolderAction, this.getViewer(), null));
		actions.push(this.instantiationService.createInstance(RefreshViewExplorerAction, this, 'explorer-action refresh-explorer'));
		actions.push(this.instantiationService.createInstance(CollapseAction, this.getViewer(), true, 'explorer-action collapse-explorer'));

		// Set Order
		for (let i = 0; i < actions.length; i++) {
129
			const action = actions[i];
E
Erich Gamma 已提交
130 131 132
			action.order = 10 * (i + 1);
		}

I
isidor 已提交
133
		return actions;
E
Erich Gamma 已提交
134 135 136
	}

	public create(): TPromise<void> {
B
Benjamin Pasero 已提交
137

138 139 140
		// Update configuration
		const configuration = this.configurationService.getConfiguration<IFilesConfiguration>();
		this.onConfigurationUpdated(configuration);
E
Erich Gamma 已提交
141

142
		// Load and Fill Viewer
143
		return this.doRefresh().then(() => {
E
Erich Gamma 已提交
144

145
			// When the explorer viewer is loaded, listen to changes to the editor input
146
			this.toDispose.push(this.editorGroupService.onEditorsChanged(() => this.onEditorsChanged()));
E
Erich Gamma 已提交
147

148
			// Also handle configuration updates
149
			this.toDispose.push(this.configurationService.onDidUpdateConfiguration(e => this.onConfigurationUpdated(e.config, true)));
E
Erich Gamma 已提交
150 151 152
		});
	}

153
	private onEditorsChanged(): void {
154 155 156 157
		if (!this.autoReveal) {
			return; // do not touch selection or focus if autoReveal === false
		}

158
		let clearSelection = true;
159
		let clearFocus = false;
E
Erich Gamma 已提交
160

161 162 163
		// Handle files
		const activeFile = this.getActiveFile();
		if (activeFile) {
E
Erich Gamma 已提交
164 165

			// Always remember last opened file
166
			this.settings[ExplorerView.MEMENTO_LAST_ACTIVE_FILE_RESOURCE] = activeFile.toString();
E
Erich Gamma 已提交
167

168
			// Select file if input is inside workspace
169 170
			if (this.isVisible && this.contextService.isInsideWorkspace(activeFile)) {
				const selection = this.hasSelection(activeFile);
171
				if (!selection) {
172
					this.select(activeFile).done(null, errors.onUnexpectedError);
E
Erich Gamma 已提交
173
				}
174 175

				clearSelection = false;
E
Erich Gamma 已提交
176 177 178
			}
		}

179
		// Handle closed or untitled file (convince explorer to not reopen any file when getting visible)
180
		const activeInput = this.editorService.getActiveEditorInput();
181
		if (activeInput instanceof UntitledEditorInput || !activeInput) {
182
			this.settings[ExplorerView.MEMENTO_LAST_ACTIVE_FILE_RESOURCE] = void 0;
183
			clearFocus = true;
184 185
		}

E
Erich Gamma 已提交
186
		// Otherwise clear
187
		if (clearSelection) {
188
			this.explorerViewer.clearSelection();
E
Erich Gamma 已提交
189
		}
190 191 192 193

		if (clearFocus) {
			this.explorerViewer.clearFocus();
		}
E
Erich Gamma 已提交
194 195 196
	}

	private onConfigurationUpdated(configuration: IFilesConfiguration, refresh?: boolean): void {
B
Benjamin Pasero 已提交
197 198 199 200
		if (this.isDisposed) {
			return; // guard against possible race condition when config change causes recreate of views
		}

201
		this.autoReveal = configuration && configuration.explorer && configuration.explorer.autoReveal;
E
Erich Gamma 已提交
202 203 204 205 206 207 208 209 210

		// Push down config updates to components of viewer
		let needsRefresh = false;
		if (this.filter) {
			needsRefresh = this.filter.updateConfiguration(configuration);
		}

		// Refresh viewer as needed
		if (refresh && needsRefresh) {
211
			this.doRefresh().done(null, errors.onUnexpectedError);
E
Erich Gamma 已提交
212 213 214
		}
	}

215
	public focusBody(): void {
216
		let keepFocus = false;
217 218 219 220

		// Make sure the current selected element is revealed
		if (this.explorerViewer) {
			if (this.autoReveal) {
221
				const selection = this.explorerViewer.getSelection();
222 223 224 225 226 227 228
				if (selection.length > 0) {
					this.reveal(selection[0], 0.5).done(null, errors.onUnexpectedError);
				}
			}

			// Pass Focus to Viewer
			this.explorerViewer.DOMFocus();
229
			keepFocus = true;
230
		}
E
Erich Gamma 已提交
231

232
		// Open the focused element in the editor if there is currently no file opened
233 234
		const activeFile = this.getActiveFile();
		if (!activeFile) {
235
			this.openFocusedElement(keepFocus);
E
Erich Gamma 已提交
236 237 238 239 240 241 242 243 244 245
		}
	}

	public setVisible(visible: boolean): TPromise<void> {
		return super.setVisible(visible).then(() => {

			// Show
			if (visible) {

				// If a refresh was requested and we are now visible, run it
A
Alex Dima 已提交
246
				let refreshPromise = TPromise.as(null);
E
Erich Gamma 已提交
247
				if (this.shouldRefresh) {
248
					refreshPromise = this.doRefresh();
E
Erich Gamma 已提交
249 250 251
					this.shouldRefresh = false; // Reset flag
				}

252 253 254 255
				if (!this.autoReveal) {
					return refreshPromise; // do not react to setVisible call if autoReveal === false
				}

256
				// Always select the current navigated file in explorer if input is file editor input
257
				// unless autoReveal is set to false
258 259
				const activeFile = this.getActiveFile();
				if (activeFile) {
E
Erich Gamma 已提交
260
					return refreshPromise.then(() => {
261
						return this.select(activeFile);
E
Erich Gamma 已提交
262 263 264 265 266
					});
				}

				// Return now if the workbench has not yet been created - in this case the workbench takes care of restoring last used editors
				if (!this.partService.isCreated()) {
A
Alex Dima 已提交
267
					return TPromise.as(null);
E
Erich Gamma 已提交
268 269 270
				}

				// Otherwise restore last used file: By lastActiveFileResource
271
				const root = this.getInput();
E
Erich Gamma 已提交
272 273 274 275 276 277
				let lastActiveFileResource: URI;
				if (this.settings[ExplorerView.MEMENTO_LAST_ACTIVE_FILE_RESOURCE]) {
					lastActiveFileResource = URI.parse(this.settings[ExplorerView.MEMENTO_LAST_ACTIVE_FILE_RESOURCE]);
				}

				if (lastActiveFileResource && root && root.find(lastActiveFileResource)) {
278
					this.editorService.openEditor({ resource: lastActiveFileResource, options: { revealIfVisible: true } }).done(null, errors.onUnexpectedError);
E
Erich Gamma 已提交
279 280 281 282 283 284

					return refreshPromise;
				}

				// Otherwise restore last used file: By Explorer selection
				return refreshPromise.then(() => {
285
					this.openFocusedElement();
E
Erich Gamma 已提交
286 287 288 289 290
				});
			}
		});
	}

291
	private openFocusedElement(preserveFocus?: boolean): void {
292
		const stat: FileStat = this.explorerViewer.getFocus();
E
Erich Gamma 已提交
293
		if (stat && !stat.isDirectory) {
294
			this.editorService.openEditor({ resource: stat.resource, options: { preserveFocus, revealIfVisible: true } }).done(null, errors.onUnexpectedError);
E
Erich Gamma 已提交
295 296 297
		}
	}

298
	private getActiveFile(): URI {
299
		const input = this.editorService.getActiveEditorInput();
300 301 302 303

		// ignore diff editor inputs (helps to get out of diffing when returning to explorer)
		if (input instanceof DiffEditorInput) {
			return null;
E
Erich Gamma 已提交
304 305
		}

306
		// check for files
307
		return toResource(input, { supportSideBySide: true, filter: 'file' });
E
Erich Gamma 已提交
308 309 310 311 312 313 314
	}

	private getInput(): FileStat {
		return this.explorerViewer ? (<FileStat>this.explorerViewer.getInput()) : null;
	}

	public createViewer(container: Builder): ITree {
B
Benjamin Pasero 已提交
315
		const dataSource = this.instantiationService.createInstance(FileDataSource);
316
		const renderer = this.instantiationService.createInstance(FileRenderer, this.viewletState, this.actionRunner);
B
Benjamin Pasero 已提交
317 318
		const controller = this.instantiationService.createInstance(FileController, this.viewletState);
		const sorter = new FileSorter();
E
Erich Gamma 已提交
319
		this.filter = this.instantiationService.createInstance(FileFilter);
B
Benjamin Pasero 已提交
320 321
		const dnd = this.instantiationService.createInstance(FileDragAndDrop);
		const accessibilityProvider = this.instantiationService.createInstance(FileAccessibilityProvider);
E
Erich Gamma 已提交
322 323

		this.explorerViewer = new Tree(container.getHTMLElement(), {
B
Benjamin Pasero 已提交
324 325 326 327
			dataSource,
			renderer,
			controller,
			sorter,
E
Erich Gamma 已提交
328
			filter: this.filter,
B
Benjamin Pasero 已提交
329 330
			dnd,
			accessibilityProvider
E
Erich Gamma 已提交
331
		}, {
332
				autoExpandSingleChildren: true,
333 334 335
				ariaLabel: nls.localize('treeAriaLabel', "Files Explorer"),
				twistiePixels: 16,
				showTwistie: false
336
			});
E
Erich Gamma 已提交
337 338 339 340

		this.toDispose.push(lifecycle.toDisposable(() => renderer.dispose()));

		// Update Viewer based on File Change Events
341 342
		this.toDispose.push(this.fileService.onAfterOperation(e => this.onFileOperation(e)));
		this.toDispose.push(this.fileService.onFileChanges(e => this.onFileChanges(e)));
E
Erich Gamma 已提交
343

344
		// Update resource context based on focused element
345 346 347 348
		this.toDispose.push(this.explorerViewer.addListener2('focus', (e: { focus: FileStat }) => {
			this.resourceContext.set(e.focus && e.focus.resource);
			this.folderContext.set(e.focus && e.focus.isDirectory);
		}));
349

E
Erich Gamma 已提交
350 351 352
		return this.explorerViewer;
	}

M
Maxime Quandalle 已提交
353
	public getOptimalWidth(): number {
354
		const parentNode = this.explorerViewer.getHTMLElement();
355
		const childNodes = [].slice.call(parentNode.querySelectorAll('.explorer-item > a'));
B
polish  
Benjamin Pasero 已提交
356

M
Maxime Quandalle 已提交
357 358 359
		return DOM.getLargestChildWidth(parentNode, childNodes);
	}

360
	private onFileOperation(e: FileOperationEvent): void {
E
Erich Gamma 已提交
361 362 363 364 365 366
		let modelElement: FileStat;
		let parent: FileStat;
		let parentResource: URI;
		let parentElement: FileStat;

		// Add
367 368
		if (e.operation === FileOperation.CREATE || e.operation === FileOperation.IMPORT || e.operation === FileOperation.COPY) {
			const addedElement = e.target;
E
Erich Gamma 已提交
369 370 371 372 373 374
			parentResource = URI.file(paths.dirname(addedElement.resource.fsPath));
			parentElement = this.getInput().find(parentResource);

			if (parentElement) {

				// Add the new file to its parent (Model)
375
				const childElement = FileStat.create(addedElement);
376
				parentElement.removeChild(childElement); // make sure to remove any previous version of the file if any
E
Erich Gamma 已提交
377 378
				parentElement.addChild(childElement);

379
				const refreshPromise = () => {
E
Erich Gamma 已提交
380 381 382 383 384 385 386 387

					// Refresh the Parent (View)
					return this.explorerViewer.refresh(parentElement).then(() => {
						return this.reveal(childElement, 0.5).then(() => {

							// Focus new element
							this.explorerViewer.setFocus(childElement);

388
							// Open new file in editor (pinned)
E
Erich Gamma 已提交
389
							if (!childElement.isDirectory) {
B
polish  
Benjamin Pasero 已提交
390
								return this.editorService.openEditor({ resource: childElement.resource, options: { pinned: true } });
E
Erich Gamma 已提交
391 392 393 394 395 396
							}
						});
					});
				};

				// For file imports, use a delayer to not refresh too many times when multiple files are imported
397
				if (e.operation === FileOperation.IMPORT) {
E
Erich Gamma 已提交
398 399 400 401 402 403 404 405 406 407 408
					this.explorerImportDelayer.trigger(refreshPromise).done(null, errors.onUnexpectedError);
				}

				// Otherwise just refresh immediately
				else {
					refreshPromise().done(null, errors.onUnexpectedError);
				}
			}
		}

		// Move (including Rename)
409 410 411
		else if (e.operation === FileOperation.MOVE) {
			const oldResource = e.resource;
			const newElement = e.target;
E
Erich Gamma 已提交
412

413
			const oldParentResource = URI.file(paths.dirname(oldResource.fsPath));
414
			const newParentResource = URI.file(paths.dirname(newElement.resource.fsPath));
E
Erich Gamma 已提交
415 416 417

			// Only update focus if renamed/moved element is selected
			let updateFocus = false;
418
			const focus: FileStat = this.explorerViewer.getFocus();
419
			if (focus && focus.resource && focus.resource.toString() === oldResource.toString()) {
E
Erich Gamma 已提交
420 421 422 423 424
				updateFocus = true;
			}

			// Handle Rename
			if (oldParentResource && newParentResource && oldParentResource.toString() === newParentResource.toString()) {
425
				modelElement = this.getInput().find(oldResource);
E
Erich Gamma 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
				if (modelElement) {

					// Rename File (Model)
					modelElement.rename(newElement);

					// Update Parent (View)
					parent = modelElement.parent;
					if (parent) {
						this.explorerViewer.refresh(parent).done(() => {

							// Select in Viewer if set
							if (updateFocus) {
								this.explorerViewer.setFocus(modelElement);
							}
						}, errors.onUnexpectedError);
					}
				}
			}

			// Handle Move
			else if (oldParentResource && newParentResource) {
447 448
				const oldParent = this.getInput().find(oldParentResource);
				const newParent = this.getInput().find(newParentResource);
449
				modelElement = this.getInput().find(oldResource);
E
Erich Gamma 已提交
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469

				if (oldParent && newParent && modelElement) {

					// Move in Model
					modelElement.move(newParent, (callback: () => void) => {

						// Update old parent
						this.explorerViewer.refresh(oldParent, true).done(callback, errors.onUnexpectedError);
					}, () => {

						// Update new parent
						this.explorerViewer.refresh(newParent, true).done(() => {
							return this.explorerViewer.expand(newParent);
						}, errors.onUnexpectedError);
					});
				}
			}
		}

		// Delete
470 471
		else if (e.operation === FileOperation.DELETE) {
			modelElement = this.getInput().find(e.resource);
E
Erich Gamma 已提交
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
			if (modelElement && modelElement.parent) {
				parent = modelElement.parent;

				// Remove Element from Parent (Model)
				parent.removeChild(modelElement);

				// Refresh Parent (View)
				this.explorerViewer.refresh(parent).done(() => {

					// Ensure viewer has keyboard focus if event originates from viewer
					this.explorerViewer.DOMFocus();
				}, errors.onUnexpectedError);
			}
		}
	}

	private onFileChanges(e: FileChangesEvent): void {

490 491 492 493
		// Ensure memento state does not capture a deleted file (we run this from a timeout because
		// delete events can result in UI activity that will fill the memento again when multiple
		// editors are closing)
		setTimeout(() => {
494
			const lastActiveResource: string = this.settings[ExplorerView.MEMENTO_LAST_ACTIVE_FILE_RESOURCE];
495 496 497 498
			if (lastActiveResource && e.contains(URI.parse(lastActiveResource), FileChangeType.DELETED)) {
				this.settings[ExplorerView.MEMENTO_LAST_ACTIVE_FILE_RESOURCE] = null;
			}
		});
E
Erich Gamma 已提交
499 500 501 502

		// Check if an explorer refresh is necessary (delayed to give internal events a chance to react first)
		// Note: there is no guarantee when the internal events are fired vs real ones. Code has to deal with the fact that one might
		// be fired first over the other or not at all.
503
		setTimeout(() => {
E
Erich Gamma 已提交
504 505 506
			if (!this.shouldRefresh && this.shouldRefreshFromEvent(e)) {
				this.refreshFromEvent();
			}
507
		}, ExplorerView.EXPLORER_FILE_CHANGES_REACT_DELAY);
E
Erich Gamma 已提交
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
	}

	private shouldRefreshFromEvent(e: FileChangesEvent): boolean {

		// Filter to the ones we care
		e = this.filterToAddRemovedOnWorkspacePath(e, (event, segments) => {
			if (segments[0] !== '.git') {
				return true; // we like all things outside .git
			}

			return segments.length === 1; // we only care about the .git folder itself
		});

		// We only ever refresh from files/folders that got added or deleted
		if (e.gotAdded() || e.gotDeleted()) {
523 524
			const added = e.getAdded();
			const deleted = e.getDeleted();
E
Erich Gamma 已提交
525

526
			const root = this.getInput();
E
Erich Gamma 已提交
527 528 529 530 531
			if (!root) {
				return false;
			}

			// Check added: Refresh if added file/folder is not part of resolved root and parent is part of it
532
			const ignoredPaths: { [fsPath: string]: boolean } = <{ [fsPath: string]: boolean }>{};
E
Erich Gamma 已提交
533
			for (let i = 0; i < added.length; i++) {
534
				const change = added[i];
E
Erich Gamma 已提交
535 536 537 538 539
				if (!this.contextService.isInsideWorkspace(change.resource)) {
					continue; // out of workspace file
				}

				// Find parent
540
				const parent = paths.dirname(change.resource.fsPath);
E
Erich Gamma 已提交
541 542 543 544 545 546 547

				// Continue if parent was already determined as to be ignored
				if (ignoredPaths[parent]) {
					continue;
				}

				// Compute if parent is visible and added file not yet part of it
548
				const parentStat = root.find(URI.file(parent));
E
Erich Gamma 已提交
549 550 551 552 553 554 555 556 557 558 559 560
				if (parentStat && parentStat.isDirectoryResolved && !root.find(change.resource)) {
					return true;
				}

				// Keep track of path that can be ignored for faster lookup
				if (!parentStat || !parentStat.isDirectoryResolved) {
					ignoredPaths[parent] = true;
				}
			}

			// Check deleted: Refresh if deleted file/folder part of resolved root
			for (let j = 0; j < deleted.length; j++) {
561
				const del = deleted[j];
E
Erich Gamma 已提交
562 563 564 565 566 567 568 569 570 571 572 573 574 575
				if (!this.contextService.isInsideWorkspace(del.resource)) {
					continue; // out of workspace file
				}

				if (root.find(del.resource)) {
					return true;
				}
			}
		}

		return false;
	}

	private filterToAddRemovedOnWorkspacePath(e: FileChangesEvent, fn: (change: IFileChange, workspacePathSegments: string[]) => boolean): FileChangesEvent {
B
polish  
Benjamin Pasero 已提交
576
		return new FileChangesEvent(e.changes.filter(change => {
E
Erich Gamma 已提交
577 578 579 580
			if (change.type === FileChangeType.UPDATED) {
				return false; // we only want added / removed
			}

581
			const workspacePath = this.contextService.toWorkspaceRelativePath(change.resource);
E
Erich Gamma 已提交
582 583 584 585
			if (!workspacePath) {
				return false; // not inside workspace
			}

586
			const segments = workspacePath.split(/\//);
E
Erich Gamma 已提交
587 588 589 590 591 592 593 594 595

			return fn(change, segments);
		}));
	}

	private refreshFromEvent(): void {
		if (this.isVisible) {
			this.explorerRefreshDelayer.trigger(() => {
				if (!this.explorerViewer.getHighlight()) {
596
					return this.doRefresh();
E
Erich Gamma 已提交
597 598
				}

A
Alex Dima 已提交
599
				return TPromise.as(null);
E
Erich Gamma 已提交
600 601 602 603 604 605 606 607 608
			}).done(null, errors.onUnexpectedError);
		} else {
			this.shouldRefresh = true;
		}
	}

	/**
	 * Refresh the contents of the explorer to get up to date data from the disk about the file structure.
	 */
609 610 611 612 613 614 615 616 617 618 619
	public refresh(): TPromise<void> {
		if (!this.explorerViewer || this.explorerViewer.getHighlight()) {
			return TPromise.as(null);
		}

		// Focus
		this.explorerViewer.DOMFocus();

		// Find resource to focus from active editor input if set
		let resourceToFocus: URI;
		if (this.autoReveal) {
620
			resourceToFocus = this.getActiveFile();
621
			if (!resourceToFocus) {
622
				const selection = this.explorerViewer.getSelection();
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
				if (selection && selection.length === 1) {
					resourceToFocus = (<FileStat>selection[0]).resource;
				}
			}
		}

		return this.doRefresh().then(() => {
			if (resourceToFocus) {
				return this.select(resourceToFocus, true);
			}

			return TPromise.as(null);
		});
	}

	private doRefresh(): TPromise<void> {
639 640
		const root = this.getInput();
		const targetsToResolve: URI[] = [];
E
Erich Gamma 已提交
641 642 643
		let targetsToExpand: URI[] = [];

		if (this.settings[ExplorerView.MEMENTO_EXPANDED_FOLDER_RESOURCES]) {
B
Benjamin Pasero 已提交
644
			targetsToExpand = this.settings[ExplorerView.MEMENTO_EXPANDED_FOLDER_RESOURCES].map((e: string) => URI.parse(e));
E
Erich Gamma 已提交
645 646 647 648
		}

		// First time refresh: Receive target through active editor input or selection and also include settings from previous session
		if (!root) {
649 650
			const activeFile = this.getActiveFile();
			if (activeFile) {
651
				targetsToResolve.push(activeFile);
E
Erich Gamma 已提交
652 653 654 655 656 657 658 659 660 661 662 663 664
			}

			if (targetsToExpand.length) {
				targetsToResolve.push(...targetsToExpand);
			}
		}

		// Subsequent refresh: Receive targets through expanded folders in tree
		else {
			this.getResolvedDirectories(root, targetsToResolve);
		}

		// Load Root Stat with given target path configured
665
		const options: IResolveFileOptions = { resolveTo: targetsToResolve };
666
		const promise = this.fileService.resolveFile(this.contextService.getWorkspace().resource, options).then(stat => {
667
			let explorerPromise: TPromise<void>;
E
Erich Gamma 已提交
668 669

			// Convert to model
670
			const modelStat = FileStat.create(stat, options.resolveTo);
E
Erich Gamma 已提交
671 672 673 674 675 676 677 678 679 680

			// First time refresh: The stat becomes the input of the viewer
			if (!root) {
				explorerPromise = this.explorerViewer.setInput(modelStat).then(() => {

					// Make sure to expand all folders that where expanded in the previous session
					if (targetsToExpand) {
						return this.explorerViewer.expandAll(targetsToExpand.map(expand => this.getInput().find(expand)));
					}

A
Alex Dima 已提交
681
					return TPromise.as(null);
E
Erich Gamma 已提交
682 683 684 685 686 687 688 689 690 691
				});
			}

			// Subsequent refresh: Merge stat into our local model and refresh tree
			else {
				FileStat.mergeLocalWithDisk(modelStat, root);

				explorerPromise = this.explorerViewer.refresh(root);
			}

692
			return explorerPromise;
693
		}, (e: any) => TPromise.wrapError(e));
E
Erich Gamma 已提交
694

695
		this.progressService.showWhile(promise, this.partService.isCreated() ? 800 : 3200 /* less ugly initial startup */);
E
Erich Gamma 已提交
696 697 698 699 700 701 702 703 704

		return promise;
	}

	/**
	 * Given a stat, fills an array of path that make all folders below the stat that are resolved directories.
	 */
	private getResolvedDirectories(stat: FileStat, resolvedDirectories: URI[]): void {
		if (stat.isDirectoryResolved) {
705
			if (stat.resource.toString() !== this.contextService.getWorkspace().resource.toString()) {
E
Erich Gamma 已提交
706 707 708

				// Drop those path which are parents of the current one
				for (let i = resolvedDirectories.length - 1; i >= 0; i--) {
709
					const resource = resolvedDirectories[i];
E
Erich Gamma 已提交
710 711 712 713 714 715 716 717 718 719 720
					if (stat.resource.toString().indexOf(resource.toString()) === 0) {
						resolvedDirectories.splice(i);
					}
				}

				// Add to the list of path to resolve
				resolvedDirectories.push(stat.resource);
			}

			// Recurse into children
			for (let i = 0; i < stat.children.length; i++) {
721
				const child = stat.children[i];
E
Erich Gamma 已提交
722 723 724 725 726 727 728 729 730
				this.getResolvedDirectories(child, resolvedDirectories);
			}
		}
	}

	/**
	 * Selects and reveal the file element provided by the given resource if its found in the explorer. Will try to
	 * resolve the path from the disk in case the explorer is not yet expanded to the file yet.
	 */
731
	public select(resource: URI, reveal: boolean = this.autoReveal): TPromise<void> {
E
Erich Gamma 已提交
732 733

		// Require valid path
734
		if (!resource || resource.toString() === this.contextService.getWorkspace().resource.toString()) {
A
Alex Dima 已提交
735
			return TPromise.as(null);
E
Erich Gamma 已提交
736 737 738
		}

		// If path already selected, just reveal and return
739
		const selection = this.hasSelection(resource);
740 741
		if (selection) {
			return reveal ? this.reveal(selection, 0.5) : TPromise.as(null);
E
Erich Gamma 已提交
742 743 744
		}

		// First try to get the stat object from the input to avoid a roundtrip
745
		const root = this.getInput();
E
Erich Gamma 已提交
746
		if (!root) {
A
Alex Dima 已提交
747
			return TPromise.as(null);
E
Erich Gamma 已提交
748 749
		}

750
		const fileStat = root.find(resource);
E
Erich Gamma 已提交
751
		if (fileStat) {
752
			return this.doSelect(fileStat, reveal);
E
Erich Gamma 已提交
753 754 755
		}

		// Stat needs to be resolved first and then revealed
756
		const options: IResolveFileOptions = { resolveTo: [resource] };
757
		return this.fileService.resolveFile(this.contextService.getWorkspace().resource, options).then(stat => {
E
Erich Gamma 已提交
758 759

			// Convert to model
760
			const modelStat = FileStat.create(stat, options.resolveTo);
E
Erich Gamma 已提交
761 762 763 764 765

			// Update Input with disk Stat
			FileStat.mergeLocalWithDisk(modelStat, root);

			// Select and Reveal
B
polish  
Benjamin Pasero 已提交
766 767
			return this.explorerViewer.refresh(root).then(() => this.doSelect(root.find(resource), reveal));

E
Erich Gamma 已提交
768 769 770
		}, (e: any) => this.messageService.show(Severity.Error, e));
	}

771
	private hasSelection(resource: URI): FileStat {
772
		const currentSelection: FileStat[] = this.explorerViewer.getSelection();
B
polish  
Benjamin Pasero 已提交
773

774 775 776 777 778 779 780 781 782 783
		for (let i = 0; i < currentSelection.length; i++) {
			if (currentSelection[i].resource.toString() === resource.toString()) {
				return currentSelection[i];
			}
		}

		return null;
	}

	private doSelect(fileStat: FileStat, reveal: boolean): TPromise<void> {
E
Erich Gamma 已提交
784
		if (!fileStat) {
A
Alex Dima 已提交
785
			return TPromise.as(null);
E
Erich Gamma 已提交
786 787 788 789 790 791 792
		}

		// Special case: we are asked to reveal and select an element that is not visible
		// In this case we take the parent element so that we are at least close to it.
		if (!this.filter.isVisible(this.tree, fileStat)) {
			fileStat = fileStat.parent;
			if (!fileStat) {
A
Alex Dima 已提交
793
				return TPromise.as(null);
E
Erich Gamma 已提交
794 795 796
			}
		}

797 798 799 800 801 802 803 804 805
		// Reveal depending on flag
		let revealPromise: TPromise<void>;
		if (reveal) {
			revealPromise = this.reveal(fileStat, 0.5);
		} else {
			revealPromise = TPromise.as(null);
		}

		return revealPromise.then(() => {
E
Erich Gamma 已提交
806 807 808 809 810 811 812 813 814 815 816
			if (!fileStat.isDirectory) {
				this.explorerViewer.setSelection([fileStat]); // Since folders can not be opened, only select files
			}

			this.explorerViewer.setFocus(fileStat);
		});
	}

	public shutdown(): void {

		// Keep list of expanded folders to restore on next load
817
		const root = this.getInput();
E
Erich Gamma 已提交
818
		if (root) {
819
			const expanded = this.explorerViewer.getExpandedElements()
820
				.filter((e: FileStat) => e.resource.toString() !== this.contextService.getWorkspace().resource.toString())
E
Erich Gamma 已提交
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
				.map((e: FileStat) => e.resource.toString());

			this.settings[ExplorerView.MEMENTO_EXPANDED_FOLDER_RESOURCES] = expanded;
		}

		super.shutdown();
	}

	public dispose(): void {
		if (this.toolBar) {
			this.toolBar.dispose();
		}

		super.dispose();
	}
J
Johannes Rieken 已提交
836
}