feat(input): expose editor search api for custom uis - #2533
Open
calmondev wants to merge 2 commits into
Open
Conversation
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.
Description
I'm building a project that needs a more customized code editor experience. One of the first places where I needed that customization was search. I wanted to keep the editor's matching, highlighting, scrolling, and replace behavior, but render my own search UI.
While looking into the current API, I found that search could only really be enabled or disabled through
.searchable(true). The built-in panel works well, but there was no practical public API for building a custom search UI on top of the same editor behavior. This PR adds that missing API surface without exposing the built-inSearchPanel.Added APIs include:
set_search_query(...)clear_search(...)search_next(...)search_previous(...)search_match_count(...)current_search_match_index(...)replace_current_search_match(...)replace_all_search_matches(...)range_to_utf16(...).replace_text_in_range_silent(...)expects UTF-16 ranges, and this keeps replace-all consistent with the individual replace path and the other editor callers. For a full-document range, the previous0..state.text.len()value would generally still clamp to the document end, so this is a consistency/safety cleanup rather than a confirmed bug fix.Other changes:
InputState, so the built-in panel and custom UIs use the same search state..searchable(false)is set, the input does not register its built-inSearchaction handler, so parent UIs can handleCtrl+F/Cmd+Fthemselves.SearchPanelandSearchMatcherremain private to the input module.SearchPanelinto private functions insearch.rs.This PR also adds a compact custom search example in
crates/story/examples/custom_search.rsand updates the English editor docs. The Chinese docs were intentionally left unchanged so the translation can be handled by someone who can validate the wording (I don't know Chinese 😞 ).Screenshot
The custom search example:
Break Changes
None.
How to Test
Ran the focused search tests:
cargo test -p gpui-component input::searchThis includes
test_builtin_search_panel_updates_editor_search, which makes sure the built-in search panel still updates search state after moving the matcher state ontoInputState.Checked the custom search example compiles:
Checked the main component crate compiles:
Checked formatting/whitespace in the diff:
Checklist
cargo runfor story tests related to the changes.