mirror of
https://github.com/helix-editor/helix.git
synced 2025-01-19 13:37:06 +04:00
Add View::apply for adjusting jumplist ranges
Applying a transaction to a View adjusts the ranges in the jumplist to ensure that they remain within the text of the document and follow regular selection invariants (for example, must be have a width of at least one).
This commit is contained in:
parent
a85e386298
commit
d418f0795d
@ -3,7 +3,9 @@
|
|||||||
gutter::{self, Gutter},
|
gutter::{self, Gutter},
|
||||||
Document, DocumentId, ViewId,
|
Document, DocumentId, ViewId,
|
||||||
};
|
};
|
||||||
use helix_core::{pos_at_visual_coords, visual_coords_at_pos, Position, RopeSlice, Selection};
|
use helix_core::{
|
||||||
|
pos_at_visual_coords, visual_coords_at_pos, Position, RopeSlice, Selection, Transaction,
|
||||||
|
};
|
||||||
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
@ -62,6 +64,22 @@ pub fn remove(&mut self, doc_id: &DocumentId) {
|
|||||||
pub fn get(&self) -> &[Jump] {
|
pub fn get(&self) -> &[Jump] {
|
||||||
&self.jumps
|
&self.jumps
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Applies a [`Transaction`] of changes to the jumplist.
|
||||||
|
/// This is necessary to ensure that changes to documents do not leave jump-list
|
||||||
|
/// selections pointing to parts of the text which no longer exist.
|
||||||
|
fn apply(&mut self, transaction: &Transaction, doc: &Document) {
|
||||||
|
let text = doc.text().slice(..);
|
||||||
|
|
||||||
|
for (doc_id, selection) in &mut self.jumps {
|
||||||
|
if doc.id() == *doc_id {
|
||||||
|
*selection = selection
|
||||||
|
.clone()
|
||||||
|
.map(transaction.changes())
|
||||||
|
.ensure_invariants(text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@ -334,6 +352,14 @@ pub fn remove_document(&mut self, doc_id: &DocumentId) {
|
|||||||
// (None, None) => return,
|
// (None, None) => return,
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
/// Applies a [`Transaction`] to the view.
|
||||||
|
/// Instead of calling this function directly, use [crate::apply_transaction]
|
||||||
|
/// which applies a transaction to the [`Document`] and view together.
|
||||||
|
pub fn apply(&mut self, transaction: &Transaction, doc: &Document) -> bool {
|
||||||
|
self.jumps.apply(transaction, doc);
|
||||||
|
true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
Loading…
Reference in New Issue
Block a user