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