on master help sign in
Drop refcell use, make view simply ref doc.id.
commit8328fe926d34d12031cb50db47a9dd39ed681a84 authorBlaž Hrastnik <blaz@mxxn.io> dateTue 23 Mar 2021, 17:47 (6 years ago) parent3f9a94f
8 files changed+233−193
helix-term/src/application.rs+1−3 helix-term/src/commands.rs+110−104 helix-term/src/ui/editor.rs+13−14 helix-term/src/ui/mod.rs+35−36 helix-view/src/document.rs+4 helix-view/src/editor.rs+46−24 helix-view/src/tree.rs+12−2 helix-view/src/view.rs+12−10
helix-term/src/application.rs+1−3
@@ -129,10 +129,9 @@ impl Application {
129129 .editor
130130 .documents
131131 .iter_mut()
132− .find(|(_, doc)| doc.borrow().path() == path.as_ref());
132+ .find(|(_, doc)| doc.path() == path.as_ref());
133133
134134 if let Some((_, doc)) = doc {
135− let mut doc = doc.borrow_mut();
136135 let text = doc.text();
137136
138137 let diagnostics = params
@@ -167,7 +166,6 @@ impl Application {
167166 .collect();
168167
169168 doc.diagnostics = diagnostics;
170− drop(doc);
171169 // TODO: we want to process all the events in queue, then render. publishDiagnostic tends to send a whole bunch of events
172170 self.render();
173171 }
helix-term/src/commands.rs+110−104
@@ -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, &regex);
628+ _search(doc, &contents, &regex);
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();
helix-term/src/ui/editor.rs+13−14
@@ -39,6 +39,7 @@ impl EditorView {
3939 }
4040 pub fn render_view(
4141 &self,
42+ doc: &Document,
4243 view: &View,
4344 viewport: Rect,
4445 surface: &mut Surface,
@@ -51,13 +52,12 @@ impl EditorView {
5152 viewport.width - OFFSET,
5253 viewport.height.saturating_sub(1),
5354 ); // - 1 for statusline
54− self.render_buffer(view, area, surface, theme, is_focused);
55+ self.render_buffer(&doc, view, area, surface, theme, is_focused);
5556
5657 // clear with background color
5758 // TODO: this seems to prevent setting style later
5859 // surface.set_style(viewport, theme.get("ui.background"));
5960
60− let doc = view.doc.borrow();
6161 self.render_diagnostics(&doc, area, surface, theme, is_focused);
6262
6363 let area = Rect::new(
@@ -71,16 +71,16 @@ impl EditorView {
7171
7272 pub fn render_buffer(
7373 &self,
74+ doc: &Document,
7475 view: &View,
7576 viewport: Rect,
7677 surface: &mut Surface,
7778 theme: &Theme,
7879 is_focused: bool,
7980 ) {
80− let doc = view.doc.borrow();
8181 let text = doc.text().slice(..);
8282
83− let last_line = view.last_line();
83+ let last_line = view.last_line(doc);
8484
8585 let range = {
8686 // calculate viewport byte ranges
@@ -211,16 +211,14 @@ impl EditorView {
211211 // let selection_style = Style::default().bg(Color::Rgb(94, 0, 128));
212212 let selection_style = Style::default().bg(Color::Rgb(84, 0, 153));
213213
214− for selection in view
215− .doc
216− .borrow()
214+ for selection in doc
217215 .selection()
218216 .iter()
219217 .filter(|range| range.overlaps(&screen))
220218 {
221219 // TODO: render also if only one of the ranges is in viewport
222− let mut start = view.screen_coords_at_pos(text, selection.anchor);
223− let mut end = view.screen_coords_at_pos(text, selection.head);
220+ let mut start = view.screen_coords_at_pos(doc, text, selection.anchor);
221+ let mut end = view.screen_coords_at_pos(doc, text, selection.head);
224222
225223 // cursor
226224 if let Some(end) = end {
@@ -292,7 +290,6 @@ impl EditorView {
292290 let info: Style = theme.get("info");
293291 let hint: Style = theme.get("hint");
294292
295− let last_line = view.last_line();
296293 for (i, line) in (view.first_line..last_line).enumerate() {
297294 use helix_core::diagnostic::Severity;
298295 if let Some(diagnostic) = doc.diagnostics.iter().find(|d| d.line == line) {
@@ -431,11 +428,12 @@ impl Component for EditorView {
431428 match event {
432429 Event::Resize(width, height) => {
433430 // HAXX: offset the render area height by 1 to account for prompt/commandline
434− cx.editor.tree.resize(Rect::new(0, 0, width, height - 1));
431+ cx.editor.resize(Rect::new(0, 0, width, height - 1));
435432 EventResult::Consumed(None)
436433 }
437434 Event::Key(event) => {
438− let mode = cx.editor.view().doc.borrow().mode();
435+ let id = cx.editor.view().doc;
436+ let mode = cx.editor.document(id).unwrap().mode();
439437
440438 let mut cxt = commands::Context {
441439 editor: &mut cx.editor,
@@ -491,7 +489,7 @@ impl Component for EditorView {
491489 // appease borrowck
492490 let callback = cxt.callback.take();
493491 drop(cxt);
494− cx.editor.view_mut().ensure_cursor_in_view();
492+ cx.editor.ensure_cursor_in_view(cx.editor.tree.focus);
495493
496494 EventResult::Consumed(callback)
497495 }
@@ -501,7 +499,8 @@ impl Component for EditorView {
501499
502500 fn render(&self, mut area: Rect, surface: &mut Surface, cx: &mut Context) {
503501 for (view, is_focused) in cx.editor.tree.views() {
504− self.render_view(view, view.area, surface, &cx.editor.theme, is_focused);
502+ let doc = cx.editor.document(view.doc).unwrap();
503+ self.render_view(doc, view, view.area, surface, &cx.editor.theme, is_focused);
505504 }
506505 }
507506
helix-term/src/ui/mod.rs+35−36
@@ -40,7 +40,8 @@ pub fn regex_prompt(
4040 match event {
4141 PromptEvent::Abort => {
4242 // TODO: also revert text
43− let doc = &mut editor.view().doc.borrow_mut();
43+ let id = editor.view().doc;
44+ let doc = &mut editor.documents[id];
4445 doc.set_selection(snapshot.clone());
4546 }
4647 PromptEvent::Validate => {
@@ -54,18 +55,17 @@ pub fn regex_prompt(
5455
5556 match Regex::new(input) {
5657 Ok(regex) => {
57− let view = &mut editor.view_mut();
58− let mut doc = view.doc.borrow_mut();
58+ // let view = &mut editor.view_mut();
59+ let id = editor.view().doc;
60+ let doc = &mut editor.documents[id];
5961
6062 // revert state to what it was before the last update
6163 // TODO: also revert text
6264 doc.set_selection(snapshot.clone());
6365
64− fun(&mut doc, regex);
66+ fun(doc, regex);
6567
66− drop(doc);
67−
68− view.ensure_cursor_in_view();
68+ editor.ensure_cursor_in_view(editor.view().id);
6969 }
7070 Err(_err) => (), // TODO: mark command line as error
7171 }
@@ -101,40 +101,39 @@ pub fn file_picker(root: &str) -> Picker<PathBuf> {
101101 path.strip_prefix("./").unwrap().to_str().unwrap().into()
102102 },
103103 move |editor: &mut Editor, path: &PathBuf| {
104− editor.open(path.into());
104+ let document_id = editor.open(path.into()).expect("editor.open failed");
105105 },
106106 )
107107}
108108
109109use helix_view::View;
110−pub fn buffer_picker(views: &[View], current: usize) -> Picker<(Option<PathBuf>, usize)> {
111− unimplemented!();
112− // use helix_view::Editor;
113− // Picker::new(
114− // views
115− // .iter()
116− // .enumerate()
117− // .map(|(i, view)| (view.doc.relative_path().map(Path::to_path_buf), i))
118− // .collect(),
119− // move |(path, index): &(Option<PathBuf>, usize)| {
120− // // format_fn
121− // match path {
122− // Some(path) => {
123− // if *index == current {
124− // format!("{} (*)", path.to_str().unwrap()).into()
125− // } else {
126− // path.to_str().unwrap().into()
127− // }
128− // }
129− // None => "[NEW]".into(),
130− // }
131− // },
132− // |editor: &mut Editor, &(_, index): &(Option<PathBuf>, usize)| {
133− // if index < editor.views.len() {
134− // editor.focus = index;
135− // }
136− // },
137− // )
110+pub fn buffer_picker(buffers: &[Document], current: usize) -> Picker<(Option<PathBuf>, usize)> {
111+ use helix_view::Editor;
112+ Picker::new(
113+ buffers
114+ .iter()
115+ .enumerate()
116+ .map(|(i, doc)| (doc.relative_path().map(Path::to_path_buf), i))
117+ .collect(),
118+ move |(path, index): &(Option<PathBuf>, usize)| {
119+ // format_fn
120+ match path {
121+ Some(path) => {
122+ if *index == current {
123+ format!("{} (*)", path.to_str().unwrap()).into()
124+ } else {
125+ path.to_str().unwrap().into()
126+ }
127+ }
128+ None => "[NEW]".into(),
129+ }
130+ },
131+ |editor: &mut Editor, &(_, index): &(Option<PathBuf>, usize)| {
132+ // if index < editor.views.len() {
133+ // editor.focus = index;
134+ // }
135+ },
136+ )
138137}
139138
140139pub mod completers {
helix-view/src/document.rs+4
@@ -8,6 +8,8 @@ use helix_core::{
88 ChangeSet, Diagnostic, History, Rope, Selection, State, Syntax, Transaction,
99};
1010
11+use crate::DocumentId;
12+
1113#[derive(Copy, Clone, PartialEq, Eq, Hash)]
1214pub enum Mode {
1315 Normal,
@@ -18,6 +20,7 @@ pub enum Mode {
1820
1921pub struct Document {
2022 // rope + selection
23+ pub(crate) id: DocumentId,
2124 state: State,
2225 path: Option<PathBuf>,
2326
@@ -66,6 +69,7 @@ impl Document {
6669 let old_state = None;
6770
6871 Self {
72+ id: DocumentId::default(),
6973 path: None,
7074 state: State::new(text),
7175 mode: Mode::Normal,
helix-view/src/editor.rs+46−24
@@ -1,7 +1,7 @@
11use crate::{theme::Theme, tree::Tree, Document, DocumentId, View};
2+use tui::layout::Rect;
23
34use std::path::PathBuf;
4−use std::{cell::RefCell, rc::Rc};
55
66use slotmap::{DefaultKey as Key, SlotMap};
77
@@ -9,7 +9,7 @@ use anyhow::Error;
99
1010pub struct Editor {
1111 pub tree: Tree,
12− pub documents: SlotMap<DocumentId, Rc<RefCell<Document>>>,
12+ pub documents: SlotMap<DocumentId, Document>,
1313 pub count: Option<usize>,
1414 pub theme: Theme,
1515 pub language_servers: helix_lsp::Registry,
@@ -34,18 +34,25 @@ impl Editor {
3434 }
3535 }
3636
37− pub fn open(&mut self, path: PathBuf) -> Result<(), Error> {
38− // TODO: issues with doc already being borrowed if called from inside goto()
37+ fn _refresh(&mut self) {
38+ for (view, _) in self.tree.views_mut() {
39+ let doc = &self.documents[view.doc];
40+ view.ensure_cursor_in_view(doc)
41+ }
42+ }
3943
40− let existing_view = self
41− .tree
42− .views()
43− .find(|(view, _)| view.doc.borrow().path() == Some(&path));
44+ pub fn open(&mut self, path: PathBuf) -> Result<DocumentId, Error> {
45+ let existing_view = self.documents().find(|doc| doc.path() == Some(&path));
4446
45− if let Some((view, _)) = existing_view {
46− self.tree.focus = view.id;
47− return Ok(());
48− }
47+ // TODO:
48+ // if view with doc, focus it
49+ // else open new split
50+
51+ // if let Some((view, _)) = existing_view {
52+ // let id = view.doc.id;
53+ // self.tree.focus = view.id;
54+ // return Ok(id);
55+ // }
4956
5057 let mut doc = Document::load(path, self.theme.scopes())?;
5158
@@ -73,13 +80,14 @@ impl Editor {
7380 .unwrap();
7481 }
7582
76− let doc = Rc::new(RefCell::new(doc));
77− // TODO: store id as doc.id
78− let id = self.documents.insert(doc.clone());
83+ let id = self.documents.insert(doc);
84+ self.documents[id].id = id;
7985
80− let view = View::new(doc)?;
86+ let view = View::new(id)?;
8187 self.tree.insert(view);
82− Ok(())
88+ self._refresh();
89+
90+ Ok(id)
8391 }
8492
8593 pub fn close(&mut self, id: Key) {
@@ -88,7 +96,7 @@ impl Editor {
8896 let language_servers = &mut self.language_servers;
8997 let executor = self.executor;
9098
91− let doc = view.doc.borrow();
99+ let doc = &self.documents[view.doc];
92100
93101 let language_server = doc
94102 .language
@@ -99,14 +107,14 @@ impl Editor {
99107 smol::block_on(language_server.text_document_did_close(doc.identifier())).unwrap();
100108 }
101109
102− drop(doc); // to stop borrowing self.tree
103−
104110 // self.documents.remove(view.doc);
105111 self.tree.remove(id);
112+ self._refresh();
106113 }
107114
108− pub fn resize(&mut self) {
109− self.tree.focus_next();
115+ pub fn resize(&mut self, area: Rect) {
116+ self.tree.resize(area);
117+ self._refresh();
110118 }
111119
112120 pub fn focus_next(&mut self) {
@@ -125,12 +133,26 @@ impl Editor {
125133 self.tree.get_mut(self.tree.focus)
126134 }
127135
136+ pub fn ensure_cursor_in_view(&mut self, id: Key) {
137+ let view = self.tree.get_mut(id);
138+ let doc = &self.documents[view.doc];
139+ view.ensure_cursor_in_view(doc)
140+ }
141+
142+ pub fn document(&self, id: DocumentId) -> Option<&Document> {
143+ self.documents.get(id)
144+ }
145+
146+ pub fn documents(&self) -> impl Iterator<Item = &Document> {
147+ self.documents.iter().map(|(_id, doc)| doc)
148+ }
149+
128150 pub fn cursor_position(&self) -> Option<helix_core::Position> {
129151 const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter
130152 let view = self.view();
131− let doc = view.doc.borrow();
153+ let doc = &self.documents[view.doc];
132154 let cursor = doc.selection().cursor();
133− if let Some(mut pos) = view.screen_coords_at_pos(doc.text().slice(..), cursor) {
155+ if let Some(mut pos) = view.screen_coords_at_pos(doc, doc.text().slice(..), cursor) {
134156 pos.col += view.area.x as usize + OFFSET as usize;
135157 pos.row += view.area.y as usize;
136158 return Some(pos);
helix-view/src/tree.rs+12−2
@@ -164,7 +164,18 @@ impl Tree {
164164 self.recalculate()
165165 }
166166
167− pub fn views(&mut self) -> impl Iterator<Item = (&mut View, bool)> {
167+ pub fn views(&self) -> impl Iterator<Item = (&View, bool)> {
168+ let focus = self.focus;
169+ self.nodes.iter().filter_map(move |(key, node)| match node {
170+ Node {
171+ content: Content::View(view),
172+ ..
173+ } => Some((view.as_ref(), focus == key)),
174+ _ => None,
175+ })
176+ }
177+
178+ pub fn views_mut(&mut self) -> impl Iterator<Item = (&mut View, bool)> {
168179 let focus = self.focus;
169180 self.nodes
170181 .iter_mut()
@@ -231,7 +242,6 @@ impl Tree {
231242 Content::View(view) => {
232243 // debug!!("setting view area {:?}", area);
233244 view.area = area;
234− view.ensure_cursor_in_view();
235245 } // TODO: call f()
236246 Content::Container(container) => {
237247 // debug!!("setting container area {:?}", area);
helix-view/src/view.rs+12−10
@@ -1,7 +1,6 @@
11use anyhow::Error;
22
33use std::borrow::Cow;
4−use std::{cell::RefCell, rc::Rc};
54
65use crate::{Document, DocumentId};
76use helix_core::{
@@ -15,13 +14,13 @@ pub const PADDING: usize = 5;
1514
1615pub struct View {
1716 pub id: Key,
18− pub doc: Rc<RefCell<Document>>,
17+ pub doc: DocumentId,
1918 pub first_line: usize,
2019 pub area: Rect,
2120}
2221
2322impl View {
24− pub fn new(doc: Rc<RefCell<Document>>) -> Result<Self, Error> {
23+ pub fn new(doc: DocumentId) -> Result<Self, Error> {
2524 let view = Self {
2625 id: Key::default(),
2726 doc,
@@ -32,8 +31,7 @@ impl View {
3231 Ok(view)
3332 }
3433
35− pub fn ensure_cursor_in_view(&mut self) {
36− let doc = self.doc.borrow();
34+ pub fn ensure_cursor_in_view(&mut self, doc: &Document) {
3735 let cursor = doc.selection().cursor();
3836 let line = doc.text().char_to_line(cursor);
3937 let document_end = self.first_line + (self.area.height as usize).saturating_sub(2);
@@ -51,8 +49,7 @@ impl View {
5149
5250 /// Calculates the last visible line on screen
5351 #[inline]
54− pub fn last_line(&self) -> usize {
55− let doc = self.doc.borrow();
52+ pub fn last_line(&self, doc: &Document) -> usize {
5653 let height = self.area.height.saturating_sub(1); // - 1 for statusline
5754 std::cmp::min(
5855 self.first_line + height as usize,
@@ -63,10 +60,15 @@ impl View {
6360 /// Translates a document position to an absolute position in the terminal.
6461 /// Returns a (line, col) position if the position is visible on screen.
6562 // TODO: Could return width as well for the character width at cursor.
66− pub fn screen_coords_at_pos(&self, text: RopeSlice, pos: usize) -> Option<Position> {
63+ pub fn screen_coords_at_pos(
64+ &self,
65+ doc: &Document,
66+ text: RopeSlice,
67+ pos: usize,
68+ ) -> Option<Position> {
6769 let line = text.char_to_line(pos);
6870
69− if line < self.first_line || line > self.last_line() {
71+ if line < self.first_line || line > self.last_line(doc) {
7072 // Line is not visible on screen
7173 return None;
7274 }
@@ -74,7 +76,7 @@ impl View {
7476 let line_start = text.line_to_char(line);
7577 let line_slice = text.slice(line_start..pos);
7678 let mut col = 0;
77− let tab_width = self.doc.borrow().tab_width();
79+ let tab_width = doc.tab_width();
7880
7981 for grapheme in RopeGraphemes::new(line_slice) {
8082 if grapheme == "\t" {