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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as dom from 'vs/base/browser/dom';
import { IKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { ActionBar } from 'vs/base/browser/ui/actionbar/actionbar';
import { Button, IButtonOptions } from 'vs/base/browser/ui/button/button';
import { FindInput, IFindInputOptions } from 'vs/base/browser/ui/findinput/findInput';
import { ReplaceInput } from 'vs/base/browser/ui/findinput/replaceInput';
import { IMessage, InputBox } from 'vs/base/browser/ui/inputbox/inputBox';
import { Widget } from 'vs/base/browser/ui/widget';
import { Action } from 'vs/base/common/actions';
import { Delayer } from 'vs/base/common/async';
import { Emitter, Event } from 'vs/base/common/event';
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
import { CONTEXT_FIND_WIDGET_NOT_VISIBLE } from 'vs/editor/contrib/find/browser/findModel';
import * as nls from 'vs/nls';
import { IClipboardService } from 'vs/platform/clipboard/common/clipboardService';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { ContextKeyExpr, IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IContextViewService } from 'vs/platform/contextview/browser/contextView';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { KeybindingsRegistry, KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { ISearchConfigurationProperties } from 'vs/workbench/services/search/common/search';
import { attachFindReplaceInputBoxStyler, attachInputBoxStyler } from 'vs/platform/theme/common/styler';
import { IThemeService, ThemeIcon } from 'vs/platform/theme/common/themeService';
import { ContextScopedFindInput, ContextScopedReplaceInput } from 'vs/platform/history/browser/contextScopedHistoryWidget';
import { appendKeyBindingLabel, isSearchViewFocused, getSearchView } from 'vs/workbench/contrib/search/browser/searchActions';
import * as Constants from 'vs/workbench/contrib/search/common/constants';
import { IAccessibilityService } from 'vs/platform/accessibility/common/accessibility';
import { isMacintosh } from 'vs/base/common/platform';
import { Toggle } from 'vs/base/browser/ui/toggle/toggle';
import { IViewsService } from 'vs/workbench/common/views';
import { searchReplaceAllIcon, searchHideReplaceIcon, searchShowContextIcon, searchShowReplaceIcon } from 'vs/workbench/contrib/search/browser/searchIcons';
import { ToggleSearchEditorContextLinesCommandId } from 'vs/workbench/contrib/searchEditor/browser/constants';
import { showHistoryKeybindingHint } from 'vs/platform/history/browser/historyWidgetKeybindingHint';
/** Specified in searchview.css */
export const SingleLineInputHeight = 24;
export interface ISearchWidgetOptions {
value?: string;
replaceValue?: string;
isRegex?: boolean;
isCaseSensitive?: boolean;
isWholeWords?: boolean;
searchHistory?: string[];
replaceHistory?: string[];
preserveCase?: boolean;
_hideReplaceToggle?: boolean; // TODO: Search Editor's replace experience
showContextToggle?: boolean;
}
class ReplaceAllAction extends Action {
static readonly ID: string = 'search.action.replaceAll';
constructor(private _searchWidget: SearchWidget) {
super(ReplaceAllAction.ID, '', ThemeIcon.asClassName(searchReplaceAllIcon), false);
}
set searchWidget(searchWidget: SearchWidget) {
this._searchWidget = searchWidget;
}
override run(): Promise<any> {
if (this._searchWidget) {
return this._searchWidget.triggerReplaceAll();
}
return Promise.resolve(null);
}
}
const ctrlKeyMod = (isMacintosh ? KeyMod.WinCtrl : KeyMod.CtrlCmd);
function stopPropagationForMultiLineUpwards(event: IKeyboardEvent, value: string, textarea: HTMLTextAreaElement | null) {
const isMultiline = !!value.match(/\n/);
if (textarea && (isMultiline || textarea.clientHeight > SingleLineInputHeight) && textarea.selectionStart > 0) {
event.stopPropagation();
return;
}
}
function stopPropagationForMultiLineDownwards(event: IKeyboardEvent, value: string, textarea: HTMLTextAreaElement | null) {
const isMultiline = !!value.match(/\n/);
if (textarea && (isMultiline || textarea.clientHeight > SingleLineInputHeight) && textarea.selectionEnd < textarea.value.length) {
event.stopPropagation();
return;
}
}
export class SearchWidget extends Widget {
private static readonly INPUT_MAX_HEIGHT = 134;
private static readonly REPLACE_ALL_DISABLED_LABEL = nls.localize('search.action.replaceAll.disabled.label', "Replace All (Submit Search to Enable)");
private static readonly REPLACE_ALL_ENABLED_LABEL = (keyBindingService2: IKeybindingService): string => {
const kb = keyBindingService2.lookupKeybinding(ReplaceAllAction.ID);
return appendKeyBindingLabel(nls.localize('search.action.replaceAll.enabled.label', "Replace All"), kb, keyBindingService2);
};
domNode!: HTMLElement;
searchInput!: FindInput;
searchInputFocusTracker!: dom.IFocusTracker;
private searchInputBoxFocused: IContextKey<boolean>;
private replaceContainer!: HTMLElement;
replaceInput!: ReplaceInput;
replaceInputFocusTracker!: dom.IFocusTracker;
private replaceInputBoxFocused: IContextKey<boolean>;
private toggleReplaceButton!: Button;
private replaceAllAction!: ReplaceAllAction;
private replaceActive: IContextKey<boolean>;
private replaceActionBar!: ActionBar;
private _replaceHistoryDelayer: Delayer<void>;
private ignoreGlobalFindBufferOnNextFocus = false;
private previousGlobalFindBufferValue: string | null = null;
private _onSearchSubmit = this._register(new Emitter<{ triggeredOnType: boolean; delay: number }>());
readonly onSearchSubmit: Event<{ triggeredOnType: boolean; delay: number }> = this._onSearchSubmit.event;
private _onSearchCancel = this._register(new Emitter<{ focus: boolean }>());
readonly onSearchCancel: Event<{ focus: boolean }> = this._onSearchCancel.event;
private _onReplaceToggled = this._register(new Emitter<void>());
readonly onReplaceToggled: Event<void> = this._onReplaceToggled.event;
private _onReplaceStateChange = this._register(new Emitter<boolean>());
readonly onReplaceStateChange: Event<boolean> = this._onReplaceStateChange.event;
private _onPreserveCaseChange = this._register(new Emitter<boolean>());
readonly onPreserveCaseChange: Event<boolean> = this._onPreserveCaseChange.event;
private _onReplaceValueChanged = this._register(new Emitter<void>());
readonly onReplaceValueChanged: Event<void> = this._onReplaceValueChanged.event;
private _onReplaceAll = this._register(new Emitter<void>());
readonly onReplaceAll: Event<void> = this._onReplaceAll.event;
private _onBlur = this._register(new Emitter<void>());
readonly onBlur: Event<void> = this._onBlur.event;
private _onDidHeightChange = this._register(new Emitter<void>());
readonly onDidHeightChange: Event<void> = this._onDidHeightChange.event;
private readonly _onDidToggleContext = new Emitter<void>();
readonly onDidToggleContext: Event<void> = this._onDidToggleContext.event;
private showContextToggle!: Toggle;
public contextLinesInput!: InputBox;
constructor(
container: HTMLElement,
options: ISearchWidgetOptions,
@IContextViewService private readonly contextViewService: IContextViewService,
@IThemeService private readonly themeService: IThemeService,
@IContextKeyService private readonly contextKeyService: IContextKeyService,
@IKeybindingService private readonly keybindingService: IKeybindingService,
@IClipboardService private readonly clipboardServce: IClipboardService,
@IConfigurationService private readonly configurationService: IConfigurationService,
@IAccessibilityService private readonly accessibilityService: IAccessibilityService
) {
super();
this.replaceActive = Constants.ReplaceActiveKey.bindTo(this.contextKeyService);
this.searchInputBoxFocused = Constants.SearchInputBoxFocusedKey.bindTo(this.contextKeyService);
this.replaceInputBoxFocused = Constants.ReplaceInputBoxFocusedKey.bindTo(this.contextKeyService);
this._replaceHistoryDelayer = new Delayer<void>(500);
this.render(container, options);
this.configurationService.onDidChangeConfiguration(e => {
if (e.affectsConfiguration('editor.accessibilitySupport')) {
this.updateAccessibilitySupport();
}
});
this.accessibilityService.onDidChangeScreenReaderOptimized(() => this.updateAccessibilitySupport());
this.updateAccessibilitySupport();
}
focus(select: boolean = true, focusReplace: boolean = false, suppressGlobalSearchBuffer = false): void {
this.ignoreGlobalFindBufferOnNextFocus = suppressGlobalSearchBuffer;
if (focusReplace && this.isReplaceShown()) {
this.replaceInput.focus();
if (select) {
this.replaceInput.select();
}
} else {
this.searchInput.focus();
if (select) {
this.searchInput.select();
}
}
}
setWidth(width: number) {
this.searchInput.inputBox.layout();
this.replaceInput.width = width - 28;
this.replaceInput.inputBox.layout();
}
clear() {
this.searchInput.clear();
this.replaceInput.setValue('');
this.setReplaceAllActionState(false);
}
isReplaceShown(): boolean {
return !this.replaceContainer.classList.contains('disabled');
}
isReplaceActive(): boolean {
return !!this.replaceActive.get();
}
getReplaceValue(): string {
return this.replaceInput.getValue();
}
toggleReplace(show?: boolean): void {
if (show === undefined || show !== this.isReplaceShown()) {
this.onToggleReplaceButton();
}
}
getSearchHistory(): string[] {
return this.searchInput.inputBox.getHistory();
}
getReplaceHistory(): string[] {
return this.replaceInput.inputBox.getHistory();
}
clearHistory(): void {
this.searchInput.inputBox.clearHistory();
this.replaceInput.inputBox.clearHistory();
}
showNextSearchTerm() {
this.searchInput.inputBox.showNextValue();
}
showPreviousSearchTerm() {
this.searchInput.inputBox.showPreviousValue();
}
showNextReplaceTerm() {
this.replaceInput.inputBox.showNextValue();
}
showPreviousReplaceTerm() {
this.replaceInput.inputBox.showPreviousValue();
}
searchInputHasFocus(): boolean {
return !!this.searchInputBoxFocused.get();
}
replaceInputHasFocus(): boolean {
return this.replaceInput.inputBox.hasFocus();
}
focusReplaceAllAction(): void {
this.replaceActionBar.focus(true);
}
focusRegexAction(): void {
this.searchInput.focusOnRegex();
}
private render(container: HTMLElement, options: ISearchWidgetOptions): void {
this.domNode = dom.append(container, dom.$('.search-widget'));
this.domNode.style.position = 'relative';
if (!options._hideReplaceToggle) {
this.renderToggleReplaceButton(this.domNode);
}
this.renderSearchInput(this.domNode, options);
this.renderReplaceInput(this.domNode, options);
}
private updateAccessibilitySupport(): void {
this.searchInput.setFocusInputOnOptionClick(!this.accessibilityService.isScreenReaderOptimized());
}
private renderToggleReplaceButton(parent: HTMLElement): void {
const opts: IButtonOptions = {
buttonBackground: undefined,
buttonBorder: undefined,
buttonForeground: undefined,
buttonHoverBackground: undefined
};
this.toggleReplaceButton = this._register(new Button(parent, opts));
this.toggleReplaceButton.element.setAttribute('aria-expanded', 'false');
this.toggleReplaceButton.element.classList.add('toggle-replace-button');
this.toggleReplaceButton.icon = searchHideReplaceIcon;
// TODO@joao need to dispose this listener eventually
this.toggleReplaceButton.onDidClick(() => this.onToggleReplaceButton());
this.toggleReplaceButton.element.title = nls.localize('search.replace.toggle.button.title', "Toggle Replace");
}
private renderSearchInput(parent: HTMLElement, options: ISearchWidgetOptions): void {
const inputOptions: IFindInputOptions = {
label: nls.localize('label.Search', 'Search: Type Search Term and press Enter to search'),
validation: (value: string) => this.validateSearchInput(value),
placeholder: nls.localize('search.placeHolder', "Search"),
appendCaseSensitiveLabel: appendKeyBindingLabel('', this.keybindingService.lookupKeybinding(Constants.ToggleCaseSensitiveCommandId), this.keybindingService),
appendWholeWordsLabel: appendKeyBindingLabel('', this.keybindingService.lookupKeybinding(Constants.ToggleWholeWordCommandId), this.keybindingService),
appendRegexLabel: appendKeyBindingLabel('', this.keybindingService.lookupKeybinding(Constants.ToggleRegexCommandId), this.keybindingService),
history: options.searchHistory,
showHistoryHint: () => showHistoryKeybindingHint(this.keybindingService),
flexibleHeight: true,
flexibleMaxHeight: SearchWidget.INPUT_MAX_HEIGHT
};
const searchInputContainer = dom.append(parent, dom.$('.search-container.input-box'));
this.searchInput = this._register(new ContextScopedFindInput(searchInputContainer, this.contextViewService, inputOptions, this.contextKeyService, true));
this._register(attachFindReplaceInputBoxStyler(this.searchInput, this.themeService));
this.searchInput.onKeyDown((keyboardEvent: IKeyboardEvent) => this.onSearchInputKeyDown(keyboardEvent));
this.searchInput.setValue(options.value || '');
this.searchInput.setRegex(!!options.isRegex);
this.searchInput.setCaseSensitive(!!options.isCaseSensitive);
this.searchInput.setWholeWords(!!options.isWholeWords);
this._register(this.searchInput.onCaseSensitiveKeyDown((keyboardEvent: IKeyboardEvent) => this.onCaseSensitiveKeyDown(keyboardEvent)));
this._register(this.searchInput.onRegexKeyDown((keyboardEvent: IKeyboardEvent) => this.onRegexKeyDown(keyboardEvent)));
this._register(this.searchInput.inputBox.onDidChange(() => this.onSearchInputChanged()));
this._register(this.searchInput.inputBox.onDidHeightChange(() => this._onDidHeightChange.fire()));
this._register(this.onReplaceValueChanged(() => {
this._replaceHistoryDelayer.trigger(() => this.replaceInput.inputBox.addToHistory());
}));
this.searchInputFocusTracker = this._register(dom.trackFocus(this.searchInput.inputBox.inputElement));
this._register(this.searchInputFocusTracker.onDidFocus(async () => {
this.searchInputBoxFocused.set(true);
const useGlobalFindBuffer = this.searchConfiguration.globalFindClipboard;
if (!this.ignoreGlobalFindBufferOnNextFocus && useGlobalFindBuffer) {
const globalBufferText = await this.clipboardServce.readFindText();
if (globalBufferText && this.previousGlobalFindBufferValue !== globalBufferText) {
this.searchInput.inputBox.addToHistory();
this.searchInput.setValue(globalBufferText);
this.searchInput.select();
}
this.previousGlobalFindBufferValue = globalBufferText;
}
this.ignoreGlobalFindBufferOnNextFocus = false;
}));
this._register(this.searchInputFocusTracker.onDidBlur(() => this.searchInputBoxFocused.set(false)));
this.showContextToggle = new Toggle({
isChecked: false,
title: appendKeyBindingLabel(nls.localize('showContext', "Toggle Context Lines"), this.keybindingService.lookupKeybinding(ToggleSearchEditorContextLinesCommandId), this.keybindingService),
icon: searchShowContextIcon
});
this._register(this.showContextToggle.onChange(() => this.onContextLinesChanged()));
if (options.showContextToggle) {
this.contextLinesInput = new InputBox(searchInputContainer, this.contextViewService, { type: 'number' });
this.contextLinesInput.element.classList.add('context-lines-input');
this.contextLinesInput.value = '' + (this.configurationService.getValue<ISearchConfigurationProperties>('search').searchEditor.defaultNumberOfContextLines ?? 1);
this._register(this.contextLinesInput.onDidChange(() => this.onContextLinesChanged()));
this._register(attachInputBoxStyler(this.contextLinesInput, this.themeService));
dom.append(searchInputContainer, this.showContextToggle.domNode);
}
}
private onContextLinesChanged() {
this._onDidToggleContext.fire();
if (this.contextLinesInput.value.includes('-')) {
this.contextLinesInput.value = '0';
}
this._onDidToggleContext.fire();
}
public setContextLines(lines: number) {
if (!this.contextLinesInput) { return; }
if (lines === 0) {
this.showContextToggle.checked = false;
} else {
this.showContextToggle.checked = true;
this.contextLinesInput.value = '' + lines;
}
}
private renderReplaceInput(parent: HTMLElement, options: ISearchWidgetOptions): void {
this.replaceContainer = dom.append(parent, dom.$('.replace-container.disabled'));
const replaceBox = dom.append(this.replaceContainer, dom.$('.replace-input'));
this.replaceInput = this._register(new ContextScopedReplaceInput(replaceBox, this.contextViewService, {
label: nls.localize('label.Replace', 'Replace: Type replace term and press Enter to preview'),
placeholder: nls.localize('search.replace.placeHolder', "Replace"),
appendPreserveCaseLabel: appendKeyBindingLabel('', this.keybindingService.lookupKeybinding(Constants.TogglePreserveCaseId), this.keybindingService),
history: options.replaceHistory,
showHistoryHint: () => showHistoryKeybindingHint(this.keybindingService),
flexibleHeight: true,
flexibleMaxHeight: SearchWidget.INPUT_MAX_HEIGHT
}, this.contextKeyService, true));
this._register(this.replaceInput.onDidOptionChange(viaKeyboard => {
if (!viaKeyboard) {
this._onPreserveCaseChange.fire(this.replaceInput.getPreserveCase());
}
}));
this._register(attachFindReplaceInputBoxStyler(this.replaceInput, this.themeService));
this.replaceInput.onKeyDown((keyboardEvent) => this.onReplaceInputKeyDown(keyboardEvent));
this.replaceInput.setValue(options.replaceValue || '');
this._register(this.replaceInput.inputBox.onDidChange(() => this._onReplaceValueChanged.fire()));
this._register(this.replaceInput.inputBox.onDidHeightChange(() => this._onDidHeightChange.fire()));
this.replaceAllAction = new ReplaceAllAction(this);
this.replaceAllAction.label = SearchWidget.REPLACE_ALL_DISABLED_LABEL;
this.replaceActionBar = this._register(new ActionBar(this.replaceContainer));
this.replaceActionBar.push([this.replaceAllAction], { icon: true, label: false });
this.onkeydown(this.replaceActionBar.domNode, (keyboardEvent) => this.onReplaceActionbarKeyDown(keyboardEvent));
this.replaceInputFocusTracker = this._register(dom.trackFocus(this.replaceInput.inputBox.inputElement));
this._register(this.replaceInputFocusTracker.onDidFocus(() => this.replaceInputBoxFocused.set(true)));
this._register(this.replaceInputFocusTracker.onDidBlur(() => this.replaceInputBoxFocused.set(false)));
this._register(this.replaceInput.onPreserveCaseKeyDown((keyboardEvent: IKeyboardEvent) => this.onPreserveCaseKeyDown(keyboardEvent)));
}
triggerReplaceAll(): Promise<any> {
this._onReplaceAll.fire();
return Promise.resolve(null);
}
private onToggleReplaceButton(): void {
this.replaceContainer.classList.toggle('disabled');
if (this.isReplaceShown()) {
this.toggleReplaceButton.element.classList.remove(...ThemeIcon.asClassNameArray(searchHideReplaceIcon));
this.toggleReplaceButton.element.classList.add(...ThemeIcon.asClassNameArray(searchShowReplaceIcon));
} else {
this.toggleReplaceButton.element.classList.remove(...ThemeIcon.asClassNameArray(searchShowReplaceIcon));
this.toggleReplaceButton.element.classList.add(...ThemeIcon.asClassNameArray(searchHideReplaceIcon));
}
this.toggleReplaceButton.element.setAttribute('aria-expanded', this.isReplaceShown() ? 'true' : 'false');
this.updateReplaceActiveState();
this._onReplaceToggled.fire();
}
setValue(value: string) {
this.searchInput.setValue(value);
}
setReplaceAllActionState(enabled: boolean): void {
if (this.replaceAllAction.enabled !== enabled) {
this.replaceAllAction.enabled = enabled;
this.replaceAllAction.label = enabled ? SearchWidget.REPLACE_ALL_ENABLED_LABEL(this.keybindingService) : SearchWidget.REPLACE_ALL_DISABLED_LABEL;
this.updateReplaceActiveState();
}
}
private updateReplaceActiveState(): void {
const currentState = this.isReplaceActive();
const newState = this.isReplaceShown() && this.replaceAllAction.enabled;
if (currentState !== newState) {
this.replaceActive.set(newState);
this._onReplaceStateChange.fire(newState);
this.replaceInput.inputBox.layout();
}
}
private validateSearchInput(value: string): IMessage | null {
if (value.length === 0) {
return null;
}
if (!this.searchInput.getRegex()) {
return null;
}
try {
new RegExp(value, 'u');
} catch (e) {
return { content: e.message };
}
return null;
}
private onSearchInputChanged(): void {
this.searchInput.clearMessage();
this.setReplaceAllActionState(false);
if (this.searchConfiguration.searchOnType) {
if (this.searchInput.getRegex()) {
try {
const regex = new RegExp(this.searchInput.getValue(), 'ug');
const matchienessHeuristic = `
~!@#$%^&*()_+
\`1234567890-=
qwertyuiop[]\\
QWERTYUIOP{}|
asdfghjkl;'
ASDFGHJKL:"
zxcvbnm,./
ZXCVBNM<>? `.match(regex)?.length ?? 0;
const delayMultiplier =
matchienessHeuristic < 50 ? 1 :
matchienessHeuristic < 100 ? 5 : // expressions like `.` or `\w`
10; // only things matching empty string
this.submitSearch(true, this.searchConfiguration.searchOnTypeDebouncePeriod * delayMultiplier);
} catch {
// pass
}
} else {
this.submitSearch(true, this.searchConfiguration.searchOnTypeDebouncePeriod);
}
}
}
private onSearchInputKeyDown(keyboardEvent: IKeyboardEvent) {
if (keyboardEvent.equals(ctrlKeyMod | KeyCode.Enter)) {
this.searchInput.inputBox.insertAtCursor('\n');
keyboardEvent.preventDefault();
}
if (keyboardEvent.equals(KeyCode.Enter)) {
this.searchInput.onSearchSubmit();
this.submitSearch();
keyboardEvent.preventDefault();
}
else if (keyboardEvent.equals(KeyCode.Escape)) {
this._onSearchCancel.fire({ focus: true });
keyboardEvent.preventDefault();
}
else if (keyboardEvent.equals(KeyCode.Tab)) {
if (this.isReplaceShown()) {
this.replaceInput.focus();
} else {
this.searchInput.focusOnCaseSensitive();
}
keyboardEvent.preventDefault();
}
else if (keyboardEvent.equals(KeyCode.UpArrow)) {
stopPropagationForMultiLineUpwards(keyboardEvent, this.searchInput.getValue(), this.searchInput.domNode.querySelector('textarea'));
}
else if (keyboardEvent.equals(KeyCode.DownArrow)) {
stopPropagationForMultiLineDownwards(keyboardEvent, this.searchInput.getValue(), this.searchInput.domNode.querySelector('textarea'));
}
}
private onCaseSensitiveKeyDown(keyboardEvent: IKeyboardEvent) {
if (keyboardEvent.equals(KeyMod.Shift | KeyCode.Tab)) {
if (this.isReplaceShown()) {
this.replaceInput.focus();
keyboardEvent.preventDefault();
}
}
}
private onRegexKeyDown(keyboardEvent: IKeyboardEvent) {
if (keyboardEvent.equals(KeyCode.Tab)) {
if (this.isReplaceShown()) {
this.replaceInput.focusOnPreserve();
keyboardEvent.preventDefault();
}
}
}
private onPreserveCaseKeyDown(keyboardEvent: IKeyboardEvent) {
if (keyboardEvent.equals(KeyCode.Tab)) {
if (this.isReplaceActive()) {
this.focusReplaceAllAction();
} else {
this._onBlur.fire();
}
keyboardEvent.preventDefault();
}
else if (keyboardEvent.equals(KeyMod.Shift | KeyCode.Tab)) {
this.focusRegexAction();
keyboardEvent.preventDefault();
}
}
private onReplaceInputKeyDown(keyboardEvent: IKeyboardEvent) {
if (keyboardEvent.equals(ctrlKeyMod | KeyCode.Enter)) {
this.replaceInput.inputBox.insertAtCursor('\n');
keyboardEvent.preventDefault();
}
if (keyboardEvent.equals(KeyCode.Enter)) {
this.submitSearch();
keyboardEvent.preventDefault();
}
else if (keyboardEvent.equals(KeyCode.Tab)) {
this.searchInput.focusOnCaseSensitive();
keyboardEvent.preventDefault();
}
else if (keyboardEvent.equals(KeyMod.Shift | KeyCode.Tab)) {
this.searchInput.focus();
keyboardEvent.preventDefault();
}
else if (keyboardEvent.equals(KeyCode.UpArrow)) {
stopPropagationForMultiLineUpwards(keyboardEvent, this.replaceInput.getValue(), this.replaceInput.domNode.querySelector('textarea'));
}
else if (keyboardEvent.equals(KeyCode.DownArrow)) {
stopPropagationForMultiLineDownwards(keyboardEvent, this.replaceInput.getValue(), this.replaceInput.domNode.querySelector('textarea'));
}
}
private onReplaceActionbarKeyDown(keyboardEvent: IKeyboardEvent) {
if (keyboardEvent.equals(KeyMod.Shift | KeyCode.Tab)) {
this.focusRegexAction();
keyboardEvent.preventDefault();
}
}
private async submitSearch(triggeredOnType = false, delay: number = 0): Promise<void> {
this.searchInput.validate();
if (!this.searchInput.inputBox.isInputValid()) {
return;
}
const value = this.searchInput.getValue();
const useGlobalFindBuffer = this.searchConfiguration.globalFindClipboard;
if (value && useGlobalFindBuffer) {
await this.clipboardServce.writeFindText(value);
}
this._onSearchSubmit.fire({ triggeredOnType, delay });
}
getContextLines() {
return this.showContextToggle.checked ? +this.contextLinesInput.value : 0;
}
modifyContextLines(increase: boolean) {
const current = +this.contextLinesInput.value;
const modified = current + (increase ? 1 : -1);
this.showContextToggle.checked = modified !== 0;
this.contextLinesInput.value = '' + modified;
}
toggleContextLines() {
this.showContextToggle.checked = !this.showContextToggle.checked;
this.onContextLinesChanged();
}
override dispose(): void {
this.setReplaceAllActionState(false);
super.dispose();
}
private get searchConfiguration(): ISearchConfigurationProperties {
return this.configurationService.getValue<ISearchConfigurationProperties>('search');
}
}
export function registerContributions() {
KeybindingsRegistry.registerCommandAndKeybindingRule({
id: ReplaceAllAction.ID,
weight: KeybindingWeight.WorkbenchContrib,
when: ContextKeyExpr.and(Constants.SearchViewVisibleKey, Constants.ReplaceActiveKey, CONTEXT_FIND_WIDGET_NOT_VISIBLE),
primary: KeyMod.Alt | KeyMod.CtrlCmd | KeyCode.Enter,
handler: accessor => {
const viewsService = accessor.get(IViewsService);
if (isSearchViewFocused(viewsService)) {
const searchView = getSearchView(viewsService);
if (searchView) {
new ReplaceAllAction(searchView.searchAndReplaceWidget).run();
}
}
}
});
}
|