core: Move state into the history module

pull/4651/head
Blaž Hrastnik 2 years ago
parent 13126823f8
commit c94feed83d
No known key found for this signature in database
GPG Key ID: 1238B9C4AD889640

@ -100,43 +100,41 @@ mod test {
#[test] #[test]
fn test_find_line_comment() { fn test_find_line_comment() {
use crate::State;
// four lines, two space indented, except for line 1 which is blank. // four lines, two space indented, except for line 1 which is blank.
let doc = Rope::from(" 1\n\n 2\n 3"); let mut doc = Rope::from(" 1\n\n 2\n 3");
let mut state = State::new(doc);
// select whole document // 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); let res = find_line_comment("//", text, 0..3);
// (commented = true, to_change = [line 0, line 2], min = col 2, margin = 1) // (commented = true, to_change = [line 0, line 2], min = col 2, margin = 1)
assert_eq!(res, (false, vec![0, 2], 2, 1)); assert_eq!(res, (false, vec![0, 2], 2, 1));
// comment // comment
let transaction = toggle_line_comments(&state.doc, &state.selection, None); let transaction = toggle_line_comments(&doc, &selection, None);
transaction.apply(&mut state.doc); transaction.apply(&mut doc);
state.selection = state.selection.map(transaction.changes()); 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 // uncomment
let transaction = toggle_line_comments(&state.doc, &state.selection, None); let transaction = toggle_line_comments(&doc, &selection, None);
transaction.apply(&mut state.doc); transaction.apply(&mut doc);
state.selection = state.selection.map(transaction.changes()); selection = selection.map(transaction.changes());
assert_eq!(state.doc, " 1\n\n 2\n 3"); assert_eq!(doc, " 1\n\n 2\n 3");
assert!(selection.len() == 1); // to ignore the selection unused warning
// 0 margin comments // 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. // 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); let transaction = toggle_line_comments(&doc, &selection, None);
transaction.apply(&mut state.doc); transaction.apply(&mut doc);
state.selection = state.selection.map(transaction.changes()); selection = selection.map(transaction.changes());
assert_eq!(state.doc, " 1\n\n 2\n 3"); 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 // TODO: account for uncommenting with uneven comment indentation
} }

@ -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 once_cell::sync::Lazy;
use regex::Regex; use regex::Regex;
use std::num::NonZeroUsize; use std::num::NonZeroUsize;
use std::time::{Duration, Instant}; 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. /// Stores the history of changes to a buffer.
/// ///
/// Currently the history is represented as a vector of revisions. The vector /// Currently the history is represented as a vector of revisions. The vector
@ -366,12 +372,16 @@ impl std::str::FromStr for UndoKind {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
use crate::Selection;
#[test] #[test]
fn test_undo_redo() { fn test_undo_redo() {
let mut history = History::default(); let mut history = History::default();
let doc = Rope::from("hello"); let doc = Rope::from("hello");
let mut state = State::new(doc); let mut state = State {
doc,
selection: Selection::point(0),
};
let transaction1 = let transaction1 =
Transaction::change(&state.doc, vec![(5, 5, Some(" world!".into()))].into_iter()); Transaction::change(&state.doc, vec![(5, 5, Some(" world!".into()))].into_iter());
@ -420,7 +430,10 @@ mod test {
fn test_earlier_later() { fn test_earlier_later() {
let mut history = History::default(); let mut history = History::default();
let doc = Rope::from("a\n"); 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) { fn undo(history: &mut History, state: &mut State) {
if let Some(transaction) = history.undo() { if let Some(transaction) = history.undo() {

@ -21,7 +21,6 @@ pub mod register;
pub mod search; pub mod search;
pub mod selection; pub mod selection;
pub mod shellwords; pub mod shellwords;
mod state;
pub mod surround; pub mod surround;
pub mod syntax; pub mod syntax;
pub mod test; pub mod test;
@ -103,7 +102,6 @@ pub use smallvec::{smallvec, SmallVec};
pub use syntax::Syntax; pub use syntax::Syntax;
pub use diagnostic::Diagnostic; pub use diagnostic::Diagnostic;
pub use state::State;
pub use line_ending::{LineEnding, DEFAULT_LINE_ENDING}; pub use line_ending::{LineEnding, DEFAULT_LINE_ENDING};
pub use transaction::{Assoc, Change, ChangeSet, Operation, Transaction}; pub use transaction::{Assoc, Change, ChangeSet, Operation, Transaction};

@ -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),
}
}
}

@ -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()); .any(|value| get_line_ending_of_str(value).is_some());
// Only compiled once. // Only compiled once.
#[allow(clippy::trivial_regex)]
static REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\r\n|\r|\n").unwrap()); static REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\r\n|\r|\n").unwrap());
let mut values = values let mut values = values
.iter() .iter()

@ -16,11 +16,11 @@ use std::sync::Arc;
use helix_core::{ use helix_core::{
encoding, encoding,
history::{History, UndoKind}, history::{History, State, UndoKind},
indent::{auto_detect_indent_style, IndentStyle}, indent::{auto_detect_indent_style, IndentStyle},
line_ending::auto_detect_line_ending, line_ending::auto_detect_line_ending,
syntax::{self, LanguageConfiguration}, syntax::{self, LanguageConfiguration},
ChangeSet, Diagnostic, LineEnding, Rope, RopeBuilder, Selection, State, Syntax, Transaction, ChangeSet, Diagnostic, LineEnding, Rope, RopeBuilder, Selection, Syntax, Transaction,
DEFAULT_LINE_ENDING, DEFAULT_LINE_ENDING,
}; };

Loading…
Cancel
Save