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