-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update highlight when replace string matches search expression #15881
Update highlight when replace string matches search expression #15881
Conversation
Thanks for making a pull request to jupyterlab! |
The way that we keep track of matches needs to be rewritten as a revision to this pull request. Especially because both find and replace support regular expressions, the number of matches may go up or down by any number as a result of a single "replace" operation. The "replace current" function needs to:
For reference, see CodeMirror’s search code’s “replaceNext'” function: https://github.com/codemirror/search/blob/0d8af3e4cc6e9e9bc2350952698015ff639ae857/src/search.ts#L465-L490 For the sake of the search dialog's result count, we might need to stop the search and start another search after the replacement is done. @krassowski please stop me if this change is getting far beyond the scope you'd expect for a fix of this nature. 😄 |
@krassowski This WIP change forcibly highlights the next match if the replacement text also matches the query. This is not ideal; replacing Also, I ran into a problem where, in the first cell with matches, I'm looping through the matches after a single step with no matches at all. I think this is related to #15809, specifically a quirk with the logic of how |
I agree that search results count may change during the replace operation (or even during notebook loading); this does not mean that we should resort to restarting the search. I see these as two different things:
Some editors/browsers do not update search results on modification at all, until user presses the "next" button.
Based on a quick check of the most popular editors, I believe users would be surprised by such behaviour and instead expect the highlight to move to the next original match; here are the editors I tried:
I used |
I am thinking if we could just cleverly adjust For example, starting with:
if we replace active
but if we instead replace with
It looks like some logic from |
20b8805
to
a67f9a7
Compare
a67f9a7
to
21ed046
Compare
@krassowski Thanks for your patience! I'm ready to take another look at this change. I had looked into searching the replaced text for matches, but what if the replacement creates an additional match beyond the replacement text? For example, start with
Replace first instance, and we expect the status to be:
… but if we compare |
21ed046
to
ead370b
Compare
Updated the PR to correctly update the highlight to, after replacing a target string with a replacement that contains the target string, move forward to the next highlight after the replacement, if possible. Please try it out. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested it locally. The replacement behaviour looks good but there are two edge cases which are regressions:
- When you press replace on the last match you see
-/x
rather than get the highlight back at the first match (1/x
); you need to press replace again for it to cycle back to the front in the same cell - When you do press replace again instead of moving to the matches in the next cell, it moves you to the first match in the current cell
The lint check failures are relevant:
/home/runner/work/jupyterlab/jupyterlab/packages/codemirror/src/searchprovider.ts
Error: 364:9 error Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator @typescript-eslint/no-floating-promises
Error: 379:13 error Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator @typescript-eslint/no-floating-promises
And js-notebook
test failure looks relevant too:
● @jupyterlab/notebook › NotebookSearchProvider › #replaceCurrentMatch() › should replace with a longer text and highlight next
expect(received).toBe(expected) // Object.is equality
Expected: 0
Received: null
f0aa36e
to
ee5d022
Compare
From a little debugging, it looks like the new code in |
The most recent code uses a promise for the codemirror updates. |
Noting that this does not modify the public API and can be merged in RC and probably even backported to 4.1.x |
7d56768
to
0567837
Compare
The most recent code changes should fix the highlight order when replacing source text with target text that contains the source text. Screen.Recording.2024-04-23.at.2.22.02.PM.mov |
@krassowski If you have time, could you please look at this revised PR? Thanks! |
It still does not work in one tricky case; let's not block this PR on it - I opened #16242 to track it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @JasonWeill, let's merge and iterate!
…erlab#15881) * Revert "Alternate description for disabled filters" This reverts commit 059fb7e. * Revert "Revert "Alternate description for disabled filters"" This reverts commit 27ed1d0. * WIP: Advances currentMatch if new text matches query * WIP: Track whether new substituted text also matches query * Fix typo * Update comment * Minor refactoring * WIP: Advance highlight when replacement text matches * Updates highlights after replacing keyword * Fix promise handling errors * Handles codemirror update inside a promise * Adds test case for string replacement that contains query string * Advances highlight after replacing text with a superset of the text * Copy edits
References
Fixes #15572, #13968, and #15809.
Code changes
Updates search providers so that, if the replacement string also matches the search query, the highlight and selection move to the next search result.
User-facing changes
After replacing a string with a superset of the string, such as
foo
withfood
, the next search result highlights. The just-replaced string does not remain highlighted.Backwards-incompatible changes
None.