From c94feed83d746e71fb030639d740af85162b0763 Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Tue, 8 Nov 2022 21:03:54 +0900 Subject: core: Move state into the history module --- helix-core/src/comment.rs | 40 +++++++++++++++++++--------------------- helix-core/src/history.rs | 19 ++++++++++++++++--- helix-core/src/lib.rs | 2 -- helix-core/src/state.rs | 17 ----------------- helix-term/src/commands.rs | 1 - helix-view/src/document.rs | 4 ++-- 6 files changed, 37 insertions(+), 46 deletions(-) delete mode 100644 helix-core/src/state.rs diff --git a/helix-core/src/comment.rs b/helix-core/src/comment.rs index 44f6cdfe..ec5d7a45 100644 --- a/helix-core/src/comment.rs +++ b/helix-core/src/comment.rs @@ -100,43 +100,41 @@ mod test { #[test] fn test_find_line_comment() { - use crate::State; - // four lines, two space indented, except for line 1 which is blank. - let doc = Rope::from(" 1\n\n 2\n 3"); - - let mut state = State::new(doc); + let mut doc = Rope::from(" 1\n\n 2\n 3"); // select whole document - state.selection = Selection::single(0, state.doc.len_chars() - 1); + let mut selection = Selection::single(0, doc.len_chars() - 1); - let text = state.doc.slice(..); + let text = doc.slice(..); let res = find_line_comment("//", text, 0..3); // (commented = true, to_change = [line 0, line 2], min = col 2, margin = 1) assert_eq!(res, (false, vec![0, 2], 2, 1)); // comment - let transaction = toggle_line_comments(&state.doc, &state.selection, None); - transaction.apply(&mut state.doc); - state.selection = state.selection.map(transaction.changes()); + let transaction = toggle_line_comments(&doc, &selection, None); + transaction.apply(&mut doc); + selection = selection.map(transaction.changes()); - assert_eq!(state.doc, " // 1\n\n // 2\n // 3"); + assert_eq!(doc, " // 1\n\n // 2\n // 3"); // uncomment - let transaction = toggle_line_comments(&state.doc, &state.selection, None); - transaction.apply(&mut state.doc); - state.selection = state.selection.map(transaction.changes()); - assert_eq!(state.doc, " 1\n\n 2\n 3"); + let transaction = toggle_line_comments(&doc, &selection, None); + transaction.apply(&mut doc); + selection = selection.map(transaction.changes()); + assert_eq!(doc, " 1\n\n 2\n 3"); + assert!(selection.len() == 1); // to ignore the selection unused warning // 0 margin comments - state.doc = Rope::from(" //1\n\n //2\n //3"); + doc = Rope::from(" //1\n\n //2\n //3"); // reset the selection. - state.selection = Selection::single(0, state.doc.len_chars() - 1); + selection = Selection::single(0, doc.len_chars() - 1); - let transaction = toggle_line_comments(&state.doc, &state.selection, None); - transaction.apply(&mut state.doc); - state.selection = state.selection.map(transaction.changes()); - assert_eq!(state.doc, " 1\n\n 2\n 3"); + let transaction = toggle_line_comments(&doc, &selection, None); + transaction.apply(&mut doc); + selection = selection.map(transaction.changes()); + assert_eq!(doc, " 1\n\n 2\n 3"); + assert!(selection.len() == 1); // to ignore the selection unused warning // TODO: account for uncommenting with uneven comment indentation } diff --git a/helix-core/src/history.rs b/helix-core/src/history.rs index 5cd72b07..51174c02 100644 --- a/helix-core/src/history.rs +++ b/helix-core/src/history.rs @@ -1,9 +1,15 @@ -use crate::{Assoc, ChangeSet, Range, Rope, State, Transaction}; +use crate::{Assoc, ChangeSet, Range, Rope, Selection, Transaction}; use once_cell::sync::Lazy; use regex::Regex; use std::num::NonZeroUsize; use std::time::{Duration, Instant}; +#[derive(Debug, Clone)] +pub struct State { + pub doc: Rope, + pub selection: Selection, +} + /// Stores the history of changes to a buffer. /// /// Currently the history is represented as a vector of revisions. The vector @@ -366,12 +372,16 @@ impl std::str::FromStr for UndoKind { #[cfg(test)] mod test { use super::*; + use crate::Selection; #[test] fn test_undo_redo() { let mut history = History::default(); let doc = Rope::from("hello"); - let mut state = State::new(doc); + let mut state = State { + doc, + selection: Selection::point(0), + }; let transaction1 = Transaction::change(&state.doc, vec![(5, 5, Some(" world!".into()))].into_iter()); @@ -420,7 +430,10 @@ mod test { fn test_earlier_later() { let mut history = History::default(); let doc = Rope::from("a\n"); - let mut state = State::new(doc); + let mut state = State { + doc, + selection: Selection::point(0), + }; fn undo(history: &mut History, state: &mut State) { if let Some(transaction) = history.undo() { diff --git a/helix-core/src/lib.rs b/helix-core/src/lib.rs index 8f869e35..5f60c048 100644 --- a/helix-core/src/lib.rs +++ b/helix-core/src/lib.rs @@ -21,7 +21,6 @@ pub mod register; pub mod search; pub mod selection; pub mod shellwords; -mod state; pub mod surround; pub mod syntax; pub mod test; @@ -103,7 +102,6 @@ pub use smallvec::{smallvec, SmallVec}; pub use syntax::Syntax; pub use diagnostic::Diagnostic; -pub use state::State; pub use line_ending::{LineEnding, DEFAULT_LINE_ENDING}; pub use transaction::{Assoc, Change, ChangeSet, Operation, Transaction}; diff --git a/helix-core/src/state.rs b/helix-core/src/state.rs deleted file mode 100644 index dcc4b11b..00000000 --- a/helix-core/src/state.rs +++ /dev/null @@ -1,17 +0,0 @@ -use crate::{Rope, Selection}; - -#[derive(Debug, Clone)] -pub struct State { - pub doc: Rope, - pub selection: Selection, -} - -impl State { - #[must_use] - pub fn new(doc: Rope) -> Self { - Self { - doc, - selection: Selection::point(0), - } - } -} diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index ae9e35f1..31498a7b 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -3465,7 +3465,6 @@ fn paste_impl(values: &[String], doc: &mut Document, view: &mut View, action: Pa .any(|value| get_line_ending_of_str(value).is_some()); // Only compiled once. - #[allow(clippy::trivial_regex)] static REGEX: Lazy = Lazy::new(|| Regex::new(r"\r\n|\r|\n").unwrap()); let mut values = values .iter() diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 9a7febd2..08708528 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -16,11 +16,11 @@ use std::sync::Arc; use helix_core::{ encoding, - history::{History, UndoKind}, + history::{History, State, UndoKind}, indent::{auto_detect_indent_style, IndentStyle}, line_ending::auto_detect_line_ending, syntax::{self, LanguageConfiguration}, - ChangeSet, Diagnostic, LineEnding, Rope, RopeBuilder, Selection, State, Syntax, Transaction, + ChangeSet, Diagnostic, LineEnding, Rope, RopeBuilder, Selection, Syntax, Transaction, DEFAULT_LINE_ENDING, }; -- cgit v1.2.3-70-g09d2