Skip to content

ReplaceAll + InSelection needs better post-replace selected-text behavior #9270

@sasumner

Description

@sasumner

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

  1. Start with text foobar. Select this text.
  2. Do a Replace All operation with the In Selection checkbox ticked, with a Find what of foo and a Replace with of FOO.
  3. Notice post-replace that the selected text is FOObar (good):
    image
  4. Without user-change of the current text selection, do a Replace All operation with the In Selection checkbox ticked, with a Find what of bar and a Replace with of BAR.
  5. Notice post-replace that the selected text is FOO (not so good, probably FOOBAR should be the selected text):
    image
  6. 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.
  7. Notice post-replace that the resulting text is FOO!!BAR and that nothing is selected. This is "not so good" when one compares with the "Expected result" final text/selection:
    image

Expected Behavior

  1. Start with text foobar. Select this text.
  2. Do a Replace All operation with the In Selection checkbox ticked, with a Find what of foo and a Replace with of FOO.
  3. Notice post-replace that the selected text is FOObar (good):
    image
  4. Without user-change of the current text selection, do a Replace All operation with the In Selection checkbox ticked, with a Find what of bar and a Replace with of BAR.
  5. Notice post-replace that the selected text is FOOBAR (desired!):
    image
  6. 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.
  7. Notice post-replace that the resulting text is FOOBAR!! and that that entire text is selected (desired!):
    image

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

Metadata

Metadata

Assignees

Labels

enhancementProposed enhancements of existing features

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions