searchEditor.contribution.ts 15.3 KB
Newer Older
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
import * as objects from 'vs/base/common/objects';
8
import { extname } from 'vs/base/common/resources';
J
Jackson Kearl 已提交
9
import { URI } from 'vs/base/common/uri';
10
import { ServicesAccessor } from 'vs/editor/browser/editorExtensions';
11
import { Range } from 'vs/editor/common/core/range';
12 13
import { ToggleCaseSensitiveKeybinding, ToggleRegexKeybinding, ToggleWholeWordKeybinding } from 'vs/editor/contrib/find/findModel';
import { localize } from 'vs/nls';
14
import { Action2, MenuId, registerAction2 } from 'vs/platform/actions/common/actions';
15
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
16 17
import { ContextKeyExpr, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
J
Jackson Kearl 已提交
18
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
19 20 21
import { KeybindingsRegistry, KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle';
import { Registry } from 'vs/platform/registry/common/platform';
J
Jackson Kearl 已提交
22
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
23
import { EditorDescriptor, Extensions as EditorExtensions, IEditorRegistry } from 'vs/workbench/browser/editor';
J
Jackson Kearl 已提交
24
import { Extensions as WorkbenchExtensions, IWorkbenchContribution, IWorkbenchContributionsRegistry } from 'vs/workbench/common/contributions';
25 26 27 28
import { ActiveEditorContext, Extensions as EditorInputExtensions, IEditorInputFactory, IEditorInputFactoryRegistry } from 'vs/workbench/common/editor';
import { IViewsService } from 'vs/workbench/common/views';
import { getSearchView } from 'vs/workbench/contrib/search/browser/searchActions';
import { searchRefreshIcon } from 'vs/workbench/contrib/search/browser/searchIcons';
29
import * as SearchConstants from 'vs/workbench/contrib/search/common/constants';
J
Jackson Kearl 已提交
30
import * as SearchEditorConstants from 'vs/workbench/contrib/searchEditor/browser/constants';
31
import { SearchEditor } from 'vs/workbench/contrib/searchEditor/browser/searchEditor';
32
import { createEditorFromSearchResult, modifySearchEditorContextLinesCommand, openNewSearchEditor, selectAllSearchEditorMatchesCommand, toggleSearchEditorCaseSensitiveCommand, toggleSearchEditorContextLinesCommand, toggleSearchEditorRegexCommand, toggleSearchEditorWholeWordCommand } from 'vs/workbench/contrib/searchEditor/browser/searchEditorActions';
33
import { getOrMakeSearchEditorInput, SearchConfiguration, SearchEditorInput, SEARCH_EDITOR_EXT } from 'vs/workbench/contrib/searchEditor/browser/searchEditorInput';
34
import { parseSavedSearchEditor } from 'vs/workbench/contrib/searchEditor/browser/searchEditorSerialization';
35
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
36

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

const OpenInEditorCommandId = 'search.action.openInEditor';
const OpenNewEditorToSideCommandId = 'search.action.openNewEditorToSide';
const FocusQueryEditorWidgetCommandId = 'search.action.focusQueryEditorWidget';

const ToggleSearchEditorCaseSensitiveCommandId = 'toggleSearchEditorCaseSensitive';
const ToggleSearchEditorWholeWordCommandId = 'toggleSearchEditorWholeWord';
const ToggleSearchEditorRegexCommandId = 'toggleSearchEditorRegex';
const ToggleSearchEditorContextLinesCommandId = 'toggleSearchEditorContextLines';
const IncreaseSearchEditorContextLinesCommandId = 'increaseSearchEditorContextLines';
const DecreaseSearchEditorContextLinesCommandId = 'decreaseSearchEditorContextLines';

const RerunSearchEditorSearchCommandId = 'rerunSearchEditorSearch';
const CleanSearchEditorStateCommandId = 'cleanSearchEditorState';
const SelectAllSearchEditorMatchesCommandId = 'selectAllSearchEditorMatches';



55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
//#region Editor Descriptior
Registry.as<IEditorRegistry>(EditorExtensions.Editors).registerEditor(
	EditorDescriptor.create(
		SearchEditor,
		SearchEditor.ID,
		localize('searchEditor', "Search Editor")
	),
	[
		new SyncDescriptor(SearchEditorInput)
	]
);
//#endregion

//#region Startup Contribution
class SearchEditorContribution implements IWorkbenchContribution {
	constructor(
		@IEditorService private readonly editorService: IEditorService,
		@IInstantiationService protected readonly instantiationService: IInstantiationService,
		@ITelemetryService protected readonly telemetryService: ITelemetryService,
		@IContextKeyService protected readonly contextKeyService: IContextKeyService,
	) {

R
rebornix 已提交
77 78 79 80
		this.editorService.overrideOpenEditor({
			open: (editor, options, group) => {
				const resource = editor.resource;
				if (!resource) { return undefined; }
J
Jackson Kearl 已提交
81

82
				if (extname(resource) !== SEARCH_EDITOR_EXT) {
R
rebornix 已提交
83 84
					return undefined;
				}
85

86
				if (editor instanceof SearchEditorInput && group.isOpened(editor)) {
R
rebornix 已提交
87 88
					return undefined;
				}
89

R
rebornix 已提交
90
				this.telemetryService.publicLog2('searchEditor/openSavedSearchEditor');
J
Jackson Kearl 已提交
91

R
rebornix 已提交
92 93 94 95
				return {
					override: (async () => {
						const { config } = await instantiationService.invokeFunction(parseSavedSearchEditor, resource);
						const input = instantiationService.invokeFunction(getOrMakeSearchEditorInput, { backingUri: resource, config });
96
						return editorService.openEditor(input, { ...options, override: false }, group);
R
rebornix 已提交
97 98 99
					})()
				};
			}
100 101 102 103 104 105 106 107 108
		});
	}
}

const workbenchContributionsRegistry = Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench);
workbenchContributionsRegistry.registerWorkbenchContribution(SearchEditorContribution, LifecyclePhase.Starting);
//#endregion

//#region Input Factory
109
type SerializedSearchEditor = { modelUri: string, dirty: boolean, config: SearchConfiguration, name: string, matchRanges: Range[], backingUri: string };
110 111
class SearchEditorInputFactory implements IEditorInputFactory {

112 113 114
	canSerialize(input: SearchEditorInput) {
		return !input.isDisposed();
	}
115 116

	serialize(input: SearchEditorInput) {
117 118 119
		let modelUri = undefined;
		if (input.modelUri.path || input.modelUri.fragment) {
			modelUri = input.modelUri.toString();
120
		}
121
		if (!modelUri) { return undefined; }
122

123
		const config = input.config;
124
		const dirty = input.isDirty();
125
		const matchRanges = input.getMatchRanges();
126
		const backingUri = input.backingUri;
127

128
		return JSON.stringify({ modelUri: modelUri.toString(), dirty, config, name: input.getName(), matchRanges, backingUri: backingUri?.toString() } as SerializedSearchEditor);
129 130 131
	}

	deserialize(instantiationService: IInstantiationService, serializedEditorInput: string): SearchEditorInput | undefined {
132 133 134
		const { modelUri, dirty, config, matchRanges, backingUri } = JSON.parse(serializedEditorInput) as SerializedSearchEditor;
		if (config && (config.query !== undefined) && (modelUri !== undefined)) {
			const input = instantiationService.invokeFunction(getOrMakeSearchEditorInput, { config, modelUri: URI.parse(modelUri), backingUri: backingUri ? URI.parse(backingUri) : undefined });
135
			input.setDirty(dirty);
136
			input.setMatchRanges(matchRanges);
137 138 139 140 141 142 143 144 145 146 147 148 149
			return input;
		}
		return undefined;
	}
}

Registry.as<IEditorInputFactoryRegistry>(EditorInputExtensions.EditorInputFactories).registerEditorInputFactory(
	SearchEditorInput.ID,
	SearchEditorInputFactory);
//#endregion

//#region Commands
KeybindingsRegistry.registerCommandAndKeybindingRule(objects.assign({
150
	id: ToggleSearchEditorCaseSensitiveCommandId,
151 152 153 154 155 156
	weight: KeybindingWeight.WorkbenchContrib,
	when: ContextKeyExpr.and(SearchEditorConstants.InSearchEditor, SearchConstants.SearchInputBoxFocusedKey),
	handler: toggleSearchEditorCaseSensitiveCommand
}, ToggleCaseSensitiveKeybinding));

KeybindingsRegistry.registerCommandAndKeybindingRule(objects.assign({
157
	id: ToggleSearchEditorWholeWordCommandId,
158 159 160 161 162 163
	weight: KeybindingWeight.WorkbenchContrib,
	when: ContextKeyExpr.and(SearchEditorConstants.InSearchEditor, SearchConstants.SearchInputBoxFocusedKey),
	handler: toggleSearchEditorWholeWordCommand
}, ToggleWholeWordKeybinding));

KeybindingsRegistry.registerCommandAndKeybindingRule(objects.assign({
164
	id: ToggleSearchEditorRegexCommandId,
165 166 167 168 169 170
	weight: KeybindingWeight.WorkbenchContrib,
	when: ContextKeyExpr.and(SearchEditorConstants.InSearchEditor, SearchConstants.SearchInputBoxFocusedKey),
	handler: toggleSearchEditorRegexCommand
}, ToggleRegexKeybinding));

KeybindingsRegistry.registerCommandAndKeybindingRule({
171
	id: ToggleSearchEditorContextLinesCommandId,
172 173 174 175 176 177
	weight: KeybindingWeight.WorkbenchContrib,
	when: ContextKeyExpr.and(SearchEditorConstants.InSearchEditor),
	handler: toggleSearchEditorContextLinesCommand,
	primary: KeyMod.Alt | KeyCode.KEY_L,
	mac: { primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.KEY_L }
});
178

179
KeybindingsRegistry.registerCommandAndKeybindingRule({
180
	id: IncreaseSearchEditorContextLinesCommandId,
181 182 183
	weight: KeybindingWeight.WorkbenchContrib,
	when: ContextKeyExpr.and(SearchEditorConstants.InSearchEditor),
	handler: (accessor: ServicesAccessor) => modifySearchEditorContextLinesCommand(accessor, true),
J
Jackson Kearl 已提交
184
	primary: KeyMod.Alt | KeyCode.US_EQUAL
185 186 187
});

KeybindingsRegistry.registerCommandAndKeybindingRule({
188
	id: DecreaseSearchEditorContextLinesCommandId,
189 190 191
	weight: KeybindingWeight.WorkbenchContrib,
	when: ContextKeyExpr.and(SearchEditorConstants.InSearchEditor),
	handler: (accessor: ServicesAccessor) => modifySearchEditorContextLinesCommand(accessor, false),
J
Jackson Kearl 已提交
192
	primary: KeyMod.Alt | KeyCode.US_MINUS
193 194
});

195
KeybindingsRegistry.registerCommandAndKeybindingRule({
196
	id: SelectAllSearchEditorMatchesCommandId,
197 198 199 200 201 202
	weight: KeybindingWeight.WorkbenchContrib,
	when: ContextKeyExpr.and(SearchEditorConstants.InSearchEditor),
	primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_L,
	handler: selectAllSearchEditorMatchesCommand
});

203
CommandsRegistry.registerCommand(
204
	CleanSearchEditorStateCommandId,
205
	(accessor: ServicesAccessor) => {
206 207 208
		const activeEditorPane = accessor.get(IEditorService).activeEditorPane;
		if (activeEditorPane instanceof SearchEditor) {
			activeEditorPane.cleanState();
209 210
		}
	});
211 212 213
//#endregion

//#region Actions
B
Benjamin Pasero 已提交
214
const category = { value: localize('search', "Search Editor"), original: 'Search Editor' };
215

216
export type OpenSearchEditorArgs = Partial<SearchConfiguration & { triggerSearch: boolean, focusResults: boolean }>;
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
const openArgDescription = {
	description: 'Open a new search editor. Arguments passed can include variables like ${relativeFileDirname}.',
	args: [{
		name: 'Open new Search Editor args',
		schema: {
			properties: {
				query: { type: 'string' },
				includes: { type: 'string' },
				excludes: { type: 'string' },
				contextLines: { type: 'number' },
				wholeWord: { type: 'boolean' },
				caseSensitive: { type: 'boolean' },
				regexp: { type: 'boolean' },
				useIgnores: { type: 'boolean' },
				showIncludesExcludes: { type: 'boolean' },
232 233
				triggerSearch: { type: 'boolean' },
				focusResults: { type: 'boolean' },
234 235 236 237 238
			}
		}
	}]
} as const;

239 240 241 242
registerAction2(class extends Action2 {
	constructor() {
		super({
			id: 'search.searchEditor.action.deleteFileResults',
B
Benjamin Pasero 已提交
243
			title: { value: localize('searchEditor.deleteResultBlock', "Delete File Results"), original: 'Delete File Results' },
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
			keybinding: {
				weight: KeybindingWeight.EditorContrib,
				when: SearchEditorConstants.InSearchEditor,
				primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.Backspace,
			},
			precondition: SearchEditorConstants.InSearchEditor,
			category,
			f1: true,
		});
	}

	async run(accessor: ServicesAccessor) {
		const contextService = accessor.get(IContextKeyService).getContext(document.activeElement);
		if (contextService.getValue(SearchEditorConstants.InSearchEditor.serialize())) {
			(accessor.get(IEditorService).activeEditorPane as SearchEditor).deleteResultBlock();
		}
	}
});

263 264 265 266
registerAction2(class extends Action2 {
	constructor() {
		super({
			id: SearchEditorConstants.OpenNewEditorCommandId,
B
Benjamin Pasero 已提交
267
			title: { value: localize('search.openNewSearchEditor', "Open new Search Editor"), original: 'Open new Search Editor' },
268 269
			category,
			f1: true,
270
			description: openArgDescription
271 272
		});
	}
273
	async run(accessor: ServicesAccessor, args: OpenSearchEditorArgs) {
274 275 276 277 278 279 280 281
		await accessor.get(IInstantiationService).invokeFunction(openNewSearchEditor, args);
	}
});

registerAction2(class extends Action2 {
	constructor() {
		super({
			id: OpenNewEditorToSideCommandId,
B
Benjamin Pasero 已提交
282
			title: { value: localize('search.openNewEditorToSide', "Open new Search Editor to the Side"), original: 'Open new Search Editor to the Side' },
283 284
			category,
			f1: true,
285
			description: openArgDescription
286 287
		});
	}
288
	async run(accessor: ServicesAccessor, args: OpenSearchEditorArgs) {
289 290 291
		await accessor.get(IInstantiationService).invokeFunction(openNewSearchEditor, args, true);
	}
});
292

293 294 295
registerAction2(class extends Action2 {
	constructor() {
		super({
296
			id: OpenInEditorCommandId,
B
Benjamin Pasero 已提交
297
			title: { value: localize('search.openResultsInEditor', "Open Results in Editor"), original: 'Open Results in Editor' },
298 299 300
			category,
			f1: true,
			keybinding: {
J
Jackson Kearl 已提交
301
				primary: KeyMod.Alt | KeyCode.Enter,
302
				when: ContextKeyExpr.and(SearchConstants.HasSearchResults, SearchConstants.SearchViewFocusedKey),
J
Jackson Kearl 已提交
303 304 305 306
				weight: KeybindingWeight.WorkbenchContrib,
				mac: {
					primary: KeyMod.CtrlCmd | KeyCode.Enter
				}
307 308 309 310 311 312 313 314 315 316 317 318
			},
		});
	}
	async run(accessor: ServicesAccessor) {
		const viewsService = accessor.get(IViewsService);
		const instantiationService = accessor.get(IInstantiationService);
		const searchView = getSearchView(viewsService);
		if (searchView) {
			await instantiationService.invokeFunction(createEditorFromSearchResult, searchView.searchResult, searchView.searchIncludePattern.getValue(), searchView.searchExcludePattern.getValue());
		}
	}
});
319

320 321 322
registerAction2(class extends Action2 {
	constructor() {
		super({
323
			id: RerunSearchEditorSearchCommandId,
B
Benjamin Pasero 已提交
324
			title: { value: localize('search.rerunSearchInEditor', "Search Again"), original: 'Search Again' },
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
			category,
			keybinding: {
				primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_R,
				when: SearchEditorConstants.InSearchEditor,
				weight: KeybindingWeight.EditorContrib
			},
			icon: searchRefreshIcon,
			menu: [{
				id: MenuId.EditorTitle,
				group: 'navigation',
				when: ActiveEditorContext.isEqualTo(SearchEditorConstants.SearchEditorID)
			},
			{
				id: MenuId.CommandPalette,
				when: ActiveEditorContext.isEqualTo(SearchEditorConstants.SearchEditorID)
			}]
		});
	}
	async run(accessor: ServicesAccessor) {
		const editorService = accessor.get(IEditorService);
		const input = editorService.activeEditor;
		if (input instanceof SearchEditorInput) {
			(editorService.activeEditorPane as SearchEditor).triggerSearch({ resetCursor: false });
		}
	}
});
351

352 353 354
registerAction2(class extends Action2 {
	constructor() {
		super({
355
			id: FocusQueryEditorWidgetCommandId,
B
Benjamin Pasero 已提交
356
			title: { value: localize('search.action.focusQueryEditorWidget', "Focus Search Editor Input"), original: 'Focus Search Editor Input' },
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
			category,
			menu: {
				id: MenuId.CommandPalette,
				when: ActiveEditorContext.isEqualTo(SearchEditorConstants.SearchEditorID)
			},
			keybinding: {
				primary: KeyCode.Escape,
				when: SearchEditorConstants.InSearchEditor,
				weight: KeybindingWeight.EditorContrib
			}
		});
	}
	async run(accessor: ServicesAccessor) {
		const editorService = accessor.get(IEditorService);
		const input = editorService.activeEditor;
		if (input instanceof SearchEditorInput) {
			(editorService.activeEditorPane as SearchEditor).focusSearchInput();
		}
	}
376
});
377
//#endregion