Skip to content

Commit

Permalink
fix #47400
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornix committed Oct 28, 2020
1 parent ee0aff2 commit 5b39aab
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
32 changes: 21 additions & 11 deletions src/vs/editor/contrib/find/findController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ import { IStorageKeysSyncRegistryService } from 'vs/platform/userDataSync/common

const SEARCH_STRING_MAX_LENGTH = 524288;

export function getSelectionSearchString(editor: ICodeEditor): string | null {
export function getSelectionSearchString(editor: ICodeEditor, seedSearchStringFromSelection: 'single' | 'multiple' = 'single'): string | null {
if (!editor.hasModel()) {
return null;
}

const selection = editor.getSelection();
// if selection spans multiple lines, default search string to empty
if (selection.startLineNumber === selection.endLineNumber) {

if (seedSearchStringFromSelection === 'single' && selection.startLineNumber === selection.endLineNumber) {
if (selection.isEmpty()) {
const wordAtPosition = editor.getConfiguredWordAtPosition(selection.getStartPosition());
if (wordAtPosition) {
Expand All @@ -48,6 +49,10 @@ export function getSelectionSearchString(editor: ICodeEditor): string | null {
return editor.getModel().getValueInRange(selection);
}
}
} else if (seedSearchStringFromSelection === 'multiple') {
if (editor.getModel().getValueLengthInRange(selection) < SEARCH_STRING_MAX_LENGTH) {
return editor.getModel().getValueInRange(selection);
}
}

return null;
Expand All @@ -61,7 +66,7 @@ export const enum FindStartFocusAction {

export interface IFindStartOptions {
forceRevealReplace: boolean;
seedSearchStringFromSelection: boolean;
seedSearchStringFromSelection: 'none' | 'single' | 'multiple';
seedSearchStringFromGlobalClipboard: boolean;
shouldFocus: FindStartFocusAction;
shouldAnimate: boolean;
Expand Down Expand Up @@ -122,7 +127,7 @@ export class CommonFindController extends Disposable implements IEditorContribut
if (shouldRestartFind) {
this._start({
forceRevealReplace: false,
seedSearchStringFromSelection: false && this._editor.getOption(EditorOption.find).seedSearchStringFromSelection,
seedSearchStringFromSelection: 'none',
seedSearchStringFromGlobalClipboard: false,
shouldFocus: FindStartFocusAction.NoFocusChange,
shouldAnimate: false,
Expand Down Expand Up @@ -278,15 +283,20 @@ export class CommonFindController extends Disposable implements IEditorContribut
isRevealed: true
};

if (opts.seedSearchStringFromSelection) {
let selectionSearchString = getSelectionSearchString(this._editor);
if (opts.seedSearchStringFromSelection === 'single') {
let selectionSearchString = getSelectionSearchString(this._editor, opts.seedSearchStringFromSelection);
if (selectionSearchString) {
if (this._state.isRegex) {
stateChanges.searchString = strings.escapeRegExpCharacters(selectionSearchString);
} else {
stateChanges.searchString = selectionSearchString;
}
}
} else if (opts.seedSearchStringFromSelection === 'multiple' && !opts.updateSearchScope) {
let selectionSearchString = getSelectionSearchString(this._editor, opts.seedSearchStringFromSelection);
if (selectionSearchString) {
stateChanges.searchString = selectionSearchString;
}
}

if (!stateChanges.searchString && opts.seedSearchStringFromGlobalClipboard) {
Expand Down Expand Up @@ -493,7 +503,7 @@ export class StartFindAction extends MultiEditorAction {
if (controller) {
await controller.start({
forceRevealReplace: false,
seedSearchStringFromSelection: editor.getOption(EditorOption.find).seedSearchStringFromSelection,
seedSearchStringFromSelection: editor.getOption(EditorOption.find).seedSearchStringFromSelection ? 'single' : 'none',
seedSearchStringFromGlobalClipboard: editor.getOption(EditorOption.find).globalFindClipboard,
shouldFocus: FindStartFocusAction.FocusFindInput,
shouldAnimate: true,
Expand Down Expand Up @@ -528,7 +538,7 @@ export class StartFindWithSelectionAction extends EditorAction {
if (controller) {
await controller.start({
forceRevealReplace: false,
seedSearchStringFromSelection: true,
seedSearchStringFromSelection: 'multiple',
seedSearchStringFromGlobalClipboard: false,
shouldFocus: FindStartFocusAction.NoFocusChange,
shouldAnimate: true,
Expand All @@ -546,7 +556,7 @@ export abstract class MatchFindAction extends EditorAction {
if (controller && !this._run(controller)) {
await controller.start({
forceRevealReplace: false,
seedSearchStringFromSelection: (controller.getState().searchString.length === 0) && editor.getOption(EditorOption.find).seedSearchStringFromSelection,
seedSearchStringFromSelection: (controller.getState().searchString.length === 0) && editor.getOption(EditorOption.find).seedSearchStringFromSelection ? 'single' : 'multiple',
seedSearchStringFromGlobalClipboard: true,
shouldFocus: FindStartFocusAction.NoFocusChange,
shouldAnimate: true,
Expand Down Expand Up @@ -659,7 +669,7 @@ export abstract class SelectionMatchFindAction extends EditorAction {
if (!this._run(controller)) {
await controller.start({
forceRevealReplace: false,
seedSearchStringFromSelection: editor.getOption(EditorOption.find).seedSearchStringFromSelection,
seedSearchStringFromSelection: editor.getOption(EditorOption.find).seedSearchStringFromSelection ? 'single' : 'none',
seedSearchStringFromGlobalClipboard: false,
shouldFocus: FindStartFocusAction.NoFocusChange,
shouldAnimate: true,
Expand Down Expand Up @@ -765,7 +775,7 @@ export class StartFindReplaceAction extends MultiEditorAction {
if (controller) {
await controller.start({
forceRevealReplace: true,
seedSearchStringFromSelection: seedSearchStringFromSelection,
seedSearchStringFromSelection: seedSearchStringFromSelection ? 'single' : 'none',
seedSearchStringFromGlobalClipboard: editor.getOption(EditorOption.find).seedSearchStringFromSelection,
shouldFocus: shouldFocus,
shouldAnimate: true,
Expand Down
14 changes: 7 additions & 7 deletions src/vs/editor/contrib/find/test/findController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ suite('FindController', async () => {
findController.setSearchString(testRegexString);
await findController.start({
forceRevealReplace: false,
seedSearchStringFromSelection: false,
seedSearchStringFromSelection: 'none',
seedSearchStringFromGlobalClipboard: false,
shouldFocus: FindStartFocusAction.FocusFindInput,
shouldAnimate: false,
Expand All @@ -298,7 +298,7 @@ suite('FindController', async () => {
let findController = editor.registerAndInstantiateContribution(TestFindController.ID, TestFindController);
await findController.start({
forceRevealReplace: false,
seedSearchStringFromSelection: false,
seedSearchStringFromSelection: 'none',
seedSearchStringFromGlobalClipboard: false,
shouldFocus: FindStartFocusAction.NoFocusChange,
shouldAnimate: false,
Expand Down Expand Up @@ -524,9 +524,9 @@ suite('FindController query options persistence', async () => {
], { serviceCollection: serviceCollection, find: { autoFindInSelection: 'always', globalFindClipboard: false } }, async (editor) => {
// clipboardState = '';
let findController = editor.registerAndInstantiateContribution(TestFindController.ID, TestFindController);
const findConfig = {
const findConfig: IFindStartOptions = {
forceRevealReplace: false,
seedSearchStringFromSelection: false,
seedSearchStringFromSelection: 'none',
seedSearchStringFromGlobalClipboard: false,
shouldFocus: FindStartFocusAction.NoFocusChange,
shouldAnimate: false,
Expand Down Expand Up @@ -558,7 +558,7 @@ suite('FindController query options persistence', async () => {

await findController.start({
forceRevealReplace: false,
seedSearchStringFromSelection: false,
seedSearchStringFromSelection: 'none',
seedSearchStringFromGlobalClipboard: false,
shouldFocus: FindStartFocusAction.NoFocusChange,
shouldAnimate: false,
Expand All @@ -582,7 +582,7 @@ suite('FindController query options persistence', async () => {

await findController.start({
forceRevealReplace: false,
seedSearchStringFromSelection: false,
seedSearchStringFromSelection: 'none',
seedSearchStringFromGlobalClipboard: false,
shouldFocus: FindStartFocusAction.NoFocusChange,
shouldAnimate: false,
Expand All @@ -607,7 +607,7 @@ suite('FindController query options persistence', async () => {

await findController.start({
forceRevealReplace: false,
seedSearchStringFromSelection: false,
seedSearchStringFromSelection: 'none',
seedSearchStringFromGlobalClipboard: false,
shouldFocus: FindStartFocusAction.NoFocusChange,
shouldAnimate: false,
Expand Down

0 comments on commit 5b39aab

Please sign in to comment.