-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Description
Description of the Issue
According to this recent discussion in the Community forum...
https://community.notepad-plus-plus.org/topic/20470
...the Replace All feature with In Selection qualifier could work better in regards to what happens to the active selection after the replacement is finished.
Currently, what happens to the active selection is whatever Scintilla thinks should happen, because Notepad++ code for this operation does not do anything to change the selection (I know, because I single-stepped it to see).
So...we can do better; let's propose some new rules. After a Replace All + In Selection operation:
-
post-replace selection start = pre-replace selection start
-
if replacement operation lengthens the document, post-replace selection end = pre-replace selection end + length of increase
-
if replacement operation shortens the document, post-replace selection end = pre-replace selection end - length of decrease
-
if replacement operation shortens the document such that all text in the pre-replace selection is eliminated, there will be no active selection post-replace
These proposed changes facilitate one situation in particular: When this operation is recorded into a macro that does several of these in a row. Currently, because the selection is not-particularly deterministic, this type of multiple-operation macro is not realistic.
The referenced Community topic above does a good job at describing an example, but let's repeat it here, with some small changes:
Actual Behavior
- Start with text
foobar. Select this text. - Do a Replace All operation with the In Selection checkbox ticked, with a Find what of
fooand a Replace with ofFOO. - Notice post-replace that the selected text is
FOObar(good):

- Without user-change of the current text selection, do a Replace All operation with the In Selection checkbox ticked, with a Find what of
barand a Replace with ofBAR. - Notice post-replace that the selected text is
FOO(not so good, probablyFOOBARshould be the selected text):

- Without user-change of the current text selection, do a Replace All operation with the In Selection checkbox ticked, with a Find what of
.+and a Replace with of${0}!!. Do this with Regular expression set as the Search mode. - Notice post-replace that the resulting text is
FOO!!BARand that nothing is selected. This is "not so good" when one compares with the "Expected result" final text/selection:

Expected Behavior
- Start with text
foobar. Select this text. - Do a Replace All operation with the In Selection checkbox ticked, with a Find what of
fooand a Replace with ofFOO. - Notice post-replace that the selected text is
FOObar(good):

- Without user-change of the current text selection, do a Replace All operation with the In Selection checkbox ticked, with a Find what of
barand a Replace with ofBAR. - Notice post-replace that the selected text is
FOOBAR(desired!):

- Without user-change of the current text selection, do a Replace All operation with the In Selection checkbox ticked, with a Find what of
.+and a Replace with of${0}!!. Do this with Regular expression set as the Search mode. - Notice post-replace that the resulting text is
FOOBAR!!and that that entire text is selected (desired!):

Debug Information
Notepad++ v7.9.1 (64-bit)
Build time : Nov 2 2020 - 01:07:46
Path : C:\.........\npp.7.9.1.portable.x64\notepad++.exe
Admin mode : OFF
Local Conf mode : ON
OS Name : Windows 10 Enterprise (64-bit)
OS Version : 1809
OS Build : 17763.1577
Current ANSI codepage : 1252
Plugins : mimeTools.dll NppConverter.dll NppExport.dll