PythonScript icon indicating copy to clipboard operation
PythonScript copied to clipboard

Feature request: editor.rereplace() could return number of replacements made

Open alankilborn opened this issue 5 years ago • 4 comments

This is just a nice-to-have:

For editor.rereplace() there doesn't seem to be a good way to know the number of replacements that were actually made.
Suggestion: It's return value (currently always None) could be an integer with that count.

Workaround # 1 is to run editor.research() first with the same "find" expression, and, like the .research() example in the docs shows, add the matches to a list, and then do a len() on the list, but this is wasteful as the search is done twice this way (once in .research() and once in .rereplace()).

Workaround # 2 is to use a function as the replacement "text" in the .rereplace() call, and add in a custom counter, for example:

replacement_count = 0
def add_1(m):
    global replacement_count
    replacement_count += 1
    return 'Y' + str(int(m.group(1)) + 1)
editor.rereplace('X([0-9]+)', add_1);

Workaround # 2 is fine but again it is extra work that Pythonscript itself could do for me.

Request is also made for editor.replace() and truly even editor.research() and editor.search() could also return match/replacement counts.

Request is made against 1.5.x Pythonscript (and presumably the new 3.x beta as well).

alankilborn avatar Jan 06 '21 17:01 alankilborn

As starting point see: https://github.com/bruderstein/PythonScript/blob/master/PythonScript/src/ScintillaPython.cpp#L55 and implementation at https://github.com/bruderstein/PythonScript/blob/master/PythonScript/src/ScintillaWrapper.cpp#L784-L864 (maybe related https://github.com/bruderstein/PythonScript/blob/3e0245161d87806a6c0bfad72dfcba72d8e7dcc3/PythonScript/src/PythonScript.cpp#L408-L412)

Further investigations necessary to check if the request could be implemented.

chcg avatar Jan 06 '21 18:01 chcg

@chcg

I'm not sure what your response is telling me. Is it pointing me to the place where I could make some changes and create a PR? :-) Or is it notes you you guys, as the implementers? Or ...? Is there hope for my feature request?

alankilborn avatar Jan 06 '21 19:01 alankilborn

@alankilborn Updated the comment. It was just an internal note from a first code inspection. If you want to create a PR your welcome.

chcg avatar Jan 06 '21 20:01 chcg

Another person wanting something like this: https://community.notepad-plus-plus.org/topic/23494/exit-python-loop-when-replacement-count-is-0

alankilborn avatar Sep 17 '22 14:09 alankilborn