on master help sign in
core: Move state into the history module
commitc94feed83d746e71fb030639d740af85162b0763 authorBlaž Hrastnik <blaz@mxxn.io> dateTue 8 Nov 2022, 21:03 (4 years ago) parent1312682
6 files changed+37−46
helix-core/src/comment.rs+19−21 helix-core/src/history.rs+16−3 helix-core/src/lib.rs−2 helix-core/src/state.rs−17 helix-term/src/commands.rs−1 helix-view/src/document.rs+2−2
helix-core/src/comment.rs+19−21
@@ -100,43 +100,41 @@ mod test {
100100
101101 #[test]
102102 fn test_find_line_comment() {
103− use crate::State;
104−
105103 // four lines, two space indented, except for line 1 which is blank.
106− let doc = Rope::from(" 1\n\n 2\n 3");
107−
108− let mut state = State::new(doc);
104+ let mut doc = Rope::from(" 1\n\n 2\n 3");
109105 // select whole document
110− state.selection = Selection::single(0, state.doc.len_chars() - 1);
106+ let mut selection = Selection::single(0, doc.len_chars() - 1);
111107
112− let text = state.doc.slice(..);
108+ let text = doc.slice(..);
113109
114110 let res = find_line_comment("//", text, 0..3);
115111 // (commented = true, to_change = [line 0, line 2], min = col 2, margin = 1)
116112 assert_eq!(res, (false, vec![0, 2], 2, 1));
117113
118114 // comment
119− let transaction = toggle_line_comments(&state.doc, &state.selection, None);
120− transaction.apply(&mut state.doc);
121− state.selection = state.selection.map(transaction.changes());
115+ let transaction = toggle_line_comments(&doc, &selection, None);
116+ transaction.apply(&mut doc);
117+ selection = selection.map(transaction.changes());
122118
123− assert_eq!(state.doc, " // 1\n\n // 2\n // 3");
119+ assert_eq!(doc, " // 1\n\n // 2\n // 3");
124120
125121 // uncomment
126− let transaction = toggle_line_comments(&state.doc, &state.selection, None);
127− transaction.apply(&mut state.doc);
128− state.selection = state.selection.map(transaction.changes());
129− assert_eq!(state.doc, " 1\n\n 2\n 3");
122+ let transaction = toggle_line_comments(&doc, &selection, None);
123+ transaction.apply(&mut doc);
124+ selection = selection.map(transaction.changes());
125+ assert_eq!(doc, " 1\n\n 2\n 3");
126+ assert!(selection.len() == 1); // to ignore the selection unused warning
130127
131128 // 0 margin comments
132− state.doc = Rope::from(" //1\n\n //2\n //3");
129+ doc = Rope::from(" //1\n\n //2\n //3");
133130 // reset the selection.
134− state.selection = Selection::single(0, state.doc.len_chars() - 1);
131+ selection = Selection::single(0, doc.len_chars() - 1);
135132
136− let transaction = toggle_line_comments(&state.doc, &state.selection, None);
137− transaction.apply(&mut state.doc);
138− state.selection = state.selection.map(transaction.changes());
139− assert_eq!(state.doc, " 1\n\n 2\n 3");
133+ let transaction = toggle_line_comments(&doc, &selection, None);
134+ transaction.apply(&mut doc);
135+ selection = selection.map(transaction.changes());
136+ assert_eq!(doc, " 1\n\n 2\n 3");
137+ assert!(selection.len() == 1); // to ignore the selection unused warning
140138
141139 // TODO: account for uncommenting with uneven comment indentation
142140 }
helix-core/src/history.rs+16−3
@@ -1,9 +1,15 @@
1−use crate::{Assoc, ChangeSet, Range, Rope, State, Transaction};
1+use crate::{Assoc, ChangeSet, Range, Rope, Selection, Transaction};
22use once_cell::sync::Lazy;
33use regex::Regex;
44use std::num::NonZeroUsize;
55use std::time::{Duration, Instant};
66
7+#[derive(Debug, Clone)]
8+pub struct State {
9+ pub doc: Rope,
10+ pub selection: Selection,
11+}
12+
713/// Stores the history of changes to a buffer.
814///
915/// Currently the history is represented as a vector of revisions. The vector
@@ -366,12 +372,16 @@ impl std::str::FromStr for UndoKind {
366372#[cfg(test)]
367373mod test {
368374 use super::*;
375+ use crate::Selection;
369376
370377 #[test]
371378 fn test_undo_redo() {
372379 let mut history = History::default();
373380 let doc = Rope::from("hello");
374− let mut state = State::new(doc);
381+ let mut state = State {
382+ doc,
383+ selection: Selection::point(0),
384+ };
375385
376386 let transaction1 =
377387 Transaction::change(&state.doc, vec![(5, 5, Some(" world!".into()))].into_iter());
@@ -420,7 +430,10 @@ mod test {
420430 fn test_earlier_later() {
421431 let mut history = History::default();
422432 let doc = Rope::from("a\n");
423− let mut state = State::new(doc);
433+ let mut state = State {
434+ doc,
435+ selection: Selection::point(0),
436+ };
424437
425438 fn undo(history: &mut History, state: &mut State) {
426439 if let Some(transaction) = history.undo() {
helix-core/src/lib.rs−2
@@ -21,7 +21,6 @@ pub mod register;
2121pub mod search;
2222pub mod selection;
2323pub mod shellwords;
24−mod state;
2524pub mod surround;
2625pub mod syntax;
2726pub mod test;
@@ -103,7 +102,6 @@ pub use smallvec::{smallvec, SmallVec};
103102pub use syntax::Syntax;
104103
105104pub use diagnostic::Diagnostic;
106−pub use state::State;
107105
108106pub use line_ending::{LineEnding, DEFAULT_LINE_ENDING};
109107pub use transaction::{Assoc, Change, ChangeSet, Operation, Transaction};
helix-core/src/state.rsdeleted−17
@@ -1,17 +0,0 @@
1−use crate::{Rope, Selection};
2−
3−#[derive(Debug, Clone)]
4−pub struct State {
5− pub doc: Rope,
6− pub selection: Selection,
7−}
8−
9−impl State {
10− #[must_use]
11− pub fn new(doc: Rope) -> Self {
12− Self {
13− doc,
14− selection: Selection::point(0),
15− }
16− }
17−}
helix-term/src/commands.rs−1
@@ -3465,7 +3465,6 @@ fn paste_impl(values: &[String], doc: &mut Document, view: &mut View, action: Pa
34653465 .any(|value| get_line_ending_of_str(value).is_some());
34663466
34673467 // Only compiled once.
3468− #[allow(clippy::trivial_regex)]
34693468 static REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\r\n|\r|\n").unwrap());
34703469 let mut values = values
34713470 .iter()
helix-view/src/document.rs+2−2
@@ -16,11 +16,11 @@ use std::sync::Arc;
1616
1717use helix_core::{
1818 encoding,
19− history::{History, UndoKind},
19+ history::{History, State, UndoKind},
2020 indent::{auto_detect_indent_style, IndentStyle},
2121 line_ending::auto_detect_line_ending,
2222 syntax::{self, LanguageConfiguration},
23− ChangeSet, Diagnostic, LineEnding, Rope, RopeBuilder, Selection, State, Syntax, Transaction,
23+ ChangeSet, Diagnostic, LineEnding, Rope, RopeBuilder, Selection, Syntax, Transaction,
2424 DEFAULT_LINE_ENDING,
2525};
2626