Skip to main content

replace_and_keep_selection

Function replace_and_keep_selection 

Source
pub fn replace_and_keep_selection(
    edit: &mut TextEdit,
    config: &Config,
    metrics: &Metrics<'_>,
    text: &str,
    max_len: Option<u32>,
) -> bool
Expand description

Replaces the selection with text and keeps the inserted text selected.

The whole difference from replace_selection, and the reason both exist: one leaves a caret, the other leaves a selection over what it just put in.


let mut edit = TextEdit::new("AB", &config, &metrics, true);
edit.set_selection(0, 1);
assert!(ops::replace_and_keep_selection(&mut edit, &config, &metrics, "XYZ", None));
assert_eq!(edit.text, "XYZB");
// The difference from `replace_selection`: the insertion stays selected.
assert_eq!(edit.selected_text(), "XYZ");

// A reversed input selection still comes back selected forwards.
edit.set_selection(3, 0);
ops::replace_and_keep_selection(&mut edit, &config, &metrics, "12", None);
assert_eq!(edit.text, "12B");
assert_eq!(edit.selection_indices(), (0, 2));