diff --git a/src/vs/editor/contrib/find/common/findController.ts b/src/vs/editor/contrib/find/common/findController.ts index dca8998c28bf0584f7efc4ee4e82f2e4d08b86d9..8217c1f4fbe48ba5180f018bed172bb2d0e00f0d 100644 --- a/src/vs/editor/contrib/find/common/findController.ts +++ b/src/vs/editor/contrib/find/common/findController.ts @@ -13,6 +13,7 @@ import {Range} from 'vs/editor/common/core/range'; import {Selection} from 'vs/editor/common/core/selection'; import {EditorAction} from 'vs/editor/common/editorAction'; import {Behaviour} from 'vs/editor/common/editorActionEnablement'; +import * as strings from 'vs/base/common/strings'; import * as editorCommon from 'vs/editor/common/editorCommon'; import {CommonEditorRegistry, ContextKey, EditorActionDescriptor} from 'vs/editor/common/editorCommonExtensions'; import {FIND_IDS, FindModelBoundToEditorModel} from 'vs/editor/contrib/find/common/findModel'; @@ -161,7 +162,11 @@ export class CommonFindController extends Disposable implements editorCommon.IEd if (opts.seedSearchStringFromSelection) { let selectionSearchString = this.getSelectionSearchString(); if (selectionSearchString) { - stateChanges.searchString = selectionSearchString; + if (this._state.isRegex) { + stateChanges.searchString = strings.escapeRegExpCharacters(selectionSearchString); + } else { + stateChanges.searchString = selectionSearchString; + } } } diff --git a/src/vs/editor/contrib/find/test/common/findController.test.ts b/src/vs/editor/contrib/find/test/common/findController.test.ts index 084602eb991f7d86b87a7cb6005b3c0ac8bda62f..327b34a49c97b4fb69e345cd7feabb535922181d 100644 --- a/src/vs/editor/contrib/find/test/common/findController.test.ts +++ b/src/vs/editor/contrib/find/test/common/findController.test.ts @@ -7,6 +7,7 @@ import * as assert from 'assert'; import {EditOperation} from 'vs/editor/common/core/editOperation'; import {Position} from 'vs/editor/common/core/position'; +import {Selection} from 'vs/editor/common/core/selection'; import {Range} from 'vs/editor/common/core/range'; import {IRange} from 'vs/editor/common/editorCommon'; import {CommonFindController, FindStartFocusAction, IFindStartOptions, NextMatchFindAction, StartFindAction} from 'vs/editor/contrib/find/common/findController'; @@ -94,7 +95,6 @@ suite('FindController', () => { 'import nls = require(\'vs/nls\');' ], {}, (editor, cursor) => { - // The cursor is at the very top, of the file, at the first ABC let findController = editor.registerAndInstantiateContribution(TestFindController); let nextMatchFindAction = new NextMatchFindAction({id:'',label:''}, editor); @@ -113,4 +113,32 @@ suite('FindController', () => { nextMatchFindAction.dispose(); }); }); + + test('issue #6149: Auto-escape highlighted text for search and replace regex mode', () => { + withMockCodeEditor([ + 'var x = (3 * 5)', + 'var y = (3 * 5)', + 'var z = (3 * 5)', + ], {}, (editor, cursor) => { + + let findController = editor.registerAndInstantiateContribution(TestFindController); + let startFindAction = new StartFindAction({id:'',label:''}, editor); + let nextMatchFindAction = new NextMatchFindAction({id:'',label:''}, editor); + + editor.setSelection(new Selection(1, 9, 1, 13)); + + findController.toggleRegex(); + startFindAction.run(); + + nextMatchFindAction.run(); + assert.deepEqual(fromRange(editor.getSelection()), [2, 9, 2, 13]); + + nextMatchFindAction.run(); + assert.deepEqual(fromRange(editor.getSelection()), [1, 9, 1, 13]); + + findController.dispose(); + startFindAction.dispose(); + nextMatchFindAction.dispose(); + }); + }); });