Skip to content
gpdoc
Playground

Find and replace

Highlight, navigate, and replace matching text in a document.

Register Find and replace to power a search bar or a bulk text edit. Searches span adjacent marked text within a block, so formatting inside a word does not split the match.

Run this from a Quickstart project.

Terminal window
cargo add gpdoc-plugin-find-and-replace

Use search_highlights with the built-in paint_match painter to display matches in the Quickstart editor.

use gpdoc::{EditorConfig, PluginViewExt, ViewPlugin};
use gpdoc_plugin_find_and_replace::{find_and_replace, paint_match, search_highlights};
use gpdoc_plugin_paragraph::paragraph;
let config = EditorConfig::builder()
.plugin(paragraph())
.plugin(find_and_replace().with_view(highlights))
.build()
.expect("configure editor");
fn highlights() -> ViewPlugin {
search_highlights(paint_match)
}

Run SetSearchTerm("draft".into()) on a document containing draft and draft. Both matches receive a highlight.

The plugin measures the visible fragments of each match and calls your painter for each fragment. A match that wraps across lines has a rectangle on each line.

search_highlights(...) Maps search results to the editor's text layout
└── MatchHighlight One visible fragment of a search result
├── current Whether this result is the active match
├── bounds Rectangle to paint in the window
└── range Document range of the entire match

Replace highlights in the example. Use hit.current to decide which fragments to pass to the built-in painter.

fn highlights() -> ViewPlugin {
search_highlights(|hit, window, cx| {
if hit.current {
paint_match(hit, window, cx);
}
})
}

Run GoToNextResult to move the highlight through the matches. The plugin draws the highlight above mark backgrounds and below the native selection.

Use the configuration above to replace both occurrences of draft in one transaction.

use gpdoc::{Editor, Node};
use gpdoc_plugin_find_and_replace::{results, ReplaceAll, SetReplaceTerm, SetSearchTerm};
let mut editor = Editor::from_document(&config, vec![Node::textblock("paragraph", "draft and draft")])?;
editor.run(SetSearchTerm("draft".into()))?;
assert_eq!(results(&editor).ranges.len(), 2);
editor.run(SetReplaceTerm("final".into()))?;
editor.run(ReplaceAll)?;
assert_eq!(
editor.document().blocks(),
[Node::textblock("paragraph", "final and final")],
);

The default plugin searches one text block at a time. In find_and_replace_with, returning true for a block lets its text continue the preceding block for both search and replacement.

use gpdoc_plugin_find_and_replace::find_and_replace_with;
let plugin = find_and_replace_with(|block| block.kind == "paragraph");

Set term to a string. An empty query clears the matches.

editor.run(gpdoc_plugin_find_and_replace::SetSearchTerm("draft".into()))?;

Set term to the literal replacement string. The default empty string deletes the matched text.

editor.run(gpdoc_plugin_find_and_replace::SetReplaceTerm("final".into()))?;

Matching ignores case by default. Set caseSensitive to true to distinguish Draft from draft.

editor.run(gpdoc_plugin_find_and_replace::SetCaseSensitive(true))?;

Set wholeWord to true to match complete words in a literal search. The default is false. In regex mode, include word boundaries in the pattern itself.

editor.run(gpdoc_plugin_find_and_replace::SetWholeWord(true))?;

Set useRegex to true to interpret the query as a Rust regular expression. The default is literal matching. Invalid patterns yield no matches.

editor.run(gpdoc_plugin_find_and_replace::SetUseRegex(true))?;

Select the first match on the first call. Subsequent calls advance and wrap to the beginning.

editor.run(gpdoc_plugin_find_and_replace::GoToNextResult)?;

Select the first match on the first call. Subsequent calls move backward and wrap to the last result.

editor.run(gpdoc_plugin_find_and_replace::GoToPreviousResult)?;

Replace the current match, or the first if you have not navigated yet. The next remaining match becomes selected.

editor.run(gpdoc_plugin_find_and_replace::Replace)?;

Replace every match in one transaction.

editor.run(gpdoc_plugin_find_and_replace::ReplaceAll)?;

Clear the search term, highlights, and current index while preserving the replacement text and matching options.

editor.run(gpdoc_plugin_find_and_replace::ClearSearch)?;

Call results(&editor) to read a SearchResults value from the find-and-replace plugin. Its ranges are document positions with exclusive ends. Its current field is the zero-based index of the active match. A new query starts at the first match. An empty result has current: None. A custom view can use this index to emphasize it.

let matches = results(&editor);
let active = matches.current.and_then(|index| matches.ranges.get(index));

Run an independent query against a document. SearchOptions defaults to an empty term: String and false for case_sensitive, whole_word, and use_regex.

use gpdoc_plugin_find_and_replace::{search, SearchOptions};
let options = SearchOptions { term: "draft".into(), ..Default::default() };
let matches = search(editor.document(), &options);

Pass a continuation predicate to the standalone search. It returns document ranges using the same rules as the configured plugin.

use gpdoc_plugin_find_and_replace::search_with;
let matches = search_with(editor.document(), &options, |block| block.kind == "paragraph");

find-and-replace/src/lib.rs