@@ -41,8 +41,9 @@ impl<'a> Context<'a> {
4141 }
4242
4343 #[inline]
44− pub fn doc(&mut self) -> std::cell::RefMut<Document> {
45− self.editor.view().doc.borrow_mut()
44+ pub fn doc(&mut self) -> &mut Document {
45+ let id = self.editor.view().doc;
46+ &mut self.editor.documents[id]
4647 }
4748
4849 /// Push a new component onto the compositor.
@@ -72,7 +73,7 @@ pub type Command = fn(cx: &mut Context);
7273
7374pub fn move_char_left(cx: &mut Context) {
7475 let count = cx.count;
75− let mut doc = cx.doc();
76+ let doc = cx.doc();
7677 let text = doc.text().slice(..);
7778 let selection = doc.selection().transform(|range| {
7879 movement::move_horizontally(
@@ -88,7 +89,7 @@ pub fn move_char_left(cx: &mut Context) {
8889
8990pub fn move_char_right(cx: &mut Context) {
9091 let count = cx.count;
91− let mut doc = cx.doc();
92+ let doc = cx.doc();
9293 let text = doc.text().slice(..);
9394 let selection = doc.selection().transform(|range| {
9495 movement::move_horizontally(
@@ -104,7 +105,7 @@ pub fn move_char_right(cx: &mut Context) {
104105
105106pub fn move_line_up(cx: &mut Context) {
106107 let count = cx.count;
107− let mut doc = cx.doc();
108+ let doc = cx.doc();
108109 let text = doc.text().slice(..);
109110 let selection = doc.selection().transform(|range| {
110111 movement::move_vertically(
@@ -120,7 +121,7 @@ pub fn move_line_up(cx: &mut Context) {
120121
121122pub fn move_line_down(cx: &mut Context) {
122123 let count = cx.count;
123− let mut doc = cx.doc();
124+ let doc = cx.doc();
124125 let text = doc.text().slice(..);
125126 let selection = doc.selection().transform(|range| {
126127 movement::move_vertically(
@@ -135,7 +136,7 @@ pub fn move_line_down(cx: &mut Context) {
135136}
136137
137138pub fn move_line_end(cx: &mut Context) {
138− let mut doc = cx.doc();
139+ let doc = cx.doc();
139140 let lines = selection_lines(doc.text(), doc.selection());
140141
141142 let positions = lines
@@ -155,7 +156,7 @@ pub fn move_line_end(cx: &mut Context) {
155156}
156157
157158pub fn move_line_start(cx: &mut Context) {
158− let mut doc = cx.doc();
159+ let doc = cx.doc();
159160 let lines = selection_lines(doc.text(), doc.selection());
160161
161162 let positions = lines
@@ -177,7 +178,7 @@ pub fn move_line_start(cx: &mut Context) {
177178
178179pub fn move_next_word_start(cx: &mut Context) {
179180 let count = cx.count;
180− let mut doc = cx.doc();
181+ let doc = cx.doc();
181182 let text = doc.text().slice(..);
182183
183184 let selection = doc.selection().transform(|range| {
@@ -190,7 +191,7 @@ pub fn move_next_word_start(cx: &mut Context) {
190191
191192pub fn move_prev_word_start(cx: &mut Context) {
192193 let count = cx.count;
193− let mut doc = cx.doc();
194+ let doc = cx.doc();
194195 let text = doc.text().slice(..);
195196
196197 let selection = doc.selection().transform(|range| {
@@ -203,7 +204,7 @@ pub fn move_prev_word_start(cx: &mut Context) {
203204
204205pub fn move_next_word_end(cx: &mut Context) {
205206 let count = cx.count;
206− let mut doc = cx.doc();
207+ let doc = cx.doc();
207208 let text = doc.text().slice(..);
208209
209210 let selection = doc.selection().transform(|range| {
@@ -215,14 +216,14 @@ pub fn move_next_word_end(cx: &mut Context) {
215216}
216217
217218pub fn move_file_start(cx: &mut Context) {
218− let mut doc = cx.doc();
219+ let doc = cx.doc();
219220 doc.set_selection(Selection::point(0));
220221
221222 doc.mode = Mode::Normal;
222223}
223224
224225pub fn move_file_end(cx: &mut Context) {
225− let mut doc = cx.doc();
226+ let doc = cx.doc();
226227 let text = doc.text();
227228 let last_line = text.line_to_char(text.len_lines().saturating_sub(2));
228229 doc.set_selection(Selection::point(last_line));
@@ -232,7 +233,7 @@ pub fn move_file_end(cx: &mut Context) {
232233
233234pub fn extend_next_word_start(cx: &mut Context) {
234235 let count = cx.count;
235− let mut doc = cx.doc();
236+ let doc = cx.doc();
236237 let text = doc.text().slice(..);
237238
238239 let selection = doc.selection().transform(|mut range| {
@@ -245,7 +246,7 @@ pub fn extend_next_word_start(cx: &mut Context) {
245246
246247pub fn extend_prev_word_start(cx: &mut Context) {
247248 let count = cx.count;
248− let mut doc = cx.doc();
249+ let doc = cx.doc();
249250 let text = doc.text().slice(..);
250251
251252 let selection = doc.selection().transform(|mut range| {
@@ -257,7 +258,7 @@ pub fn extend_prev_word_start(cx: &mut Context) {
257258
258259pub fn extend_next_word_end(cx: &mut Context) {
259260 let count = cx.count;
260− let mut doc = cx.doc();
261+ let doc = cx.doc();
261262 let text = doc.text().slice(..);
262263
263264 let selection = doc.selection().transform(|mut range| {
@@ -286,7 +287,7 @@ where
286287 ..
287288 } = event
288289 {
289− let mut doc = cx.doc();
290+ let doc = cx.doc();
290291 let text = doc.text().slice(..);
291292
292293 let selection = doc.selection().transform(|mut range| {
@@ -390,7 +391,7 @@ pub fn replace(cx: &mut Context) {
390391 {
391392 let text = Tendril::from_char(ch);
392393
393− let mut doc = cx.doc();
394+ let doc = cx.doc();
394395
395396 let transaction =
396397 Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
@@ -403,15 +404,14 @@ pub fn replace(cx: &mut Context) {
403404 })
404405}
405406
406−fn scroll(view: &mut View, offset: usize, direction: Direction) {
407+fn scroll(cx: &mut Context, offset: usize, direction: Direction) {
407408 use Direction::*;
408− // we use short lived borrows since view's methods read from doc too
409− let doc = view.doc.borrow();
410− let text = doc.text().slice(..);
409+ let view = cx.editor.view();
410+ let doc = cx.editor.document(view.doc).unwrap();
411411 let cursor = coords_at_pos(doc.text().slice(..), doc.selection().cursor());
412412 let doc_last_line = doc.text().len_lines() - 1;
413413
414− let last_line = view.last_line();
414+ let last_line = view.last_line(doc);
415415
416416 if direction == Backward && view.first_line == 0
417417 || direction == Forward && last_line == doc_last_line
@@ -424,6 +424,9 @@ fn scroll(view: &mut View, offset: usize, direction: Direction) {
424424 // cursor visual offset
425425 let cursor_off = cursor.row - view.first_line;
426426
427+ // upgrade to mut reference
428+ let view = cx.editor.view_mut();
429+
427430 view.first_line = match direction {
428431 Forward => view.first_line + offset,
429432 Backward => view.first_line.saturating_sub(offset),
@@ -433,37 +436,47 @@ fn scroll(view: &mut View, offset: usize, direction: Direction) {
433436 // clamp into viewport
434437 let line = (view.first_line + cursor_off).clamp(
435438 view.first_line + scrolloff,
436− view.last_line().saturating_sub(scrolloff),
439+ last_line.saturating_sub(scrolloff),
437440 );
438441
442+ // view drops here
443+
444+ // upgrade to mut reference
445+ let doc = cx.doc();
446+
447+ let text = doc.text().slice(..);
439448 let pos = pos_at_coords(text, Position::new(line, cursor.col)); // this func will properly truncate to line end
440− drop(doc); // upgrade to mutable borrow
441− view.doc.borrow_mut().set_selection(Selection::point(pos));
449+
450+ doc.set_selection(Selection::point(pos));
442451}
443452
444453pub fn page_up(cx: &mut Context) {
445454 let view = cx.view();
446− scroll(view, view.area.height as usize, Direction::Backward);
455+ let offset = view.area.height as usize;
456+ scroll(cx, offset, Direction::Backward);
447457}
448458
449459pub fn page_down(cx: &mut Context) {
450460 let view = cx.view();
451− scroll(view, view.area.height as usize, Direction::Forward);
461+ let offset = view.area.height as usize;
462+ scroll(cx, offset, Direction::Forward);
452463}
453464
454465pub fn half_page_up(cx: &mut Context) {
455466 let view = cx.view();
456− scroll(view, view.area.height as usize / 2, Direction::Backward);
467+ let offset = view.area.height as usize / 2;
468+ scroll(cx, offset, Direction::Backward);
457469}
458470
459471pub fn half_page_down(cx: &mut Context) {
460472 let view = cx.view();
461− scroll(view, view.area.height as usize / 2, Direction::Forward);
473+ let offset = view.area.height as usize / 2;
474+ scroll(cx, offset, Direction::Forward);
462475}
463476
464477pub fn extend_char_left(cx: &mut Context) {
465478 let count = cx.count;
466− let mut doc = cx.doc();
479+ let doc = cx.doc();
467480 let text = doc.text().slice(..);
468481 let selection = doc.selection().transform(|range| {
469482 movement::move_horizontally(
@@ -479,7 +492,7 @@ pub fn extend_char_left(cx: &mut Context) {
479492
480493pub fn extend_char_right(cx: &mut Context) {
481494 let count = cx.count;
482− let mut doc = cx.doc();
495+ let doc = cx.doc();
483496 let text = doc.text().slice(..);
484497 let selection = doc.selection().transform(|range| {
485498 movement::move_horizontally(
@@ -495,7 +508,7 @@ pub fn extend_char_right(cx: &mut Context) {
495508
496509pub fn extend_line_up(cx: &mut Context) {
497510 let count = cx.count;
498− let mut doc = cx.doc();
511+ let doc = cx.doc();
499512 let text = doc.text().slice(..);
500513 let selection = doc.selection().transform(|range| {
501514 movement::move_vertically(
@@ -511,7 +524,7 @@ pub fn extend_line_up(cx: &mut Context) {
511524
512525pub fn extend_line_down(cx: &mut Context) {
513526 let count = cx.count;
514− let mut doc = cx.doc();
527+ let doc = cx.doc();
515528 let text = doc.text().slice(..);
516529 let selection = doc.selection().transform(|range| {
517530 movement::move_vertically(
@@ -526,7 +539,7 @@ pub fn extend_line_down(cx: &mut Context) {
526539}
527540
528541pub fn select_all(cx: &mut Context) {
529− let mut doc = cx.doc();
542+ let doc = cx.doc();
530543
531544 let end = doc.text().len_chars().saturating_sub(1);
532545 doc.set_selection(Selection::single(0, end))
@@ -554,7 +567,7 @@ pub fn split_selection(cx: &mut Context) {
554567}
555568
556569pub fn split_selection_on_newline(cx: &mut Context) {
557− let mut doc = cx.doc();
570+ let doc = cx.doc();
558571 let text = doc.text().slice(..);
559572 // only compile the regex once
560573 #[allow(clippy::trivial_regex)]
@@ -586,14 +599,13 @@ fn _search(doc: &mut Document, contents: &str, regex: &Regex) {
586599
587600// TODO: use one function for search vs extend
588601pub fn search(cx: &mut Context) {
589− let mut doc = cx.doc();
602+ let doc = cx.doc();
590603
591604 // TODO: could probably share with select_on_matches?
592605
593606 // HAXX: sadly we can't avoid allocating a single string for the whole buffer since we can't
594607 // feed chunks into the regex yet
595608 let contents = doc.text().slice(..).to_string();
596− drop(doc);
597609
598610 let prompt = ui::regex_prompt(cx, "search:".to_string(), move |doc, regex| {
599611 let text = doc.text();
@@ -610,20 +622,19 @@ pub fn search(cx: &mut Context) {
610622pub fn search_next(cx: &mut Context) {
611623 if let Some(query) = register::get('\\') {
612624 let query = query.first().unwrap();
613− let mut doc = cx.doc();
625+ let doc = cx.doc();
614626 let contents = doc.text().slice(..).to_string();
615627 let regex = Regex::new(&query).unwrap();
616− _search(&mut doc, &contents, ®ex);
628+ _search(doc, &contents, ®ex);
617629 }
618630}
619631
620632pub fn search_selection(cx: &mut Context) {
621− let mut doc = cx.doc();
633+ let doc = cx.doc();
622634 let contents = doc.text().slice(..);
623635 let query = doc.selection().primary().fragment(contents);
624636 let regex = regex::escape(&query);
625637 register::set('\\', vec![regex]);
626− drop(doc);
627638 search_next(cx);
628639}
629640
@@ -635,7 +646,7 @@ pub fn search_selection(cx: &mut Context) {
635646
636647pub fn select_line(cx: &mut Context) {
637648 let count = cx.count;
638− let mut doc = cx.doc();
649+ let doc = cx.doc();
639650
640651 let pos = doc.selection().primary();
641652 let text = doc.text();
@@ -648,7 +659,7 @@ pub fn select_line(cx: &mut Context) {
648659}
649660pub fn extend_line(cx: &mut Context) {
650661 let count = cx.count;
651− let mut doc = cx.doc();
662+ let doc = cx.doc();
652663
653664 let pos = doc.selection().primary();
654665 let text = doc.text();
@@ -676,21 +687,20 @@ fn _delete_selection(doc: &mut Document) {
676687}
677688
678689pub fn delete_selection(cx: &mut Context) {
679− let mut doc = cx.doc();
680− _delete_selection(&mut doc);
690+ let doc = cx.doc();
691+ _delete_selection(doc);
681692
682693 doc.append_changes_to_history();
683694}
684695
685696pub fn change_selection(cx: &mut Context) {
686− let mut doc = cx.doc();
687− _delete_selection(&mut doc);
688− drop(doc);
697+ let doc = cx.doc();
698+ _delete_selection(doc);
689699 insert_mode(cx);
690700}
691701
692702pub fn collapse_selection(cx: &mut Context) {
693− let mut doc = cx.doc();
703+ let doc = cx.doc();
694704 let selection = doc
695705 .selection()
696706 .transform(|range| Range::new(range.head, range.head));
@@ -699,7 +709,7 @@ pub fn collapse_selection(cx: &mut Context) {
699709}
700710
701711pub fn flip_selections(cx: &mut Context) {
702− let mut doc = cx.doc();
712+ let doc = cx.doc();
703713 let selection = doc
704714 .selection()
705715 .transform(|range| Range::new(range.head, range.anchor));
@@ -713,8 +723,8 @@ fn enter_insert_mode(doc: &mut Document) {
713723
714724// inserts at the start of each selection
715725pub fn insert_mode(cx: &mut Context) {
716− let mut doc = cx.doc();
717− enter_insert_mode(&mut doc);
726+ let doc = cx.doc();
727+ enter_insert_mode(doc);
718728
719729 let selection = doc
720730 .selection()
@@ -724,8 +734,8 @@ pub fn insert_mode(cx: &mut Context) {
724734
725735// inserts at the end of each selection
726736pub fn append_mode(cx: &mut Context) {
727− let mut doc = cx.doc();
728− enter_insert_mode(&mut doc);
737+ let doc = cx.doc();
738+ enter_insert_mode(doc);
729739 doc.restore_cursor = true;
730740
731741 let text = doc.text().slice(..);
@@ -792,7 +802,9 @@ pub fn command_mode(cx: &mut Context) {
792802 }
793803 ["w"] | ["write"] => {
794804 // TODO: non-blocking via save() command
795− smol::block_on(editor.view().doc.borrow().save());
805+ let id = editor.view().doc;
806+ let doc = &mut editor.document(id).unwrap();
807+ smol::block_on(doc.save());
796808 }
797809
798810 _ => (),
@@ -807,8 +819,9 @@ pub fn file_picker(cx: &mut Context) {
807819}
808820
809821pub fn buffer_picker(cx: &mut Context) {
810− unimplemented!()
811− // let picker = ui::buffer_picker(&editor.views, editor.focus);
822+ // let documents = cx.editor.documents.iter().map(||).collect();
823+ // (document_id, relative_path/name) mappings
824+ // let picker = ui::buffer_picker(&documents, editor.focus);
812825 // cx.push_layer(Box::new(picker));
813826}
814827
@@ -827,18 +840,16 @@ fn selection_lines(doc: &Rope, selection: &Selection) -> Vec<usize> {
827840
828841// I inserts at the start of each line with a selection
829842pub fn prepend_to_line(cx: &mut Context) {
830− let mut doc = cx.doc();
831− enter_insert_mode(&mut doc);
832− drop(doc);
843+ let doc = cx.doc();
844+ enter_insert_mode(doc);
833845
834846 move_line_start(cx);
835847}
836848
837849// A inserts at the end of each line with a selection
838850pub fn append_to_line(cx: &mut Context) {
839− let mut doc = cx.doc();
840− enter_insert_mode(&mut doc);
841− drop(doc);
851+ let doc = cx.doc();
852+ enter_insert_mode(doc);
842853
843854 move_line_end(cx);
844855}
@@ -846,8 +857,8 @@ pub fn append_to_line(cx: &mut Context) {
846857// o inserts a new line after each line with a selection
847858pub fn open_below(cx: &mut Context) {
848859 let count = cx.count;
849− let mut doc = cx.doc();
850− enter_insert_mode(&mut doc);
860+ let doc = cx.doc();
861+ enter_insert_mode(doc);
851862
852863 let lines = selection_lines(doc.text(), doc.selection());
853864
@@ -897,8 +908,8 @@ pub fn open_below(cx: &mut Context) {
897908// O inserts a new line before each line with a selection
898909pub fn open_above(cx: &mut Context) {
899910 let count = cx.count;
900− let mut doc = cx.doc();
901− enter_insert_mode(&mut doc);
911+ let doc = cx.doc();
912+ enter_insert_mode(doc);
902913
903914 let lines = selection_lines(doc.text(), doc.selection());
904915
@@ -947,7 +958,7 @@ pub fn open_above(cx: &mut Context) {
947958}
948959
949960pub fn normal_mode(cx: &mut Context) {
950− let mut doc = cx.doc();
961+ let doc = cx.doc();
951962
952963 doc.mode = Mode::Normal;
953964
@@ -986,7 +997,7 @@ fn goto(cx: &mut Context, locations: Vec<lsp::Location>) {
986997 match locations.as_slice() {
987998 [location] => {
988999 cx.editor.open(PathBuf::from(location.uri.path()));
989− let mut doc = cx.doc();
1000+ let doc = cx.doc();
9901001 let definition_pos = location.range.start;
9911002 let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text(), definition_pos);
9921003 doc.set_selection(Selection::point(new_pos));
@@ -1003,7 +1014,8 @@ fn goto(cx: &mut Context, locations: Vec<lsp::Location>) {
10031014 move |editor: &mut Editor, item| {
10041015 editor.open(PathBuf::from(item.uri.path()));
10051016 // TODO: issues with doc already being broo
1006− let mut doc = &mut editor.view().doc.borrow_mut();
1017+ let id = editor.view().doc;
1018+ let doc = &mut editor.documents[id];
10071019 let definition_pos = item.range.start;
10081020 let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text(), definition_pos);
10091021 doc.set_selection(Selection::point(new_pos));
@@ -1015,7 +1027,7 @@ fn goto(cx: &mut Context, locations: Vec<lsp::Location>) {
10151027}
10161028
10171029pub fn goto_definition(cx: &mut Context) {
1018− let mut doc = cx.doc();
1030+ let doc = cx.doc();
10191031 let language_server = match doc.language_server() {
10201032 Some(language_server) => language_server,
10211033 None => return,
@@ -1027,12 +1039,11 @@ pub fn goto_definition(cx: &mut Context) {
10271039 // TODO: handle fails
10281040 let res =
10291041 smol::block_on(language_server.goto_definition(doc.identifier(), pos)).unwrap_or_default();
1030− drop(doc);
10311042 goto(cx, res);
10321043}
10331044
10341045pub fn goto_type_definition(cx: &mut Context) {
1035− let mut doc = cx.doc();
1046+ let doc = cx.doc();
10361047 let language_server = match doc.language_server() {
10371048 Some(language_server) => language_server,
10381049 None => return,
@@ -1044,12 +1055,11 @@ pub fn goto_type_definition(cx: &mut Context) {
10441055 // TODO: handle fails
10451056 let res = smol::block_on(language_server.goto_type_definition(doc.identifier(), pos))
10461057 .unwrap_or_default();
1047− drop(doc);
10481058 goto(cx, res);
10491059}
10501060
10511061pub fn goto_implementation(cx: &mut Context) {
1052− let mut doc = cx.doc();
1062+ let doc = cx.doc();
10531063 let language_server = match doc.language_server() {
10541064 Some(language_server) => language_server,
10551065 None => return,
@@ -1061,12 +1071,11 @@ pub fn goto_implementation(cx: &mut Context) {
10611071 // TODO: handle fails
10621072 let res = smol::block_on(language_server.goto_implementation(doc.identifier(), pos))
10631073 .unwrap_or_default();
1064− drop(doc);
10651074 goto(cx, res);
10661075}
10671076
10681077pub fn goto_reference(cx: &mut Context) {
1069− let mut doc = cx.doc();
1078+ let doc = cx.doc();
10701079 let language_server = match doc.language_server() {
10711080 Some(language_server) => language_server,
10721081 None => return,
@@ -1078,7 +1087,6 @@ pub fn goto_reference(cx: &mut Context) {
10781087 // TODO: handle fails
10791088 let res =
10801089 smol::block_on(language_server.goto_reference(doc.identifier(), pos)).unwrap_or_default();
1081− drop(doc);
10821090 goto(cx, res);
10831091}
10841092
@@ -1092,7 +1100,7 @@ pub mod insert {
10921100
10931101 // TODO: insert means add text just before cursor, on exit we should be on the last letter.
10941102 pub fn insert_char(cx: &mut Context, c: char) {
1095− let mut doc = cx.doc();
1103+ let doc = cx.doc();
10961104
10971105 // run through insert hooks, stopping on the first one that returns Some(t)
10981106 for hook in HOOKS {
@@ -1109,7 +1117,7 @@ pub mod insert {
11091117 }
11101118
11111119 pub fn insert_tab(cx: &mut Context) {
1112− let mut doc = cx.doc();
1120+ let doc = cx.doc();
11131121 // TODO: round out to nearest indentation level (for example a line with 3 spaces should
11141122 // indent by one to reach 4 spaces).
11151123
@@ -1119,7 +1127,7 @@ pub mod insert {
11191127 }
11201128
11211129 pub fn insert_newline(cx: &mut Context) {
1122− let mut doc = cx.doc();
1130+ let doc = cx.doc();
11231131 let text = doc.text().slice(..);
11241132 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
11251133 let indent_level =
@@ -1136,7 +1144,7 @@ pub mod insert {
11361144 // TODO: handle indent-aware delete
11371145 pub fn delete_char_backward(cx: &mut Context) {
11381146 let count = cx.count;
1139− let mut doc = cx.doc();
1147+ let doc = cx.doc();
11401148 let text = doc.text().slice(..);
11411149 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
11421150 (
@@ -1150,7 +1158,7 @@ pub mod insert {
11501158
11511159 pub fn delete_char_forward(cx: &mut Context) {
11521160 let count = cx.count;
1153− let mut doc = cx.doc();
1161+ let doc = cx.doc();
11541162 let text = doc.text().slice(..);
11551163 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
11561164 (
@@ -1180,7 +1188,7 @@ pub fn redo(cx: &mut Context) {
11801188
11811189pub fn yank(cx: &mut Context) {
11821190 // TODO: should selections be made end inclusive?
1183− let mut doc = cx.doc();
1191+ let doc = cx.doc();
11841192 let values = doc
11851193 .selection()
11861194 .fragments(doc.text().slice(..))
@@ -1221,7 +1229,7 @@ pub fn paste(cx: &mut Context) {
12211229
12221230 let mut values = values.into_iter().map(Tendril::from).chain(repeat);
12231231
1224− let mut doc = cx.doc();
1232+ let doc = cx.doc();
12251233
12261234 let transaction = if linewise {
12271235 // paste on the next line
@@ -1260,8 +1268,8 @@ fn get_lines(doc: &Document) -> Vec<usize> {
12601268}
12611269
12621270pub fn indent(cx: &mut Context) {
1263− let mut doc = cx.doc();
1264− let lines = get_lines(&mut doc);
1271+ let doc = cx.doc();
1272+ let lines = get_lines(doc);
12651273
12661274 // Indent by one level
12671275 let indent = Tendril::from(doc.indent_unit());
@@ -1278,8 +1286,8 @@ pub fn indent(cx: &mut Context) {
12781286}
12791287
12801288pub fn unindent(cx: &mut Context) {
1281− let mut doc = cx.doc();
1282− let lines = get_lines(&mut doc);
1289+ let doc = cx.doc();
1290+ let lines = get_lines(doc);
12831291 let mut changes = Vec::with_capacity(lines.len());
12841292 let tab_width = doc.tab_width();
12851293
@@ -1313,7 +1321,7 @@ pub fn unindent(cx: &mut Context) {
13131321
13141322pub fn format_selections(cx: &mut Context) {
13151323 use helix_lsp::lsp;
1316− let mut doc = cx.doc();
1324+ let doc = cx.doc();
13171325
13181326 // via lsp if available
13191327 // else via tree-sitter indentation calculations
@@ -1351,7 +1359,7 @@ pub fn format_selections(cx: &mut Context) {
13511359
13521360pub fn join_selections(cx: &mut Context) {
13531361 use movement::skip_over_next;
1354− let mut doc = cx.doc();
1362+ let doc = cx.doc();
13551363 let text = doc.text();
13561364 let slice = doc.text().slice(..);
13571365
@@ -1407,7 +1415,7 @@ pub fn keep_selections(cx: &mut Context) {
14071415}
14081416
14091417pub fn keep_primary_selection(cx: &mut Context) {
1410− let mut doc = cx.doc();
1418+ let doc = cx.doc();
14111419
14121420 let range = doc.selection().primary();
14131421 let selection = Selection::single(range.anchor, range.head);
@@ -1424,7 +1432,7 @@ pub fn save(cx: &mut Context) {
14241432}
14251433
14261434pub fn completion(cx: &mut Context) {
1427− let mut doc = cx.doc();
1435+ let doc = cx.doc();
14281436
14291437 let language_server = match doc.language_server() {
14301438 Some(language_server) => language_server,
@@ -1438,8 +1446,6 @@ pub fn completion(cx: &mut Context) {
14381446
14391447 let res = smol::block_on(language_server.completion(doc.identifier(), pos)).unwrap_or_default();
14401448
1441− drop(doc);
1442−
14431449 // TODO: if no completion, show some message or something
14441450 if !res.is_empty() {
14451451 // let snapshot = doc.state.clone();
@@ -1455,11 +1461,13 @@ pub fn completion(cx: &mut Context) {
14551461 match event {
14561462 PromptEvent::Abort => {
14571463 // revert state
1458− let doc = &mut editor.view().doc.borrow_mut();
1464+ // let id = editor.view().doc;
1465+ // let doc = &mut editor.documents[id];
14591466 // doc.state = snapshot.clone();
14601467 }
14611468 PromptEvent::Validate => {
1462− let doc = &mut editor.view().doc.borrow_mut();
1469+ let id = editor.view().doc;
1470+ let doc = &mut editor.documents[id];
14631471
14641472 // revert state to what it was before the last update
14651473 // doc.state = snapshot.clone();
@@ -1525,7 +1533,7 @@ pub fn completion(cx: &mut Context) {
15251533pub fn hover(cx: &mut Context) {
15261534 use helix_lsp::lsp;
15271535
1528− let mut doc = cx.doc();
1536+ let doc = cx.doc();
15291537
15301538 let language_server = match doc.language_server() {
15311539 Some(language_server) => language_server,
@@ -1542,8 +1550,6 @@ pub fn hover(cx: &mut Context) {
15421550 let res = smol::block_on(language_server.text_document_hover(doc.identifier(), pos))
15431551 .unwrap_or_default();
15441552
1545− drop(doc);
1546−
15471553 if let Some(hover) = res {
15481554 // hover.contents / .range <- used for visualizing
15491555 let contents = match hover.contents {
@@ -1574,7 +1580,7 @@ pub fn next_view(cx: &mut Context) {
15741580
15751581// comments
15761582pub fn toggle_comments(cx: &mut Context) {
1577− let mut doc = cx.doc();
1583+ let doc = cx.doc();
15781584 let transaction = comment::toggle_line_comments(doc.text(), doc.selection());
15791585
15801586 doc.apply(&transaction);
@@ -1584,7 +1590,7 @@ pub fn toggle_comments(cx: &mut Context) {
15841590// tree sitter node selection
15851591
15861592pub fn expand_selection(cx: &mut Context) {
1587− let mut doc = cx.doc();
1593+ let doc = cx.doc();
15881594
15891595 if let Some(syntax) = doc.syntax() {
15901596 let text = doc.text().slice(..);
@@ -1594,7 +1600,7 @@ pub fn expand_selection(cx: &mut Context) {
15941600}
15951601
15961602pub fn match_brackets(cx: &mut Context) {
1597− let mut doc = cx.doc();
1603+ let doc = cx.doc();
15981604
15991605 if let Some(syntax) = doc.syntax() {
16001606 let pos = doc.selection().cursor();