Skip to content

Phase I of #156633 Expose API for extensions to extend find/replace #156796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
24 changes: 15 additions & 9 deletions src/vs/editor/contrib/find/browser/findController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export function getSelectionSearchString(editor: ICodeEditor, seedSearchStringFr
}

export const enum FindStartFocusAction {
NoReveal,
NoFocusChange,
FocusFindInput,
FocusReplaceInput
Expand All @@ -69,6 +70,7 @@ export interface IFindStartOptions {
shouldAnimate: boolean;
updateSearchScope: boolean;
loop: boolean;
moveCursor?: boolean;
}

export interface IFindStartArguments {
Expand All @@ -79,6 +81,8 @@ export interface IFindStartArguments {
isCaseSensitive?: boolean;
preserveCase?: boolean;
findInSelection?: boolean;
shouldReveal?: boolean;
moveCursor?: boolean;
}

export class CommonFindController extends Disposable implements IEditorContribution {
Expand Down Expand Up @@ -291,10 +295,7 @@ export class CommonFindController extends Disposable implements IEditorContribut
return;
}

const stateChanges: INewFindReplaceState = {
...newState,
isRevealed: true
};
const stateChanges: INewFindReplaceState = opts.shouldFocus !== FindStartFocusAction.NoReveal ? { ...newState, isRevealed: true, } : { ...newState };

if (opts.seedSearchStringFromSelection === 'single') {
const selectionSearchString = getSelectionSearchString(this._editor, opts.seedSearchStringFromSelection, opts.seedSearchStringFromNonEmptySelection);
Expand Down Expand Up @@ -344,7 +345,7 @@ export class CommonFindController extends Disposable implements IEditorContribut
this._state.change(stateChanges, false);

if (!this._model) {
this._model = new FindModelBoundToEditorModel(this._editor, this._state);
this._model = new FindModelBoundToEditorModel(this._editor, this._state, opts.moveCursor);
}
}

Expand Down Expand Up @@ -560,6 +561,7 @@ const findArgDescription = {
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')
},
findInSelection: { type: 'boolean' },
shouldReveal: { type: 'boolean' },
}
}
}]
Expand Down Expand Up @@ -599,12 +601,16 @@ export class StartFindWithArgsAction extends EditorAction {
// preserveCaseOverride: args.preserveCaseOverride,
} : {};

const noSearchStringPassed = !args?.searchString;
const editorConfigSeedFromSelection = editor.getOption(EditorOption.find).seedSearchStringFromSelection;
const noCurrentSearchString = controller.getState().searchString.length === 0;
await controller.start({
forceRevealReplace: false,
seedSearchStringFromSelection: (controller.getState().searchString.length === 0) && editor.getOption(EditorOption.find).seedSearchStringFromSelection !== 'never' ? 'single' : 'none',
seedSearchStringFromNonEmptySelection: editor.getOption(EditorOption.find).seedSearchStringFromSelection === 'selection',
seedSearchStringFromGlobalClipboard: true,
shouldFocus: FindStartFocusAction.FocusFindInput,
seedSearchStringFromSelection: noSearchStringPassed && noCurrentSearchString && editorConfigSeedFromSelection !== 'never' ? 'single' : 'none',
seedSearchStringFromNonEmptySelection: noSearchStringPassed && editorConfigSeedFromSelection === 'selection',
seedSearchStringFromGlobalClipboard: noSearchStringPassed,
shouldFocus: args?.shouldReveal === false ? FindStartFocusAction.NoReveal : FindStartFocusAction.FocusFindInput,
moveCursor: args?.moveCursor,
shouldAnimate: true,
updateSearchScope: args?.findInSelection || false,
loop: editor.getOption(EditorOption.find).loop
Expand Down
4 changes: 2 additions & 2 deletions src/vs/editor/contrib/find/browser/findModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class FindModelBoundToEditorModel {
private readonly _updateDecorationsScheduler: RunOnceScheduler;
private _isDisposed: boolean;

constructor(editor: IActiveCodeEditor, state: FindReplaceState) {
constructor(editor: IActiveCodeEditor, state: FindReplaceState, moveCursor?: boolean) {
this._editor = editor;
this._state = state;
this._isDisposed = false;
Expand Down Expand Up @@ -124,7 +124,7 @@ export class FindModelBoundToEditorModel {

this._toDispose.add(this._state.onFindReplaceStateChange((e) => this._onStateChanged(e)));

this.research(false, this._state.searchScope);
this.research(!!moveCursor, this._state.searchScope);
}

public dispose(): void {
Expand Down
44 changes: 40 additions & 4 deletions src/vs/editor/contrib/find/test/browser/findController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import { EditOperation } from 'vs/editor/common/core/editOperation';
import { Position } from 'vs/editor/common/core/position';
import { Range } from 'vs/editor/common/core/range';
import { Selection } from 'vs/editor/common/core/selection';
import { CommonFindController, FindStartFocusAction, IFindStartOptions, NextMatchFindAction, NextSelectionMatchFindAction, StartFindAction, StartFindReplaceAction, StartFindWithSelectionAction } from 'vs/editor/contrib/find/browser/findController';
import { CommonFindController, FindStartFocusAction, IFindStartOptions, NextMatchFindAction, NextSelectionMatchFindAction, StartFindAction, StartFindReplaceAction, StartFindWithArgsAction, StartFindWithSelectionAction } from 'vs/editor/contrib/find/browser/findController';
import { CONTEXT_FIND_INPUT_FOCUSED } from 'vs/editor/contrib/find/browser/findModel';
import { INewFindReplaceState } from 'vs/editor/contrib/find/browser/findState';
import { withAsyncTestCodeEditor } from 'vs/editor/test/browser/testCodeEditor';
import { IClipboardService } from 'vs/platform/clipboard/common/clipboardService';
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
Expand All @@ -41,10 +42,10 @@ class TestFindController extends CommonFindController {
this.hasFocus = false;
}

protected override async _start(opts: IFindStartOptions): Promise<void> {
await super._start(opts);
protected override async _start(opts: IFindStartOptions, newState?: INewFindReplaceState): Promise<void> {
await super._start(opts, newState);

if (opts.shouldFocus !== FindStartFocusAction.NoFocusChange) {
if (opts.shouldFocus !== FindStartFocusAction.NoFocusChange && opts.shouldFocus !== FindStartFocusAction.NoReveal) {
this.hasFocus = true;
}

Expand Down Expand Up @@ -491,6 +492,41 @@ suite('FindController', async () => {
findController.dispose();
});
});

test('issue #156633, shouldReveal', async () => {
await withAsyncTestCodeEditor([
'ABC',
'ABC',
'XYZ',
], { serviceCollection: serviceCollection }, async (editor) => {
const findController = editor.registerAndInstantiateContribution(TestFindController.ID, TestFindController);

let findState = findController.getState();
assert.deepStrictEqual(findState.matchesCount, 0);

const startFindWithArgsAction = new StartFindWithArgsAction();
startFindWithArgsAction.run(null, editor, {
searchString: "XYZ",
shouldReveal: false,
});
findState = findController.getState();
assert.deepStrictEqual(findState.isRevealed, false);
assert.deepStrictEqual(findState.matchesCount, 1);

new NextMatchFindAction().run(null, editor);
findState = findController.getState();
assert.deepStrictEqual(findState.isRevealed, false);

startFindWithArgsAction.run(null, editor, {
searchString: "XYZ",
shouldReveal: true,
});
findState = findController.getState();
assert.deepStrictEqual(findState.isRevealed, true);

findController.dispose();
});
});
});

suite('FindController query options persistence', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,7 @@ KeybindingsRegistry.registerCommandAndKeybindingRule({
matchWholeWord: { 'type': 'boolean' },
useExcludeSettingsAndIgnoreFiles: { 'type': 'boolean' },
onlyOpenEditors: { 'type': 'boolean' },
shouldReveal: { 'type': 'boolean' },
}
}
},
Expand Down
5 changes: 4 additions & 1 deletion src/vs/workbench/contrib/search/browser/searchActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export interface IFindInFilesArgs {
matchWholeWord?: boolean;
useExcludeSettingsAndIgnoreFiles?: boolean;
onlyOpenEditors?: boolean;
shouldReveal?: boolean;
}

export const FindInFilesCommand: ICommandHandler = (accessor, args: IFindInFilesArgs = {}) => {
Expand All @@ -159,7 +160,9 @@ export const FindInFilesCommand: ICommandHandler = (accessor, args: IFindInFiles
} else {
updatedText = openedView.updateTextFromFindWidgetOrSelection({ allowUnselectedWord: typeof args.replace !== 'string' });
}
openedView.searchAndReplaceWidget.focus(undefined, updatedText, updatedText);
if (args.shouldReveal !== false) {
openedView.searchAndReplaceWidget.focus(undefined, updatedText, updatedText);
}
}
});
} else {
Expand Down