Abstract Transaction::change from change_by_selection.

pull/1/head
Blaž Hrastnik 4 years ago
parent 593f33dca6
commit d86f0feafc

@ -1,5 +1,9 @@
use crate::{Rope, Selection, SelectionRange, State, Tendril}; use crate::{Rope, Selection, SelectionRange, State, Tendril};
// TODO: make Change enum internal and export this as the public Change type
/// (from, to, replacement)
type Change2 = (usize, usize, Option<Tendril>);
// TODO: divided into three different operations, I sort of like having just // TODO: divided into three different operations, I sort of like having just
// Splice { extent, Option<text>, distance } better. // Splice { extent, Option<text>, distance } better.
// insert: Splice { extent: 0, text: Some("a"), distance: 2 } // insert: Splice { extent: 0, text: Some("a"), distance: 2 }
@ -342,15 +346,11 @@ impl Transaction {
true true
} }
pub fn change_by_selection<F>(state: &State, f: F) -> Self /// Generate a transaction from a set of changes.
where // TODO: take an owned iter instead of Vec
F: Fn(&SelectionRange) -> (usize, usize, Option<Tendril>), pub fn change(state: &State, changes: Vec<Change2>) -> Self {
{
let len = state.doc.len_chars(); let len = state.doc.len_chars();
let ranges = state.selection.ranges(); let mut acc = Vec::with_capacity(2 * changes.len() + 1);
let mut acc = Vec::with_capacity(2 * ranges.len() + 1);
let changes = ranges.iter().map(f);
// TODO: verify ranges are ordered and not overlapping. // TODO: verify ranges are ordered and not overlapping.
@ -370,6 +370,14 @@ impl Transaction {
Self::from(ChangeSet { changes: acc, len }) Self::from(ChangeSet { changes: acc, len })
} }
/// Generate a transaction with a change per selection range.
pub fn change_by_selection<F>(state: &State, f: F) -> Self
where
F: Fn(&SelectionRange) -> Change2,
{
Self::change(state, state.selection.ranges.iter().map(f).collect())
}
/// Insert text at each selection head. /// Insert text at each selection head.
pub fn insert(state: &State, text: Tendril) -> Self { pub fn insert(state: &State, text: Tendril) -> Self {
Self::change_by_selection(state, |range| (range.head, range.head, Some(text.clone()))) Self::change_by_selection(state, |range| (range.head, range.head, Some(text.clone())))

Loading…
Cancel
Save