From 9442a6a2e4b5ed860b6f89272c1737acfe04ee80 Mon Sep 17 00:00:00 2001 From: smart-bo Date: Wed, 6 Oct 2021 14:45:27 -0400 Subject: [PATCH] Add 'replace in files' command --- ...arch-in-workspace-frontend-contribution.ts | 21 +++++++++++++++++-- .../browser/search-in-workspace-widget.tsx | 15 +++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/packages/search-in-workspace/src/browser/search-in-workspace-frontend-contribution.ts b/packages/search-in-workspace/src/browser/search-in-workspace-frontend-contribution.ts index 172cf518f0f1a..b8cc5dd1834f7 100644 --- a/packages/search-in-workspace/src/browser/search-in-workspace-frontend-contribution.ts +++ b/packages/search-in-workspace/src/browser/search-in-workspace-frontend-contribution.ts @@ -38,7 +38,11 @@ export namespace SearchInWorkspaceCommands { id: 'search-in-workspace.open', category: SEARCH_CATEGORY, label: 'Find in Files' - + }; + export const REPLACE_IN_FILES: Command = { + id: 'search-in-workspace.replace', + category: SEARCH_CATEGORY, + label: 'Replace in Files' }; export const FIND_IN_FOLDER: Command = { id: 'search-in-workspace.in-folder', @@ -123,6 +127,14 @@ export class SearchInWorkspaceFrontendContribution extends AbstractViewContribut } }); + commands.registerCommand(SearchInWorkspaceCommands.REPLACE_IN_FILES, { + isEnabled: () => this.workspaceService.tryGetRoots().length > 0, + execute: async () => { + const widget = await this.openView({ activate: true }); + widget.updateSearchAndReplaceTerm(this.getSearchTerm()); + } + }); + commands.registerCommand(SearchInWorkspaceCommands.FIND_IN_FOLDER, this.newMultiUriAwareCommandHandler({ execute: async uris => { const resources: string[] = []; @@ -212,7 +224,12 @@ export class SearchInWorkspaceFrontendContribution extends AbstractViewContribut commandId: SearchInWorkspaceCommands.FIND_IN_FOLDER.id }); menus.registerMenuAction(CommonMenus.EDIT_FIND, { - commandId: SearchInWorkspaceCommands.OPEN_SIW_WIDGET.id + commandId: SearchInWorkspaceCommands.OPEN_SIW_WIDGET.id, + order: '2' + }); + menus.registerMenuAction(CommonMenus.EDIT_FIND, { + commandId: SearchInWorkspaceCommands.REPLACE_IN_FILES.id, + order: '3' }); } diff --git a/packages/search-in-workspace/src/browser/search-in-workspace-widget.tsx b/packages/search-in-workspace/src/browser/search-in-workspace-widget.tsx index d31b17056e1f7..17cf5a1dcd864 100644 --- a/packages/search-in-workspace/src/browser/search-in-workspace-widget.tsx +++ b/packages/search-in-workspace/src/browser/search-in-workspace-widget.tsx @@ -233,6 +233,21 @@ export class SearchInWorkspaceWidget extends BaseWidget implements StatefulWidge this.refresh(); } + /** + * Update the replace term and input fields. + * @param term the replace in files term. + */ + updateSearchAndReplaceTerm(term: string): void { + this.searchTerm = term; + const search = document.getElementById('search-input-field'); + if (search) { + (search as HTMLInputElement).value = term; + } + this.showReplaceField = true; + this.resultTreeWidget.showReplaceButtons = this.showReplaceField; + this.update(); + } + hasResultList(): boolean { return this.hasResults; }