core: transaction: Resolve some TODOs

pull/1741/head
Blaž Hrastnik 3 years ago
parent adf97e088e
commit 376d99a51d
No known key found for this signature in database
GPG Key ID: 1238B9C4AD889640

@ -21,7 +21,6 @@ pub enum Assoc {
After, After,
} }
// ChangeSpec = Change | ChangeSet | Vec<Change>
#[derive(Debug, Default, Clone, PartialEq, Eq)] #[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct ChangeSet { pub struct ChangeSet {
pub(crate) changes: Vec<Operation>, pub(crate) changes: Vec<Operation>,
@ -50,7 +49,6 @@ impl ChangeSet {
} }
// TODO: from iter // TODO: from iter
//
#[doc(hidden)] // used by lsp to convert to LSP changes #[doc(hidden)] // used by lsp to convert to LSP changes
pub fn changes(&self) -> &[Operation] { pub fn changes(&self) -> &[Operation] {
@ -415,8 +413,6 @@ impl ChangeSet {
pub struct Transaction { pub struct Transaction {
changes: ChangeSet, changes: ChangeSet,
selection: Option<Selection>, selection: Option<Selection>,
// effects, annotations
// scroll_into_view
} }
impl Transaction { impl Transaction {
@ -440,14 +436,12 @@ impl Transaction {
/// Returns true if applied successfully. /// Returns true if applied successfully.
pub fn apply(&self, doc: &mut Rope) -> bool { pub fn apply(&self, doc: &mut Rope) -> bool {
if !self.changes.is_empty() { if self.changes.is_empty() {
// apply changes to the document return true;
if !self.changes.apply(doc) {
return false;
}
} }
true // apply changes to the document
self.changes.apply(doc)
} }
/// Generate a transaction that reverts this one. /// Generate a transaction that reverts this one.
@ -475,7 +469,7 @@ impl Transaction {
/// Generate a transaction from a set of changes. /// Generate a transaction from a set of changes.
pub fn change<I>(doc: &Rope, changes: I) -> Self pub fn change<I>(doc: &Rope, changes: I) -> Self
where where
I: IntoIterator<Item = Change> + Iterator, I: Iterator<Item = Change>,
{ {
let len = doc.len_chars(); let len = doc.len_chars();
@ -483,12 +477,11 @@ impl Transaction {
let size = upper.unwrap_or(lower); let size = upper.unwrap_or(lower);
let mut changeset = ChangeSet::with_capacity(2 * size + 1); // rough estimate let mut changeset = ChangeSet::with_capacity(2 * size + 1); // rough estimate
// TODO: verify ranges are ordered and not overlapping or change will panic.
// TODO: test for (pos, pos, None) to factor out as nothing
let mut last = 0; let mut last = 0;
for (from, to, tendril) in changes { for (from, to, tendril) in changes {
// Verify ranges are ordered and not overlapping
debug_assert!(last <= from);
// Retain from last "to" to current "from" // Retain from last "to" to current "from"
changeset.retain(from - last); changeset.retain(from - last);
let span = to - from; let span = to - from;
@ -694,7 +687,7 @@ mod test {
let mut doc = Rope::from("hello world!\ntest 123"); let mut doc = Rope::from("hello world!\ntest 123");
let transaction = Transaction::change( let transaction = Transaction::change(
&doc, &doc,
// (1, 1, None) is a useless 0-width delete // (1, 1, None) is a useless 0-width delete that gets factored out
vec![(1, 1, None), (6, 11, Some("void".into())), (12, 17, None)].into_iter(), vec![(1, 1, None), (6, 11, Some("void".into())), (12, 17, None)].into_iter(),
); );
transaction.apply(&mut doc); transaction.apply(&mut doc);

Loading…
Cancel
Save