From 1dcb420e69d5c1cefc003045e80d6b7ca6610f2c Mon Sep 17 00:00:00 2001 From: Rob Lourens Date: Tue, 3 Apr 2018 15:10:37 -0700 Subject: [PATCH] #8594 - implement copy path context menu item --- .../parts/search/browser/searchActions.ts | 12 +++++++++ .../parts/search/browser/searchResultsView.ts | 4 ++- .../parts/search/common/constants.ts | 2 ++ .../electron-browser/search.contribution.ts | 27 ++++++++++++++++--- 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/vs/workbench/parts/search/browser/searchActions.ts b/src/vs/workbench/parts/search/browser/searchActions.ts index 868a64b10a7f1..94a6efb8886bf 100644 --- a/src/vs/workbench/parts/search/browser/searchActions.ts +++ b/src/vs/workbench/parts/search/browser/searchActions.ts @@ -22,6 +22,10 @@ import { OS } from 'vs/base/common/platform'; import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey'; import { IPanelService } from 'vs/workbench/services/panel/common/panelService'; import { VIEW_ID } from 'vs/platform/search/common/search'; +import { IClipboardService } from 'vs/platform/clipboard/common/clipboardService'; +import { ICommandHandler } from 'vs/platform/commands/common/commands'; +import { Schemas } from 'vs/base/common/network'; +import { getPathLabel } from 'vs/base/common/labels'; export function isSearchViewFocused(viewletService: IViewletService, panelService: IPanelService): boolean { let searchView = getSearchView(viewletService, panelService); @@ -630,3 +634,11 @@ export class ReplaceAction extends AbstractSearchAndReplaceAction { return false; } } + +export const copyPathCommand: ICommandHandler = (accessor, fileMatch: FileMatch) => { + const clipboardService = accessor.get(IClipboardService); + + const resource = fileMatch.resource(); + const text = resource.scheme === Schemas.file ? getPathLabel(resource) : resource.toString(); + clipboardService.writeText(text); +}; diff --git a/src/vs/workbench/parts/search/browser/searchResultsView.ts b/src/vs/workbench/parts/search/browser/searchResultsView.ts index 1577ee03f2264..4515d7d943d73 100644 --- a/src/vs/workbench/parts/search/browser/searchResultsView.ts +++ b/src/vs/workbench/parts/search/browser/searchResultsView.ts @@ -388,7 +388,9 @@ export class SearchTreeController extends WorkbenchTreeController { const actions: IAction[] = []; fillInActions(this.contextMenu, { shouldForwardArgs: true }, actions, this.contextMenuService); return TPromise.as(actions); - } + }, + + getActionsContext: () => element }); return true; diff --git a/src/vs/workbench/parts/search/common/constants.ts b/src/vs/workbench/parts/search/common/constants.ts index fe9c22e072513..a2e93246ec691 100644 --- a/src/vs/workbench/parts/search/common/constants.ts +++ b/src/vs/workbench/parts/search/common/constants.ts @@ -12,6 +12,8 @@ export const FocusSearchFromResults = 'search.action.focusSearchFromResults'; export const OpenMatchToSide = 'search.action.openResultToSide'; export const CancelActionId = 'search.action.cancel'; export const RemoveActionId = 'search.action.remove'; +export const CopyPathCommandId = 'search.action.copyPath'; +export const CopyMatchCommandId = 'search.action.copyMatch'; export const ReplaceActionId = 'search.action.replace'; export const ReplaceAllInFileActionId = 'search.action.replaceAllInFile'; export const ReplaceAllInFolderActionId = 'search.action.replaceAllInFolder'; diff --git a/src/vs/workbench/parts/search/electron-browser/search.contribution.ts b/src/vs/workbench/parts/search/electron-browser/search.contribution.ts index e26158e086fa2..1715c84ce87a4 100644 --- a/src/vs/workbench/parts/search/electron-browser/search.contribution.ts +++ b/src/vs/workbench/parts/search/electron-browser/search.contribution.ts @@ -53,12 +53,12 @@ import { getMultiSelectedResources } from 'vs/workbench/parts/files/browser/file import { Schemas } from 'vs/base/common/network'; import { PanelRegistry, Extensions as PanelExtensions, PanelDescriptor } from 'vs/workbench/browser/panel'; import { IPanelService } from 'vs/workbench/services/panel/common/panelService'; -import { openSearchView, getSearchView, ReplaceAllInFolderAction, ReplaceAllAction, CloseReplaceAction, FocusNextInputAction, FocusPreviousInputAction, FocusNextSearchResultAction, FocusPreviousSearchResultAction, ReplaceInFilesAction, FindInFilesAction, FocusActiveEditorCommand, toggleCaseSensitiveCommand, ShowNextSearchTermAction, ShowPreviousSearchTermAction, toggleRegexCommand, ShowPreviousSearchIncludeAction, ShowNextSearchIncludeAction, CollapseDeepestExpandedLevelAction, toggleWholeWordCommand, RemoveAction, ReplaceAction, ClearSearchResultsAction } from 'vs/workbench/parts/search/browser/searchActions'; +import { openSearchView, getSearchView, ReplaceAllInFolderAction, ReplaceAllAction, CloseReplaceAction, FocusNextInputAction, FocusPreviousInputAction, FocusNextSearchResultAction, FocusPreviousSearchResultAction, ReplaceInFilesAction, FindInFilesAction, FocusActiveEditorCommand, toggleCaseSensitiveCommand, ShowNextSearchTermAction, ShowPreviousSearchTermAction, toggleRegexCommand, ShowPreviousSearchIncludeAction, ShowNextSearchIncludeAction, CollapseDeepestExpandedLevelAction, toggleWholeWordCommand, RemoveAction, ReplaceAction, ClearSearchResultsAction, copyPathCommand } from 'vs/workbench/parts/search/browser/searchActions'; import { VIEW_ID, ISearchConfigurationProperties } from 'vs/platform/search/common/search'; import { IWorkbenchContributionsRegistry, Extensions as WorkbenchExtensions } from 'vs/workbench/common/contributions'; import { LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle'; import { SearchViewLocationUpdater } from 'vs/workbench/parts/search/browser/searchViewLocationUpdater'; -import { IConfigurationService } from '../../../../platform/configuration/common/configuration'; +import { IConfigurationService } from 'vs/platform/configuration/common/configuration'; registerSingleton(ISearchWorkbenchService, SearchWorkbenchService); replaceContributions(); @@ -235,6 +235,27 @@ MenuRegistry.appendMenuItem(MenuId.SearchContext, { order: 2 }); +KeybindingsRegistry.registerCommandAndKeybindingRule({ + id: Constants.CopyPathCommandId, + weight: KeybindingsRegistry.WEIGHT.workbenchContrib(), + when: Constants.FileFocusKey, + primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.KEY_C, + win: { + primary: KeyMod.Shift | KeyMod.Alt | KeyCode.KEY_C + }, + handler: copyPathCommand +}); + +MenuRegistry.appendMenuItem(MenuId.SearchContext, { + command: { + id: Constants.CopyPathCommandId, + title: nls.localize('copyPathLabel', "Copy Path") + }, + when: Constants.FileFocusKey, + group: 'search', + order: 3 +}); + CommandsRegistry.registerCommand({ id: Constants.ToggleSearchViewPositionCommandId, handler: (accessor) => { @@ -254,7 +275,7 @@ MenuRegistry.appendMenuItem(MenuId.SearchContext, { }, when: Constants.SearchViewVisibleKey, group: 'search_2', - order: 3 + order: 1 }); const FIND_IN_FOLDER_ID = 'filesExplorer.findInFolder';