aboutsummaryrefslogtreecommitdiff
path: root/helix-view
diff options
context:
space:
mode:
Diffstat (limited to 'helix-view')
-rw-r--r--helix-view/src/document.rs11
-rw-r--r--helix-view/src/lib.rs13
2 files changed, 19 insertions, 5 deletions
diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs
index 631c540b..93708339 100644
--- a/helix-view/src/document.rs
+++ b/helix-view/src/document.rs
@@ -24,7 +24,7 @@ use helix_core::{
DEFAULT_LINE_ENDING,
};
-use crate::{DocumentId, Editor, View, ViewId};
+use crate::{apply_transaction, DocumentId, Editor, View, ViewId};
/// 8kB of buffer space for encoding and decoding `Rope`s.
const BUF_SIZE: usize = 8192;
@@ -617,8 +617,7 @@ impl Document {
// This is not considered a modification of the contents of the file regardless
// of the encoding.
let transaction = helix_core::diff::compare_ropes(self.text(), &rope);
- self.apply(&transaction, view.id);
- view.apply(&transaction, self);
+ apply_transaction(&transaction, self, view);
self.append_changes_to_history(view.id);
self.reset_modified();
@@ -811,6 +810,9 @@ impl Document {
}
/// Apply a [`Transaction`] to the [`Document`] to change its text.
+ /// Instead of calling this function directly, use [crate::apply_transaction]
+ /// to ensure that the transaction is applied to the appropriate [`View`] as
+ /// well.
pub fn apply(&mut self, transaction: &Transaction, view_id: ViewId) -> bool {
// store the state just before any changes are made. This allows us to undo to the
// state just before a transaction was applied.
@@ -865,8 +867,7 @@ impl Document {
pub fn restore(&mut self, view: &mut View) {
if let Some(revert) = self.savepoint.take() {
- self.apply(&revert, view.id);
- view.apply(&revert, self);
+ apply_transaction(&revert, self, view);
}
}
diff --git a/helix-view/src/lib.rs b/helix-view/src/lib.rs
index 0145677d..276be441 100644
--- a/helix-view/src/lib.rs
+++ b/helix-view/src/lib.rs
@@ -64,6 +64,19 @@ pub fn align_view(doc: &Document, view: &mut View, align: Align) {
view.offset.row = line.saturating_sub(relative);
}
+/// Applies a [`helix_core::Transaction`] to the given [`Document`]
+/// and [`View`].
+pub fn apply_transaction(
+ transaction: &helix_core::Transaction,
+ doc: &mut Document,
+ view: &mut View,
+) -> bool {
+ // This is a short function but it's easy to call `Document::apply`
+ // without calling `View::apply` or in the wrong order. The transaction
+ // must be applied to the document before the view.
+ doc.apply(transaction, view.id) && view.apply(transaction, doc)
+}
+
pub use document::Document;
pub use editor::Editor;
pub use theme::Theme;