@@ -41,8 +41,9 @@ impl<'a> Context<'a> {
44− pub fn doc(&mut self) -> std::cell::RefMut<Document> {
44+ pub fn doc(&mut self) -> &mut Document {
45− self.editor.view().doc.borrow_mut()
45+ let id = self.editor.view().doc;
46+ &mut self.editor.documents[id]
48 /// Push a new component onto the compositor.
49 /// Push a new component onto the compositor.
@@ -72,7 +73,7 @@ pub type Command = fn(cx: &mut Context);
73pub fn move_char_left(cx: &mut Context) {
74pub fn move_char_left(cx: &mut Context) {
74 let count = cx.count;
75 let count = cx.count;
75− let mut doc = cx.doc();
76+ let doc = cx.doc();
76 let text = doc.text().slice(..);
77 let text = doc.text().slice(..);
77 let selection = doc.selection().transform(|range| {
78 let selection = doc.selection().transform(|range| {
78 movement::move_horizontally(
79 movement::move_horizontally(
@@ -88,7 +89,7 @@ pub fn move_char_left(cx: &mut Context) {
89pub fn move_char_right(cx: &mut Context) {
90pub fn move_char_right(cx: &mut Context) {
90 let count = cx.count;
91 let count = cx.count;
91− let mut doc = cx.doc();
92+ let doc = cx.doc();
92 let text = doc.text().slice(..);
93 let text = doc.text().slice(..);
93 let selection = doc.selection().transform(|range| {
94 let selection = doc.selection().transform(|range| {
94 movement::move_horizontally(
95 movement::move_horizontally(
@@ -104,7 +105,7 @@ pub fn move_char_right(cx: &mut Context) {
105pub fn move_line_up(cx: &mut Context) {
106pub fn move_line_up(cx: &mut Context) {
106 let count = cx.count;
107 let count = cx.count;
107− let mut doc = cx.doc();
108+ let doc = cx.doc();
108 let text = doc.text().slice(..);
109 let text = doc.text().slice(..);
109 let selection = doc.selection().transform(|range| {
110 let selection = doc.selection().transform(|range| {
110 movement::move_vertically(
111 movement::move_vertically(
@@ -120,7 +121,7 @@ pub fn move_line_up(cx: &mut Context) {
121pub fn move_line_down(cx: &mut Context) {
122pub fn move_line_down(cx: &mut Context) {
122 let count = cx.count;
123 let count = cx.count;
123− let mut doc = cx.doc();
124+ let doc = cx.doc();
124 let text = doc.text().slice(..);
125 let text = doc.text().slice(..);
125 let selection = doc.selection().transform(|range| {
126 let selection = doc.selection().transform(|range| {
126 movement::move_vertically(
127 movement::move_vertically(
@@ -135,7 +136,7 @@ pub fn move_line_down(cx: &mut Context) {
137pub fn move_line_end(cx: &mut Context) {
138pub fn move_line_end(cx: &mut Context) {
138− let mut doc = cx.doc();
139+ let doc = cx.doc();
139 let lines = selection_lines(doc.text(), doc.selection());
140 let lines = selection_lines(doc.text(), doc.selection());
141 let positions = lines
142 let positions = lines
@@ -155,7 +156,7 @@ pub fn move_line_end(cx: &mut Context) {
157pub fn move_line_start(cx: &mut Context) {
158pub fn move_line_start(cx: &mut Context) {
158− let mut doc = cx.doc();
159+ let doc = cx.doc();
159 let lines = selection_lines(doc.text(), doc.selection());
160 let lines = selection_lines(doc.text(), doc.selection());
161 let positions = lines
162 let positions = lines
@@ -177,7 +178,7 @@ pub fn move_line_start(cx: &mut Context) {
178pub fn move_next_word_start(cx: &mut Context) {
179pub fn move_next_word_start(cx: &mut Context) {
179 let count = cx.count;
180 let count = cx.count;
180− let mut doc = cx.doc();
181+ let doc = cx.doc();
181 let text = doc.text().slice(..);
182 let text = doc.text().slice(..);
183 let selection = doc.selection().transform(|range| {
184 let selection = doc.selection().transform(|range| {
@@ -190,7 +191,7 @@ pub fn move_next_word_start(cx: &mut Context) {
191pub fn move_prev_word_start(cx: &mut Context) {
192pub fn move_prev_word_start(cx: &mut Context) {
192 let count = cx.count;
193 let count = cx.count;
193− let mut doc = cx.doc();
194+ let doc = cx.doc();
194 let text = doc.text().slice(..);
195 let text = doc.text().slice(..);
196 let selection = doc.selection().transform(|range| {
197 let selection = doc.selection().transform(|range| {
@@ -203,7 +204,7 @@ pub fn move_prev_word_start(cx: &mut Context) {
204pub fn move_next_word_end(cx: &mut Context) {
205pub fn move_next_word_end(cx: &mut Context) {
205 let count = cx.count;
206 let count = cx.count;
206− let mut doc = cx.doc();
207+ let doc = cx.doc();
207 let text = doc.text().slice(..);
208 let text = doc.text().slice(..);
209 let selection = doc.selection().transform(|range| {
210 let selection = doc.selection().transform(|range| {
@@ -215,14 +216,14 @@ pub fn move_next_word_end(cx: &mut Context) {
217pub fn move_file_start(cx: &mut Context) {
218pub fn move_file_start(cx: &mut Context) {
218− let mut doc = cx.doc();
219+ let doc = cx.doc();
219 doc.set_selection(Selection::point(0));
220 doc.set_selection(Selection::point(0));
221 doc.mode = Mode::Normal;
222 doc.mode = Mode::Normal;
224pub fn move_file_end(cx: &mut Context) {
225pub fn move_file_end(cx: &mut Context) {
225− let mut doc = cx.doc();
226+ let doc = cx.doc();
226 let text = doc.text();
227 let text = doc.text();
227 let last_line = text.line_to_char(text.len_lines().saturating_sub(2));
228 let last_line = text.line_to_char(text.len_lines().saturating_sub(2));
228 doc.set_selection(Selection::point(last_line));
229 doc.set_selection(Selection::point(last_line));
@@ -232,7 +233,7 @@ pub fn move_file_end(cx: &mut Context) {
233pub fn extend_next_word_start(cx: &mut Context) {
234pub fn extend_next_word_start(cx: &mut Context) {
234 let count = cx.count;
235 let count = cx.count;
235− let mut doc = cx.doc();
236+ let doc = cx.doc();
236 let text = doc.text().slice(..);
237 let text = doc.text().slice(..);
238 let selection = doc.selection().transform(|mut range| {
239 let selection = doc.selection().transform(|mut range| {
@@ -245,7 +246,7 @@ pub fn extend_next_word_start(cx: &mut Context) {
246pub fn extend_prev_word_start(cx: &mut Context) {
247pub fn extend_prev_word_start(cx: &mut Context) {
247 let count = cx.count;
248 let count = cx.count;
248− let mut doc = cx.doc();
249+ let doc = cx.doc();
249 let text = doc.text().slice(..);
250 let text = doc.text().slice(..);
251 let selection = doc.selection().transform(|mut range| {
252 let selection = doc.selection().transform(|mut range| {
@@ -257,7 +258,7 @@ pub fn extend_prev_word_start(cx: &mut Context) {
258pub fn extend_next_word_end(cx: &mut Context) {
259pub fn extend_next_word_end(cx: &mut Context) {
259 let count = cx.count;
260 let count = cx.count;
260− let mut doc = cx.doc();
261+ let doc = cx.doc();
261 let text = doc.text().slice(..);
262 let text = doc.text().slice(..);
263 let selection = doc.selection().transform(|mut range| {
264 let selection = doc.selection().transform(|mut range| {
@@ -286,7 +287,7 @@ where
287 } = event
288 } = event
289− let mut doc = cx.doc();
290+ let doc = cx.doc();
290 let text = doc.text().slice(..);
291 let text = doc.text().slice(..);
292 let selection = doc.selection().transform(|mut range| {
293 let selection = doc.selection().transform(|mut range| {
@@ -390,7 +391,7 @@ pub fn replace(cx: &mut Context) {
391 let text = Tendril::from_char(ch);
392 let text = Tendril::from_char(ch);
393− let mut doc = cx.doc();
394+ let doc = cx.doc();
395 let transaction =
396 let transaction =
396 Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
397 Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
@@ -403,15 +404,14 @@ pub fn replace(cx: &mut Context) {
406−fn scroll(view: &mut View, offset: usize, direction: Direction) {
407+fn scroll(cx: &mut Context, offset: usize, direction: Direction) {
407 use Direction::*;
408 use Direction::*;
408− // we use short lived borrows since view's methods read from doc too
409+ let view = cx.editor.view();
409− let doc = view.doc.borrow();
410+ let doc = cx.editor.document(view.doc).unwrap();
410− let text = doc.text().slice(..);
411 let cursor = coords_at_pos(doc.text().slice(..), doc.selection().cursor());
411 let cursor = coords_at_pos(doc.text().slice(..), doc.selection().cursor());
412 let doc_last_line = doc.text().len_lines() - 1;
412 let doc_last_line = doc.text().len_lines() - 1;
414− let last_line = view.last_line();
414+ let last_line = view.last_line(doc);
416 if direction == Backward && view.first_line == 0
416 if direction == Backward && view.first_line == 0
417 || direction == Forward && last_line == doc_last_line
417 || direction == Forward && last_line == doc_last_line
@@ -424,6 +424,9 @@ fn scroll(view: &mut View, offset: usize, direction: Direction) {
424 // cursor visual offset
424 // cursor visual offset
425 let cursor_off = cursor.row - view.first_line;
425 let cursor_off = cursor.row - view.first_line;
427+ // upgrade to mut reference
428+ let view = cx.editor.view_mut();
427 view.first_line = match direction {
430 view.first_line = match direction {
428 Forward => view.first_line + offset,
431 Forward => view.first_line + offset,
429 Backward => view.first_line.saturating_sub(offset),
432 Backward => view.first_line.saturating_sub(offset),
@@ -433,37 +436,47 @@ fn scroll(view: &mut View, offset: usize, direction: Direction) {
433 // clamp into viewport
436 // clamp into viewport
434 let line = (view.first_line + cursor_off).clamp(
437 let line = (view.first_line + cursor_off).clamp(
435 view.first_line + scrolloff,
438 view.first_line + scrolloff,
436− view.last_line().saturating_sub(scrolloff),
439+ last_line.saturating_sub(scrolloff),
444+ // upgrade to mut reference
447+ let text = doc.text().slice(..);
439 let pos = pos_at_coords(text, Position::new(line, cursor.col)); // this func will properly truncate to line end
448 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
449+
441− view.doc.borrow_mut().set_selection(Selection::point(pos));
450+ doc.set_selection(Selection::point(pos));
444pub fn page_up(cx: &mut Context) {
453pub fn page_up(cx: &mut Context) {
445 let view = cx.view();
454 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);
449pub fn page_down(cx: &mut Context) {
459pub fn page_down(cx: &mut Context) {
450 let view = cx.view();
460 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);
454pub fn half_page_up(cx: &mut Context) {
465pub fn half_page_up(cx: &mut Context) {
455 let view = cx.view();
466 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);
459pub fn half_page_down(cx: &mut Context) {
471pub fn half_page_down(cx: &mut Context) {
460 let view = cx.view();
472 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);
464pub fn extend_char_left(cx: &mut Context) {
477pub fn extend_char_left(cx: &mut Context) {
465 let count = cx.count;
478 let count = cx.count;
466− let mut doc = cx.doc();
479+ let doc = cx.doc();
467 let text = doc.text().slice(..);
480 let text = doc.text().slice(..);
468 let selection = doc.selection().transform(|range| {
481 let selection = doc.selection().transform(|range| {
469 movement::move_horizontally(
482 movement::move_horizontally(
@@ -479,7 +492,7 @@ pub fn extend_char_left(cx: &mut Context) {
480pub fn extend_char_right(cx: &mut Context) {
493pub fn extend_char_right(cx: &mut Context) {
481 let count = cx.count;
494 let count = cx.count;
482− let mut doc = cx.doc();
495+ let doc = cx.doc();
483 let text = doc.text().slice(..);
496 let text = doc.text().slice(..);
484 let selection = doc.selection().transform(|range| {
497 let selection = doc.selection().transform(|range| {
485 movement::move_horizontally(
498 movement::move_horizontally(
@@ -495,7 +508,7 @@ pub fn extend_char_right(cx: &mut Context) {
496pub fn extend_line_up(cx: &mut Context) {
509pub fn extend_line_up(cx: &mut Context) {
497 let count = cx.count;
510 let count = cx.count;
498− let mut doc = cx.doc();
511+ let doc = cx.doc();
499 let text = doc.text().slice(..);
512 let text = doc.text().slice(..);
500 let selection = doc.selection().transform(|range| {
513 let selection = doc.selection().transform(|range| {
501 movement::move_vertically(
514 movement::move_vertically(
@@ -511,7 +524,7 @@ pub fn extend_line_up(cx: &mut Context) {
512pub fn extend_line_down(cx: &mut Context) {
525pub fn extend_line_down(cx: &mut Context) {
513 let count = cx.count;
526 let count = cx.count;
514− let mut doc = cx.doc();
527+ let doc = cx.doc();
515 let text = doc.text().slice(..);
528 let text = doc.text().slice(..);
516 let selection = doc.selection().transform(|range| {
529 let selection = doc.selection().transform(|range| {
517 movement::move_vertically(
530 movement::move_vertically(
@@ -526,7 +539,7 @@ pub fn extend_line_down(cx: &mut Context) {
528pub fn select_all(cx: &mut Context) {
541pub fn select_all(cx: &mut Context) {
529− let mut doc = cx.doc();
542+ let doc = cx.doc();
531 let end = doc.text().len_chars().saturating_sub(1);
544 let end = doc.text().len_chars().saturating_sub(1);
532 doc.set_selection(Selection::single(0, end))
545 doc.set_selection(Selection::single(0, end))
@@ -554,7 +567,7 @@ pub fn split_selection(cx: &mut Context) {
556pub fn split_selection_on_newline(cx: &mut Context) {
569pub fn split_selection_on_newline(cx: &mut Context) {
557− let mut doc = cx.doc();
570+ let doc = cx.doc();
558 let text = doc.text().slice(..);
571 let text = doc.text().slice(..);
559 // only compile the regex once
572 // only compile the regex once
560 #[allow(clippy::trivial_regex)]
573 #[allow(clippy::trivial_regex)]
@@ -586,14 +599,13 @@ fn _search(doc: &mut Document, contents: &str, regex: &Regex) {
587// TODO: use one function for search vs extend
600// TODO: use one function for search vs extend
588pub fn search(cx: &mut Context) {
601pub fn search(cx: &mut Context) {
589− let mut doc = cx.doc();
602+ let doc = cx.doc();
591 // TODO: could probably share with select_on_matches?
604 // TODO: could probably share with select_on_matches?
593 // HAXX: sadly we can't avoid allocating a single string for the whole buffer since we can't
606 // HAXX: sadly we can't avoid allocating a single string for the whole buffer since we can't
594 // feed chunks into the regex yet
607 // feed chunks into the regex yet
595 let contents = doc.text().slice(..).to_string();
608 let contents = doc.text().slice(..).to_string();
598 let prompt = ui::regex_prompt(cx, "search:".to_string(), move |doc, regex| {
610 let prompt = ui::regex_prompt(cx, "search:".to_string(), move |doc, regex| {
599 let text = doc.text();
611 let text = doc.text();
@@ -610,20 +622,19 @@ pub fn search(cx: &mut Context) {
610pub fn search_next(cx: &mut Context) {
622pub fn search_next(cx: &mut Context) {
611 if let Some(query) = register::get('\\') {
623 if let Some(query) = register::get('\\') {
612 let query = query.first().unwrap();
624 let query = query.first().unwrap();
613− let mut doc = cx.doc();
625+ let doc = cx.doc();
614 let contents = doc.text().slice(..).to_string();
626 let contents = doc.text().slice(..).to_string();
615 let regex = Regex::new(&query).unwrap();
627 let regex = Regex::new(&query).unwrap();
616− _search(&mut doc, &contents, ®ex);
628+ _search(doc, &contents, ®ex);
620pub fn search_selection(cx: &mut Context) {
632pub fn search_selection(cx: &mut Context) {
621− let mut doc = cx.doc();
633+ let doc = cx.doc();
622 let contents = doc.text().slice(..);
634 let contents = doc.text().slice(..);
623 let query = doc.selection().primary().fragment(contents);
635 let query = doc.selection().primary().fragment(contents);
624 let regex = regex::escape(&query);
636 let regex = regex::escape(&query);
625 register::set('\\', vec![regex]);
637 register::set('\\', vec![regex]);
627 search_next(cx);
638 search_next(cx);
@@ -635,7 +646,7 @@ pub fn search_selection(cx: &mut Context) {
636pub fn select_line(cx: &mut Context) {
647pub fn select_line(cx: &mut Context) {
637 let count = cx.count;
648 let count = cx.count;
638− let mut doc = cx.doc();
649+ let doc = cx.doc();
640 let pos = doc.selection().primary();
651 let pos = doc.selection().primary();
641 let text = doc.text();
652 let text = doc.text();
@@ -648,7 +659,7 @@ pub fn select_line(cx: &mut Context) {
649pub fn extend_line(cx: &mut Context) {
660pub fn extend_line(cx: &mut Context) {
650 let count = cx.count;
661 let count = cx.count;
651− let mut doc = cx.doc();
662+ let doc = cx.doc();
653 let pos = doc.selection().primary();
664 let pos = doc.selection().primary();
654 let text = doc.text();
665 let text = doc.text();
@@ -676,21 +687,20 @@ fn _delete_selection(doc: &mut Document) {
678pub fn delete_selection(cx: &mut Context) {
689pub fn delete_selection(cx: &mut Context) {
679− let mut doc = cx.doc();
690+ let doc = cx.doc();
680− _delete_selection(&mut doc);
691+ _delete_selection(doc);
682 doc.append_changes_to_history();
693 doc.append_changes_to_history();
685pub fn change_selection(cx: &mut Context) {
696pub fn change_selection(cx: &mut Context) {
686− let mut doc = cx.doc();
697+ let doc = cx.doc();
687− _delete_selection(&mut doc);
698+ _delete_selection(doc);
689 insert_mode(cx);
699 insert_mode(cx);
692pub fn collapse_selection(cx: &mut Context) {
702pub fn collapse_selection(cx: &mut Context) {
693− let mut doc = cx.doc();
703+ let doc = cx.doc();
694 let selection = doc
704 let selection = doc
695 .selection()
705 .selection()
696 .transform(|range| Range::new(range.head, range.head));
706 .transform(|range| Range::new(range.head, range.head));
@@ -699,7 +709,7 @@ pub fn collapse_selection(cx: &mut Context) {
701pub fn flip_selections(cx: &mut Context) {
711pub fn flip_selections(cx: &mut Context) {
702− let mut doc = cx.doc();
712+ let doc = cx.doc();
703 let selection = doc
713 let selection = doc
704 .selection()
714 .selection()
705 .transform(|range| Range::new(range.head, range.anchor));
715 .transform(|range| Range::new(range.head, range.anchor));
@@ -713,8 +723,8 @@ fn enter_insert_mode(doc: &mut Document) {
714// inserts at the start of each selection
724// inserts at the start of each selection
715pub fn insert_mode(cx: &mut Context) {
725pub fn insert_mode(cx: &mut Context) {
716− let mut doc = cx.doc();
726+ let doc = cx.doc();
717− enter_insert_mode(&mut doc);
727+ enter_insert_mode(doc);
719 let selection = doc
729 let selection = doc
720 .selection()
730 .selection()
@@ -724,8 +734,8 @@ pub fn insert_mode(cx: &mut Context) {
725// inserts at the end of each selection
735// inserts at the end of each selection
726pub fn append_mode(cx: &mut Context) {
736pub fn append_mode(cx: &mut Context) {
727− let mut doc = cx.doc();
737+ let doc = cx.doc();
728− enter_insert_mode(&mut doc);
738+ enter_insert_mode(doc);
729 doc.restore_cursor = true;
739 doc.restore_cursor = true;
731 let text = doc.text().slice(..);
741 let text = doc.text().slice(..);
@@ -792,7 +802,9 @@ pub fn command_mode(cx: &mut Context) {
793 ["w"] | ["write"] => {
803 ["w"] | ["write"] => {
794 // TODO: non-blocking via save() command
804 // TODO: non-blocking via save() command
805+ let id = editor.view().doc;
806+ let doc = &mut editor.document(id).unwrap();
795− smol::block_on(editor.view().doc.borrow().save());
807+ smol::block_on(doc.save());
@@ -807,8 +819,9 @@ pub fn file_picker(cx: &mut Context) {
809pub fn buffer_picker(cx: &mut Context) {
821pub fn buffer_picker(cx: &mut Context) {
810− unimplemented!()
822+ // let documents = cx.editor.documents.iter().map(||).collect();
823+ // (document_id, relative_path/name) mappings
811− // let picker = ui::buffer_picker(&editor.views, editor.focus);
824+ // let picker = ui::buffer_picker(&documents, editor.focus);
812 // cx.push_layer(Box::new(picker));
825 // cx.push_layer(Box::new(picker));
@@ -827,18 +840,16 @@ fn selection_lines(doc: &Rope, selection: &Selection) -> Vec<usize> {
828// I inserts at the start of each line with a selection
841// I inserts at the start of each line with a selection
829pub fn prepend_to_line(cx: &mut Context) {
842pub fn prepend_to_line(cx: &mut Context) {
830− let mut doc = cx.doc();
843+ let doc = cx.doc();
831− enter_insert_mode(&mut doc);
844+ enter_insert_mode(doc);
834 move_line_start(cx);
846 move_line_start(cx);
837// A inserts at the end of each line with a selection
849// A inserts at the end of each line with a selection
838pub fn append_to_line(cx: &mut Context) {
850pub fn append_to_line(cx: &mut Context) {
839− let mut doc = cx.doc();
851+ let doc = cx.doc();
840− enter_insert_mode(&mut doc);
852+ enter_insert_mode(doc);
843 move_line_end(cx);
854 move_line_end(cx);
@@ -846,8 +857,8 @@ pub fn append_to_line(cx: &mut Context) {
846// o inserts a new line after each line with a selection
857// o inserts a new line after each line with a selection
847pub fn open_below(cx: &mut Context) {
858pub fn open_below(cx: &mut Context) {
848 let count = cx.count;
859 let count = cx.count;
849− let mut doc = cx.doc();
860+ let doc = cx.doc();
850− enter_insert_mode(&mut doc);
861+ enter_insert_mode(doc);
852 let lines = selection_lines(doc.text(), doc.selection());
863 let lines = selection_lines(doc.text(), doc.selection());
@@ -897,8 +908,8 @@ pub fn open_below(cx: &mut Context) {
897// O inserts a new line before each line with a selection
908// O inserts a new line before each line with a selection
898pub fn open_above(cx: &mut Context) {
909pub fn open_above(cx: &mut Context) {
899 let count = cx.count;
910 let count = cx.count;
900− let mut doc = cx.doc();
911+ let doc = cx.doc();
901− enter_insert_mode(&mut doc);
912+ enter_insert_mode(doc);
903 let lines = selection_lines(doc.text(), doc.selection());
914 let lines = selection_lines(doc.text(), doc.selection());
@@ -947,7 +958,7 @@ pub fn open_above(cx: &mut Context) {
949pub fn normal_mode(cx: &mut Context) {
960pub fn normal_mode(cx: &mut Context) {
950− let mut doc = cx.doc();
961+ let doc = cx.doc();
952 doc.mode = Mode::Normal;
963 doc.mode = Mode::Normal;
@@ -986,7 +997,7 @@ fn goto(cx: &mut Context, locations: Vec<lsp::Location>) {
986 match locations.as_slice() {
997 match locations.as_slice() {
987 [location] => {
998 [location] => {
988 cx.editor.open(PathBuf::from(location.uri.path()));
999 cx.editor.open(PathBuf::from(location.uri.path()));
989− let mut doc = cx.doc();
1000+ let doc = cx.doc();
990 let definition_pos = location.range.start;
1001 let definition_pos = location.range.start;
991 let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text(), definition_pos);
1002 let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text(), definition_pos);
992 doc.set_selection(Selection::point(new_pos));
1003 doc.set_selection(Selection::point(new_pos));
@@ -1003,7 +1014,8 @@ fn goto(cx: &mut Context, locations: Vec<lsp::Location>) {
1003 move |editor: &mut Editor, item| {
1014 move |editor: &mut Editor, item| {
1004 editor.open(PathBuf::from(item.uri.path()));
1015 editor.open(PathBuf::from(item.uri.path()));
1005 // TODO: issues with doc already being broo
1016 // 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];
1007 let definition_pos = item.range.start;
1019 let definition_pos = item.range.start;
1008 let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text(), definition_pos);
1020 let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text(), definition_pos);
1009 doc.set_selection(Selection::point(new_pos));
1021 doc.set_selection(Selection::point(new_pos));
@@ -1015,7 +1027,7 @@ fn goto(cx: &mut Context, locations: Vec<lsp::Location>) {
1017pub fn goto_definition(cx: &mut Context) {
1029pub fn goto_definition(cx: &mut Context) {
1018− let mut doc = cx.doc();
1030+ let doc = cx.doc();
1019 let language_server = match doc.language_server() {
1031 let language_server = match doc.language_server() {
1020 Some(language_server) => language_server,
1032 Some(language_server) => language_server,
1021 None => return,
1033 None => return,
@@ -1027,12 +1039,11 @@ pub fn goto_definition(cx: &mut Context) {
1027 // TODO: handle fails
1039 // TODO: handle fails
1028 let res =
1040 let res =
1029 smol::block_on(language_server.goto_definition(doc.identifier(), pos)).unwrap_or_default();
1041 smol::block_on(language_server.goto_definition(doc.identifier(), pos)).unwrap_or_default();
1031 goto(cx, res);
1042 goto(cx, res);
1034pub fn goto_type_definition(cx: &mut Context) {
1045pub fn goto_type_definition(cx: &mut Context) {
1035− let mut doc = cx.doc();
1046+ let doc = cx.doc();
1036 let language_server = match doc.language_server() {
1047 let language_server = match doc.language_server() {
1037 Some(language_server) => language_server,
1048 Some(language_server) => language_server,
1038 None => return,
1049 None => return,
@@ -1044,12 +1055,11 @@ pub fn goto_type_definition(cx: &mut Context) {
1044 // TODO: handle fails
1055 // TODO: handle fails
1045 let res = smol::block_on(language_server.goto_type_definition(doc.identifier(), pos))
1056 let res = smol::block_on(language_server.goto_type_definition(doc.identifier(), pos))
1046 .unwrap_or_default();
1057 .unwrap_or_default();
1048 goto(cx, res);
1058 goto(cx, res);
1051pub fn goto_implementation(cx: &mut Context) {
1061pub fn goto_implementation(cx: &mut Context) {
1052− let mut doc = cx.doc();
1062+ let doc = cx.doc();
1053 let language_server = match doc.language_server() {
1063 let language_server = match doc.language_server() {
1054 Some(language_server) => language_server,
1064 Some(language_server) => language_server,
1055 None => return,
1065 None => return,
@@ -1061,12 +1071,11 @@ pub fn goto_implementation(cx: &mut Context) {
1061 // TODO: handle fails
1071 // TODO: handle fails
1062 let res = smol::block_on(language_server.goto_implementation(doc.identifier(), pos))
1072 let res = smol::block_on(language_server.goto_implementation(doc.identifier(), pos))
1063 .unwrap_or_default();
1073 .unwrap_or_default();
1065 goto(cx, res);
1074 goto(cx, res);
1068pub fn goto_reference(cx: &mut Context) {
1077pub fn goto_reference(cx: &mut Context) {
1069− let mut doc = cx.doc();
1078+ let doc = cx.doc();
1070 let language_server = match doc.language_server() {
1079 let language_server = match doc.language_server() {
1071 Some(language_server) => language_server,
1080 Some(language_server) => language_server,
1072 None => return,
1081 None => return,
@@ -1078,7 +1087,6 @@ pub fn goto_reference(cx: &mut Context) {
1078 // TODO: handle fails
1087 // TODO: handle fails
1079 let res =
1088 let res =
1080 smol::block_on(language_server.goto_reference(doc.identifier(), pos)).unwrap_or_default();
1089 smol::block_on(language_server.goto_reference(doc.identifier(), pos)).unwrap_or_default();
1082 goto(cx, res);
1090 goto(cx, res);
@@ -1092,7 +1100,7 @@ pub mod insert {
1093 // TODO: insert means add text just before cursor, on exit we should be on the last letter.
1101 // TODO: insert means add text just before cursor, on exit we should be on the last letter.
1094 pub fn insert_char(cx: &mut Context, c: char) {
1102 pub fn insert_char(cx: &mut Context, c: char) {
1095− let mut doc = cx.doc();
1103+ let doc = cx.doc();
1097 // run through insert hooks, stopping on the first one that returns Some(t)
1105 // run through insert hooks, stopping on the first one that returns Some(t)
1098 for hook in HOOKS {
1106 for hook in HOOKS {
@@ -1109,7 +1117,7 @@ pub mod insert {
1111 pub fn insert_tab(cx: &mut Context) {
1119 pub fn insert_tab(cx: &mut Context) {
1112− let mut doc = cx.doc();
1120+ let doc = cx.doc();
1113 // TODO: round out to nearest indentation level (for example a line with 3 spaces should
1121 // TODO: round out to nearest indentation level (for example a line with 3 spaces should
1114 // indent by one to reach 4 spaces).
1122 // indent by one to reach 4 spaces).
@@ -1119,7 +1127,7 @@ pub mod insert {
1121 pub fn insert_newline(cx: &mut Context) {
1129 pub fn insert_newline(cx: &mut Context) {
1122− let mut doc = cx.doc();
1130+ let doc = cx.doc();
1123 let text = doc.text().slice(..);
1131 let text = doc.text().slice(..);
1124 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
1132 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
1125 let indent_level =
1133 let indent_level =
@@ -1136,7 +1144,7 @@ pub mod insert {
1136 // TODO: handle indent-aware delete
1144 // TODO: handle indent-aware delete
1137 pub fn delete_char_backward(cx: &mut Context) {
1145 pub fn delete_char_backward(cx: &mut Context) {
1138 let count = cx.count;
1146 let count = cx.count;
1139− let mut doc = cx.doc();
1147+ let doc = cx.doc();
1140 let text = doc.text().slice(..);
1148 let text = doc.text().slice(..);
1141 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
1149 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
@@ -1150,7 +1158,7 @@ pub mod insert {
1151 pub fn delete_char_forward(cx: &mut Context) {
1159 pub fn delete_char_forward(cx: &mut Context) {
1152 let count = cx.count;
1160 let count = cx.count;
1153− let mut doc = cx.doc();
1161+ let doc = cx.doc();
1154 let text = doc.text().slice(..);
1162 let text = doc.text().slice(..);
1155 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
1163 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
@@ -1180,7 +1188,7 @@ pub fn redo(cx: &mut Context) {
1181pub fn yank(cx: &mut Context) {
1189pub fn yank(cx: &mut Context) {
1182 // TODO: should selections be made end inclusive?
1190 // TODO: should selections be made end inclusive?
1183− let mut doc = cx.doc();
1191+ let doc = cx.doc();
1184 let values = doc
1192 let values = doc
1185 .selection()
1193 .selection()
1186 .fragments(doc.text().slice(..))
1194 .fragments(doc.text().slice(..))
@@ -1221,7 +1229,7 @@ pub fn paste(cx: &mut Context) {
1222 let mut values = values.into_iter().map(Tendril::from).chain(repeat);
1230 let mut values = values.into_iter().map(Tendril::from).chain(repeat);
1224− let mut doc = cx.doc();
1232+ let doc = cx.doc();
1226 let transaction = if linewise {
1234 let transaction = if linewise {
1227 // paste on the next line
1235 // paste on the next line
@@ -1260,8 +1268,8 @@ fn get_lines(doc: &Document) -> Vec<usize> {
1262pub fn indent(cx: &mut Context) {
1270pub fn indent(cx: &mut Context) {
1263− let mut doc = cx.doc();
1271+ let doc = cx.doc();
1264− let lines = get_lines(&mut doc);
1272+ let lines = get_lines(doc);
1266 // Indent by one level
1274 // Indent by one level
1267 let indent = Tendril::from(doc.indent_unit());
1275 let indent = Tendril::from(doc.indent_unit());
@@ -1278,8 +1286,8 @@ pub fn indent(cx: &mut Context) {
1280pub fn unindent(cx: &mut Context) {
1288pub fn unindent(cx: &mut Context) {
1281− let mut doc = cx.doc();
1289+ let doc = cx.doc();
1282− let lines = get_lines(&mut doc);
1290+ let lines = get_lines(doc);
1283 let mut changes = Vec::with_capacity(lines.len());
1291 let mut changes = Vec::with_capacity(lines.len());
1284 let tab_width = doc.tab_width();
1292 let tab_width = doc.tab_width();
@@ -1313,7 +1321,7 @@ pub fn unindent(cx: &mut Context) {
1314pub fn format_selections(cx: &mut Context) {
1322pub fn format_selections(cx: &mut Context) {
1315 use helix_lsp::lsp;
1323 use helix_lsp::lsp;
1316− let mut doc = cx.doc();
1324+ let doc = cx.doc();
1318 // via lsp if available
1326 // via lsp if available
1319 // else via tree-sitter indentation calculations
1327 // else via tree-sitter indentation calculations
@@ -1351,7 +1359,7 @@ pub fn format_selections(cx: &mut Context) {
1352pub fn join_selections(cx: &mut Context) {
1360pub fn join_selections(cx: &mut Context) {
1353 use movement::skip_over_next;
1361 use movement::skip_over_next;
1354− let mut doc = cx.doc();
1362+ let doc = cx.doc();
1355 let text = doc.text();
1363 let text = doc.text();
1356 let slice = doc.text().slice(..);
1364 let slice = doc.text().slice(..);
@@ -1407,7 +1415,7 @@ pub fn keep_selections(cx: &mut Context) {
1409pub fn keep_primary_selection(cx: &mut Context) {
1417pub fn keep_primary_selection(cx: &mut Context) {
1410− let mut doc = cx.doc();
1418+ let doc = cx.doc();
1412 let range = doc.selection().primary();
1420 let range = doc.selection().primary();
1413 let selection = Selection::single(range.anchor, range.head);
1421 let selection = Selection::single(range.anchor, range.head);
@@ -1424,7 +1432,7 @@ pub fn save(cx: &mut Context) {
1426pub fn completion(cx: &mut Context) {
1434pub fn completion(cx: &mut Context) {
1427− let mut doc = cx.doc();
1435+ let doc = cx.doc();
1429 let language_server = match doc.language_server() {
1437 let language_server = match doc.language_server() {
1430 Some(language_server) => language_server,
1438 Some(language_server) => language_server,
@@ -1438,8 +1446,6 @@ pub fn completion(cx: &mut Context) {
1439 let res = smol::block_on(language_server.completion(doc.identifier(), pos)).unwrap_or_default();
1447 let res = smol::block_on(language_server.completion(doc.identifier(), pos)).unwrap_or_default();
1443 // TODO: if no completion, show some message or something
1449 // TODO: if no completion, show some message or something
1444 if !res.is_empty() {
1450 if !res.is_empty() {
1445 // let snapshot = doc.state.clone();
1451 // let snapshot = doc.state.clone();
@@ -1455,11 +1461,13 @@ pub fn completion(cx: &mut Context) {
1455 match event {
1461 match event {
1456 PromptEvent::Abort => {
1462 PromptEvent::Abort => {
1457 // revert state
1463 // revert state
1464+ // let id = editor.view().doc;
1458− let doc = &mut editor.view().doc.borrow_mut();
1465+ // let doc = &mut editor.documents[id];
1459 // doc.state = snapshot.clone();
1466 // doc.state = snapshot.clone();
1461 PromptEvent::Validate => {
1468 PromptEvent::Validate => {
1469+ let id = editor.view().doc;
1462− let doc = &mut editor.view().doc.borrow_mut();
1470+ let doc = &mut editor.documents[id];
1464 // revert state to what it was before the last update
1472 // revert state to what it was before the last update
1465 // doc.state = snapshot.clone();
1473 // doc.state = snapshot.clone();
@@ -1525,7 +1533,7 @@ pub fn completion(cx: &mut Context) {
1525pub fn hover(cx: &mut Context) {
1533pub fn hover(cx: &mut Context) {
1526 use helix_lsp::lsp;
1534 use helix_lsp::lsp;
1528− let mut doc = cx.doc();
1536+ let doc = cx.doc();
1530 let language_server = match doc.language_server() {
1538 let language_server = match doc.language_server() {
1531 Some(language_server) => language_server,
1539 Some(language_server) => language_server,
@@ -1542,8 +1550,6 @@ pub fn hover(cx: &mut Context) {
1542 let res = smol::block_on(language_server.text_document_hover(doc.identifier(), pos))
1550 let res = smol::block_on(language_server.text_document_hover(doc.identifier(), pos))
1543 .unwrap_or_default();
1551 .unwrap_or_default();
1547 if let Some(hover) = res {
1553 if let Some(hover) = res {
1548 // hover.contents / .range <- used for visualizing
1554 // hover.contents / .range <- used for visualizing
1549 let contents = match hover.contents {
1555 let contents = match hover.contents {
@@ -1574,7 +1580,7 @@ pub fn next_view(cx: &mut Context) {
1575// comments
1581// comments
1576pub fn toggle_comments(cx: &mut Context) {
1582pub fn toggle_comments(cx: &mut Context) {
1577− let mut doc = cx.doc();
1583+ let doc = cx.doc();
1578 let transaction = comment::toggle_line_comments(doc.text(), doc.selection());
1584 let transaction = comment::toggle_line_comments(doc.text(), doc.selection());
1580 doc.apply(&transaction);
1586 doc.apply(&transaction);
@@ -1584,7 +1590,7 @@ pub fn toggle_comments(cx: &mut Context) {
1584// tree sitter node selection
1590// tree sitter node selection
1586pub fn expand_selection(cx: &mut Context) {
1592pub fn expand_selection(cx: &mut Context) {
1587− let mut doc = cx.doc();
1593+ let doc = cx.doc();
1589 if let Some(syntax) = doc.syntax() {
1595 if let Some(syntax) = doc.syntax() {
1590 let text = doc.text().slice(..);
1596 let text = doc.text().slice(..);
@@ -1594,7 +1600,7 @@ pub fn expand_selection(cx: &mut Context) {
1596pub fn match_brackets(cx: &mut Context) {
1602pub fn match_brackets(cx: &mut Context) {
1597− let mut doc = cx.doc();
1603+ let doc = cx.doc();
1599 if let Some(syntax) = doc.syntax() {
1605 if let Some(syntax) = doc.syntax() {
1600 let pos = doc.selection().cursor();
1606 let pos = doc.selection().cursor();