@@ -10,7 +10,7 @@ use helix_core::{
10use once_cell::sync::Lazy;
10use once_cell::sync::Lazy;
12use crate::{
12use crate::{
13− compositor::Compositor,
13+ compositor::{Callback, Compositor},
14 ui::{self, Popup, Prompt, PromptEvent},
14 ui::{self, Popup, Prompt, PromptEvent},
@@ -41,8 +41,8 @@ impl<'a> Context<'a> {
44− pub fn doc(&mut self) -> &mut Document {
44+ pub fn doc(&mut self) -> std::cell::RefMut<Document> {
45− &mut self.editor.view_mut().doc
45+ self.editor.view().doc.borrow_mut()
48 /// Push a new component onto the compositor.
48 /// Push a new component onto the compositor.
@@ -72,7 +72,7 @@ pub type Command = fn(cx: &mut Context);
73pub fn move_char_left(cx: &mut Context) {
73pub fn move_char_left(cx: &mut Context) {
74 let count = cx.count;
74 let count = cx.count;
75− let doc = cx.doc();
75+ let mut doc = cx.doc();
76 let text = doc.text().slice(..);
76 let text = doc.text().slice(..);
77 let selection = doc.selection().transform(|range| {
77 let selection = doc.selection().transform(|range| {
78 movement::move_horizontally(
78 movement::move_horizontally(
@@ -88,7 +88,7 @@ pub fn move_char_left(cx: &mut Context) {
89pub fn move_char_right(cx: &mut Context) {
89pub fn move_char_right(cx: &mut Context) {
90 let count = cx.count;
90 let count = cx.count;
91− let doc = cx.doc();
91+ let mut doc = cx.doc();
92 let text = doc.text().slice(..);
92 let text = doc.text().slice(..);
93 let selection = doc.selection().transform(|range| {
93 let selection = doc.selection().transform(|range| {
94 movement::move_horizontally(
94 movement::move_horizontally(
@@ -104,7 +104,7 @@ pub fn move_char_right(cx: &mut Context) {
105pub fn move_line_up(cx: &mut Context) {
105pub fn move_line_up(cx: &mut Context) {
106 let count = cx.count;
106 let count = cx.count;
107− let doc = cx.doc();
107+ let mut doc = cx.doc();
108 let text = doc.text().slice(..);
108 let text = doc.text().slice(..);
109 let selection = doc.selection().transform(|range| {
109 let selection = doc.selection().transform(|range| {
110 movement::move_vertically(
110 movement::move_vertically(
@@ -120,7 +120,7 @@ pub fn move_line_up(cx: &mut Context) {
121pub fn move_line_down(cx: &mut Context) {
121pub fn move_line_down(cx: &mut Context) {
122 let count = cx.count;
122 let count = cx.count;
123− let doc = cx.doc();
123+ let mut doc = cx.doc();
124 let text = doc.text().slice(..);
124 let text = doc.text().slice(..);
125 let selection = doc.selection().transform(|range| {
125 let selection = doc.selection().transform(|range| {
126 movement::move_vertically(
126 movement::move_vertically(
@@ -135,7 +135,7 @@ pub fn move_line_down(cx: &mut Context) {
137pub fn move_line_end(cx: &mut Context) {
137pub fn move_line_end(cx: &mut Context) {
138− let doc = cx.doc();
138+ let mut doc = cx.doc();
139 let lines = selection_lines(doc.text(), doc.selection());
139 let lines = selection_lines(doc.text(), doc.selection());
141 let positions = lines
141 let positions = lines
@@ -155,7 +155,7 @@ pub fn move_line_end(cx: &mut Context) {
157pub fn move_line_start(cx: &mut Context) {
157pub fn move_line_start(cx: &mut Context) {
158− let doc = cx.doc();
158+ let mut doc = cx.doc();
159 let lines = selection_lines(doc.text(), doc.selection());
159 let lines = selection_lines(doc.text(), doc.selection());
161 let positions = lines
161 let positions = lines
@@ -177,7 +177,7 @@ pub fn move_line_start(cx: &mut Context) {
178pub fn move_next_word_start(cx: &mut Context) {
178pub fn move_next_word_start(cx: &mut Context) {
179 let count = cx.count;
179 let count = cx.count;
180− let doc = cx.doc();
180+ let mut doc = cx.doc();
181 let text = doc.text().slice(..);
181 let text = doc.text().slice(..);
183 let selection = doc.selection().transform(|range| {
183 let selection = doc.selection().transform(|range| {
@@ -190,7 +190,7 @@ pub fn move_next_word_start(cx: &mut Context) {
191pub fn move_prev_word_start(cx: &mut Context) {
191pub fn move_prev_word_start(cx: &mut Context) {
192 let count = cx.count;
192 let count = cx.count;
193− let doc = cx.doc();
193+ let mut doc = cx.doc();
194 let text = doc.text().slice(..);
194 let text = doc.text().slice(..);
196 let selection = doc.selection().transform(|range| {
196 let selection = doc.selection().transform(|range| {
@@ -203,7 +203,7 @@ pub fn move_prev_word_start(cx: &mut Context) {
204pub fn move_next_word_end(cx: &mut Context) {
204pub fn move_next_word_end(cx: &mut Context) {
205 let count = cx.count;
205 let count = cx.count;
206− let doc = cx.doc();
206+ let mut doc = cx.doc();
207 let text = doc.text().slice(..);
207 let text = doc.text().slice(..);
209 let selection = doc.selection().transform(|range| {
209 let selection = doc.selection().transform(|range| {
@@ -215,14 +215,14 @@ pub fn move_next_word_end(cx: &mut Context) {
217pub fn move_file_start(cx: &mut Context) {
217pub fn move_file_start(cx: &mut Context) {
218− let doc = cx.doc();
218+ let mut doc = cx.doc();
219 doc.set_selection(Selection::point(0));
219 doc.set_selection(Selection::point(0));
221 doc.mode = Mode::Normal;
221 doc.mode = Mode::Normal;
224pub fn move_file_end(cx: &mut Context) {
224pub fn move_file_end(cx: &mut Context) {
225− let doc = cx.doc();
225+ let mut doc = cx.doc();
226 let text = doc.text();
226 let text = doc.text();
227 let last_line = text.line_to_char(text.len_lines().saturating_sub(2));
227 let last_line = text.line_to_char(text.len_lines().saturating_sub(2));
228 doc.set_selection(Selection::point(last_line));
228 doc.set_selection(Selection::point(last_line));
@@ -232,7 +232,7 @@ pub fn move_file_end(cx: &mut Context) {
233pub fn extend_next_word_start(cx: &mut Context) {
233pub fn extend_next_word_start(cx: &mut Context) {
234 let count = cx.count;
234 let count = cx.count;
235− let doc = cx.doc();
235+ let mut doc = cx.doc();
236 let text = doc.text().slice(..);
236 let text = doc.text().slice(..);
238 let selection = doc.selection().transform(|mut range| {
238 let selection = doc.selection().transform(|mut range| {
@@ -245,7 +245,7 @@ pub fn extend_next_word_start(cx: &mut Context) {
246pub fn extend_prev_word_start(cx: &mut Context) {
246pub fn extend_prev_word_start(cx: &mut Context) {
247 let count = cx.count;
247 let count = cx.count;
248− let doc = cx.doc();
248+ let mut doc = cx.doc();
249 let text = doc.text().slice(..);
249 let text = doc.text().slice(..);
251 let selection = doc.selection().transform(|mut range| {
251 let selection = doc.selection().transform(|mut range| {
@@ -257,7 +257,7 @@ pub fn extend_prev_word_start(cx: &mut Context) {
258pub fn extend_next_word_end(cx: &mut Context) {
258pub fn extend_next_word_end(cx: &mut Context) {
259 let count = cx.count;
259 let count = cx.count;
260− let doc = cx.doc();
260+ let mut doc = cx.doc();
261 let text = doc.text().slice(..);
261 let text = doc.text().slice(..);
263 let selection = doc.selection().transform(|mut range| {
263 let selection = doc.selection().transform(|mut range| {
@@ -286,7 +286,7 @@ where
287 } = event
287 } = event
289− let doc = cx.doc();
289+ let mut doc = cx.doc();
290 let text = doc.text().slice(..);
290 let text = doc.text().slice(..);
292 let selection = doc.selection().transform(|mut range| {
292 let selection = doc.selection().transform(|mut range| {
@@ -382,11 +382,13 @@ pub fn extend_prev_char(cx: &mut Context) {
383fn scroll(view: &mut View, offset: usize, direction: Direction) {
383fn scroll(view: &mut View, offset: usize, direction: Direction) {
384 use Direction::*;
384 use Direction::*;
385+ // we use short lived borrows since view's methods read from doc too
385− let text = view.doc.text().slice(..);
386+ let doc = view.doc.borrow();
387+ let text = doc.text().slice(..);
388+ let cursor = coords_at_pos(doc.text().slice(..), doc.selection().cursor());
389+ let doc_last_line = doc.text().len_lines() - 1;
387 let last_line = view.last_line();
391 let last_line = view.last_line();
388− let cursor = coords_at_pos(text, view.doc.selection().cursor());
389− let doc_last_line = text.len_lines() - 1;
391 if direction == Backward && view.first_line == 0
393 if direction == Backward && view.first_line == 0
392 || direction == Forward && last_line == doc_last_line
394 || direction == Forward && last_line == doc_last_line
@@ -412,7 +414,8 @@ fn scroll(view: &mut View, offset: usize, direction: Direction) {
414 let pos = pos_at_coords(text, Position::new(line, cursor.col)); // this func will properly truncate to line end
416 let pos = pos_at_coords(text, Position::new(line, cursor.col)); // this func will properly truncate to line end
417+ drop(doc); // upgrade to mutable borrow
415− view.doc.set_selection(Selection::point(pos));
418+ view.doc.borrow_mut().set_selection(Selection::point(pos));
418pub fn page_up(cx: &mut Context) {
421pub fn page_up(cx: &mut Context) {
@@ -437,7 +440,7 @@ pub fn half_page_down(cx: &mut Context) {
438pub fn extend_char_left(cx: &mut Context) {
441pub fn extend_char_left(cx: &mut Context) {
439 let count = cx.count;
442 let count = cx.count;
440− let doc = cx.doc();
443+ let mut doc = cx.doc();
441 let text = doc.text().slice(..);
444 let text = doc.text().slice(..);
442 let selection = doc.selection().transform(|range| {
445 let selection = doc.selection().transform(|range| {
443 movement::move_horizontally(
446 movement::move_horizontally(
@@ -453,7 +456,7 @@ pub fn extend_char_left(cx: &mut Context) {
454pub fn extend_char_right(cx: &mut Context) {
457pub fn extend_char_right(cx: &mut Context) {
455 let count = cx.count;
458 let count = cx.count;
456− let doc = cx.doc();
459+ let mut doc = cx.doc();
457 let text = doc.text().slice(..);
460 let text = doc.text().slice(..);
458 let selection = doc.selection().transform(|range| {
461 let selection = doc.selection().transform(|range| {
459 movement::move_horizontally(
462 movement::move_horizontally(
@@ -469,7 +472,7 @@ pub fn extend_char_right(cx: &mut Context) {
470pub fn extend_line_up(cx: &mut Context) {
473pub fn extend_line_up(cx: &mut Context) {
471 let count = cx.count;
474 let count = cx.count;
472− let doc = cx.doc();
475+ let mut doc = cx.doc();
473 let text = doc.text().slice(..);
476 let text = doc.text().slice(..);
474 let selection = doc.selection().transform(|range| {
477 let selection = doc.selection().transform(|range| {
475 movement::move_vertically(
478 movement::move_vertically(
@@ -485,7 +488,7 @@ pub fn extend_line_up(cx: &mut Context) {
486pub fn extend_line_down(cx: &mut Context) {
489pub fn extend_line_down(cx: &mut Context) {
487 let count = cx.count;
490 let count = cx.count;
488− let doc = cx.doc();
491+ let mut doc = cx.doc();
489 let text = doc.text().slice(..);
492 let text = doc.text().slice(..);
490 let selection = doc.selection().transform(|range| {
493 let selection = doc.selection().transform(|range| {
491 movement::move_vertically(
494 movement::move_vertically(
@@ -500,12 +503,10 @@ pub fn extend_line_down(cx: &mut Context) {
502pub fn select_all(cx: &mut Context) {
505pub fn select_all(cx: &mut Context) {
503− let doc = cx.doc();
506+ let mut doc = cx.doc();
508+ let end = doc.text().len_chars().saturating_sub(1);
505− doc.set_selection(Selection::single(
509+ doc.set_selection(Selection::single(0, end))
507− doc.text().len_chars().saturating_sub(1),
511pub fn select_regex(cx: &mut Context) {
512pub fn select_regex(cx: &mut Context) {
@@ -530,7 +531,7 @@ pub fn split_selection(cx: &mut Context) {
532pub fn split_selection_on_newline(cx: &mut Context) {
533pub fn split_selection_on_newline(cx: &mut Context) {
533− let doc = cx.doc();
534+ let mut doc = cx.doc();
534 let text = doc.text().slice(..);
535 let text = doc.text().slice(..);
535 // only compile the regex once
536 // only compile the regex once
536 #[allow(clippy::trivial_regex)]
537 #[allow(clippy::trivial_regex)]
@@ -562,13 +563,14 @@ fn _search(doc: &mut Document, contents: &str, regex: &Regex) {
563// TODO: use one function for search vs extend
564// TODO: use one function for search vs extend
564pub fn search(cx: &mut Context) {
565pub fn search(cx: &mut Context) {
565− let doc = cx.doc();
566+ let mut doc = cx.doc();
567 // TODO: could probably share with select_on_matches?
568 // TODO: could probably share with select_on_matches?
569 // HAXX: sadly we can't avoid allocating a single string for the whole buffer since we can't
570 // HAXX: sadly we can't avoid allocating a single string for the whole buffer since we can't
570 // feed chunks into the regex yet
571 // feed chunks into the regex yet
571 let contents = doc.text().slice(..).to_string();
572 let contents = doc.text().slice(..).to_string();
573 let prompt = ui::regex_prompt(cx, "search:".to_string(), move |doc, regex| {
575 let prompt = ui::regex_prompt(cx, "search:".to_string(), move |doc, regex| {
574 let text = doc.text();
576 let text = doc.text();
@@ -585,19 +587,20 @@ pub fn search(cx: &mut Context) {
585pub fn search_next(cx: &mut Context) {
587pub fn search_next(cx: &mut Context) {
586 if let Some(query) = register::get('\\') {
588 if let Some(query) = register::get('\\') {
587 let query = query.first().unwrap();
589 let query = query.first().unwrap();
588− let doc = cx.doc();
590+ let mut doc = cx.doc();
589 let contents = doc.text().slice(..).to_string();
591 let contents = doc.text().slice(..).to_string();
590 let regex = Regex::new(&query).unwrap();
592 let regex = Regex::new(&query).unwrap();
591− _search(doc, &contents, ®ex);
593+ _search(&mut doc, &contents, ®ex);
595pub fn search_selection(cx: &mut Context) {
597pub fn search_selection(cx: &mut Context) {
596− let doc = cx.doc();
598+ let mut doc = cx.doc();
597 let contents = doc.text().slice(..);
599 let contents = doc.text().slice(..);
598 let query = doc.selection().primary().fragment(contents);
600 let query = doc.selection().primary().fragment(contents);
599 let regex = regex::escape(&query);
601 let regex = regex::escape(&query);
600 register::set('\\', vec![regex]);
602 register::set('\\', vec![regex]);
601 search_next(cx);
604 search_next(cx);
@@ -609,7 +612,7 @@ pub fn search_selection(cx: &mut Context) {
610pub fn select_line(cx: &mut Context) {
613pub fn select_line(cx: &mut Context) {
611 let count = cx.count;
614 let count = cx.count;
612− let doc = cx.doc();
615+ let mut doc = cx.doc();
614 let pos = doc.selection().primary();
617 let pos = doc.selection().primary();
615 let text = doc.text();
618 let text = doc.text();
@@ -622,7 +625,7 @@ pub fn select_line(cx: &mut Context) {
623pub fn extend_line(cx: &mut Context) {
626pub fn extend_line(cx: &mut Context) {
624 let count = cx.count;
627 let count = cx.count;
625− let doc = cx.doc();
628+ let mut doc = cx.doc();
627 let pos = doc.selection().primary();
630 let pos = doc.selection().primary();
628 let text = doc.text();
631 let text = doc.text();
@@ -650,20 +653,21 @@ fn _delete_selection(doc: &mut Document) {
652pub fn delete_selection(cx: &mut Context) {
655pub fn delete_selection(cx: &mut Context) {
653− let doc = cx.doc();
656+ let mut doc = cx.doc();
654− _delete_selection(doc);
657+ _delete_selection(&mut doc);
656 doc.append_changes_to_history();
659 doc.append_changes_to_history();
659pub fn change_selection(cx: &mut Context) {
662pub fn change_selection(cx: &mut Context) {
660− let doc = cx.doc();
663+ let mut doc = cx.doc();
661− _delete_selection(doc);
664+ _delete_selection(&mut doc);
662 insert_mode(cx);
666 insert_mode(cx);
665pub fn collapse_selection(cx: &mut Context) {
669pub fn collapse_selection(cx: &mut Context) {
666− let doc = cx.doc();
670+ let mut doc = cx.doc();
667 let selection = doc
671 let selection = doc
668 .selection()
672 .selection()
669 .transform(|range| Range::new(range.head, range.head));
673 .transform(|range| Range::new(range.head, range.head));
@@ -672,7 +676,7 @@ pub fn collapse_selection(cx: &mut Context) {
674pub fn flip_selections(cx: &mut Context) {
678pub fn flip_selections(cx: &mut Context) {
675− let doc = cx.doc();
679+ let mut doc = cx.doc();
676 let selection = doc
680 let selection = doc
677 .selection()
681 .selection()
678 .transform(|range| Range::new(range.head, range.anchor));
682 .transform(|range| Range::new(range.head, range.anchor));
@@ -686,8 +690,8 @@ fn enter_insert_mode(doc: &mut Document) {
687// inserts at the start of each selection
691// inserts at the start of each selection
688pub fn insert_mode(cx: &mut Context) {
692pub fn insert_mode(cx: &mut Context) {
689− let doc = cx.doc();
693+ let mut doc = cx.doc();
690− enter_insert_mode(doc);
694+ enter_insert_mode(&mut doc);
692 let selection = doc
696 let selection = doc
693 .selection()
697 .selection()
@@ -697,8 +701,8 @@ pub fn insert_mode(cx: &mut Context) {
698// inserts at the end of each selection
702// inserts at the end of each selection
699pub fn append_mode(cx: &mut Context) {
703pub fn append_mode(cx: &mut Context) {
700− let doc = cx.doc();
704+ let mut doc = cx.doc();
701− enter_insert_mode(doc);
705+ enter_insert_mode(&mut doc);
702 doc.restore_cursor = true;
706 doc.restore_cursor = true;
704 let text = doc.text().slice(..);
708 let text = doc.text().slice(..);
@@ -765,7 +769,7 @@ pub fn command_mode(cx: &mut Context) {
766 ["w"] | ["write"] => {
770 ["w"] | ["write"] => {
767 // TODO: non-blocking via save() command
771 // TODO: non-blocking via save() command
768− smol::block_on(editor.view_mut().doc.save());
772+ smol::block_on(editor.view().doc.borrow().save());
@@ -800,16 +804,18 @@ fn selection_lines(doc: &Rope, selection: &Selection) -> Vec<usize> {
801// I inserts at the start of each line with a selection
805// I inserts at the start of each line with a selection
802pub fn prepend_to_line(cx: &mut Context) {
806pub fn prepend_to_line(cx: &mut Context) {
803− let doc = cx.doc();
807+ let mut doc = cx.doc();
804− enter_insert_mode(doc);
808+ enter_insert_mode(&mut doc);
806 move_line_start(cx);
811 move_line_start(cx);
809// A inserts at the end of each line with a selection
814// A inserts at the end of each line with a selection
810pub fn append_to_line(cx: &mut Context) {
815pub fn append_to_line(cx: &mut Context) {
811− let doc = cx.doc();
816+ let mut doc = cx.doc();
812− enter_insert_mode(doc);
817+ enter_insert_mode(&mut doc);
814 move_line_end(cx);
820 move_line_end(cx);
@@ -817,8 +823,8 @@ pub fn append_to_line(cx: &mut Context) {
817// o inserts a new line after each line with a selection
823// o inserts a new line after each line with a selection
818pub fn open_below(cx: &mut Context) {
824pub fn open_below(cx: &mut Context) {
819 let count = cx.count;
825 let count = cx.count;
820− let doc = cx.doc();
826+ let mut doc = cx.doc();
821− enter_insert_mode(doc);
827+ enter_insert_mode(&mut doc);
823 let lines = selection_lines(doc.text(), doc.selection());
829 let lines = selection_lines(doc.text(), doc.selection());
@@ -875,7 +881,7 @@ pub fn open_below(cx: &mut Context) {
875// O inserts a new line before each line with a selection
881// O inserts a new line before each line with a selection
877pub fn normal_mode(cx: &mut Context) {
883pub fn normal_mode(cx: &mut Context) {
878− let doc = cx.doc();
884+ let mut doc = cx.doc();
880 doc.mode = Mode::Normal;
886 doc.mode = Mode::Normal;
@@ -909,14 +915,12 @@ pub fn exit_select_mode(cx: &mut Context) {
911fn goto(cx: &mut Context, locations: Vec<lsp::Location>) {
917fn goto(cx: &mut Context, locations: Vec<lsp::Location>) {
914− doc.mode = Mode::Normal;
918+ cx.doc().mode = Mode::Normal;
916 match locations.as_slice() {
920 match locations.as_slice() {
917 [location] => {
921 [location] => {
918 cx.editor.open(PathBuf::from(location.uri.path()));
922 cx.editor.open(PathBuf::from(location.uri.path()));
919− let doc = cx.doc();
923+ let mut doc = cx.doc();
920 let definition_pos = location.range.start;
924 let definition_pos = location.range.start;
921 let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text(), definition_pos);
925 let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text(), definition_pos);
922 doc.set_selection(Selection::point(new_pos));
926 doc.set_selection(Selection::point(new_pos));
@@ -932,7 +936,8 @@ fn goto(cx: &mut Context, locations: Vec<lsp::Location>) {
933 move |editor: &mut Editor, item| {
937 move |editor: &mut Editor, item| {
934 editor.open(PathBuf::from(item.uri.path()));
938 editor.open(PathBuf::from(item.uri.path()));
939+ // TODO: issues with doc already being broo
935− let mut doc = &mut editor.view_mut().doc;
940+ let mut doc = &mut editor.view().doc.borrow_mut();
936 let definition_pos = item.range.start;
941 let definition_pos = item.range.start;
937 let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text(), definition_pos);
942 let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text(), definition_pos);
938 doc.set_selection(Selection::point(new_pos));
943 doc.set_selection(Selection::point(new_pos));
@@ -944,7 +949,7 @@ fn goto(cx: &mut Context, locations: Vec<lsp::Location>) {
946pub fn goto_definition(cx: &mut Context) {
951pub fn goto_definition(cx: &mut Context) {
947− let doc = cx.doc();
952+ let mut doc = cx.doc();
948 let language_server = match doc.language_server() {
953 let language_server = match doc.language_server() {
949 Some(language_server) => language_server,
954 Some(language_server) => language_server,
950 None => return,
955 None => return,
@@ -956,11 +961,12 @@ pub fn goto_definition(cx: &mut Context) {
956 // TODO: handle fails
961 // TODO: handle fails
957 let res =
962 let res =
958 smol::block_on(language_server.goto_definition(doc.identifier(), pos)).unwrap_or_default();
963 smol::block_on(language_server.goto_definition(doc.identifier(), pos)).unwrap_or_default();
959 goto(cx, res);
965 goto(cx, res);
962pub fn goto_type_definition(cx: &mut Context) {
968pub fn goto_type_definition(cx: &mut Context) {
963− let doc = cx.doc();
969+ let mut doc = cx.doc();
964 let language_server = match doc.language_server() {
970 let language_server = match doc.language_server() {
965 Some(language_server) => language_server,
971 Some(language_server) => language_server,
966 None => return,
972 None => return,
@@ -972,11 +978,12 @@ pub fn goto_type_definition(cx: &mut Context) {
972 // TODO: handle fails
978 // TODO: handle fails
973 let res = smol::block_on(language_server.goto_type_definition(doc.identifier(), pos))
979 let res = smol::block_on(language_server.goto_type_definition(doc.identifier(), pos))
974 .unwrap_or_default();
980 .unwrap_or_default();
975 goto(cx, res);
982 goto(cx, res);
978pub fn goto_implementation(cx: &mut Context) {
985pub fn goto_implementation(cx: &mut Context) {
979− let doc = cx.doc();
986+ let mut doc = cx.doc();
980 let language_server = match doc.language_server() {
987 let language_server = match doc.language_server() {
981 Some(language_server) => language_server,
988 Some(language_server) => language_server,
982 None => return,
989 None => return,
@@ -988,11 +995,12 @@ pub fn goto_implementation(cx: &mut Context) {
988 // TODO: handle fails
995 // TODO: handle fails
989 let res = smol::block_on(language_server.goto_implementation(doc.identifier(), pos))
996 let res = smol::block_on(language_server.goto_implementation(doc.identifier(), pos))
990 .unwrap_or_default();
997 .unwrap_or_default();
991 goto(cx, res);
999 goto(cx, res);
994pub fn goto_reference(cx: &mut Context) {
1002pub fn goto_reference(cx: &mut Context) {
995− let doc = cx.doc();
1003+ let mut doc = cx.doc();
996 let language_server = match doc.language_server() {
1004 let language_server = match doc.language_server() {
997 Some(language_server) => language_server,
1005 Some(language_server) => language_server,
998 None => return,
1006 None => return,
@@ -1004,6 +1012,7 @@ pub fn goto_reference(cx: &mut Context) {
1004 // TODO: handle fails
1012 // TODO: handle fails
1005 let res =
1013 let res =
1006 smol::block_on(language_server.goto_reference(doc.identifier(), pos)).unwrap_or_default();
1014 smol::block_on(language_server.goto_reference(doc.identifier(), pos)).unwrap_or_default();
1007 goto(cx, res);
1016 goto(cx, res);
@@ -1017,7 +1026,7 @@ pub mod insert {
1018 // TODO: insert means add text just before cursor, on exit we should be on the last letter.
1027 // TODO: insert means add text just before cursor, on exit we should be on the last letter.
1019 pub fn insert_char(cx: &mut Context, c: char) {
1028 pub fn insert_char(cx: &mut Context, c: char) {
1020− let doc = cx.doc();
1029+ let mut doc = cx.doc();
1022 // run through insert hooks, stopping on the first one that returns Some(t)
1031 // run through insert hooks, stopping on the first one that returns Some(t)
1023 for hook in HOOKS {
1032 for hook in HOOKS {
@@ -1034,7 +1043,7 @@ pub mod insert {
1036 pub fn insert_tab(cx: &mut Context) {
1045 pub fn insert_tab(cx: &mut Context) {
1037− let doc = cx.doc();
1046+ let mut doc = cx.doc();
1038 // TODO: round out to nearest indentation level (for example a line with 3 spaces should
1047 // TODO: round out to nearest indentation level (for example a line with 3 spaces should
1039 // indent by one to reach 4 spaces).
1048 // indent by one to reach 4 spaces).
@@ -1044,7 +1053,7 @@ pub mod insert {
1046 pub fn insert_newline(cx: &mut Context) {
1055 pub fn insert_newline(cx: &mut Context) {
1047− let doc = cx.doc();
1056+ let mut doc = cx.doc();
1048 let text = doc.text().slice(..);
1057 let text = doc.text().slice(..);
1049 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
1058 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
1050 let indent_level =
1059 let indent_level =
@@ -1061,7 +1070,7 @@ pub mod insert {
1061 // TODO: handle indent-aware delete
1070 // TODO: handle indent-aware delete
1062 pub fn delete_char_backward(cx: &mut Context) {
1071 pub fn delete_char_backward(cx: &mut Context) {
1063 let count = cx.count;
1072 let count = cx.count;
1064− let doc = cx.doc();
1073+ let mut doc = cx.doc();
1065 let text = doc.text().slice(..);
1074 let text = doc.text().slice(..);
1066 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
1075 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
@@ -1075,7 +1084,7 @@ pub mod insert {
1076 pub fn delete_char_forward(cx: &mut Context) {
1085 pub fn delete_char_forward(cx: &mut Context) {
1077 let count = cx.count;
1086 let count = cx.count;
1078− let doc = cx.doc();
1087+ let mut doc = cx.doc();
1079 let text = doc.text().slice(..);
1088 let text = doc.text().slice(..);
1080 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
1089 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
@@ -1105,7 +1114,7 @@ pub fn redo(cx: &mut Context) {
1106pub fn yank(cx: &mut Context) {
1115pub fn yank(cx: &mut Context) {
1107 // TODO: should selections be made end inclusive?
1116 // TODO: should selections be made end inclusive?
1108− let doc = cx.doc();
1117+ let mut doc = cx.doc();
1109 let values = doc
1118 let values = doc
1110 .selection()
1119 .selection()
1111 .fragments(doc.text().slice(..))
1120 .fragments(doc.text().slice(..))
@@ -1146,7 +1155,7 @@ pub fn paste(cx: &mut Context) {
1147 let mut values = values.into_iter().map(Tendril::from).chain(repeat);
1156 let mut values = values.into_iter().map(Tendril::from).chain(repeat);
1149− let doc = cx.doc();
1158+ let mut doc = cx.doc();
1151 let transaction = if linewise {
1160 let transaction = if linewise {
1152 // paste on the next line
1161 // paste on the next line
@@ -1185,8 +1194,8 @@ fn get_lines(doc: &Document) -> Vec<usize> {
1187pub fn indent(cx: &mut Context) {
1196pub fn indent(cx: &mut Context) {
1188− let doc = cx.doc();
1197+ let mut doc = cx.doc();
1189− let lines = get_lines(doc);
1198+ let lines = get_lines(&mut doc);
1191 // Indent by one level
1200 // Indent by one level
1192 let indent = Tendril::from(doc.indent_unit());
1201 let indent = Tendril::from(doc.indent_unit());
@@ -1203,8 +1212,8 @@ pub fn indent(cx: &mut Context) {
1205pub fn unindent(cx: &mut Context) {
1214pub fn unindent(cx: &mut Context) {
1206− let doc = cx.doc();
1215+ let mut doc = cx.doc();
1207− let lines = get_lines(doc);
1216+ let lines = get_lines(&mut doc);
1208 let mut changes = Vec::with_capacity(lines.len());
1217 let mut changes = Vec::with_capacity(lines.len());
1209 let tab_width = doc.tab_width();
1218 let tab_width = doc.tab_width();
@@ -1238,7 +1247,7 @@ pub fn unindent(cx: &mut Context) {
1239pub fn format_selections(cx: &mut Context) {
1248pub fn format_selections(cx: &mut Context) {
1240 use helix_lsp::lsp;
1249 use helix_lsp::lsp;
1241− let doc = cx.doc();
1250+ let mut doc = cx.doc();
1243 // via lsp if available
1252 // via lsp if available
1244 // else via tree-sitter indentation calculations
1253 // else via tree-sitter indentation calculations
@@ -1276,7 +1285,7 @@ pub fn format_selections(cx: &mut Context) {
1277pub fn join_selections(cx: &mut Context) {
1286pub fn join_selections(cx: &mut Context) {
1278 use movement::skip_over_next;
1287 use movement::skip_over_next;
1279− let doc = cx.doc();
1288+ let mut doc = cx.doc();
1280 let text = doc.text();
1289 let text = doc.text();
1281 let slice = doc.text().slice(..);
1290 let slice = doc.text().slice(..);
@@ -1319,7 +1328,6 @@ pub fn join_selections(cx: &mut Context) {
1321pub fn keep_selections(cx: &mut Context) {
1330pub fn keep_selections(cx: &mut Context) {
1322− let doc = cx.doc();
1323 // keep selections matching regex
1331 // keep selections matching regex
1324 let prompt = ui::regex_prompt(cx, "keep:".to_string(), |doc, regex| {
1332 let prompt = ui::regex_prompt(cx, "keep:".to_string(), |doc, regex| {
1325 let text = doc.text().slice(..);
1333 let text = doc.text().slice(..);
@@ -1333,7 +1341,7 @@ pub fn keep_selections(cx: &mut Context) {
1335pub fn keep_primary_selection(cx: &mut Context) {
1343pub fn keep_primary_selection(cx: &mut Context) {
1336− let doc = cx.doc();
1344+ let mut doc = cx.doc();
1338 let range = doc.selection().primary();
1346 let range = doc.selection().primary();
1339 let selection = Selection::single(range.anchor, range.head);
1347 let selection = Selection::single(range.anchor, range.head);
@@ -1350,7 +1358,7 @@ pub fn save(cx: &mut Context) {
1352pub fn completion(cx: &mut Context) {
1360pub fn completion(cx: &mut Context) {
1353− let doc = cx.doc();
1361+ let mut doc = cx.doc();
1355 let language_server = match doc.language_server() {
1363 let language_server = match doc.language_server() {
1356 Some(language_server) => language_server,
1364 Some(language_server) => language_server,
@@ -1364,6 +1372,8 @@ pub fn completion(cx: &mut Context) {
1365 let res = smol::block_on(language_server.completion(doc.identifier(), pos)).unwrap_or_default();
1373 let res = smol::block_on(language_server.completion(doc.identifier(), pos)).unwrap_or_default();
1367 // TODO: if no completion, show some message or something
1377 // TODO: if no completion, show some message or something
1368 if !res.is_empty() {
1378 if !res.is_empty() {
1369 // let snapshot = doc.state.clone();
1379 // let snapshot = doc.state.clone();
@@ -1379,11 +1389,11 @@ pub fn completion(cx: &mut Context) {
1379 match event {
1389 match event {
1380 PromptEvent::Abort => {
1390 PromptEvent::Abort => {
1381 // revert state
1391 // revert state
1382− let doc = &mut editor.view_mut().doc;
1392+ let doc = &mut editor.view().doc.borrow_mut();
1383 // doc.state = snapshot.clone();
1393 // doc.state = snapshot.clone();
1385 PromptEvent::Validate => {
1395 PromptEvent::Validate => {
1386− let doc = &mut editor.view_mut().doc;
1396+ let doc = &mut editor.view().doc.borrow_mut();
1388 // revert state to what it was before the last update
1398 // revert state to what it was before the last update
1389 // doc.state = snapshot.clone();
1399 // doc.state = snapshot.clone();
@@ -1449,7 +1459,7 @@ pub fn completion(cx: &mut Context) {
1449pub fn hover(cx: &mut Context) {
1459pub fn hover(cx: &mut Context) {
1450 use helix_lsp::lsp;
1460 use helix_lsp::lsp;
1452− let doc = cx.doc();
1462+ let mut doc = cx.doc();
1454 let language_server = match doc.language_server() {
1464 let language_server = match doc.language_server() {
1455 Some(language_server) => language_server,
1465 Some(language_server) => language_server,
@@ -1466,6 +1476,8 @@ pub fn hover(cx: &mut Context) {
1466 let res = smol::block_on(language_server.text_document_hover(doc.identifier(), pos))
1476 let res = smol::block_on(language_server.text_document_hover(doc.identifier(), pos))
1467 .unwrap_or_default();
1477 .unwrap_or_default();
1469 if let Some(hover) = res {
1481 if let Some(hover) = res {
1470 // hover.contents / .range <- used for visualizing
1482 // hover.contents / .range <- used for visualizing
1471 let contents = match hover.contents {
1483 let contents = match hover.contents {
@@ -1491,12 +1503,12 @@ pub fn hover(cx: &mut Context) {
1492// view movements
1504// view movements
1493pub fn next_view(cx: &mut Context) {
1505pub fn next_view(cx: &mut Context) {
1494− cx.editor.tree.focus_next()
1506+ cx.editor.focus_next()
1497// comments
1509// comments
1498pub fn toggle_comments(cx: &mut Context) {
1510pub fn toggle_comments(cx: &mut Context) {
1499− let doc = cx.doc();
1511+ let mut doc = cx.doc();
1500 let transaction = comment::toggle_line_comments(doc.text(), doc.selection());
1512 let transaction = comment::toggle_line_comments(doc.text(), doc.selection());
1502 doc.apply(&transaction);
1514 doc.apply(&transaction);
@@ -1506,7 +1518,7 @@ pub fn toggle_comments(cx: &mut Context) {
1506// tree sitter node selection
1518// tree sitter node selection
1508pub fn expand_selection(cx: &mut Context) {
1520pub fn expand_selection(cx: &mut Context) {
1509− let doc = cx.doc();
1521+ let mut doc = cx.doc();
1511 if let Some(syntax) = doc.syntax() {
1523 if let Some(syntax) = doc.syntax() {
1512 let text = doc.text().slice(..);
1524 let text = doc.text().slice(..);