@@ -4,8 +4,8 @@ use helix_core::{
4 movement, object, pos_at_coords,
4 movement, object, pos_at_coords,
5 regex::{self, Regex},
5 regex::{self, Regex},
6 register, search, selection,
6 register, search, selection,
7− state::{Direction, Granularity, State},
7+ state::{Direction, Granularity},
8− Change, ChangeSet, Position, Range, RopeSlice, Selection, Tendril, Transaction,
8+ Change, ChangeSet, Position, Range, Rope, RopeSlice, Selection, Tendril, Transaction,
11use once_cell::sync::Lazy;
11use once_cell::sync::Lazy;
@@ -107,7 +107,7 @@ pub fn move_line_down(cx: &mut Context) {
108pub fn move_line_end(cx: &mut Context) {
108pub fn move_line_end(cx: &mut Context) {
109 let doc = cx.doc();
109 let doc = cx.doc();
110− let lines = selection_lines(&doc.state);
110+ let lines = selection_lines(doc.text(), doc.selection());
112 let positions = lines
112 let positions = lines
113 .into_iter()
113 .into_iter()
@@ -127,7 +127,7 @@ pub fn move_line_end(cx: &mut Context) {
128pub fn move_line_start(cx: &mut Context) {
128pub fn move_line_start(cx: &mut Context) {
129 let doc = cx.doc();
129 let doc = cx.doc();
130− let lines = selection_lines(&doc.state);
130+ let lines = selection_lines(doc.text(), doc.selection());
132 let positions = lines
132 let positions = lines
133 .into_iter()
133 .into_iter()
@@ -586,8 +586,9 @@ pub fn extend_line(cx: &mut Context) {
586// heuristic: append changes to history after each command, unless we're in insert mode
586// heuristic: append changes to history after each command, unless we're in insert mode
588fn _delete_selection(doc: &mut Document) {
588fn _delete_selection(doc: &mut Document) {
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 doc.apply(&transaction);
592 doc.apply(&transaction);
@@ -722,12 +723,11 @@ pub fn buffer_picker(cx: &mut Context) {
724// calculate line numbers for each selection range
725// calculate line numbers for each selection range
725−fn selection_lines(state: &State) -> Vec<usize> {
726+fn selection_lines(doc: &Rope, selection: &Selection) -> Vec<usize> {
726− let mut lines = state
727− .selection
727+ let mut lines = selection
728 .ranges()
728 .ranges()
730− .map(|range| state.doc.char_to_line(range.head))
730+ .map(|range| doc.char_to_line(range.head))
731 .collect::<Vec<_>>();
731 .collect::<Vec<_>>();
733 lines.sort_unstable(); // sorting by usize so _unstable is preferred
733 lines.sort_unstable(); // sorting by usize so _unstable is preferred
@@ -758,19 +758,21 @@ pub fn open_below(cx: &mut Context) {
758 let doc = cx.doc();
758 let doc = cx.doc();
759 enter_insert_mode(doc);
759 enter_insert_mode(doc);
761− let lines = selection_lines(&doc.state);
761+ let lines = selection_lines(doc.text(), doc.selection());
763 let positions = lines.into_iter().map(|index| {
763 let positions = lines.into_iter().map(|index| {
764 // adjust all positions to the end of the line (next line minus one)
764 // adjust all positions to the end of the line (next line minus one)
765 doc.text().line_to_char(index + 1).saturating_sub(1)
765 doc.text().line_to_char(index + 1).saturating_sub(1)
768+ let text = doc.text().slice(..);
768 let changes: Vec<Change> = positions
770 let changes: Vec<Change> = positions
769 .map(|index| {
771 .map(|index| {
770 // TODO: share logic with insert_newline for indentation
772 // TODO: share logic with insert_newline for indentation
771 let indent_level = helix_core::indent::suggested_indent_for_pos(
773 let indent_level = helix_core::indent::suggested_indent_for_pos(
772 doc.syntax.as_ref(),
774 doc.syntax.as_ref(),
773− &doc.state,
775+ text,
@@ -955,7 +957,7 @@ pub mod insert {
955 pub fn insert_char(cx: &mut Context, c: char) {
957 pub fn insert_char(cx: &mut Context, c: char) {
956 let doc = cx.doc();
958 let doc = cx.doc();
957 let c = Tendril::from_char(c);
959 let c = Tendril::from_char(c);
958− let transaction = Transaction::insert(&doc.state, c);
960+ let transaction = Transaction::insert(doc.text(), doc.selection(), c);
960 doc.apply(&transaction);
962 doc.apply(&transaction);
@@ -966,10 +968,11 @@ pub mod insert {
967 pub fn insert_newline(cx: &mut Context) {
969 pub fn insert_newline(cx: &mut Context) {
968 let doc = cx.doc();
970 let doc = cx.doc();
971+ let text = doc.text().slice(..);
969− let transaction = Transaction::change_by_selection(&doc.state, |range| {
972+ let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
970 let indent_level = helix_core::indent::suggested_indent_for_pos(
973 let indent_level = helix_core::indent::suggested_indent_for_pos(
971 doc.syntax.as_ref(),
974 doc.syntax.as_ref(),
972− &doc.state,
975+ text,
973 range.head,
976 range.head,
@@ -987,7 +990,7 @@ pub mod insert {
987 let count = cx.count;
990 let count = cx.count;
988 let doc = cx.doc();
991 let doc = cx.doc();
989 let text = doc.text().slice(..);
992 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| {
992 graphemes::nth_prev_grapheme_boundary(text, range.head, count),
995 graphemes::nth_prev_grapheme_boundary(text, range.head, count),
993 range.head,
996 range.head,
@@ -1001,7 +1004,7 @@ pub mod insert {
1001 let count = cx.count;
1004 let count = cx.count;
1002 let doc = cx.doc();
1005 let doc = cx.doc();
1003 let text = doc.text().slice(..);
1006 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| {
1006 range.head,
1009 range.head,
1007 graphemes::nth_next_grapheme_boundary(text, range.head, count),
1010 graphemes::nth_next_grapheme_boundary(text, range.head, count),
@@ -1076,12 +1079,12 @@ pub fn paste(cx: &mut Context) {
1076 // paste on the next line
1079 // paste on the next line
1077 // TODO: can simply take a range + modifier and compute the right pos without ifs
1080 // TODO: can simply take a range + modifier and compute the right pos without ifs
1078 let text = doc.text();
1081 let text = doc.text();
1079− Transaction::change_by_selection(&doc.state, |range| {
1082+ Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
1080 let line_end = text.line_to_char(text.char_to_line(range.head) + 1);
1083 let line_end = text.line_to_char(text.char_to_line(range.head) + 1);
1081 (line_end, line_end, Some(values.next().unwrap()))
1084 (line_end, line_end, Some(values.next().unwrap()))
1083 } else {
1086 } else {
1084− Transaction::change_by_selection(&doc.state, |range| {
1087+ Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
1085 (range.head + 1, range.head + 1, Some(values.next().unwrap()))
1088 (range.head + 1, range.head + 1, Some(values.next().unwrap()))
@@ -1190,7 +1193,7 @@ pub fn format_selections(cx: &mut Context) {
1191 .unwrap_or_default();
1194 .unwrap_or_default();
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);
1195 doc.apply(&transaction);
1198 doc.apply(&transaction);
@@ -1352,7 +1355,7 @@ pub fn completion(cx: &mut Context) {
1353 // TODO: <-- if state has changed by further input, transaction will panic on len
1356 // TODO: <-- if state has changed by further input, transaction will panic on len
1354 let transaction =
1357 let transaction =
1355− util::generate_transaction_from_edits(&doc.state, vec![edit]);
1358+ util::generate_transaction_from_edits(doc.text(), vec![edit]);
1356 doc.apply(&transaction);
1359 doc.apply(&transaction);
1357 // TODO: doc.append_changes_to_history(); if not in insert mode?
1360 // TODO: doc.append_changes_to_history(); if not in insert mode?
@@ -1421,7 +1424,7 @@ pub fn next_view(cx: &mut Context) {
1421// comments
1424// comments
1422pub fn toggle_comments(cx: &mut Context) {
1425pub fn toggle_comments(cx: &mut Context) {
1423 let doc = cx.doc();
1426 let doc = cx.doc();
1424− let transaction = comment::toggle_line_comments(&doc.state);
1427+ let transaction = comment::toggle_line_comments(doc.text(), doc.selection());
1426 doc.apply(&transaction);
1429 doc.apply(&transaction);