aboutsummaryrefslogtreecommitdiff
path: root/helix-core/src/transaction.rs
diff options
context:
space:
mode:
Diffstat (limited to 'helix-core/src/transaction.rs')
-rw-r--r--helix-core/src/transaction.rs45
1 files changed, 14 insertions, 31 deletions
diff --git a/helix-core/src/transaction.rs b/helix-core/src/transaction.rs
index 6f3956aa..f1cb2ca1 100644
--- a/helix-core/src/transaction.rs
+++ b/helix-core/src/transaction.rs
@@ -5,8 +5,9 @@ use std::convert::TryFrom;
/// (from, to, replacement)
pub type Change = (usize, usize, Option<Tendril>);
+// TODO: pub(crate)
#[derive(Debug, Clone, PartialEq, Eq)]
-pub(crate) enum Operation {
+pub enum Operation {
/// Move cursor by n characters.
Retain(usize),
/// Delete n characters.
@@ -40,6 +41,12 @@ impl ChangeSet {
}
// TODO: from iter
+ //
+
+ #[doc(hidden)] // used by lsp to convert to LSP changes
+ pub fn changes(&self) -> &[Operation] {
+ &self.changes
+ }
#[must_use]
fn len_after(&self) -> usize {
@@ -351,22 +358,6 @@ pub struct Transaction {
// scroll_into_view
}
-/// Like std::mem::replace() except it allows the replacement value to be mapped from the
-/// original value.
-pub fn take_with<T, F>(mut_ref: &mut T, closure: F)
-where
- F: FnOnce(T) -> T,
-{
- use std::{panic, ptr};
-
- unsafe {
- let old_t = ptr::read(mut_ref);
- let new_t = panic::catch_unwind(panic::AssertUnwindSafe(|| closure(old_t)))
- .unwrap_or_else(|_| ::std::process::abort());
- ptr::write(mut_ref, new_t);
- }
-}
-
impl Transaction {
/// Create a new, empty transaction.
pub fn new(state: &mut State) -> Self {
@@ -376,29 +367,21 @@ impl Transaction {
}
}
+ pub fn changes(&self) -> &ChangeSet {
+ &self.changes
+ }
+
/// Returns true if applied successfully.
pub fn apply(&self, state: &mut State) -> bool {
if !self.changes.is_empty() {
- // TODO: also avoid mapping the selection if not necessary
-
- let old_doc = state.doc().clone();
-
// apply changes to the document
if !self.changes.apply(&mut state.doc) {
return false;
}
-
- // Compose this transaction with the previous one
- take_with(&mut state.changes, |changes| {
- changes.compose(self.changes.clone()).unwrap()
- });
-
- if let Some(syntax) = &mut state.syntax {
- // TODO: no unwrap
- syntax.update(&old_doc, &state.doc, &self.changes).unwrap();
- }
}
+ // TODO: also avoid mapping the selection if not necessary
+
// update the selection: either take the selection specified in the transaction, or map the
// current selection through changes.
state.selection = self