on master help sign in
core: Move state into the history module
c94feedBlaž Hrastnik
helix-core/src/comment.rs+19−21
@@ -100,43 +100,41 @@ mod test {
100
100
101 #[test]
101 #[test]
102 fn test_find_line_comment() {
102 fn test_find_line_comment() {
103− use crate::State;
104−
105 // four lines, two space indented, except for line 1 which is blank.
103 // four lines, two space indented, except for line 1 which is blank.
106− let doc = Rope::from(" 1\n\n 2\n 3");
104+ let mut doc = Rope::from(" 1\n\n 2\n 3");
107−
108− let mut state = State::new(doc);
109 // select whole document
105 // 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);
111
107
112− let text = state.doc.slice(..);
108+ let text = doc.slice(..);
113
109
114 let res = find_line_comment("//", text, 0..3);
110 let res = find_line_comment("//", text, 0..3);
115 // (commented = true, to_change = [line 0, line 2], min = col 2, margin = 1)
111 // (commented = true, to_change = [line 0, line 2], min = col 2, margin = 1)
116 assert_eq!(res, (false, vec![0, 2], 2, 1));
112 assert_eq!(res, (false, vec![0, 2], 2, 1));
117
113
118 // comment
114 // comment
119− let transaction = toggle_line_comments(&state.doc, &state.selection, None);
115+ let transaction = toggle_line_comments(&doc, &selection, None);
120− transaction.apply(&mut state.doc);
116+ transaction.apply(&mut doc);
121− state.selection = state.selection.map(transaction.changes());
117+ selection = selection.map(transaction.changes());
122
118
123− assert_eq!(state.doc, " // 1\n\n // 2\n // 3");
119+ assert_eq!(doc, " // 1\n\n // 2\n // 3");
124
120
125 // uncomment
121 // uncomment
126− let transaction = toggle_line_comments(&state.doc, &state.selection, None);
122+ let transaction = toggle_line_comments(&doc, &selection, None);
127− transaction.apply(&mut state.doc);
123+ transaction.apply(&mut doc);
128− state.selection = state.selection.map(transaction.changes());
124+ selection = selection.map(transaction.changes());
129− assert_eq!(state.doc, " 1\n\n 2\n 3");
125+ assert_eq!(doc, " 1\n\n 2\n 3");
126+ assert!(selection.len() == 1); // to ignore the selection unused warning
130
127
131 // 0 margin comments
128 // 0 margin comments
132− state.doc = Rope::from(" //1\n\n //2\n //3");
129+ doc = Rope::from(" //1\n\n //2\n //3");
133 // reset the selection.
130 // reset the selection.
134− state.selection = Selection::single(0, state.doc.len_chars() - 1);
131+ selection = Selection::single(0, doc.len_chars() - 1);
135
132
136− let transaction = toggle_line_comments(&state.doc, &state.selection, None);
133+ let transaction = toggle_line_comments(&doc, &selection, None);
137− transaction.apply(&mut state.doc);
134+ transaction.apply(&mut doc);
138− state.selection = state.selection.map(transaction.changes());
135+ selection = selection.map(transaction.changes());
139− assert_eq!(state.doc, " 1\n\n 2\n 3");
136+ assert_eq!(doc, " 1\n\n 2\n 3");
137+ assert!(selection.len() == 1); // to ignore the selection unused warning
140
138
141 // TODO: account for uncommenting with uneven comment indentation
139 // TODO: account for uncommenting with uneven comment indentation
142 }
140 }
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};
2use once_cell::sync::Lazy;
2use once_cell::sync::Lazy;
3use regex::Regex;
3use regex::Regex;
4use std::num::NonZeroUsize;
4use std::num::NonZeroUsize;
5use std::time::{Duration, Instant};
5use std::time::{Duration, Instant};
6
6
7+#[derive(Debug, Clone)]
8+pub struct State {
9+ pub doc: Rope,
10+ pub selection: Selection,
11+}
12+
7/// Stores the history of changes to a buffer.
13/// Stores the history of changes to a buffer.
8///
14///
9/// Currently the history is represented as a vector of revisions. The vector
15/// Currently the history is represented as a vector of revisions. The vector
@@ -366,12 +372,16 @@ impl std::str::FromStr for UndoKind {
366#[cfg(test)]
372#[cfg(test)]
367mod test {
373mod test {
368 use super::*;
374 use super::*;
375+ use crate::Selection;
369
376
370 #[test]
377 #[test]
371 fn test_undo_redo() {
378 fn test_undo_redo() {
372 let mut history = History::default();
379 let mut history = History::default();
373 let doc = Rope::from("hello");
380 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+ };
375
385
376 let transaction1 =
386 let transaction1 =
377 Transaction::change(&state.doc, vec![(5, 5, Some(" world!".into()))].into_iter());
387 Transaction::change(&state.doc, vec![(5, 5, Some(" world!".into()))].into_iter());
@@ -420,7 +430,10 @@ mod test {
420 fn test_earlier_later() {
430 fn test_earlier_later() {
421 let mut history = History::default();
431 let mut history = History::default();
422 let doc = Rope::from("a\n");
432 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+ };
424
437
425 fn undo(history: &mut History, state: &mut State) {
438 fn undo(history: &mut History, state: &mut State) {
426 if let Some(transaction) = history.undo() {
439 if let Some(transaction) = history.undo() {
helix-core/src/lib.rs−2
@@ -21,7 +21,6 @@ pub mod register;
21pub mod search;
21pub mod search;
22pub mod selection;
22pub mod selection;
23pub mod shellwords;
23pub mod shellwords;
24−mod state;
25pub mod surround;
24pub mod surround;
26pub mod syntax;
25pub mod syntax;
27pub mod test;
26pub mod test;
@@ -103,7 +102,6 @@ pub use smallvec::{smallvec, SmallVec};
103pub use syntax::Syntax;
102pub use syntax::Syntax;
104
103
105pub use diagnostic::Diagnostic;
104pub use diagnostic::Diagnostic;
106−pub use state::State;
107
105
108pub use line_ending::{LineEnding, DEFAULT_LINE_ENDING};
106pub use line_ending::{LineEnding, DEFAULT_LINE_ENDING};
109pub use transaction::{Assoc, Change, ChangeSet, Operation, Transaction};
107pub 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
3465 .any(|value| get_line_ending_of_str(value).is_some());
3465 .any(|value| get_line_ending_of_str(value).is_some());
3466
3466
3467 // Only compiled once.
3467 // Only compiled once.
3468− #[allow(clippy::trivial_regex)]
3469 static REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\r\n|\r|\n").unwrap());
3468 static REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\r\n|\r|\n").unwrap());
3470 let mut values = values
3469 let mut values = values
3471 .iter()
3470 .iter()
helix-view/src/document.rs+2−2
@@ -16,11 +16,11 @@ use std::sync::Arc;
16
16
17use helix_core::{
17use helix_core::{
18 encoding,
18 encoding,
19− history::{History, UndoKind},
19+ history::{History, State, UndoKind},
20 indent::{auto_detect_indent_style, IndentStyle},
20 indent::{auto_detect_indent_style, IndentStyle},
21 line_ending::auto_detect_line_ending,
21 line_ending::auto_detect_line_ending,
22 syntax::{self, LanguageConfiguration},
22 syntax::{self, LanguageConfiguration},
23− ChangeSet, Diagnostic, LineEnding, Rope, RopeBuilder, Selection, State, Syntax, Transaction,
23+ ChangeSet, Diagnostic, LineEnding, Rope, RopeBuilder, Selection, Syntax, Transaction,
24 DEFAULT_LINE_ENDING,
24 DEFAULT_LINE_ENDING,
25};
25};
26
26