From 9d4c3015632abba2ae6713874907825f6335b71a Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Tue, 24 Aug 2021 23:43:05 +0900 Subject: Reduce State use a bit further This is a legacy type that should be fully removed. --- helix-core/src/transaction.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'helix-core/src/transaction.rs') diff --git a/helix-core/src/transaction.rs b/helix-core/src/transaction.rs index e20e550f..f465992d 100644 --- a/helix-core/src/transaction.rs +++ b/helix-core/src/transaction.rs @@ -689,21 +689,21 @@ mod test { #[test] fn transaction_change() { - let mut state = State::new("hello world!\ntest 123".into()); + let mut doc = Rope::from("hello world!\ntest 123".into()); let transaction = Transaction::change( &state.doc, // (1, 1, None) is a useless 0-width delete vec![(1, 1, None), (6, 11, Some("void".into())), (12, 17, None)].into_iter(), ); - transaction.apply(&mut state.doc); - assert_eq!(state.doc, Rope::from_str("hello void! 123")); + transaction.apply(&mut doc); + assert_eq!(doc, Rope::from_str("hello void! 123")); } #[test] fn changes_iter() { - let state = State::new("hello world!\ntest 123".into()); + let doc = Rope::from("hello world!\ntest 123".into()); let changes = vec![(6, 11, Some("void".into())), (12, 17, None)]; - let transaction = Transaction::change(&state.doc, changes.clone().into_iter()); + let transaction = Transaction::change(&doc, changes.clone().into_iter()); assert_eq!(transaction.changes_iter().collect::>(), changes); } -- cgit v1.2.3-70-g09d2 From 68bf9fdf02a2462c57c86a7318987d385a289184 Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Thu, 26 Aug 2021 09:26:38 +0900 Subject: Fix tests broken by the State change --- helix-core/src/transaction.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'helix-core/src/transaction.rs') diff --git a/helix-core/src/transaction.rs b/helix-core/src/transaction.rs index f465992d..d15db000 100644 --- a/helix-core/src/transaction.rs +++ b/helix-core/src/transaction.rs @@ -689,9 +689,9 @@ mod test { #[test] fn transaction_change() { - let mut doc = Rope::from("hello world!\ntest 123".into()); + let mut doc = Rope::from("hello world!\ntest 123"); let transaction = Transaction::change( - &state.doc, + &doc, // (1, 1, None) is a useless 0-width delete vec![(1, 1, None), (6, 11, Some("void".into())), (12, 17, None)].into_iter(), ); @@ -701,7 +701,7 @@ mod test { #[test] fn changes_iter() { - let doc = Rope::from("hello world!\ntest 123".into()); + let doc = Rope::from("hello world!\ntest 123"); let changes = vec![(6, 11, Some("void".into())), (12, 17, None)]; let transaction = Transaction::change(&doc, changes.clone().into_iter()); assert_eq!(transaction.changes_iter().collect::>(), changes); -- cgit v1.2.3-70-g09d2 From fa4caf7e3d43a40073f6966d37f529d5197bd840 Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Thu, 26 Aug 2021 14:45:23 -0400 Subject: remove unsafe --- helix-core/src/transaction.rs | 2 +- helix-view/src/document.rs | 17 +++++------------ 2 files changed, 6 insertions(+), 13 deletions(-) (limited to 'helix-core/src/transaction.rs') diff --git a/helix-core/src/transaction.rs b/helix-core/src/transaction.rs index d15db000..d682f058 100644 --- a/helix-core/src/transaction.rs +++ b/helix-core/src/transaction.rs @@ -125,7 +125,7 @@ impl ChangeSet { /// In other words, If `this` goes `docA` → `docB` and `other` represents `docB` → `docC`, the /// returned value will represent the change `docA` → `docC`. pub fn compose(self, other: Self) -> Self { - debug_assert!(self.len_after == other.len); + assert!(self.len_after == other.len); // composing fails in weird ways if one of the sets is empty // a: [] len: 0 len_after: 1 | b: [Insert(Tendril(inline: "\n")), Retain(1)] len 1 diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index a238644a..e890a336 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -102,7 +102,7 @@ pub struct Document { language_server: Option>, } -use std::fmt; +use std::{fmt, mem}; impl fmt::Debug for Document { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("Document") @@ -301,20 +301,13 @@ pub async fn to_writer<'a, W: tokio::io::AsyncWriteExt + Unpin + ?Sized>( Ok(()) } -/// Like std::mem::replace() except it allows the replacement value to be mapped from the -/// original value. -fn take_with(mut_ref: &mut T, closure: F) +fn take_with(mut_ref: &mut T, f: F) where + T: Default, 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); - } + let t = mem::take(mut_ref); + let _ = mem::replace(mut_ref, f(t)); } use helix_lsp::lsp; -- cgit v1.2.3-70-g09d2