diff options
Diffstat (limited to 'helix-core/src')
-rw-r--r-- | helix-core/src/comment.rs | 18 | ||||
-rw-r--r-- | helix-core/src/indent.rs | 27 | ||||
-rw-r--r-- | helix-core/src/transaction.rs | 24 |
3 files changed, 36 insertions, 33 deletions
diff --git a/helix-core/src/comment.rs b/helix-core/src/comment.rs index 969d4d92..35aabda9 100644 --- a/helix-core/src/comment.rs +++ b/helix-core/src/comment.rs @@ -1,4 +1,6 @@ -use crate::{find_first_non_whitespace_char2, Change, RopeSlice, State, Tendril, Transaction}; +use crate::{ + find_first_non_whitespace_char2, Change, Rope, RopeSlice, Selection, Tendril, Transaction, +}; use core::ops::Range; use std::borrow::Cow; @@ -35,14 +37,14 @@ fn find_line_comment( (commented, skipped, min) } -pub fn toggle_line_comments(state: &State) -> Transaction { - let text = state.doc.slice(..); +pub fn toggle_line_comments(doc: &Rope, selection: &Selection) -> Transaction { + let text = doc.slice(..); let mut changes: Vec<Change> = Vec::new(); let token = "//"; let comment = Tendril::from(format!("{} ", token)); - for selection in state.selection.ranges() { + for selection in selection.ranges() { let start = text.char_to_line(selection.from()); let end = text.char_to_line(selection.to()); let lines = start..end + 1; @@ -67,7 +69,7 @@ pub fn toggle_line_comments(state: &State) -> Transaction { } } } - Transaction::change(&state.doc, changes.into_iter()) + Transaction::change(doc, changes.into_iter()) } #[cfg(test)] @@ -76,7 +78,7 @@ mod test { #[test] fn test_find_line_comment() { - use crate::{Rope, Selection}; + 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"); @@ -92,13 +94,13 @@ mod test { assert_eq!(res, (false, vec![1], 2)); // comment - let transaction = toggle_line_comments(&state); + let transaction = toggle_line_comments(&state.doc, &state.selection); transaction.apply(&mut state); assert_eq!(state.doc, " // 1\n\n // 2\n // 3"); // uncomment - let transaction = toggle_line_comments(&state); + let transaction = toggle_line_comments(&state.doc, &state.selection); transaction.apply(&mut state); assert_eq!(state.doc, " 1\n\n 2\n 3"); diff --git a/helix-core/src/indent.rs b/helix-core/src/indent.rs index ec0460d5..9b1241e5 100644 --- a/helix-core/src/indent.rs +++ b/helix-core/src/indent.rs @@ -2,7 +2,7 @@ use crate::{ find_first_non_whitespace_char, syntax::Syntax, tree_sitter::{Node, Tree}, - Rope, RopeSlice, State, + Rope, RopeSlice, }; /// To determine indentation of a newly inserted line, figure out the indentation at the last col @@ -162,12 +162,12 @@ fn calculate_indentation(node: Option<Node>, newline: bool) -> usize { increment as usize } -fn suggested_indent_for_line(syntax: Option<&Syntax>, state: &State, line_num: usize) -> usize { - let line = state.doc.line(line_num); +fn suggested_indent_for_line(syntax: Option<&Syntax>, text: RopeSlice, line_num: usize) -> usize { + let line = text.line(line_num); let current = indent_level_for_line(line); - if let Some(start) = find_first_non_whitespace_char(state.doc.slice(..), line_num) { - return suggested_indent_for_pos(syntax, state, start, false); + if let Some(start) = find_first_non_whitespace_char(text, line_num) { + return suggested_indent_for_pos(syntax, text, start, false); }; // if the line is blank, indent should be zero @@ -179,12 +179,12 @@ fn suggested_indent_for_line(syntax: Option<&Syntax>, state: &State, line_num: u // - it should look up the wrapper node and count it too when we press o/O pub fn suggested_indent_for_pos( syntax: Option<&Syntax>, - state: &State, + text: RopeSlice, pos: usize, new_line: bool, ) -> usize { if let Some(syntax) = syntax { - let byte_start = state.doc.char_to_byte(pos); + let byte_start = text.char_to_byte(pos); let node = get_highest_syntax_node_at_bytepos(syntax, byte_start); // TODO: special case for comments @@ -279,7 +279,7 @@ std::panic::set_hook(Box::new(move |info| { 1 }}} -pub fn change<I>(state: &State, changes: I) -> Self +pub fn change<I>(document: &Document, changes: I) -> Self where I: IntoIterator<Item = Change> + ExactSizeIterator, { @@ -288,20 +288,19 @@ where ", ); - let state = State::new(doc); - // TODO: set_language + let doc = Rope::from(doc); let language_config = crate::syntax::LOADER .language_config_for_scope("source.rust") .unwrap(); let highlight_config = language_config.highlight_config(&[]).unwrap(); - let syntax = Syntax::new(&state.doc, highlight_config.clone()); - let text = state.doc.slice(..); + let syntax = Syntax::new(&doc, highlight_config.clone()); + let text = doc.slice(..); - for i in 0..state.doc.len_lines() { + for i in 0..doc.len_lines() { let line = text.line(i); let indent = indent_level_for_line(line); assert_eq!( - suggested_indent_for_line(Some(&syntax), &state, i), + suggested_indent_for_line(Some(&syntax), text, i), indent, "line {}: {}", i, diff --git a/helix-core/src/transaction.rs b/helix-core/src/transaction.rs index dbc5d588..2bd100e8 100644 --- a/helix-core/src/transaction.rs +++ b/helix-core/src/transaction.rs @@ -409,9 +409,9 @@ pub struct Transaction { impl Transaction { /// Create a new, empty transaction. - pub fn new(state: &mut State) -> Self { + pub fn new(doc: &Rope) -> Self { Self { - changes: ChangeSet::new(&state.doc), + changes: ChangeSet::new(doc), selection: None, } } @@ -490,16 +490,18 @@ impl Transaction { } /// Generate a transaction with a change per selection range. - pub fn change_by_selection<F>(state: &State, f: F) -> Self + pub fn change_by_selection<F>(doc: &Rope, selection: &Selection, f: F) -> Self where F: FnMut(&Range) -> Change, { - Self::change(&state.doc, state.selection.ranges().iter().map(f)) + Self::change(doc, selection.ranges().iter().map(f)) } /// Insert text at each selection head. - pub fn insert(state: &State, text: Tendril) -> Self { - Self::change_by_selection(state, |range| (range.head, range.head, Some(text.clone()))) + pub fn insert(doc: &Rope, selection: &Selection, text: Tendril) -> Self { + Self::change_by_selection(doc, selection, |range| { + (range.head, range.head, Some(text.clone())) + }) } } @@ -628,15 +630,15 @@ mod test { #[test] fn optimized_composition() { let mut state = State::new("".into()); - let t1 = Transaction::insert(&state, Tendril::from_char('h')); + let t1 = Transaction::insert(&state.doc, &state.selection, Tendril::from_char('h')); t1.apply(&mut state); - let t2 = Transaction::insert(&state, Tendril::from_char('e')); + let t2 = Transaction::insert(&state.doc, &state.selection, Tendril::from_char('e')); t2.apply(&mut state); - let t3 = Transaction::insert(&state, Tendril::from_char('l')); + let t3 = Transaction::insert(&state.doc, &state.selection, Tendril::from_char('l')); t3.apply(&mut state); - let t4 = Transaction::insert(&state, Tendril::from_char('l')); + let t4 = Transaction::insert(&state.doc, &state.selection, Tendril::from_char('l')); t4.apply(&mut state); - let t5 = Transaction::insert(&state, Tendril::from_char('o')); + let t5 = Transaction::insert(&state.doc, &state.selection, Tendril::from_char('o')); t5.apply(&mut state); assert_eq!(state.doc, Rope::from_str("hello")); |