Make find-and-replace search from the caret's position when opened (Fixes #156) - #232
Open
PeterKnego wants to merge 1 commit into
Open
Make find-and-replace search from the caret's position when opened (Fixes #156)#232PeterKnego wants to merge 1 commit into
PeterKnego wants to merge 1 commit into
Conversation
…ixes WebCoder49#156) The findMatchState of a code-input element is only constructed when its find-and-replace dialog is first created, and it is what stores the position matches are searched from. Reopening the dialog reuses the cached one, so every search after the first carried on from wherever the previous search left off rather than from the caret, unlike the IDE behaviour users expect. Set that position from the caret each time the dialog is opened, next to where the selection is already saved so it applies whether the dialog is being created or reopened. Two of the added tests are ordered so the match the previous one left focused differs from the correct answer, so they fail without this change; the other two lock in the surrounding wrapping and reopening behaviour.
Owner
|
Sorry, since this is a breaking change, it needs to wait for the next major version (v3.0.0). I expect v3.0.0 to be quite a lot of refactoring, so this specific PR might not be useful after that. Do you use code-input.js yourself and want the fix soon? If so, I'd recommend using your fork with this fix for the meantime, but it also helps me set priorities. |
Author
|
Don't worry, release it at your own pace. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #156.
The cause
FindMatchStatestoresfocusedMatchStartIndex, the positionfocusMatch()searches forward from, and its constructor initialises it from the caret. But it is constructed in only one place — inside the branch ofshowPromptthat runs when the dialog does not yet exist:https://github.com/WebCoder49/code-input/blob/v2.8.3/plugins/find-and-replace.js#L313
Every subsequent Ctrl+F reuses the cached dialog and its
findMatchState, sofocusedMatchStartIndexstill holds the position of the match the previous search left focused. The first search after a page load starts from the caret correctly; every one after it carries on from where the last one stopped.The change
One line, next to where
showPromptalready saves the selection for both the create and the reopen paths, so it applies either way and runs before the closingupdateFindMatchesrecomputes the matches:Existing behaviour this deliberately preserves:
cancelPrompt), so reopening without moving the caret stays on that match rather than jumping forward — the same as VS Code.Behaviour change
Flagging this as the breaking change the issue was labelled: reopening the dialog now moves the focused match where previously it did not. Anything driving
showPromptprogrammatically and relying on the search resuming from the last match will see a different match focused.Tests
Four assertions added to the
FindAndReplacegroup intests/tester.js. They are automatedassertEquals — noconfirm()judgement needed — and slot in after the existing find-and-replace block, leaving the sequential state the later GoToLine tests depend on untouched.I checked they actually fail without the fix rather than assuming it, by reverting the one-line change and re-running:
The first two are ordered so the match the previous one leaves focused differs from the correct answer, which is what makes them fail when the caret is ignored. The last two cannot discriminate in a chained sequence — once the first case fails, the stale position is the first match — so they are commented as locking in the surrounding behaviour rather than guarding this fix.
That table comes from a standalone page in Chromium using a minimal custom template, driving the real
Ctrl/Cmd+Fkey events rather than callingshowPromptdirectly. The change is inshowPrompt, above any highlighting, so it is not template-specific — but I have not yet run the fulltests/prism.html/tests/hljs.htmlpages end to end, since they need a person to answer theconfirm()prompts. Happy to do that and report back if you'd like it before merging.Not included
*.min.js— left to the Auto-minify workflow.esm/— generated atprepack; no marker comments affected.code-input.d.ts— no public interface change (showPrompt's docstring is updated in the plugin itself).