-
Notifications
You must be signed in to change notification settings - Fork 30.7k
/
Copy pathsearchActionsFind.ts
409 lines (378 loc) · 15 KB
/
searchActionsFind.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { dirname } from 'vs/base/common/resources';
import * as nls from 'vs/nls';
import { ICommandService } from 'vs/platform/commands/common/commands';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { IListService, WorkbenchCompressibleObjectTree } from 'vs/platform/list/browser/listService';
import { IViewsService, ViewContainerLocation } from 'vs/workbench/common/views';
import * as Constants from 'vs/workbench/contrib/search/common/constants';
import * as SearchEditorConstants from 'vs/workbench/contrib/searchEditor/browser/constants';
import { FileMatch, FolderMatchWithResource, Match, RenderableMatch } from 'vs/workbench/contrib/search/browser/searchModel';
import { OpenSearchEditorArgs } from 'vs/workbench/contrib/searchEditor/browser/searchEditor.contribution';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { ISearchConfiguration, ISearchConfigurationProperties } from 'vs/workbench/services/search/common/search';
import { URI } from 'vs/base/common/uri';
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
import { Action2, MenuId, registerAction2 } from 'vs/platform/actions/common/actions';
import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
import { resolveResourcesForSearchIncludes } from 'vs/workbench/services/search/common/queryBuilder';
import { getMultiSelectedResources, IExplorerService } from 'vs/workbench/contrib/files/browser/files';
import { IFileService } from 'vs/platform/files/common/files';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { ExplorerFolderContext, ExplorerRootContext, FilesExplorerFocusCondition, VIEWLET_ID as VIEWLET_ID_FILES } from 'vs/workbench/contrib/files/common/files';
import { IPaneCompositePartService } from 'vs/workbench/services/panecomposite/browser/panecomposite';
import { ExplorerViewPaneContainer } from 'vs/workbench/contrib/files/browser/explorerViewlet';
import { onUnexpectedError } from 'vs/base/common/errors';
import { category, getElementsToOperateOn, getSearchView, openSearchView } from 'vs/workbench/contrib/search/browser/searchActionsBase';
import { IConfigurationResolverService } from 'vs/workbench/services/configurationResolver/common/configurationResolver';
import { IHistoryService } from 'vs/workbench/services/history/common/history';
import { Schemas } from 'vs/base/common/network';
//#region Interfaces
export interface IFindInFilesArgs {
query?: string;
replace?: string;
preserveCase?: boolean;
triggerSearch?: boolean;
filesToInclude?: string;
filesToExclude?: string;
isRegex?: boolean;
isCaseSensitive?: boolean;
matchWholeWord?: boolean;
useExcludeSettingsAndIgnoreFiles?: boolean;
onlyOpenEditors?: boolean;
}
//#endregion
registerAction2(class RestrictSearchToFolderAction extends Action2 {
constructor() {
super({
id: Constants.RestrictSearchToFolderId,
title: {
value: nls.localize('restrictResultsToFolder', "Restrict Search to Folder"),
original: 'Restrict Search to Folder'
},
category,
keybinding: {
weight: KeybindingWeight.WorkbenchContrib,
when: ContextKeyExpr.and(Constants.SearchViewVisibleKey, Constants.ResourceFolderFocusKey),
primary: KeyMod.Shift | KeyMod.Alt | KeyCode.KeyF,
},
menu: [
{
id: MenuId.SearchContext,
group: 'search',
order: 3,
when: ContextKeyExpr.and(Constants.ResourceFolderFocusKey)
}
]
});
}
async run(accessor: ServicesAccessor, folderMatch?: FolderMatchWithResource) {
await searchWithFolderCommand(accessor, false, true, undefined, folderMatch);
}
});
registerAction2(class ExcludeFolderFromSearchAction extends Action2 {
constructor() {
super({
id: Constants.ExcludeFolderFromSearchId,
title: {
value: nls.localize('excludeFolderFromSearch', "Exclude Folder from Search"),
original: 'Exclude Folder from Search'
},
category,
menu: [
{
id: MenuId.SearchContext,
group: 'search',
order: 4,
when: ContextKeyExpr.and(Constants.ResourceFolderFocusKey)
}
]
});
}
async run(accessor: ServicesAccessor, folderMatch?: FolderMatchWithResource) {
await searchWithFolderCommand(accessor, false, false, undefined, folderMatch);
}
});
registerAction2(class RevealInSideBarForSearchResultsAction extends Action2 {
constructor(
) {
super({
id: Constants.RevealInSideBarForSearchResults,
title: {
value: nls.localize('revealInSideBar', "Reveal in Explorer View"),
original: 'Reveal in Explorer View'
},
category,
menu: [{
id: MenuId.SearchContext,
when: ContextKeyExpr.and(Constants.FileFocusKey, Constants.HasSearchResults),
group: 'search_3',
order: 1
}]
});
}
override async run(accessor: ServicesAccessor, args: any): Promise<any> {
const paneCompositeService = accessor.get(IPaneCompositePartService);
const explorerService = accessor.get(IExplorerService);
const contextService = accessor.get(IWorkspaceContextService);
const searchView = getSearchView(accessor.get(IViewsService));
if (!searchView) {
return;
}
let fileMatch: FileMatch;
if (!(args instanceof FileMatch)) {
args = searchView.getControl().getFocus()[0];
}
if (args instanceof FileMatch) {
fileMatch = args;
} else {
return;
}
paneCompositeService.openPaneComposite(VIEWLET_ID_FILES, ViewContainerLocation.Sidebar, false).then((viewlet) => {
if (!viewlet) {
return;
}
const explorerViewContainer = viewlet.getViewPaneContainer() as ExplorerViewPaneContainer;
const uri = fileMatch.resource;
if (uri && contextService.isInsideWorkspace(uri)) {
const explorerView = explorerViewContainer.getExplorerView();
explorerView.setExpanded(true);
explorerService.select(uri, true).then(() => explorerView.focus(), onUnexpectedError);
}
});
}
});
// Find in Files by default is the same as View: Show Search, but can be configured to open a search editor instead with the `search.mode` binding
registerAction2(class FindInFilesAction extends Action2 {
constructor(
) {
super({
id: Constants.FindInFilesActionId,
title: {
value: nls.localize('findInFiles', "Find in Files"),
mnemonicTitle: nls.localize({ key: 'miFindInFiles', comment: ['&& denotes a mnemonic'] }, "Find &&in Files"),
original: 'Find in Files'
},
metadata: {
description: nls.localize('findInFiles.description', "Open a workspace search"),
args: [
{
name: nls.localize('findInFiles.args', "A set of options for the search"),
schema: {
type: 'object',
properties: {
query: { 'type': 'string' },
replace: { 'type': 'string' },
preserveCase: { 'type': 'boolean' },
triggerSearch: { 'type': 'boolean' },
filesToInclude: { 'type': 'string' },
filesToExclude: { 'type': 'string' },
isRegex: { 'type': 'boolean' },
isCaseSensitive: { 'type': 'boolean' },
matchWholeWord: { 'type': 'boolean' },
useExcludeSettingsAndIgnoreFiles: { 'type': 'boolean' },
onlyOpenEditors: { 'type': 'boolean' },
}
}
},
]
},
category,
keybinding: {
weight: KeybindingWeight.WorkbenchContrib,
primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KeyF,
},
menu: [{
id: MenuId.MenubarEditMenu,
group: '4_find_global',
order: 1,
}],
f1: true
});
}
override async run(accessor: ServicesAccessor, args: IFindInFilesArgs = {}): Promise<any> {
findInFilesCommand(accessor, args);
}
});
registerAction2(class FindInFolderAction extends Action2 {
// from explorer
constructor() {
super({
id: Constants.FindInFolderId,
title: {
value: nls.localize('findInFolder', "Find in Folder..."),
original: 'Find in Folder...'
},
category,
keybinding: {
weight: KeybindingWeight.WorkbenchContrib,
when: ContextKeyExpr.and(FilesExplorerFocusCondition, ExplorerFolderContext),
primary: KeyMod.Shift | KeyMod.Alt | KeyCode.KeyF,
},
menu: [
{
id: MenuId.ExplorerContext,
group: '4_search',
order: 10,
when: ContextKeyExpr.and(ExplorerFolderContext)
}
]
});
}
async run(accessor: ServicesAccessor, resource?: URI) {
await searchWithFolderCommand(accessor, true, true, resource);
}
});
registerAction2(class FindInWorkspaceAction extends Action2 {
// from explorer
constructor() {
super({
id: Constants.FindInWorkspaceId,
title: {
value: nls.localize('findInWorkspace', "Find in Workspace..."),
original: 'Find in Workspace...'
},
category,
menu: [
{
id: MenuId.ExplorerContext,
group: '4_search',
order: 10,
when: ContextKeyExpr.and(ExplorerRootContext, ExplorerFolderContext.toNegated())
}
]
});
}
async run(accessor: ServicesAccessor) {
const searchConfig = accessor.get(IConfigurationService).getValue<ISearchConfiguration>().search;
const mode = searchConfig.mode;
if (mode === 'view') {
const searchView = await openSearchView(accessor.get(IViewsService), true);
searchView?.searchInFolders();
}
else {
return accessor.get(ICommandService).executeCommand(SearchEditorConstants.OpenEditorCommandId, {
location: mode === 'newEditor' ? 'new' : 'reuse',
filesToInclude: '',
});
}
}
});
//#region Helpers
async function searchWithFolderCommand(accessor: ServicesAccessor, isFromExplorer: boolean, isIncludes: boolean, resource?: URI, folderMatch?: FolderMatchWithResource) {
const listService = accessor.get(IListService);
const fileService = accessor.get(IFileService);
const viewsService = accessor.get(IViewsService);
const contextService = accessor.get(IWorkspaceContextService);
const commandService = accessor.get(ICommandService);
const searchConfig = accessor.get(IConfigurationService).getValue<ISearchConfiguration>().search;
const mode = searchConfig.mode;
let resources: URI[];
if (isFromExplorer) {
resources = getMultiSelectedResources(resource, listService, accessor.get(IEditorService), accessor.get(IExplorerService));
} else {
const searchView = getSearchView(accessor.get(IViewsService));
if (!searchView) {
return;
}
resources = getMultiSelectedSearchResources(searchView.getControl(), folderMatch, searchConfig);
}
const resolvedResources = fileService.resolveAll(resources.map(resource => ({ resource }))).then(results => {
const folders: URI[] = [];
results.forEach(result => {
if (result.success && result.stat) {
folders.push(result.stat.isDirectory ? result.stat.resource : dirname(result.stat.resource));
}
});
return resolveResourcesForSearchIncludes(folders, contextService);
});
if (mode === 'view') {
const searchView = await openSearchView(viewsService, true);
if (resources && resources.length && searchView) {
if (isIncludes) {
searchView.searchInFolders(await resolvedResources);
} else {
searchView.searchOutsideOfFolders(await resolvedResources);
}
}
return undefined;
} else {
if (isIncludes) {
return commandService.executeCommand(SearchEditorConstants.OpenEditorCommandId, {
filesToInclude: (await resolvedResources).join(', '),
showIncludesExcludes: true,
location: mode === 'newEditor' ? 'new' : 'reuse',
});
}
else {
return commandService.executeCommand(SearchEditorConstants.OpenEditorCommandId, {
filesToExclude: (await resolvedResources).join(', '),
showIncludesExcludes: true,
location: mode === 'newEditor' ? 'new' : 'reuse',
});
}
}
}
function getMultiSelectedSearchResources(viewer: WorkbenchCompressibleObjectTree<RenderableMatch, void>, currElement: RenderableMatch | undefined, sortConfig: ISearchConfigurationProperties): URI[] {
return getElementsToOperateOn(viewer, currElement, sortConfig)
.map((renderableMatch) => ((renderableMatch instanceof Match) ? null : renderableMatch.resource))
.filter((renderableMatch): renderableMatch is URI => (renderableMatch !== null));
}
export async function findInFilesCommand(accessor: ServicesAccessor, _args: IFindInFilesArgs = {}) {
const searchConfig = accessor.get(IConfigurationService).getValue<ISearchConfiguration>().search;
const viewsService = accessor.get(IViewsService);
const commandService = accessor.get(ICommandService);
const args: IFindInFilesArgs = {};
if (Object.keys(_args).length !== 0) {
// resolve variables in the same way as in
// https://github.com/microsoft/vscode/blob/8b76efe9d317d50cb5b57a7658e09ce6ebffaf36/src/vs/workbench/contrib/searchEditor/browser/searchEditorActions.ts#L152-L158
const configurationResolverService = accessor.get(IConfigurationResolverService);
const historyService = accessor.get(IHistoryService);
const workspaceContextService = accessor.get(IWorkspaceContextService);
const activeWorkspaceRootUri = historyService.getLastActiveWorkspaceRoot();
const filteredActiveWorkspaceRootUri = activeWorkspaceRootUri?.scheme === Schemas.file || activeWorkspaceRootUri?.scheme === Schemas.vscodeRemote ? activeWorkspaceRootUri : undefined;
const lastActiveWorkspaceRoot = filteredActiveWorkspaceRootUri ? workspaceContextService.getWorkspaceFolder(filteredActiveWorkspaceRootUri) ?? undefined : undefined;
for (const entry of Object.entries(_args)) {
const name = entry[0];
const value = entry[1];
if (value !== undefined) {
(args as any)[name as any] = (typeof value === 'string') ? await configurationResolverService.resolveAsync(lastActiveWorkspaceRoot, value) : value;
}
}
}
const mode = searchConfig.mode;
if (mode === 'view') {
openSearchView(viewsService, false).then(openedView => {
if (openedView) {
const searchAndReplaceWidget = openedView.searchAndReplaceWidget;
searchAndReplaceWidget.toggleReplace(typeof args.replace === 'string');
let updatedText = false;
if (typeof args.query !== 'string') {
updatedText = openedView.updateTextFromFindWidgetOrSelection({ allowUnselectedWord: typeof args.replace !== 'string' });
}
openedView.setSearchParameters(args);
openedView.searchAndReplaceWidget.focus(undefined, updatedText, updatedText);
}
});
} else {
const convertArgs = (args: IFindInFilesArgs): OpenSearchEditorArgs => ({
location: mode === 'newEditor' ? 'new' : 'reuse',
query: args.query,
filesToInclude: args.filesToInclude,
filesToExclude: args.filesToExclude,
matchWholeWord: args.matchWholeWord,
isCaseSensitive: args.isCaseSensitive,
isRegexp: args.isRegex,
useExcludeSettingsAndIgnoreFiles: args.useExcludeSettingsAndIgnoreFiles,
onlyOpenEditors: args.onlyOpenEditors,
showIncludesExcludes: !!(args.filesToExclude || args.filesToExclude || !args.useExcludeSettingsAndIgnoreFiles),
});
commandService.executeCommand(SearchEditorConstants.OpenEditorCommandId, convertArgs(args));
}
}
//#endregion