Skip to content

Commit

Permalink
Fix #27083.
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornix committed Oct 25, 2019
1 parent f558378 commit da0afcf
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 19 deletions.
18 changes: 13 additions & 5 deletions src/vs/editor/common/config/editorOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1109,9 +1109,9 @@ export interface IEditorFindOptions {
*/
seedSearchStringFromSelection?: boolean;
/**
* Controls if Find in Selection flag is turned on when multiple lines of text are selected in the editor.
* Controls if Find in Selection flag is turned on in the editor.
*/
autoFindInSelection?: boolean;
autoFindInSelection?: 'never' | 'always' | 'multiline';
/*
* Controls whether the Find Widget should add extra lines on top of the editor.
*/
Expand All @@ -1130,7 +1130,7 @@ class EditorFind extends BaseEditorOption<EditorOption.find, EditorFindOptions>
constructor() {
const defaults: EditorFindOptions = {
seedSearchStringFromSelection: true,
autoFindInSelection: false,
autoFindInSelection: 'never',
globalFindClipboard: false,
addExtraSpaceOnTop: true
};
Expand All @@ -1143,8 +1143,14 @@ class EditorFind extends BaseEditorOption<EditorOption.find, EditorFindOptions>
description: nls.localize('find.seedSearchStringFromSelection', "Controls whether the search string in the Find Widget is seeded from the editor selection.")
},
'editor.find.autoFindInSelection': {
type: 'boolean',
type: 'string',
enum: ['never', 'always', 'multiline'],
default: defaults.autoFindInSelection,
enumDescriptions: [
nls.localize('editor.find.autoFindInSelection.never', 'Never turn on Find in selection automatically (default)'),
nls.localize('editor.find.autoFindInSelection.always', 'Always turn on Find in selection automatically'),
nls.localize('editor.find.autoFindInSelection.multiline', 'Turn on Find in selection automatically when multiple lines of content are selected.')
],
description: nls.localize('find.autoFindInSelection', "Controls whether the find operation is carried out on selected text or the entire file in the editor.")
},
'editor.find.globalFindClipboard': {
Expand All @@ -1169,7 +1175,9 @@ class EditorFind extends BaseEditorOption<EditorOption.find, EditorFindOptions>
const input = _input as IEditorFindOptions;
return {
seedSearchStringFromSelection: EditorBooleanOption.boolean(input.seedSearchStringFromSelection, this.defaultValue.seedSearchStringFromSelection),
autoFindInSelection: EditorBooleanOption.boolean(input.autoFindInSelection, this.defaultValue.autoFindInSelection),
autoFindInSelection: typeof _input.autoFindInSelection === 'boolean'
? (_input.autoFindInSelection ? 'always' : 'never')
: EditorStringEnumOption.stringSet<'never' | 'always' | 'multiline'>(input.autoFindInSelection, this.defaultValue.autoFindInSelection, ['never', 'always', 'multiline']),
globalFindClipboard: EditorBooleanOption.boolean(input.globalFindClipboard, this.defaultValue.globalFindClipboard),
addExtraSpaceOnTop: EditorBooleanOption.boolean(input.addExtraSpaceOnTop, this.defaultValue.addExtraSpaceOnTop)
};
Expand Down
24 changes: 20 additions & 4 deletions src/vs/editor/contrib/find/findController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,26 @@ export class FindController extends CommonFindController implements IFindControl
this._createFindWidget();
}

if (!this._widget!.getPosition() && this._editor.getOption(EditorOption.find).autoFindInSelection) {
// not visible yet so we need to set search scope if `editor.find.autoFindInSelection` is `true`
opts.updateSearchScope = true;
}
const selection = this._editor.getSelection();
let updateSearchScope = false;

switch (this._editor.getOption(EditorOption.find).autoFindInSelection) {
case 'always':
updateSearchScope = true;
break;
case 'never':
updateSearchScope = false;
break;
case 'multiline':
const isSelectionMultipleLine = !!selection && selection.startLineNumber !== selection.endLineNumber;
updateSearchScope = isSelectionMultipleLine;
break;

default:
break;
}

opts.updateSearchScope = updateSearchScope;

super._start(opts);

Expand Down
21 changes: 16 additions & 5 deletions src/vs/editor/contrib/find/findWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,12 +460,23 @@ export class FindWidget extends Widget implements IOverlayWidget, IHorizontalSas
this._isVisible = true;

const selection = this._codeEditor.getSelection();
const isSelection = selection ? (selection.startLineNumber !== selection.endLineNumber || selection.startColumn !== selection.endColumn) : false;
if (isSelection && this._codeEditor.getOption(EditorOption.find).autoFindInSelection) {
this._toggleSelectionFind.checked = true;
} else {
this._toggleSelectionFind.checked = false;

switch (this._codeEditor.getOption(EditorOption.find).autoFindInSelection) {
case 'always':
this._toggleSelectionFind.checked = true;
break;
case 'never':
this._toggleSelectionFind.checked = false;
break;
case 'multiline':
const isSelectionMultipleLine = !!selection && selection.startLineNumber !== selection.endLineNumber;
this._toggleSelectionFind.checked = isSelectionMultipleLine;
break;

default:
break;
}

this._tryUpdateWidgetWidth();
this._updateButtons();

Expand Down
30 changes: 27 additions & 3 deletions src/vs/editor/contrib/find/test/findController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ suite('FindController query options persistence', () => {
'var x = (3 * 5)',
'var y = (3 * 5)',
'var z = (3 * 5)',
], { serviceCollection: serviceCollection, find: { autoFindInSelection: true, globalFindClipboard: false } }, (editor, cursor) => {
], { serviceCollection: serviceCollection, find: { autoFindInSelection: 'always', globalFindClipboard: false } }, (editor, cursor) => {
// clipboardState = '';
editor.setSelection(new Range(1, 1, 2, 1));
let findController = editor.registerAndInstantiateContribution<TestFindController>(TestFindController.ID, TestFindController);
Expand All @@ -542,7 +542,7 @@ suite('FindController query options persistence', () => {
'var x = (3 * 5)',
'var y = (3 * 5)',
'var z = (3 * 5)',
], { serviceCollection: serviceCollection, find: { autoFindInSelection: true, globalFindClipboard: false } }, (editor, cursor) => {
], { serviceCollection: serviceCollection, find: { autoFindInSelection: 'always', globalFindClipboard: false } }, (editor, cursor) => {
// clipboardState = '';
editor.setSelection(new Range(1, 2, 1, 2));
let findController = editor.registerAndInstantiateContribution<TestFindController>(TestFindController.ID, TestFindController);
Expand All @@ -565,7 +565,7 @@ suite('FindController query options persistence', () => {
'var x = (3 * 5)',
'var y = (3 * 5)',
'var z = (3 * 5)',
], { serviceCollection: serviceCollection, find: { autoFindInSelection: true, globalFindClipboard: false } }, (editor, cursor) => {
], { serviceCollection: serviceCollection, find: { autoFindInSelection: 'always', globalFindClipboard: false } }, (editor, cursor) => {
// clipboardState = '';
editor.setSelection(new Range(1, 2, 1, 3));
let findController = editor.registerAndInstantiateContribution<TestFindController>(TestFindController.ID, TestFindController);
Expand All @@ -582,4 +582,28 @@ suite('FindController query options persistence', () => {
assert.deepEqual(findController.getState().searchScope, new Selection(1, 2, 1, 3));
});
});


test('issue #27083: Find in selection when multiple lines are selected', () => {
withTestCodeEditor([
'var x = (3 * 5)',
'var y = (3 * 5)',
'var z = (3 * 5)',
], { serviceCollection: serviceCollection, find: { autoFindInSelection: 'multiline', globalFindClipboard: false } }, (editor, cursor) => {
// clipboardState = '';
editor.setSelection(new Range(1, 6, 2, 1));
let findController = editor.registerAndInstantiateContribution<TestFindController>(TestFindController.ID, TestFindController);

findController.start({
forceRevealReplace: false,
seedSearchStringFromSelection: false,
seedSearchStringFromGlobalClipboard: false,
shouldFocus: FindStartFocusAction.NoFocusChange,
shouldAnimate: false,
updateSearchScope: true
});

assert.deepEqual(findController.getState().searchScope, new Selection(1, 6, 2, 1));
});
});
});
4 changes: 2 additions & 2 deletions src/vs/monaco.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3054,9 +3054,9 @@ declare namespace monaco.editor {
*/
seedSearchStringFromSelection?: boolean;
/**
* Controls if Find in Selection flag is turned on when multiple lines of text are selected in the editor.
* Controls if Find in Selection flag is turned on in the editor.
*/
autoFindInSelection?: boolean;
autoFindInSelection?: 'never' | 'always' | 'multiline';
addExtraSpaceOnTop?: boolean;
}

Expand Down

0 comments on commit da0afcf

Please sign in to comment.