From de71e77d2140b88e67d94caf0c462e0ecc1d312b Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Tue, 5 Apr 2016 11:33:31 +0200 Subject: [PATCH] better fix for new option to disable auto reveal in explorer --- src/vs/workbench/browser/viewlet.ts | 15 +- .../parts/files/browser/explorerViewlet.ts | 10 -- .../parts/files/browser/fileActions.ts | 12 +- .../parts/files/browser/files.contribution.ts | 5 + .../parts/files/browser/views/emptyView.ts | 4 - .../parts/files/browser/views/explorerView.ts | 144 +++++++++++------- src/vs/workbench/parts/files/common/files.ts | 1 + 7 files changed, 99 insertions(+), 92 deletions(-) diff --git a/src/vs/workbench/browser/viewlet.ts b/src/vs/workbench/browser/viewlet.ts index fda43807cc7..a3f075272c3 100644 --- a/src/vs/workbench/browser/viewlet.ts +++ b/src/vs/workbench/browser/viewlet.ts @@ -272,7 +272,6 @@ export class CollapseAction extends Action { export interface IViewletView { create(): TPromise; - refresh(focus: boolean, reveal: boolean, instantProgress?: boolean): TPromise; setVisible(visible: boolean): TPromise; getActions(): IAction[]; getSecondaryActions(): IAction[]; @@ -350,14 +349,10 @@ export class AdaptiveCollapsibleViewletView extends FixedCollapsibleView impleme return this.tree; } - public refresh(focus: boolean, reveal: boolean, instantProgress?: boolean): TPromise { - return TPromise.as(null); - } - public setVisible(visible: boolean): TPromise { this.isVisible = visible; - updateTreeVisibility(this.tree, this.state === CollapsibleState.EXPANDED); + updateTreeVisibility(this.tree, visible && this.state === CollapsibleState.EXPANDED); return TPromise.as(null); } @@ -477,14 +472,10 @@ export class CollapsibleViewletView extends CollapsibleView implements IViewletV return this.tree; } - public refresh(focus: boolean, reveal: boolean, instantProgress?: boolean): TPromise { - return TPromise.as(null); - } - public setVisible(visible: boolean): TPromise { this.isVisible = visible; - updateTreeVisibility(this.tree, this.state === CollapsibleState.EXPANDED); + updateTreeVisibility(this.tree, visible && this.state === CollapsibleState.EXPANDED); return TPromise.as(null); } @@ -572,7 +563,7 @@ function focus(tree: ITree): void { // Make sure the current selected element is revealed let selection = tree.getSelection(); if (selection.length > 0) { - reveal(tree, selection[0], 0.5); + reveal(tree, selection[0], 0.5).done(null, errors.onUnexpectedError); } // Pass Focus to Viewer diff --git a/src/vs/workbench/parts/files/browser/explorerViewlet.ts b/src/vs/workbench/parts/files/browser/explorerViewlet.ts index 645f05c2d7b..4ba5f27ee3a 100644 --- a/src/vs/workbench/parts/files/browser/explorerViewlet.ts +++ b/src/vs/workbench/parts/files/browser/explorerViewlet.ts @@ -96,16 +96,6 @@ export class ExplorerViewlet extends Viewlet { this.views.push(explorerView); } - /** - * Refresh the contents of the explorer to get up to date data from the disk about the file structure. - * - * @param focus if set to true, the explorer viewer will receive keyboard focus - * @param reveal if set to true, the current active input will be revealed in the explorer - */ - public refresh(focus: boolean, reveal: boolean, instantProgress?: boolean): TPromise { - return TPromise.join(this.views.map((view) => view.refresh(focus, reveal, instantProgress))).then(() => void 0); - } - public getExplorerView(): ExplorerView { return this.explorerView; } diff --git a/src/vs/workbench/parts/files/browser/fileActions.ts b/src/vs/workbench/parts/files/browser/fileActions.ts index 3f05953ccf4..483f32cf9d4 100644 --- a/src/vs/workbench/parts/files/browser/fileActions.ts +++ b/src/vs/workbench/parts/files/browser/fileActions.ts @@ -1386,15 +1386,7 @@ export class CompareResourcesAction extends Action { export class RefreshViewExplorerAction extends Action { constructor(explorerView: ExplorerView, clazz: string) { - super('workbench.files.action.refreshExplorer', nls.localize('refresh', "Refresh"), clazz, true, (context: any) => { - if (explorerView.getViewer().getHighlight()) { - return TPromise.as(null); // Global action disabled if user is in edit mode from another action - } - - explorerView.focusBody(); - - return explorerView.refresh(true, true, true); - }); + super('workbench.files.action.refreshExplorer', nls.localize('refresh', "Refresh"), clazz, true, (context: any) => explorerView.refresh()); } } @@ -2439,7 +2431,7 @@ export function getWellFormedFileName(filename: string): string { // Diagnostics support let diag: (...args: any[]) => void; if (!diag) { - diag = diagnostics.register('FileActionsDiagnostics', function(...args: any[]) { + diag = diagnostics.register('FileActionsDiagnostics', function (...args: any[]) { console.log(args[1] + ' - ' + args[0] + ' (time: ' + args[2].getTime() + ' [' + args[2].toUTCString() + '])'); }); } \ No newline at end of file diff --git a/src/vs/workbench/parts/files/browser/files.contribution.ts b/src/vs/workbench/parts/files/browser/files.contribution.ts index 6764b7fdfd3..10c9beee3ad 100644 --- a/src/vs/workbench/parts/files/browser/files.contribution.ts +++ b/src/vs/workbench/parts/files/browser/files.contribution.ts @@ -251,6 +251,11 @@ configurationRegistry.registerConfiguration({ 'type': 'boolean', 'description': nls.localize('dynamicHeight', "Controls if the height of the working files section should adapt dynamically to the number of elements or not."), 'default': true + }, + 'explorer.autoReveal': { + 'type': 'boolean', + 'description': nls.localize('autoReveal', "Controls if the explorer should automatically reveal files when opening them."), + 'default': true } } }); diff --git a/src/vs/workbench/parts/files/browser/views/emptyView.ts b/src/vs/workbench/parts/files/browser/views/emptyView.ts index 840d54aefeb..1e1c7f3caab 100644 --- a/src/vs/workbench/parts/files/browser/views/emptyView.ts +++ b/src/vs/workbench/parts/files/browser/views/emptyView.ts @@ -60,10 +60,6 @@ export class EmptyView extends CollapsibleView { return TPromise.as(null); } - public refresh(focus: boolean, reveal: boolean, instantProgress?: boolean): TPromise { - return TPromise.as(null); - } - public setVisible(visible: boolean): TPromise { return TPromise.as(null); } diff --git a/src/vs/workbench/parts/files/browser/views/explorerView.ts b/src/vs/workbench/parts/files/browser/views/explorerView.ts index a3fd593bb19..483dd3243c1 100644 --- a/src/vs/workbench/parts/files/browser/views/explorerView.ts +++ b/src/vs/workbench/parts/files/browser/views/explorerView.ts @@ -58,6 +58,8 @@ export class ExplorerView extends CollapsibleViewletView { private shouldRefresh: boolean; + private autoReveal: boolean; + private settings: any; constructor( @@ -83,6 +85,7 @@ export class ExplorerView extends CollapsibleViewletView { this.settings = settings; this.viewletState = viewletState; this.actionRunner = actionRunner; + this.autoReveal = true; this.explorerRefreshDelayer = new ThrottledDelayer(ExplorerView.EXPLORER_FILE_CHANGES_REFRESH_DELAY); this.explorerImportDelayer = new ThrottledDelayer(ExplorerView.EXPLORER_IMPORT_REFRESH_DELAY); @@ -135,7 +138,7 @@ export class ExplorerView extends CollapsibleViewletView { this.onConfigurationUpdated(configuration); // Load and Fill Viewer - return this.refresh(false, false).then(() => { + return this.doRefresh().then(() => { // When the explorer viewer is loaded, listen to changes to the editor input this.toDispose.push(this.eventService.addListener2(WorkbenchEventType.EDITOR_INPUT_CHANGING, (e: EditorEvent) => this.onEditorInputChanging(e))); @@ -164,12 +167,14 @@ export class ExplorerView extends CollapsibleViewletView { // Always remember last opened file this.settings[ExplorerView.MEMENTO_LAST_ACTIVE_FILE_RESOURCE] = fileInput.getResource().toString(); - // Reveal file if input is FileEditorInput - if (this.isVisible) { - if (this.contextService.isInsideWorkspace(fileInput.getResource())) { - this.select(fileInput.getResource(), false /* Prevent reveal so that upon opening a file the tree does not jump around */).done(null, errors.onUnexpectedError); - clearSelection = false; + // Select file if input is FileEditorInput + if (this.isVisible && this.contextService.isInsideWorkspace(fileInput.getResource())) { + let selection = this.hasSelection(fileInput.getResource()); + if (!selection) { + this.select(fileInput.getResource()).done(null, errors.onUnexpectedError); } + + clearSelection = false; } } @@ -180,6 +185,7 @@ export class ExplorerView extends CollapsibleViewletView { } private onConfigurationUpdated(configuration: IFilesConfiguration, refresh?: boolean): void { + this.autoReveal = configuration && configuration.explorer && configuration.explorer.autoReveal; // Push down config updates to components of viewer let needsRefresh = false; @@ -189,12 +195,24 @@ export class ExplorerView extends CollapsibleViewletView { // Refresh viewer as needed if (refresh && needsRefresh) { - this.refresh(false, false).done(null, errors.onUnexpectedError); + this.doRefresh().done(null, errors.onUnexpectedError); } } public focusBody(): void { - super.focusBody(); + + // Make sure the current selected element is revealed + if (this.explorerViewer) { + if (this.autoReveal) { + let selection = this.explorerViewer.getSelection(); + if (selection.length > 0) { + this.reveal(selection[0], 0.5).done(null, errors.onUnexpectedError); + } + } + + // Pass Focus to Viewer + this.explorerViewer.DOMFocus(); + } // Open the focused element in the editor if there is currently no file opened let input = this.editorService.getActiveEditorInput(); @@ -212,11 +230,11 @@ export class ExplorerView extends CollapsibleViewletView { // If a refresh was requested and we are now visible, run it let refreshPromise = TPromise.as(null); if (this.shouldRefresh) { - refreshPromise = this.refresh(false, false); + refreshPromise = this.doRefresh(); this.shouldRefresh = false; // Reset flag } - // Always reveal the current navigated file in explorer if input is file editor input + // Always select the current navigated file in explorer if input is file editor input let activeResource = this.getActiveEditorInputResource(); if (activeResource) { return refreshPromise.then(() => { @@ -581,7 +599,7 @@ export class ExplorerView extends CollapsibleViewletView { if (this.isVisible) { this.explorerRefreshDelayer.trigger(() => { if (!this.explorerViewer.getHighlight()) { - return this.refresh(false, false); + return this.doRefresh(); } return TPromise.as(null); @@ -593,11 +611,37 @@ export class ExplorerView extends CollapsibleViewletView { /** * Refresh the contents of the explorer to get up to date data from the disk about the file structure. - * - * @param focus if set to true, the explorer viewer will receive keyboard focus - * @param reveal if set to true, the current active input will be revealed in the explorer */ - public refresh(focus: boolean, reveal: boolean, instantProgress?: boolean): TPromise { + public refresh(): TPromise { + 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) { + resourceToFocus = this.getActiveEditorInputResource(); + if (!resourceToFocus) { + let selection = this.explorerViewer.getSelection(); + if (selection && selection.length === 1) { + resourceToFocus = (selection[0]).resource; + } + } + } + + return this.doRefresh().then(() => { + if (resourceToFocus) { + return this.select(resourceToFocus, true); + } + + return TPromise.as(null); + }); + } + + private doRefresh(): TPromise { let root = this.getInput(); let targetsToResolve: URI[] = []; let targetsToExpand: URI[] = []; @@ -623,18 +667,6 @@ export class ExplorerView extends CollapsibleViewletView { this.getResolvedDirectories(root, targetsToResolve); } - // Determine the path to reveal if we are set to reveal - let revealResource: URI; - if (reveal) { - revealResource = this.getActiveEditorInputResource(); - if (!revealResource) { - let selection = this.explorerViewer.getSelection(); - if (selection && selection.length === 1) { - revealResource = (selection[0]).resource; - } - } - } - // Load Root Stat with given target path configured let options: IResolveFileOptions = { resolveTo: targetsToResolve }; let promise = this.fileService.resolveFile(this.workspace.resource, options).then((stat: IFileStat) => { @@ -663,27 +695,10 @@ export class ExplorerView extends CollapsibleViewletView { explorerPromise = this.explorerViewer.refresh(root); } - return explorerPromise.then(() => { - let revealPromise: TPromise; - - // Reveal if path is set - if (revealResource) { - revealPromise = this.select(revealResource); - } else { - revealPromise = TPromise.as(null); - } - - return revealPromise.then(() => { - - // Focus if set - if (focus) { - this.explorerViewer.DOMFocus(); - } - }); - }); + return explorerPromise; }, (e: any) => TPromise.wrapError(e)); - this.progressService.showWhile(promise, instantProgress ? 0 : this.partService.isCreated() ? 800 : 3200 /* less ugly initial startup */); + this.progressService.showWhile(promise, this.partService.isCreated() ? 800 : 3200 /* less ugly initial startup */); return promise; } @@ -719,7 +734,7 @@ export class ExplorerView extends CollapsibleViewletView { * 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. */ - public select(resource: URI, reveal: boolean = true): TPromise { + public select(resource: URI, reveal: boolean = this.autoReveal): TPromise { // Require valid path if (!resource || resource.toString() === this.workspace.resource.toString()) { @@ -727,11 +742,9 @@ export class ExplorerView extends CollapsibleViewletView { } // If path already selected, just reveal and return - let currentSelection: FileStat[] = this.explorerViewer.getSelection(); - for (let i = 0; i < currentSelection.length; i++) { - if (currentSelection[i].resource.toString() === resource.toString()) { - return reveal ? this.reveal(currentSelection[i], 0.5) : TPromise.as(null); - } + let selection = this.hasSelection(resource); + if (selection) { + return reveal ? this.reveal(selection, 0.5) : TPromise.as(null); } // First try to get the stat object from the input to avoid a roundtrip @@ -742,7 +755,7 @@ export class ExplorerView extends CollapsibleViewletView { let fileStat = root.find(resource); if (fileStat) { - return this.doRevealAndSelect(fileStat); + return this.doSelect(fileStat, reveal); } // Stat needs to be resolved first and then revealed @@ -757,12 +770,23 @@ export class ExplorerView extends CollapsibleViewletView { // Select and Reveal return this.explorerViewer.refresh(root).then(() => { - return this.doRevealAndSelect(root.find(resource)); + return this.doSelect(root.find(resource), reveal); }); }, (e: any) => this.messageService.show(Severity.Error, e)); } - private doRevealAndSelect(fileStat: FileStat): TPromise { + private hasSelection(resource: URI): FileStat { + let currentSelection: FileStat[] = this.explorerViewer.getSelection(); + 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 { if (!fileStat) { return TPromise.as(null); } @@ -776,7 +800,15 @@ export class ExplorerView extends CollapsibleViewletView { } } - return this.reveal(fileStat, 0.5).then(() => { + // Reveal depending on flag + let revealPromise: TPromise; + if (reveal) { + revealPromise = this.reveal(fileStat, 0.5); + } else { + revealPromise = TPromise.as(null); + } + + return revealPromise.then(() => { if (!fileStat.isDirectory) { this.explorerViewer.setSelection([fileStat]); // Since folders can not be opened, only select files } diff --git a/src/vs/workbench/parts/files/common/files.ts b/src/vs/workbench/parts/files/common/files.ts index 6f899b950be..136f6bb383a 100644 --- a/src/vs/workbench/parts/files/common/files.ts +++ b/src/vs/workbench/parts/files/common/files.ts @@ -67,6 +67,7 @@ export interface IFilesConfiguration extends IFilesConfiguration { maxVisible: number; dynamicHeight: boolean; }; + autoReveal: boolean; }; editor: IEditorOptions; } -- GitLab