explorerView.ts 30.3 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
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';
19
import { IFilesConfiguration, ExplorerFolderContext, FilesExplorerFocussedContext, ExplorerFocussedContext } from 'vs/workbench/parts/files/common/files';
20
import { FileOperation, FileOperationEvent, IResolveFileOptions, FileChangeType, FileChangesEvent, IFileChange, IFileService, isEqualOrParent } from 'vs/platform/files/common/files';
21
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';
23
import { toResource } from 'vs/workbench/common/editor';
24
import { DiffEditorInput } from 'vs/workbench/common/editor/diffEditorInput';
J
Johannes Rieken 已提交
25 26
import { UntitledEditorInput } from 'vs/workbench/common/editor/untitledEditorInput';
import { IEditorGroupService } from 'vs/workbench/services/group/common/groupService';
M
Maxime Quandalle 已提交
27
import * as DOM from 'vs/base/browser/dom';
J
Johannes Rieken 已提交
28 29
import { CollapseAction, CollapsibleViewletView } from 'vs/workbench/browser/viewlet';
import { FileStat } from 'vs/workbench/parts/files/common/explorerViewModel';
30
import { IListService } from 'vs/platform/list/browser/listService';
J
Johannes Rieken 已提交
31 32
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
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';
40
import { IContextKeyService, IContextKey } from 'vs/platform/contextkey/common/contextkey';
J
Johannes Rieken 已提交
41
import { ResourceContextKey } from 'vs/workbench/common/resourceContextKey';
42
import { IWorkbenchThemeService, IFileIconTheme } from 'vs/workbench/services/themes/common/workbenchThemeService';
43
import { isLinux } from 'vs/base/common/platform';
44 45
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { attachListStyler } from 'vs/platform/theme/common/styler';
E
Erich Gamma 已提交
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';

B
Benjamin Pasero 已提交
56 57
	private static COMMON_SCM_FOLDERS = ['.git', '.svn', '.hg'];

E
Erich Gamma 已提交
58 59 60 61
	private explorerViewer: ITree;
	private filter: FileFilter;
	private viewletState: FileViewletState;

J
Joao Moreno 已提交
62 63
	private explorerRefreshDelayer: ThrottledDelayer<void>;
	private explorerImportDelayer: ThrottledDelayer<void>;
E
Erich Gamma 已提交
64

65
	private resourceContext: ResourceContextKey;
66
	private folderContext: IContextKey<boolean>;
67

68
	private filesExplorerFocussedContext: IContextKey<boolean>;
69 70
	private explorerFocussedContext: IContextKey<boolean>;

E
Erich Gamma 已提交
71 72
	private shouldRefresh: boolean;

73 74
	private autoReveal: boolean;

E
Erich Gamma 已提交
75 76 77 78 79 80
	private settings: any;

	constructor(
		viewletState: FileViewletState,
		actionRunner: IActionRunner,
		settings: any,
I
isidor 已提交
81
		headerSize: number,
E
Erich Gamma 已提交
82 83 84
		@IMessageService messageService: IMessageService,
		@IContextMenuService contextMenuService: IContextMenuService,
		@IInstantiationService private instantiationService: IInstantiationService,
85
		@IEditorGroupService private editorGroupService: IEditorGroupService,
E
Erich Gamma 已提交
86 87
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
		@IProgressService private progressService: IProgressService,
88
		@IListService private listService: IListService,
E
Erich Gamma 已提交
89 90 91
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
		@IFileService private fileService: IFileService,
		@IPartService private partService: IPartService,
B
Benjamin Pasero 已提交
92
		@IKeybindingService keybindingService: IKeybindingService,
93
		@IContextKeyService contextKeyService: IContextKeyService,
94
		@IConfigurationService private configurationService: IConfigurationService,
95 96
		@IWorkbenchThemeService private themeService: IWorkbenchThemeService,
		@IEnvironmentService private environmentService: IEnvironmentService
E
Erich Gamma 已提交
97
	) {
I
isidor 已提交
98
		super(actionRunner, false, nls.localize('explorerSection', "Files Explorer Section"), messageService, keybindingService, contextMenuService, headerSize);
E
Erich Gamma 已提交
99 100 101 102

		this.settings = settings;
		this.viewletState = viewletState;
		this.actionRunner = actionRunner;
103
		this.autoReveal = true;
E
Erich Gamma 已提交
104

J
Joao Moreno 已提交
105 106
		this.explorerRefreshDelayer = new ThrottledDelayer<void>(ExplorerView.EXPLORER_FILE_CHANGES_REFRESH_DELAY);
		this.explorerImportDelayer = new ThrottledDelayer<void>(ExplorerView.EXPLORER_IMPORT_REFRESH_DELAY);
107 108

		this.resourceContext = instantiationService.createInstance(ResourceContextKey);
109
		this.folderContext = ExplorerFolderContext.bindTo(contextKeyService);
110 111

		this.filesExplorerFocussedContext = FilesExplorerFocussedContext.bindTo(contextKeyService);
112
		this.explorerFocussedContext = ExplorerFocussedContext.bindTo(contextKeyService);
E
Erich Gamma 已提交
113 114 115
	}

	public renderHeader(container: HTMLElement): void {
116
		const titleDiv = $('div.title').appendTo(container);
117
		$('span').text(this.contextService.getWorkspace().name).title(labels.getPathLabel(this.contextService.getWorkspace().resource.fsPath, void 0, this.environmentService)).appendTo(titleDiv);
I
isidor 已提交
118 119

		super.renderHeader(container);
E
Erich Gamma 已提交
120 121 122 123 124
	}

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

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

I
isidor 已提交
129 130 131
		if (this.toolBar) {
			this.toolBar.setActions(prepareActions(this.getActions()), [])();
		}
132 133 134 135 136

		const onFileIconThemeChange = (fileIconTheme: IFileIconTheme) => {
			DOM.toggleClass(this.treeContainer, 'align-icons-and-twisties', fileIconTheme.hasFileIcons && !fileIconTheme.hasFolderIcons);
		};

137
		this.toDispose.push(this.themeService.onDidFileIconThemeChange(onFileIconThemeChange));
138
		onFileIconThemeChange(this.themeService.getFileIconTheme());
E
Erich Gamma 已提交
139 140
	}

I
isidor 已提交
141 142
	public getActions(): IAction[] {
		const actions: Action[] = [];
E
Erich Gamma 已提交
143

B
polish  
Benjamin Pasero 已提交
144
		actions.push(this.instantiationService.createInstance(NewFileAction, this.getViewer(), null));
E
Erich Gamma 已提交
145 146 147 148 149 150
		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++) {
151
			const action = actions[i];
E
Erich Gamma 已提交
152 153 154
			action.order = 10 * (i + 1);
		}

I
isidor 已提交
155
		return actions;
E
Erich Gamma 已提交
156 157 158
	}

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

160 161 162
		// Update configuration
		const configuration = this.configurationService.getConfiguration<IFilesConfiguration>();
		this.onConfigurationUpdated(configuration);
E
Erich Gamma 已提交
163

164
		// Load and Fill Viewer
165
		return this.doRefresh().then(() => {
E
Erich Gamma 已提交
166

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

170
			// Also handle configuration updates
171
			this.toDispose.push(this.configurationService.onDidUpdateConfiguration(e => this.onConfigurationUpdated(e.config, true)));
E
Erich Gamma 已提交
172 173 174
		});
	}

175
	private onEditorsChanged(): void {
176 177 178 179
		if (!this.autoReveal) {
			return; // do not touch selection or focus if autoReveal === false
		}

180
		let clearSelection = true;
181
		let clearFocus = false;
E
Erich Gamma 已提交
182

183 184 185
		// Handle files
		const activeFile = this.getActiveFile();
		if (activeFile) {
E
Erich Gamma 已提交
186 187

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

190
			// Select file if input is inside workspace
191 192
			if (this.isVisible && this.contextService.isInsideWorkspace(activeFile)) {
				const selection = this.hasSelection(activeFile);
193
				if (!selection) {
194
					this.select(activeFile).done(null, errors.onUnexpectedError);
E
Erich Gamma 已提交
195
				}
196 197

				clearSelection = false;
E
Erich Gamma 已提交
198 199 200
			}
		}

201
		// Handle closed or untitled file (convince explorer to not reopen any file when getting visible)
202
		const activeInput = this.editorService.getActiveEditorInput();
203
		if (activeInput instanceof UntitledEditorInput || !activeInput) {
204
			this.settings[ExplorerView.MEMENTO_LAST_ACTIVE_FILE_RESOURCE] = void 0;
205
			clearFocus = true;
206 207
		}

E
Erich Gamma 已提交
208
		// Otherwise clear
209
		if (clearSelection) {
210
			this.explorerViewer.clearSelection();
E
Erich Gamma 已提交
211
		}
212 213 214 215

		if (clearFocus) {
			this.explorerViewer.clearFocus();
		}
E
Erich Gamma 已提交
216 217 218
	}

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

223
		this.autoReveal = configuration && configuration.explorer && configuration.explorer.autoReveal;
E
Erich Gamma 已提交
224 225 226 227 228 229 230 231 232

		// 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) {
233
			this.doRefresh().done(null, errors.onUnexpectedError);
E
Erich Gamma 已提交
234 235 236
		}
	}

237
	public focusBody(): void {
238
		let keepFocus = false;
239 240 241 242

		// Make sure the current selected element is revealed
		if (this.explorerViewer) {
			if (this.autoReveal) {
243
				const selection = this.explorerViewer.getSelection();
244 245 246 247 248 249 250
				if (selection.length > 0) {
					this.reveal(selection[0], 0.5).done(null, errors.onUnexpectedError);
				}
			}

			// Pass Focus to Viewer
			this.explorerViewer.DOMFocus();
251
			keepFocus = true;
252
		}
E
Erich Gamma 已提交
253

254
		// Open the focused element in the editor if there is currently no file opened
255 256
		const activeFile = this.getActiveFile();
		if (!activeFile) {
257
			this.openFocusedElement(keepFocus);
E
Erich Gamma 已提交
258 259 260 261 262 263 264 265 266 267
		}
	}

	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
268
				let refreshPromise = TPromise.as<void>(null);
E
Erich Gamma 已提交
269
				if (this.shouldRefresh) {
270
					refreshPromise = this.doRefresh();
E
Erich Gamma 已提交
271 272 273
					this.shouldRefresh = false; // Reset flag
				}

274 275 276 277
				if (!this.autoReveal) {
					return refreshPromise; // do not react to setVisible call if autoReveal === false
				}

278
				// Always select the current navigated file in explorer if input is file editor input
279
				// unless autoReveal is set to false
280 281
				const activeFile = this.getActiveFile();
				if (activeFile) {
E
Erich Gamma 已提交
282
					return refreshPromise.then(() => {
283
						return this.select(activeFile);
E
Erich Gamma 已提交
284 285 286 287 288
					});
				}

				// 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 已提交
289
					return TPromise.as(null);
E
Erich Gamma 已提交
290 291 292 293 294 295 296 297
				}

				// Otherwise restore last used file: By lastActiveFileResource
				let lastActiveFileResource: URI;
				if (this.settings[ExplorerView.MEMENTO_LAST_ACTIVE_FILE_RESOURCE]) {
					lastActiveFileResource = URI.parse(this.settings[ExplorerView.MEMENTO_LAST_ACTIVE_FILE_RESOURCE]);
				}

298
				if (lastActiveFileResource && this.root && this.root.find(lastActiveFileResource)) {
299
					this.editorService.openEditor({ resource: lastActiveFileResource, options: { revealIfVisible: true } }).done(null, errors.onUnexpectedError);
E
Erich Gamma 已提交
300 301 302 303 304 305

					return refreshPromise;
				}

				// Otherwise restore last used file: By Explorer selection
				return refreshPromise.then(() => {
306
					this.openFocusedElement();
E
Erich Gamma 已提交
307 308
				});
			}
309
			return undefined;
E
Erich Gamma 已提交
310 311 312
		});
	}

313
	private openFocusedElement(preserveFocus?: boolean): void {
314
		const stat: FileStat = this.explorerViewer.getFocus();
E
Erich Gamma 已提交
315
		if (stat && !stat.isDirectory) {
316
			this.editorService.openEditor({ resource: stat.resource, options: { preserveFocus, revealIfVisible: true } }).done(null, errors.onUnexpectedError);
E
Erich Gamma 已提交
317 318 319
		}
	}

320
	private getActiveFile(): URI {
321
		const input = this.editorService.getActiveEditorInput();
322 323 324 325

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

328
		// check for files
329
		return toResource(input, { supportSideBySide: true, filter: 'file' });
E
Erich Gamma 已提交
330 331
	}

332
	private get root(): FileStat {
E
Erich Gamma 已提交
333 334 335 336
		return this.explorerViewer ? (<FileStat>this.explorerViewer.getInput()) : null;
	}

	public createViewer(container: Builder): ITree {
B
Benjamin Pasero 已提交
337
		const dataSource = this.instantiationService.createInstance(FileDataSource);
338
		const renderer = this.instantiationService.createInstance(FileRenderer, this.viewletState);
B
Benjamin Pasero 已提交
339 340
		const controller = this.instantiationService.createInstance(FileController, this.viewletState);
		const sorter = new FileSorter();
E
Erich Gamma 已提交
341
		this.filter = this.instantiationService.createInstance(FileFilter);
B
Benjamin Pasero 已提交
342 343
		const dnd = this.instantiationService.createInstance(FileDragAndDrop);
		const accessibilityProvider = this.instantiationService.createInstance(FileAccessibilityProvider);
E
Erich Gamma 已提交
344 345

		this.explorerViewer = new Tree(container.getHTMLElement(), {
B
Benjamin Pasero 已提交
346 347 348 349
			dataSource,
			renderer,
			controller,
			sorter,
E
Erich Gamma 已提交
350
			filter: this.filter,
B
Benjamin Pasero 已提交
351 352
			dnd,
			accessibilityProvider
E
Erich Gamma 已提交
353
		}, {
354
				autoExpandSingleChildren: true,
355
				ariaLabel: nls.localize('treeAriaLabel', "Files Explorer"),
356
				twistiePixels: 12,
357 358
				showTwistie: false,
				keyboardSupport: false
359
			});
E
Erich Gamma 已提交
360

B
Benjamin Pasero 已提交
361 362 363
		// Theme styler
		this.toDispose.push(attachListStyler(this.explorerViewer, this.themeService));

364
		// Register to list service
365
		this.toDispose.push(this.listService.register(this.explorerViewer, [this.explorerFocussedContext, this.filesExplorerFocussedContext]));
366

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

371
		// Update resource context based on focused element
A
Alex Dima 已提交
372
		this.toDispose.push(this.explorerViewer.addListener('focus', (e: { focus: FileStat }) => {
373 374 375
			this.resourceContext.set(e.focus && e.focus.resource);
			this.folderContext.set(e.focus && e.focus.isDirectory);
		}));
376

377
		// Open when selecting via keyboard
A
Alex Dima 已提交
378
		this.toDispose.push(this.explorerViewer.addListener('selection', event => {
379
			if (event && event.payload && event.payload.origin === 'keyboard') {
380
				const element = this.tree.getSelection();
B
Benjamin Pasero 已提交
381

382 383 384
				if (Array.isArray(element) && element[0] instanceof FileStat) {
					if (element[0].isDirectory) {
						this.explorerViewer.toggleExpansion(element[0]);
B
Benjamin Pasero 已提交
385 386
					}

387
					controller.openEditor(element[0], { pinned: false, sideBySide: false, preserveFocus: false });
B
Benjamin Pasero 已提交
388
				}
389 390 391
			}
		}));

E
Erich Gamma 已提交
392 393 394
		return this.explorerViewer;
	}

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

M
Maxime Quandalle 已提交
399 400 401
		return DOM.getLargestChildWidth(parentNode, childNodes);
	}

402
	private onFileOperation(e: FileOperationEvent): void {
B
Benjamin Pasero 已提交
403 404 405 406
		if (!this.root) {
			return; // ignore if not yet created
		}

E
Erich Gamma 已提交
407 408 409 410 411 412
		let modelElement: FileStat;
		let parent: FileStat;
		let parentResource: URI;
		let parentElement: FileStat;

		// Add
413 414
		if (e.operation === FileOperation.CREATE || e.operation === FileOperation.IMPORT || e.operation === FileOperation.COPY) {
			const addedElement = e.target;
E
Erich Gamma 已提交
415
			parentResource = URI.file(paths.dirname(addedElement.resource.fsPath));
416
			parentElement = this.root.find(parentResource);
E
Erich Gamma 已提交
417 418 419 420

			if (parentElement) {

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

425 426 427
				// Refresh the Parent (View)
				this.explorerViewer.refresh(parentElement).then(() => {
					return this.reveal(childElement, 0.5).then(() => {
E
Erich Gamma 已提交
428

429 430
						// Focus new element
						this.explorerViewer.setFocus(childElement);
E
Erich Gamma 已提交
431
					});
432
				}).done(null, errors.onUnexpectedError);
E
Erich Gamma 已提交
433 434 435 436
			}
		}

		// Move (including Rename)
437 438 439
		else if (e.operation === FileOperation.MOVE) {
			const oldResource = e.resource;
			const newElement = e.target;
E
Erich Gamma 已提交
440

441
			const oldParentResource = URI.file(paths.dirname(oldResource.fsPath));
442
			const newParentResource = URI.file(paths.dirname(newElement.resource.fsPath));
E
Erich Gamma 已提交
443 444

			// Only update focus if renamed/moved element is selected
445
			let restoreFocus = false;
446
			const focus: FileStat = this.explorerViewer.getFocus();
447
			if (focus && focus.resource && focus.resource.toString() === oldResource.toString()) {
448
				restoreFocus = true;
E
Erich Gamma 已提交
449 450 451
			}

			// Handle Rename
452
			if (oldParentResource && newParentResource && oldParentResource.toString() === newParentResource.toString()) {
453
				modelElement = this.root.find(oldResource);
E
Erich Gamma 已提交
454 455 456 457 458 459 460 461 462 463 464
				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
465
							if (restoreFocus) {
E
Erich Gamma 已提交
466 467 468 469 470 471 472 473 474
								this.explorerViewer.setFocus(modelElement);
							}
						}, errors.onUnexpectedError);
					}
				}
			}

			// Handle Move
			else if (oldParentResource && newParentResource) {
475 476 477
				const oldParent = this.root.find(oldParentResource);
				const newParent = this.root.find(newParentResource);
				modelElement = this.root.find(oldResource);
E
Erich Gamma 已提交
478 479 480 481 482 483 484 485 486 487 488

				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
489
						this.explorerViewer.refresh(newParent, true).done(() => this.explorerViewer.expand(newParent), errors.onUnexpectedError);
E
Erich Gamma 已提交
490 491 492 493 494 495
					});
				}
			}
		}

		// Delete
496
		else if (e.operation === FileOperation.DELETE) {
497
			modelElement = this.root.find(e.resource);
E
Erich Gamma 已提交
498 499 500 501 502 503 504
			if (modelElement && modelElement.parent) {
				parent = modelElement.parent;

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

				// Refresh Parent (View)
505
				const restoreFocus = this.explorerViewer.isDOMFocused();
E
Erich Gamma 已提交
506 507 508
				this.explorerViewer.refresh(parent).done(() => {

					// Ensure viewer has keyboard focus if event originates from viewer
509 510 511
					if (restoreFocus) {
						this.explorerViewer.DOMFocus();
					}
E
Erich Gamma 已提交
512 513 514 515 516 517 518
				}, errors.onUnexpectedError);
			}
		}
	}

	private onFileChanges(e: FileChangesEvent): void {

519 520 521 522
		// 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(() => {
523
			const lastActiveResource: string = this.settings[ExplorerView.MEMENTO_LAST_ACTIVE_FILE_RESOURCE];
524 525 526 527
			if (lastActiveResource && e.contains(URI.parse(lastActiveResource), FileChangeType.DELETED)) {
				this.settings[ExplorerView.MEMENTO_LAST_ACTIVE_FILE_RESOURCE] = null;
			}
		});
E
Erich Gamma 已提交
528 529 530 531

		// 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.
532
		setTimeout(() => {
E
Erich Gamma 已提交
533 534 535
			if (!this.shouldRefresh && this.shouldRefreshFromEvent(e)) {
				this.refreshFromEvent();
			}
536
		}, ExplorerView.EXPLORER_FILE_CHANGES_REACT_DELAY);
E
Erich Gamma 已提交
537 538 539 540 541 542
	}

	private shouldRefreshFromEvent(e: FileChangesEvent): boolean {

		// Filter to the ones we care
		e = this.filterToAddRemovedOnWorkspacePath(e, (event, segments) => {
B
Benjamin Pasero 已提交
543 544 545 546 547 548
			if (
				segments[0] !== ExplorerView.COMMON_SCM_FOLDERS[0] &&
				segments[0] !== ExplorerView.COMMON_SCM_FOLDERS[1] &&
				segments[0] !== ExplorerView.COMMON_SCM_FOLDERS[2]
			) {
				return true; // we like all things outside common SCM folders
E
Erich Gamma 已提交
549 550
			}

B
Benjamin Pasero 已提交
551
			return segments.length === 1; // otherwise we only care about the SCM folder itself
E
Erich Gamma 已提交
552 553 554 555
		});

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

559
			if (!this.root) {
E
Erich Gamma 已提交
560 561 562 563
				return false;
			}

			// Check added: Refresh if added file/folder is not part of resolved root and parent is part of it
564
			const ignoredPaths: { [fsPath: string]: boolean } = <{ [fsPath: string]: boolean }>{};
E
Erich Gamma 已提交
565
			for (let i = 0; i < added.length; i++) {
566
				const change = added[i];
E
Erich Gamma 已提交
567 568 569 570 571
				if (!this.contextService.isInsideWorkspace(change.resource)) {
					continue; // out of workspace file
				}

				// Find parent
572
				const parent = paths.dirname(change.resource.fsPath);
E
Erich Gamma 已提交
573 574 575 576 577 578 579

				// 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
580 581
				const parentStat = this.root.find(URI.file(parent));
				if (parentStat && parentStat.isDirectoryResolved && !this.root.find(change.resource)) {
E
Erich Gamma 已提交
582 583 584 585 586 587 588 589 590 591 592
					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++) {
593
				const del = deleted[j];
E
Erich Gamma 已提交
594 595 596 597
				if (!this.contextService.isInsideWorkspace(del.resource)) {
					continue; // out of workspace file
				}

598
				if (this.root.find(del.resource)) {
E
Erich Gamma 已提交
599 600 601 602 603 604 605 606 607
					return true;
				}
			}
		}

		return false;
	}

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

613
			const workspacePath = this.contextService.toWorkspaceRelativePath(change.resource);
E
Erich Gamma 已提交
614 615 616 617
			if (!workspacePath) {
				return false; // not inside workspace
			}

618
			const segments = workspacePath.split(/\//);
E
Erich Gamma 已提交
619 620 621 622 623 624 625 626 627

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

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

A
Alex Dima 已提交
631
				return TPromise.as(null);
E
Erich Gamma 已提交
632 633 634 635 636 637 638 639 640
			}).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.
	 */
641 642 643 644 645 646 647 648 649 650 651
	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) {
652
			resourceToFocus = this.getActiveFile();
653
			if (!resourceToFocus) {
654
				const selection = this.explorerViewer.getSelection();
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
				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> {
671
		const targetsToResolve: URI[] = [];
E
Erich Gamma 已提交
672 673 674
		let targetsToExpand: URI[] = [];

		if (this.settings[ExplorerView.MEMENTO_EXPANDED_FOLDER_RESOURCES]) {
B
Benjamin Pasero 已提交
675
			targetsToExpand = this.settings[ExplorerView.MEMENTO_EXPANDED_FOLDER_RESOURCES].map((e: string) => URI.parse(e));
E
Erich Gamma 已提交
676 677 678
		}

		// First time refresh: Receive target through active editor input or selection and also include settings from previous session
679
		if (!this.root) {
680 681
			const activeFile = this.getActiveFile();
			if (activeFile) {
682
				targetsToResolve.push(activeFile);
E
Erich Gamma 已提交
683 684 685 686 687 688 689 690 691
			}

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

		// Subsequent refresh: Receive targets through expanded folders in tree
		else {
692
			this.getResolvedDirectories(this.root, targetsToResolve);
E
Erich Gamma 已提交
693 694 695
		}

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

			// Convert to model
701
			const modelStat = FileStat.create(stat, options.resolveTo);
E
Erich Gamma 已提交
702 703

			// First time refresh: The stat becomes the input of the viewer
704
			if (!this.root) {
E
Erich Gamma 已提交
705 706 707 708
				explorerPromise = this.explorerViewer.setInput(modelStat).then(() => {

					// Make sure to expand all folders that where expanded in the previous session
					if (targetsToExpand) {
709
						return this.explorerViewer.expandAll(targetsToExpand.map(expand => this.root.find(expand)));
E
Erich Gamma 已提交
710 711
					}

A
Alex Dima 已提交
712
					return TPromise.as(null);
E
Erich Gamma 已提交
713 714 715 716 717
				});
			}

			// Subsequent refresh: Merge stat into our local model and refresh tree
			else {
718
				FileStat.mergeLocalWithDisk(modelStat, this.root);
E
Erich Gamma 已提交
719

720
				explorerPromise = this.explorerViewer.refresh(this.root);
E
Erich Gamma 已提交
721 722
			}

723
			return explorerPromise;
724
		}, (e: any) => TPromise.wrapError(e));
E
Erich Gamma 已提交
725

726
		this.progressService.showWhile(promise, this.partService.isCreated() ? 800 : 3200 /* less ugly initial startup */);
E
Erich Gamma 已提交
727 728 729 730 731 732 733 734 735

		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) {
736
			if (stat.resource.toString() !== this.contextService.getWorkspace().resource.toString()) {
E
Erich Gamma 已提交
737 738 739

				// Drop those path which are parents of the current one
				for (let i = resolvedDirectories.length - 1; i >= 0; i--) {
740
					const resource = resolvedDirectories[i];
741
					if (isEqualOrParent(stat.resource.fsPath, resource.fsPath, !isLinux /* ignorecase */)) {
E
Erich Gamma 已提交
742 743 744 745 746 747 748 749 750 751
						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++) {
752
				const child = stat.children[i];
E
Erich Gamma 已提交
753 754 755 756 757 758 759 760 761
				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.
	 */
762
	public select(resource: URI, reveal: boolean = this.autoReveal): TPromise<void> {
E
Erich Gamma 已提交
763 764

		// Require valid path
765
		if (!resource || resource.toString() === this.contextService.getWorkspace().resource.toString()) {
A
Alex Dima 已提交
766
			return TPromise.as(null);
E
Erich Gamma 已提交
767 768 769
		}

		// If path already selected, just reveal and return
770
		const selection = this.hasSelection(resource);
771 772
		if (selection) {
			return reveal ? this.reveal(selection, 0.5) : TPromise.as(null);
E
Erich Gamma 已提交
773 774 775
		}

		// First try to get the stat object from the input to avoid a roundtrip
776
		if (!this.root) {
A
Alex Dima 已提交
777
			return TPromise.as(null);
E
Erich Gamma 已提交
778 779
		}

780
		const fileStat = this.root.find(resource);
E
Erich Gamma 已提交
781
		if (fileStat) {
782
			return this.doSelect(fileStat, reveal);
E
Erich Gamma 已提交
783 784 785
		}

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

			// Convert to model
790
			const modelStat = FileStat.create(stat, options.resolveTo);
E
Erich Gamma 已提交
791 792

			// Update Input with disk Stat
793
			FileStat.mergeLocalWithDisk(modelStat, this.root);
E
Erich Gamma 已提交
794 795

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

E
Erich Gamma 已提交
798 799 800
		}, (e: any) => this.messageService.show(Severity.Error, e));
	}

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

804
		for (let i = 0; i < currentSelection.length; i++) {
805
			if (currentSelection[i].resource.toString() === resource.toString()) {
806 807 808 809 810 811 812 813
				return currentSelection[i];
			}
		}

		return null;
	}

	private doSelect(fileStat: FileStat, reveal: boolean): TPromise<void> {
E
Erich Gamma 已提交
814
		if (!fileStat) {
A
Alex Dima 已提交
815
			return TPromise.as(null);
E
Erich Gamma 已提交
816 817 818 819 820 821 822
		}

		// 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 已提交
823
				return TPromise.as(null);
E
Erich Gamma 已提交
824 825 826
			}
		}

827 828 829 830 831 832 833 834 835
		// 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 已提交
836 837 838 839 840 841 842 843 844 845 846
			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
847
		if (this.root) {
848
			const expanded = this.explorerViewer.getExpandedElements()
849
				.filter((e: FileStat) => e.resource.toString() !== this.contextService.getWorkspace().resource.toString())
E
Erich Gamma 已提交
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
				.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 已提交
865
}