on master help sign in
Remove State from a few more signatures.
commit59e60241864dcf711325109423d74cf6e8b6463d authorBlaž Hrastnik <blaz@mxxn.io> dateThu 18 Mar 2021, 13:45 (6 years ago) parentdbcc099
7 files changed+69−66
helix-core/src/comment.rs+10−8 helix-core/src/indent.rs+13−14 helix-core/src/transaction.rs+13−11 helix-lsp/src/lib.rs+6−9 helix-term/src/commands.rs+25−22 helix-term/src/ui/editor.rs+1−1 helix-view/src/document.rs+1−1
helix-core/src/comment.rs+10−8
@@ -1,4 +1,6 @@
1−use crate::{find_first_non_whitespace_char2, Change, RopeSlice, State, Tendril, Transaction};
1+use crate::{
2+ find_first_non_whitespace_char2, Change, Rope, RopeSlice, Selection, Tendril, Transaction,
3+};
24use core::ops::Range;
35use std::borrow::Cow;
46
@@ -35,14 +37,14 @@ fn find_line_comment(
3537 (commented, skipped, min)
3638}
3739
38−pub fn toggle_line_comments(state: &State) -> Transaction {
39− let text = state.doc.slice(..);
40+pub fn toggle_line_comments(doc: &Rope, selection: &Selection) -> Transaction {
41+ let text = doc.slice(..);
4042 let mut changes: Vec<Change> = Vec::new();
4143
4244 let token = "//";
4345 let comment = Tendril::from(format!("{} ", token));
4446
45− for selection in state.selection.ranges() {
47+ for selection in selection.ranges() {
4648 let start = text.char_to_line(selection.from());
4749 let end = text.char_to_line(selection.to());
4850 let lines = start..end + 1;
@@ -67,7 +69,7 @@ pub fn toggle_line_comments(state: &State) -> Transaction {
6769 }
6870 }
6971 }
70− Transaction::change(&state.doc, changes.into_iter())
72+ Transaction::change(doc, changes.into_iter())
7173}
7274
7375#[cfg(test)]
@@ -76,7 +78,7 @@ mod test {
7678
7779 #[test]
7880 fn test_find_line_comment() {
79− use crate::{Rope, Selection};
81+ use crate::State;
8082
8183 // four lines, two space indented, except for line 1 which is blank.
8284 let doc = Rope::from(" 1\n\n 2\n 3");
@@ -92,13 +94,13 @@ mod test {
9294 assert_eq!(res, (false, vec![1], 2));
9395
9496 // comment
95− let transaction = toggle_line_comments(&state);
97+ let transaction = toggle_line_comments(&state.doc, &state.selection);
9698 transaction.apply(&mut state);
9799
98100 assert_eq!(state.doc, " // 1\n\n // 2\n // 3");
99101
100102 // uncomment
101− let transaction = toggle_line_comments(&state);
103+ let transaction = toggle_line_comments(&state.doc, &state.selection);
102104 transaction.apply(&mut state);
103105 assert_eq!(state.doc, " 1\n\n 2\n 3");
104106
helix-core/src/indent.rs+13−14
@@ -2,7 +2,7 @@ use crate::{
22 find_first_non_whitespace_char,
33 syntax::Syntax,
44 tree_sitter::{Node, Tree},
5− Rope, RopeSlice, State,
5+ Rope, RopeSlice,
66};
77
88/// 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 {
162162 increment as usize
163163}
164164
165−fn suggested_indent_for_line(syntax: Option<&Syntax>, state: &State, line_num: usize) -> usize {
166− let line = state.doc.line(line_num);
165+fn suggested_indent_for_line(syntax: Option<&Syntax>, text: RopeSlice, line_num: usize) -> usize {
166+ let line = text.line(line_num);
167167 let current = indent_level_for_line(line);
168168
169− if let Some(start) = find_first_non_whitespace_char(state.doc.slice(..), line_num) {
170− return suggested_indent_for_pos(syntax, state, start, false);
169+ if let Some(start) = find_first_non_whitespace_char(text, line_num) {
170+ return suggested_indent_for_pos(syntax, text, start, false);
171171 };
172172
173173 // 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
179179// - it should look up the wrapper node and count it too when we press o/O
180180pub fn suggested_indent_for_pos(
181181 syntax: Option<&Syntax>,
182− state: &State,
182+ text: RopeSlice,
183183 pos: usize,
184184 new_line: bool,
185185) -> usize {
186186 if let Some(syntax) = syntax {
187− let byte_start = state.doc.char_to_byte(pos);
187+ let byte_start = text.char_to_byte(pos);
188188 let node = get_highest_syntax_node_at_bytepos(syntax, byte_start);
189189
190190 // TODO: special case for comments
@@ -279,7 +279,7 @@ std::panic::set_hook(Box::new(move |info| {
279279 1
280280}}}
281281
282−pub fn change<I>(state: &State, changes: I) -> Self
282+pub fn change<I>(document: &Document, changes: I) -> Self
283283where
284284 I: IntoIterator<Item = Change> + ExactSizeIterator,
285285{
@@ -288,20 +288,19 @@ where
288288",
289289 );
290290
291− let state = State::new(doc);
292− // TODO: set_language
291+ let doc = Rope::from(doc);
293292 let language_config = crate::syntax::LOADER
294293 .language_config_for_scope("source.rust")
295294 .unwrap();
296295 let highlight_config = language_config.highlight_config(&[]).unwrap();
297− let syntax = Syntax::new(&state.doc, highlight_config.clone());
298− let text = state.doc.slice(..);
296+ let syntax = Syntax::new(&doc, highlight_config.clone());
297+ let text = doc.slice(..);
299298
300− for i in 0..state.doc.len_lines() {
299+ for i in 0..doc.len_lines() {
301300 let line = text.line(i);
302301 let indent = indent_level_for_line(line);
303302 assert_eq!(
304− suggested_indent_for_line(Some(&syntax), &state, i),
303+ suggested_indent_for_line(Some(&syntax), text, i),
305304 indent,
306305 "line {}: {}",
307306 i,
helix-core/src/transaction.rs+13−11
@@ -409,9 +409,9 @@ pub struct Transaction {
409409
410410impl Transaction {
411411 /// Create a new, empty transaction.
412− pub fn new(state: &mut State) -> Self {
412+ pub fn new(doc: &Rope) -> Self {
413413 Self {
414− changes: ChangeSet::new(&state.doc),
414+ changes: ChangeSet::new(doc),
415415 selection: None,
416416 }
417417 }
@@ -490,16 +490,18 @@ impl Transaction {
490490 }
491491
492492 /// Generate a transaction with a change per selection range.
493− pub fn change_by_selection<F>(state: &State, f: F) -> Self
493+ pub fn change_by_selection<F>(doc: &Rope, selection: &Selection, f: F) -> Self
494494 where
495495 F: FnMut(&Range) -> Change,
496496 {
497− Self::change(&state.doc, state.selection.ranges().iter().map(f))
497+ Self::change(doc, selection.ranges().iter().map(f))
498498 }
499499
500500 /// Insert text at each selection head.
501− pub fn insert(state: &State, text: Tendril) -> Self {
502− Self::change_by_selection(state, |range| (range.head, range.head, Some(text.clone())))
501+ pub fn insert(doc: &Rope, selection: &Selection, text: Tendril) -> Self {
502+ Self::change_by_selection(doc, selection, |range| {
503+ (range.head, range.head, Some(text.clone()))
504+ })
503505 }
504506}
505507
@@ -628,15 +630,15 @@ mod test {
628630 #[test]
629631 fn optimized_composition() {
630632 let mut state = State::new("".into());
631− let t1 = Transaction::insert(&state, Tendril::from_char('h'));
633+ let t1 = Transaction::insert(&state.doc, &state.selection, Tendril::from_char('h'));
632634 t1.apply(&mut state);
633− let t2 = Transaction::insert(&state, Tendril::from_char('e'));
635+ let t2 = Transaction::insert(&state.doc, &state.selection, Tendril::from_char('e'));
634636 t2.apply(&mut state);
635− let t3 = Transaction::insert(&state, Tendril::from_char('l'));
637+ let t3 = Transaction::insert(&state.doc, &state.selection, Tendril::from_char('l'));
636638 t3.apply(&mut state);
637− let t4 = Transaction::insert(&state, Tendril::from_char('l'));
639+ let t4 = Transaction::insert(&state.doc, &state.selection, Tendril::from_char('l'));
638640 t4.apply(&mut state);
639− let t5 = Transaction::insert(&state, Tendril::from_char('o'));
641+ let t5 = Transaction::insert(&state.doc, &state.selection, Tendril::from_char('o'));
640642 t5.apply(&mut state);
641643
642644 assert_eq!(state.doc, Rope::from_str("hello"));
helix-lsp/src/lib.rs+6−9
@@ -30,7 +30,7 @@ pub enum Error {
3030
3131pub mod util {
3232 use super::*;
33− use helix_core::{Range, RopeSlice, State, Transaction};
33+ use helix_core::{Range, Rope, RopeSlice, Transaction};
3434
3535 pub fn lsp_pos_to_pos(doc: RopeSlice, pos: lsp::Position) -> usize {
3636 let line = doc.line_to_char(pos.line as usize);
@@ -52,13 +52,10 @@ pub mod util {
5252 lsp::Range::new(start, end)
5353 }
5454
55− pub fn generate_transaction_from_edits(
56− state: &State,
57− edits: Vec<lsp::TextEdit>,
58− ) -> Transaction {
59− let doc = state.doc.slice(..);
55+ pub fn generate_transaction_from_edits(doc: &Rope, edits: Vec<lsp::TextEdit>) -> Transaction {
56+ let text = doc.slice(..); // would be unnecessary if Transaction::change took Rope | RopeSlice
6057 Transaction::change(
61− &state.doc,
58+ doc,
6259 edits.into_iter().map(|edit| {
6360 // simplify "" into None for cleaner changesets
6461 let replacement = if !edit.new_text.is_empty() {
@@ -67,8 +64,8 @@ pub mod util {
6764 None
6865 };
6966
70− let start = lsp_pos_to_pos(doc, edit.range.start);
71− let end = lsp_pos_to_pos(doc, edit.range.end);
67+ let start = lsp_pos_to_pos(text, edit.range.start);
68+ let end = lsp_pos_to_pos(text, edit.range.end);
7269 (start, end, replacement)
7370 }),
7471 )
helix-term/src/commands.rs+25−22
@@ -4,8 +4,8 @@ use helix_core::{
44 movement, object, pos_at_coords,
55 regex::{self, Regex},
66 register, search, selection,
7− state::{Direction, Granularity, State},
8− Change, ChangeSet, Position, Range, RopeSlice, Selection, Tendril, Transaction,
7+ state::{Direction, Granularity},
8+ Change, ChangeSet, Position, Range, Rope, RopeSlice, Selection, Tendril, Transaction,
99};
1010
1111use once_cell::sync::Lazy;
@@ -107,7 +107,7 @@ pub fn move_line_down(cx: &mut Context) {
107107
108108pub fn move_line_end(cx: &mut Context) {
109109 let doc = cx.doc();
110− let lines = selection_lines(&doc.state);
110+ let lines = selection_lines(doc.text(), doc.selection());
111111
112112 let positions = lines
113113 .into_iter()
@@ -127,7 +127,7 @@ pub fn move_line_end(cx: &mut Context) {
127127
128128pub fn move_line_start(cx: &mut Context) {
129129 let doc = cx.doc();
130− let lines = selection_lines(&doc.state);
130+ let lines = selection_lines(doc.text(), doc.selection());
131131
132132 let positions = lines
133133 .into_iter()
@@ -586,8 +586,9 @@ pub fn extend_line(cx: &mut Context) {
586586// heuristic: append changes to history after each command, unless we're in insert mode
587587
588588fn _delete_selection(doc: &mut Document) {
589− let transaction =
590− Transaction::change_by_selection(&doc.state, |range| (range.from(), range.to() + 1, None));
589+ let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
590+ (range.from(), range.to() + 1, None)
591+ });
591592 doc.apply(&transaction);
592593}
593594
@@ -722,12 +723,11 @@ pub fn buffer_picker(cx: &mut Context) {
722723}
723724
724725// calculate line numbers for each selection range
725−fn selection_lines(state: &State) -> Vec<usize> {
726− let mut lines = state
727− .selection
726+fn selection_lines(doc: &Rope, selection: &Selection) -> Vec<usize> {
727+ let mut lines = selection
728728 .ranges()
729729 .iter()
730− .map(|range| state.doc.char_to_line(range.head))
730+ .map(|range| doc.char_to_line(range.head))
731731 .collect::<Vec<_>>();
732732
733733 lines.sort_unstable(); // sorting by usize so _unstable is preferred
@@ -758,19 +758,21 @@ pub fn open_below(cx: &mut Context) {
758758 let doc = cx.doc();
759759 enter_insert_mode(doc);
760760
761− let lines = selection_lines(&doc.state);
761+ let lines = selection_lines(doc.text(), doc.selection());
762762
763763 let positions = lines.into_iter().map(|index| {
764764 // adjust all positions to the end of the line (next line minus one)
765765 doc.text().line_to_char(index + 1).saturating_sub(1)
766766 });
767767
768+ let text = doc.text().slice(..);
769+
768770 let changes: Vec<Change> = positions
769771 .map(|index| {
770772 // TODO: share logic with insert_newline for indentation
771773 let indent_level = helix_core::indent::suggested_indent_for_pos(
772774 doc.syntax.as_ref(),
773− &doc.state,
775+ text,
774776 index,
775777 true,
776778 );
@@ -955,7 +957,7 @@ pub mod insert {
955957 pub fn insert_char(cx: &mut Context, c: char) {
956958 let doc = cx.doc();
957959 let c = Tendril::from_char(c);
958− let transaction = Transaction::insert(&doc.state, c);
960+ let transaction = Transaction::insert(doc.text(), doc.selection(), c);
959961
960962 doc.apply(&transaction);
961963 }
@@ -966,10 +968,11 @@ pub mod insert {
966968
967969 pub fn insert_newline(cx: &mut Context) {
968970 let doc = cx.doc();
969− let transaction = Transaction::change_by_selection(&doc.state, |range| {
971+ let text = doc.text().slice(..);
972+ let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
970973 let indent_level = helix_core::indent::suggested_indent_for_pos(
971974 doc.syntax.as_ref(),
972− &doc.state,
975+ text,
973976 range.head,
974977 true,
975978 );
@@ -987,7 +990,7 @@ pub mod insert {
987990 let count = cx.count;
988991 let doc = cx.doc();
989992 let text = doc.text().slice(..);
990− let transaction = Transaction::change_by_selection(&doc.state, |range| {
993+ let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
991994 (
992995 graphemes::nth_prev_grapheme_boundary(text, range.head, count),
993996 range.head,
@@ -1001,7 +1004,7 @@ pub mod insert {
10011004 let count = cx.count;
10021005 let doc = cx.doc();
10031006 let text = doc.text().slice(..);
1004− let transaction = Transaction::change_by_selection(&doc.state, |range| {
1007+ let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
10051008 (
10061009 range.head,
10071010 graphemes::nth_next_grapheme_boundary(text, range.head, count),
@@ -1076,12 +1079,12 @@ pub fn paste(cx: &mut Context) {
10761079 // paste on the next line
10771080 // TODO: can simply take a range + modifier and compute the right pos without ifs
10781081 let text = doc.text();
1079− Transaction::change_by_selection(&doc.state, |range| {
1082+ Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
10801083 let line_end = text.line_to_char(text.char_to_line(range.head) + 1);
10811084 (line_end, line_end, Some(values.next().unwrap()))
10821085 })
10831086 } else {
1084− Transaction::change_by_selection(&doc.state, |range| {
1087+ Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
10851088 (range.head + 1, range.head + 1, Some(values.next().unwrap()))
10861089 })
10871090 };
@@ -1190,7 +1193,7 @@ pub fn format_selections(cx: &mut Context) {
11901193 ))
11911194 .unwrap_or_default();
11921195
1193− let transaction = helix_lsp::util::generate_transaction_from_edits(&doc.state, edits);
1196+ let transaction = helix_lsp::util::generate_transaction_from_edits(doc.text(), edits);
11941197
11951198 doc.apply(&transaction);
11961199 }
@@ -1352,7 +1355,7 @@ pub fn completion(cx: &mut Context) {
13521355
13531356 // TODO: <-- if state has changed by further input, transaction will panic on len
13541357 let transaction =
1355− util::generate_transaction_from_edits(&doc.state, vec![edit]);
1358+ util::generate_transaction_from_edits(doc.text(), vec![edit]);
13561359 doc.apply(&transaction);
13571360 // TODO: doc.append_changes_to_history(); if not in insert mode?
13581361 }
@@ -1421,7 +1424,7 @@ pub fn next_view(cx: &mut Context) {
14211424// comments
14221425pub fn toggle_comments(cx: &mut Context) {
14231426 let doc = cx.doc();
1424− let transaction = comment::toggle_line_comments(&doc.state);
1427+ let transaction = comment::toggle_line_comments(doc.text(), doc.selection());
14251428
14261429 doc.apply(&transaction);
14271430}
helix-term/src/ui/editor.rs+1−1
@@ -6,7 +6,7 @@ use crate::ui::text_color;
66use helix_core::{
77 indent::TAB_WIDTH,
88 syntax::{self, HighlightEvent},
9− Position, Range, State,
9+ Position, Range,
1010};
1111use helix_view::{document::Mode, Document, Editor, Theme, View};
1212use std::borrow::Cow;
helix-view/src/document.rs+1−1
@@ -346,7 +346,7 @@ mod test {
346346
347347 // insert
348348
349− let transaction = Transaction::insert(&doc.state, " world".into());
349+ let transaction = Transaction::insert(doc.text(), doc.selection(), " world".into());
350350 let old_doc = doc.state.clone();
351351 doc.apply(&transaction);
352352 let changes = Client::changeset_to_changes(&old_doc.doc, doc.text(), transaction.changes());