Skip to content

Commit 8e96e0b

Browse files
#fix: 108459 (microsoft#108666)
* #fix: 108459 * fix: prevent seedSearchStringFromSelection overrides searchString arg * add: IFindStartArguments for find command arguments update: add 'findInSelection' argument update: 'Search Widget' --> 'Find Widget' * fix: prevent overriding revealReplace Co-authored-by: rebornix <[email protected]>
1 parent 206975e commit 8e96e0b

File tree

2 files changed

+104
-7
lines changed

2 files changed

+104
-7
lines changed

src/vs/editor/contrib/find/findController.ts

Lines changed: 103 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ export interface IFindStartOptions {
7171
loop: boolean;
7272
}
7373

74+
export interface IFindStartArguments {
75+
searchString?: string;
76+
replaceString?: string;
77+
isRegex?: boolean;
78+
matchWholeWord?: boolean;
79+
isCaseSensitive?: boolean;
80+
preserveCase?: boolean;
81+
findInSelection?: boolean;
82+
}
83+
7484
export class CommonFindController extends Disposable implements IEditorContribution {
7585

7686
public static readonly ID = 'editor.contrib.findController';
@@ -273,7 +283,7 @@ export class CommonFindController extends Disposable implements IEditorContribut
273283
// overwritten in subclass
274284
}
275285

276-
protected async _start(opts: IFindStartOptions): Promise<void> {
286+
protected async _start(opts: IFindStartOptions, newState?: INewFindReplaceState): Promise<void> {
277287
this.disposeModel();
278288

279289
if (!this._editor.hasModel()) {
@@ -282,6 +292,7 @@ export class CommonFindController extends Disposable implements IEditorContribut
282292
}
283293

284294
let stateChanges: INewFindReplaceState = {
295+
...newState,
285296
isRevealed: true
286297
};
287298

@@ -315,7 +326,7 @@ export class CommonFindController extends Disposable implements IEditorContribut
315326
}
316327

317328
// Overwrite isReplaceRevealed
318-
if (opts.forceRevealReplace) {
329+
if (opts.forceRevealReplace || stateChanges.isReplaceRevealed) {
319330
stateChanges.isReplaceRevealed = true;
320331
} else if (!this._findWidgetVisible.get()) {
321332
stateChanges.isReplaceRevealed = false;
@@ -337,8 +348,8 @@ export class CommonFindController extends Disposable implements IEditorContribut
337348
}
338349
}
339350

340-
public start(opts: IFindStartOptions): Promise<void> {
341-
return this._start(opts);
351+
public start(opts: IFindStartOptions, newState?: INewFindReplaceState): Promise<void> {
352+
return this._start(opts, newState);
342353
}
343354

344355
public moveToNextMatch(): boolean {
@@ -423,7 +434,7 @@ export class FindController extends CommonFindController implements IFindControl
423434
this._findOptionsWidget = null;
424435
}
425436

426-
protected override async _start(opts: IFindStartOptions): Promise<void> {
437+
protected override async _start(opts: IFindStartOptions, newState?: INewFindReplaceState): Promise<void> {
427438
if (!this._widget) {
428439
this._createFindWidget();
429440
}
@@ -447,9 +458,9 @@ export class FindController extends CommonFindController implements IFindControl
447458
break;
448459
}
449460

450-
opts.updateSearchScope = updateSearchScope;
461+
opts.updateSearchScope = opts.updateSearchScope || updateSearchScope;
451462

452-
await super._start(opts);
463+
await super._start(opts, newState);
453464

454465
if (this._widget) {
455466
if (opts.shouldFocus === FindStartFocusAction.FocusReplaceInput) {
@@ -520,6 +531,90 @@ StartFindAction.addImplementation(0, (accessor: ServicesAccessor, editor: ICodeE
520531
});
521532
});
522533

534+
const findArgDescription = {
535+
description: 'Open a new In-Editor Find Widget.',
536+
args: [{
537+
name: 'Open a new In-Editor Find Widget args',
538+
schema: {
539+
properties: {
540+
searchString: { type: 'string' },
541+
replaceString: { type: 'string' },
542+
regex: { type: 'boolean' },
543+
regexOverride: {
544+
type: 'number',
545+
description: nls.localize('actions.find.isRegexOverride', 'Overrides "Use Regular Expression" flag.\nThe flag will not be saved for the future.\n0: Do Nothing\n1: True\n2: False')
546+
},
547+
wholeWord: { type: 'boolean' },
548+
wholeWordOverride: {
549+
type: 'number',
550+
description: nls.localize('actions.find.wholeWordOverride', 'Overrides "Match Whole Word" flag.\nThe flag will not be saved for the future.\n0: Do Nothing\n1: True\n2: False')
551+
},
552+
matchCase: { type: 'boolean' },
553+
matchCaseOverride: {
554+
type: 'number',
555+
description: nls.localize('actions.find.matchCaseOverride', 'Overrides "Math Case" flag.\nThe flag will not be saved for the future.\n0: Do Nothing\n1: True\n2: False')
556+
},
557+
preserveCase: { type: 'boolean' },
558+
preserveCaseOverride: {
559+
type: 'number',
560+
description: nls.localize('actions.find.preserveCaseOverride', 'Overrides "Preserve Case" flag.\nThe flag will not be saved for the future.\n0: Do Nothing\n1: True\n2: False')
561+
},
562+
findInSelection: { type: 'boolean' },
563+
}
564+
}
565+
}]
566+
} as const;
567+
568+
export class StartFindWithArgsAction extends EditorAction {
569+
570+
constructor() {
571+
super({
572+
id: FIND_IDS.StartFindWithArgs,
573+
label: nls.localize('startFindWithArgsAction', "Find With Arguments"),
574+
alias: 'Find With Arguments',
575+
precondition: undefined,
576+
kbOpts: {
577+
kbExpr: null,
578+
primary: 0,
579+
weight: KeybindingWeight.EditorContrib
580+
},
581+
description: findArgDescription
582+
});
583+
}
584+
585+
public async run(accessor: ServicesAccessor | null, editor: ICodeEditor, args?: IFindStartArguments): Promise<void> {
586+
let controller = CommonFindController.get(editor);
587+
if (controller) {
588+
const newState: INewFindReplaceState = args ? {
589+
searchString: args.searchString,
590+
replaceString: args.replaceString,
591+
isReplaceRevealed: args.replaceString !== undefined,
592+
isRegex: args.isRegex,
593+
// isRegexOverride: args.regexOverride,
594+
wholeWord: args.matchWholeWord,
595+
// wholeWordOverride: args.wholeWordOverride,
596+
matchCase: args.isCaseSensitive,
597+
// matchCaseOverride: args.matchCaseOverride,
598+
preserveCase: args.preserveCase,
599+
// preserveCaseOverride: args.preserveCaseOverride,
600+
} : {};
601+
602+
await controller.start({
603+
forceRevealReplace: false,
604+
seedSearchStringFromSelection: (controller.getState().searchString.length === 0) && editor.getOption(EditorOption.find).seedSearchStringFromSelection !== 'never' ? 'single' : 'none',
605+
seedSearchStringFromNonEmptySelection: editor.getOption(EditorOption.find).seedSearchStringFromSelection === 'selection',
606+
seedSearchStringFromGlobalClipboard: true,
607+
shouldFocus: FindStartFocusAction.FocusFindInput,
608+
shouldAnimate: true,
609+
updateSearchScope: args?.findInSelection || false,
610+
loop: editor.getOption(EditorOption.find).loop
611+
}, newState);
612+
613+
controller.setGlobalBufferTerm(controller.getState().searchString);
614+
}
615+
}
616+
}
617+
523618
export class StartFindWithSelectionAction extends EditorAction {
524619

525620
constructor() {
@@ -772,6 +867,7 @@ StartFindReplaceAction.addImplementation(0, (accessor: ServicesAccessor, editor:
772867

773868
registerEditorContribution(CommonFindController.ID, FindController);
774869

870+
registerEditorAction(StartFindWithArgsAction);
775871
registerEditorAction(StartFindWithSelectionAction);
776872
registerEditorAction(NextMatchFindAction);
777873
registerEditorAction(PreviousMatchFindAction);

src/vs/editor/contrib/find/findModel.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export const TogglePreserveCaseKeybinding: IKeybindings = {
5555
export const FIND_IDS = {
5656
StartFindAction: 'actions.find',
5757
StartFindWithSelection: 'actions.findWithSelection',
58+
StartFindWithArgs: 'editor.actions.findWithArgs',
5859
NextMatchFindAction: 'editor.action.nextMatchFindAction',
5960
PreviousMatchFindAction: 'editor.action.previousMatchFindAction',
6061
NextSelectionMatchFindAction: 'editor.action.nextSelectionMatchFindAction',

0 commit comments

Comments
 (0)