From 01373f73aa94d8df687df736b858b22f1cf0a246 Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Thu, 2 Feb 2017 09:50:53 +0100 Subject: [PATCH] copy path command in explorer (for #4557) --- .../files/browser/fileActions.contribution.ts | 31 ++++++++----- .../parts/files/browser/fileCommands.ts | 43 +++++++++++-------- .../parts/files/browser/views/explorerView.ts | 21 ++++++--- .../files/browser/views/openEditorsView.ts | 21 ++++++--- src/vs/workbench/parts/files/common/files.ts | 3 +- 5 files changed, 80 insertions(+), 39 deletions(-) diff --git a/src/vs/workbench/parts/files/browser/fileActions.contribution.ts b/src/vs/workbench/parts/files/browser/fileActions.contribution.ts index 52c4ebf112d..4c6de8ef73c 100644 --- a/src/vs/workbench/parts/files/browser/fileActions.contribution.ts +++ b/src/vs/workbench/parts/files/browser/fileActions.contribution.ts @@ -23,7 +23,7 @@ import { KeyMod, KeyChord, KeyCode } from 'vs/base/common/keyCodes'; import { DiffEditorInput } from 'vs/workbench/common/editor/diffEditorInput'; import { ResourceEditorInput } from 'vs/workbench/common/editor/resourceEditorInput'; import { OpenFolderAction, OpenFileFolderAction } from 'vs/workbench/browser/actions/fileActions'; -import { copyPathCommand, revealInExplorerCommand, revealInOSCommand, openFolderPickerCommand, openWindowCommand, openFileInNewWindowCommand, openFocussedExplorerItemCommand, deleteFocussedExplorerItemCommand, moveFocussedExplorerItemToTrashCommand, openFocussedExplorerSideBySideItemCommand, renameFocussedExplorerItemCommand } from 'vs/workbench/parts/files/browser/fileCommands'; +import { copyPathOfFocussedExplorerItem, copyPathCommand, revealInExplorerCommand, revealInOSCommand, openFolderPickerCommand, openWindowCommand, openFileInNewWindowCommand, openFocussedExplorerViewItemCommand, deleteFocussedExplorerViewItemCommand, moveFocussedExplorerViewItemToTrashCommand, openFocussedExplorerViewItemSideBySideCommand, renameFocussedExplorerViewItemCommand } from 'vs/workbench/parts/files/browser/fileCommands'; import { CommandsRegistry, ICommandHandler } from 'vs/platform/commands/common/commands'; import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey'; import { KeybindingsRegistry } from 'vs/platform/keybinding/common/keybindingsRegistry'; @@ -239,61 +239,70 @@ CommandsRegistry.registerCommand('_files.openFolderPicker', openFolderPickerComm CommandsRegistry.registerCommand('_files.windowOpen', openWindowCommand); CommandsRegistry.registerCommand('workbench.action.files.openFileInNewWindow', openFileInNewWindowCommand); +const filesExplorerFocusCondition = ContextKeyExpr.and(ContextKeyExpr.has('explorerViewletVisible'), ContextKeyExpr.has('filesExplorerFocus')); const explorerFocusCondition = ContextKeyExpr.and(ContextKeyExpr.has('explorerViewletVisible'), ContextKeyExpr.has('explorerFocus')); KeybindingsRegistry.registerCommandAndKeybindingRule({ id: 'workbench.files.action.open', weight: KeybindingsRegistry.WEIGHT.workbenchContrib(), - when: explorerFocusCondition, + when: filesExplorerFocusCondition, primary: KeyCode.Enter, mac: { primary: KeyMod.CtrlCmd | KeyCode.DownArrow }, - handler: openFocussedExplorerItemCommand + handler: openFocussedExplorerViewItemCommand }); KeybindingsRegistry.registerCommandAndKeybindingRule({ id: 'workbench.files.action.openToSide', weight: KeybindingsRegistry.WEIGHT.workbenchContrib(), - when: explorerFocusCondition, + when: filesExplorerFocusCondition, primary: KeyMod.CtrlCmd | KeyCode.Enter, mac: { primary: KeyMod.WinCtrl | KeyCode.Enter }, - handler: openFocussedExplorerSideBySideItemCommand + handler: openFocussedExplorerViewItemSideBySideCommand }); KeybindingsRegistry.registerCommandAndKeybindingRule({ id: 'workbench.files.action.triggerRename', weight: KeybindingsRegistry.WEIGHT.workbenchContrib(), - when: explorerFocusCondition, + when: filesExplorerFocusCondition, primary: KeyCode.F2, mac: { primary: KeyCode.Enter }, - handler: renameFocussedExplorerItemCommand + handler: renameFocussedExplorerViewItemCommand }); KeybindingsRegistry.registerCommandAndKeybindingRule({ id: 'workbench.files.action.moveFileToTrash', weight: KeybindingsRegistry.WEIGHT.workbenchContrib(), - when: explorerFocusCondition, + when: filesExplorerFocusCondition, primary: KeyCode.Delete, mac: { primary: KeyMod.CtrlCmd | KeyCode.Backspace }, - handler: moveFocussedExplorerItemToTrashCommand + handler: moveFocussedExplorerViewItemToTrashCommand }); KeybindingsRegistry.registerCommandAndKeybindingRule({ id: 'workbench.files.action.delete', weight: KeybindingsRegistry.WEIGHT.workbenchContrib(), - when: explorerFocusCondition, + when: filesExplorerFocusCondition, primary: KeyMod.Shift | KeyCode.Delete, mac: { primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.Backspace }, - handler: deleteFocussedExplorerItemCommand + handler: deleteFocussedExplorerViewItemCommand +}); + +KeybindingsRegistry.registerCommandAndKeybindingRule({ + id: 'workbench.action.files.copyPath', + weight: KeybindingsRegistry.WEIGHT.workbenchContrib(), + when: explorerFocusCondition, + primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.KEY_C, + handler: copyPathOfFocussedExplorerItem }); // Editor Title Context Menu diff --git a/src/vs/workbench/parts/files/browser/fileCommands.ts b/src/vs/workbench/parts/files/browser/fileCommands.ts index c8c0f1668a0..ffb9dc143cb 100644 --- a/src/vs/workbench/parts/files/browser/fileCommands.ts +++ b/src/vs/workbench/parts/files/browser/fileCommands.ts @@ -75,10 +75,7 @@ export const revealInExplorerCommand = (accessor: ServicesAccessor, resource: UR }); }; -export const openFocussedExplorerItemCommand = (accessor: ServicesAccessor) => openFocussedExplorerItem(accessor, false); -export const openFocussedExplorerSideBySideItemCommand = (accessor: ServicesAccessor) => openFocussedExplorerItem(accessor, true); - -function openFocussedExplorerItem(accessor: ServicesAccessor, sideBySide: boolean): void { +function openFocussedExplorerViewItem(accessor: ServicesAccessor, sideBySide: boolean): void { withFocussedExplorerViewItem(accessor).then(res => { if (res) { @@ -106,18 +103,6 @@ function runActionOnFocussedExplorerViewItem(accessor: ServicesAccessor, id: str }); } -export const renameFocussedExplorerItemCommand = (accessor: ServicesAccessor) => { - runActionOnFocussedExplorerViewItem(accessor, 'workbench.files.action.triggerRename'); -}; - -export const deleteFocussedExplorerItemCommand = (accessor: ServicesAccessor) => { - runActionOnFocussedExplorerViewItem(accessor, 'workbench.files.action.moveFileToTrash', { useTrash: false }); -}; - -export const moveFocussedExplorerItemToTrashCommand = (accessor: ServicesAccessor) => { - runActionOnFocussedExplorerViewItem(accessor, 'workbench.files.action.moveFileToTrash', { useTrash: true }); -}; - function withExplorer(accessor: ServicesAccessor): TPromise { const viewletService = accessor.get(IViewletService); @@ -146,7 +131,7 @@ function withFocussedExplorerViewItem(accessor: ServicesAccessor): TPromise<{ ex }); }; -export function withFocussedExplorerResource(accessor: ServicesAccessor): TPromise { +function withFocussedExplorerFileResource(accessor: ServicesAccessor): TPromise { return withExplorer(accessor).then(explorer => { if (!explorer) { return void 0; // hidden explorer @@ -169,4 +154,28 @@ export function withFocussedExplorerResource(accessor: ServicesAccessor): TPromi return explorerItemToFileResource(focussedTree.getFocus()); }); +}; + +export const openFocussedExplorerViewItemCommand = (accessor: ServicesAccessor) => openFocussedExplorerViewItem(accessor, false); +export const openFocussedExplorerViewItemSideBySideCommand = (accessor: ServicesAccessor) => openFocussedExplorerViewItem(accessor, true); + +export const renameFocussedExplorerViewItemCommand = (accessor: ServicesAccessor) => { + runActionOnFocussedExplorerViewItem(accessor, 'workbench.files.action.triggerRename'); +}; + +export const deleteFocussedExplorerViewItemCommand = (accessor: ServicesAccessor) => { + runActionOnFocussedExplorerViewItem(accessor, 'workbench.files.action.moveFileToTrash', { useTrash: false }); +}; + +export const moveFocussedExplorerViewItemToTrashCommand = (accessor: ServicesAccessor) => { + runActionOnFocussedExplorerViewItem(accessor, 'workbench.files.action.moveFileToTrash', { useTrash: true }); +}; + + +export const copyPathOfFocussedExplorerItem = (accessor: ServicesAccessor) => { + withFocussedExplorerFileResource(accessor).then(file => { + if (file) { + copyPathCommand(accessor, file.resource); + } + }); }; \ No newline at end of file diff --git a/src/vs/workbench/parts/files/browser/views/explorerView.ts b/src/vs/workbench/parts/files/browser/views/explorerView.ts index c852ea4431a..a5bdeddfcc8 100644 --- a/src/vs/workbench/parts/files/browser/views/explorerView.ts +++ b/src/vs/workbench/parts/files/browser/views/explorerView.ts @@ -16,7 +16,7 @@ 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, ExplorerFolderContext, ExplorerFocussedContext } from 'vs/workbench/parts/files/common/files'; +import { IFilesConfiguration, ExplorerFolderContext, FilesExplorerFocussedContext, ExplorerFocussedContext } from 'vs/workbench/parts/files/common/files'; 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'; import { FileDragAndDrop, FileFilter, FileSorter, FileController, FileRenderer, FileDataSource, FileViewletState, FileAccessibilityProvider } from 'vs/workbench/parts/files/browser/views/explorerViewer'; @@ -60,6 +60,7 @@ export class ExplorerView extends CollapsibleViewletView { private resourceContext: ResourceContextKey; private folderContext: IContextKey; + private filesExplorerFocussedContext: IContextKey; private explorerFocussedContext: IContextKey; private shouldRefresh: boolean; @@ -99,6 +100,8 @@ export class ExplorerView extends CollapsibleViewletView { this.resourceContext = instantiationService.createInstance(ResourceContextKey); this.folderContext = ExplorerFolderContext.bindTo(contextKeyService); + + this.filesExplorerFocussedContext = FilesExplorerFocussedContext.bindTo(contextKeyService); this.explorerFocussedContext = ExplorerFocussedContext.bindTo(contextKeyService); } @@ -360,10 +363,18 @@ export class ExplorerView extends CollapsibleViewletView { })); // Update explorer focus context - const explorerFocusTracker = DOM.trackFocus(this.explorerViewer.getHTMLElement()); - explorerFocusTracker.addFocusListener(() => this.explorerFocussedContext.set(true)); - explorerFocusTracker.addBlurListener(() => this.explorerFocussedContext.reset()); - this.toDispose.push(explorerFocusTracker); + const viewerFocusTracker = DOM.trackFocus(this.explorerViewer.getHTMLElement()); + viewerFocusTracker.addFocusListener(() => { + setTimeout(() => { + this.filesExplorerFocussedContext.set(true); + this.explorerFocussedContext.set(true); + }, 0 /* wait for any BLUR to happen */); + }); + viewerFocusTracker.addBlurListener(() => { + this.filesExplorerFocussedContext.reset(); + this.explorerFocussedContext.reset(); + }); + this.toDispose.push(viewerFocusTracker); return this.explorerViewer; } diff --git a/src/vs/workbench/parts/files/browser/views/openEditorsView.ts b/src/vs/workbench/parts/files/browser/views/openEditorsView.ts index a21d111f04d..a1ccce25444 100644 --- a/src/vs/workbench/parts/files/browser/views/openEditorsView.ts +++ b/src/vs/workbench/parts/files/browser/views/openEditorsView.ts @@ -19,7 +19,7 @@ import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding'; import { IEditorStacksModel, IStacksModelChangeEvent, IEditorGroup } from 'vs/workbench/common/editor'; import { SaveAllAction } from 'vs/workbench/parts/files/browser/fileActions'; import { AdaptiveCollapsibleViewletView } from 'vs/workbench/browser/viewlet'; -import { IFilesConfiguration, VIEWLET_ID, OpenEditorsFocussedContext } from 'vs/workbench/parts/files/common/files'; +import { IFilesConfiguration, VIEWLET_ID, OpenEditorsFocussedContext, ExplorerFocussedContext } from 'vs/workbench/parts/files/common/files'; import { ITextFileService, AutoSaveMode } from 'vs/workbench/services/textfile/common/textfiles'; import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet'; import { OpenEditor } from 'vs/workbench/parts/files/common/explorerViewModel'; @@ -49,6 +49,7 @@ export class OpenEditorsView extends AdaptiveCollapsibleViewletView { private fullRefreshNeeded: boolean; private openEditorsFocussedContext: IContextKey; + private explorerFocussedContext: IContextKey; constructor(actionRunner: IActionRunner, settings: any, @IInstantiationService private instantiationService: IInstantiationService, @@ -65,7 +66,9 @@ export class OpenEditorsView extends AdaptiveCollapsibleViewletView { this.settings = settings; this.model = editorGroupService.getStacksModel(); + this.openEditorsFocussedContext = OpenEditorsFocussedContext.bindTo(contextKeyService); + this.explorerFocussedContext = ExplorerFocussedContext.bindTo(contextKeyService); this.structuralRefreshDelay = 0; this.structuralTreeRefreshScheduler = new RunOnceScheduler(() => this.structuralTreeUpdate(), this.structuralRefreshDelay); @@ -116,10 +119,18 @@ export class OpenEditorsView extends AdaptiveCollapsibleViewletView { }); // Update open editors focus context - const explorerFocusTracker = dom.trackFocus(this.tree.getHTMLElement()); - explorerFocusTracker.addFocusListener(() => this.openEditorsFocussedContext.set(true)); - explorerFocusTracker.addBlurListener(() => this.openEditorsFocussedContext.reset()); - this.toDispose.push(explorerFocusTracker); + const viewerFocusTracker = dom.trackFocus(this.tree.getHTMLElement()); + viewerFocusTracker.addFocusListener(() => { + setTimeout(() => { + this.openEditorsFocussedContext.set(true); + this.explorerFocussedContext.set(true); + }, 0 /* wait for any BLUR to happen */); + }); + viewerFocusTracker.addBlurListener(() => { + this.openEditorsFocussedContext.reset(); + this.explorerFocussedContext.reset(); + }); + this.toDispose.push(viewerFocusTracker); this.fullRefreshNeeded = true; this.structuralTreeUpdate(); diff --git a/src/vs/workbench/parts/files/common/files.ts b/src/vs/workbench/parts/files/common/files.ts index 131dea110e4..02d74215c4c 100644 --- a/src/vs/workbench/parts/files/common/files.ts +++ b/src/vs/workbench/parts/files/common/files.ts @@ -21,8 +21,9 @@ export const VIEWLET_ID = 'workbench.view.explorer'; */ export const ExplorerViewletVisibleContext = new RawContextKey('explorerViewletVisible', true); export const ExplorerFolderContext = new RawContextKey('explorerResourceIsFolder', false); -export const ExplorerFocussedContext = new RawContextKey('explorerFocus', false); +export const FilesExplorerFocussedContext = new RawContextKey('filesExplorerFocus', false); export const OpenEditorsFocussedContext = new RawContextKey('openEditorsFocus', false); +export const ExplorerFocussedContext = new RawContextKey('explorerFocus', false); /** * File editor input id. -- GitLab