feat(find): open the preview find bar on the selected text - #586
Merged
Conversation
Reworked from #231. Select a word in the rendered document, press Cmd/Ctrl+F, and the bar opens with that word already in it. The editor pane has done this for as long as it has used Monaco -- `seedSearchStringFromSelection` defaults to seeding -- so the two panes disagreed about the same keystroke, and the preview was the one that made you type the word you were already pointing at. The seed is `selection.toString()`, not the text of a single node. A selection that crosses `**bold**`, a link or inline code spans several nodes, and those are the words a reader is most likely to have highlighted; reading one node would have left the feature off in the commonest case. Single-line selections only, which is Monaco's rule in the other pane. A paragraph in the search box matches nothing and buries the query the user was about to type. Everything else is left alone rather than cleared: a caret with nothing selected, a selection in a modal or the tab strip, whitespace. A repeated Cmd/Ctrl+F with no selection still just re-focuses the box (#559). `setQuery` is the assignment and nothing else -- the existing `query` effect schedules the search, so a seed goes through the same debounce and the same not-yet-open guard as typing does, and the highlight walk is untouched. The decision of whether to seed is the whole feature, so it is a pure function in `utils/findSeed.ts` that the test imports and runs -- the behaviour kind AGENTS.md asks for, rather than a harness that lifts it back out of the component. One source-shape line covers the wiring, since a pure function nothing calls is dead code. Checked by mutation: dropping the single-line rule or the containment guard each fails it. 977 tests pass, svelte-check clean. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
3 tasks
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.
Reworked from #231, which bundled this with a Copy-as-Markdown command. The copy half is not landing (reasoning in that thread); this is the half that closes a real gap. The commit keeps @junglesub as the author.
What changes
Select a word in the rendered preview, press Cmd/Ctrl+F, and the bar opens with that word already in it.
The editor pane has behaved this way for as long as it has used Monaco —
seedSearchStringFromSelectiondefaults to seeding — so the two panes disagreed about the same keystroke, and the preview was the one that made you retype the word you were already pointing at.Decisions
The seed is
selection.toString(), not the text of one node. A selection crossing**bold**, a link or inline code spans several text nodes, and those are the words a reader is most likely to have highlighted. Reading a single node would have left the feature off in the commonest case.Single-line selections only — Monaco's own rule in the other pane, so the app agrees with itself about one keystroke instead of growing a second rule. A paragraph in the search box matches nothing and buries the query the user was about to type.
Nothing to seed leaves the previous query alone rather than clearing it: a caret with nothing selected, a selection in a modal or the tab strip, whitespace. A repeated Cmd/Ctrl+F still just re-focuses and re-selects the box (#559).
setQueryis the assignment and nothing else. The existingqueryeffect schedules the search, so a seed goes through the same debounce and the same not-yet-open guard as typing.applyHighlightsand the scroll-restore path are untouched.Not included: starting from the selected occurrence rather than the first match. #231 spent ~110 lines on it — a
pendingActiveRange/getRangeMatchIndex/getClosestMarkIndexfallback chain, anuntrackchange to thesanitizedHtmleffect, and an extrareapply()in the render path — inside a tree walk that mutates the nodes it is walking. It can come back on its own merits.Testing
npm test— 977 pass.npm run check— clean.The decision of whether to seed is the whole feature, so it lives in
src/lib/utils/findSeed.tsas a pure function the test imports and runs, plus one source-shape line for the wiring (a pure function nothing calls is dead code). Checked by mutation: dropping the single-line rule fails it, and so does dropping the containment guard.Manually verified on macOS with a debug build: selecting across bold and inline code, selecting in the tab strip, repeated Cmd+F with no selection, and split view.
🤖 Generated with Claude Code