on master help sign in
Store Document on the Editor type, make View reference it.
commitbf95ee27aa3c9f137e34f30a01f25e4930a8b1bb authorBlaž Hrastnik <blaz@mxxn.io> dateTue 16 Mar 2021, 18:27 (6 years ago) parent5e6716c
7 files changed+169−127
helix-term/src/application.rs+12−11 helix-term/src/commands.rs+99−87 helix-term/src/ui/editor.rs+11−10 helix-term/src/ui/mod.rs+5−4 helix-view/src/editor.rs+29−8 helix-view/src/lib.rs+3 helix-view/src/view.rs+10−7
helix-term/src/application.rs+12−11
@@ -125,15 +125,16 @@ impl Application {
125125 Notification::PublishDiagnostics(params) => {
126126 let path = Some(params.uri.to_file_path().unwrap());
127127
128− let view = self
128+ let doc = self
129129 .editor
130− .tree
131− .views()
132− .map(|(view, _key)| view)
133− .find(|view| view.doc.path() == path.as_ref());
130+ .documents
131+ .iter_mut()
132+ .find(|(_, doc)| doc.borrow().path() == path.as_ref());
133+
134+ if let Some((_, doc)) = doc {
135+ let mut doc = doc.borrow_mut();
136+ let text = doc.text();
134137
135− if let Some(view) = view {
136− let doc = view.doc.text();
137138 let diagnostics = params
138139 .diagnostics
139140 .into_iter()
@@ -144,8 +145,8 @@ impl Application {
144145 };
145146 use helix_lsp::{lsp, util::lsp_pos_to_pos};
146147 use lsp::DiagnosticSeverity;
147− let start = lsp_pos_to_pos(doc, diagnostic.range.start);
148− let end = lsp_pos_to_pos(doc, diagnostic.range.end);
148+ let start = lsp_pos_to_pos(text, diagnostic.range.start);
149+ let end = lsp_pos_to_pos(text, diagnostic.range.end);
149150
150151 Diagnostic {
151152 range: Range { start, end },
@@ -165,8 +166,8 @@ impl Application {
165166 })
166167 .collect();
167168
168− view.doc.diagnostics = diagnostics;
169−
169+ doc.diagnostics = diagnostics;
170+ drop(doc);
170171 // TODO: we want to process all the events in queue, then render. publishDiagnostic tends to send a whole bunch of events
171172 self.render();
172173 }
helix-term/src/commands.rs+99−87
@@ -10,7 +10,7 @@ use helix_core::{
1010use once_cell::sync::Lazy;
1111
1212use crate::{
13− compositor::Compositor,
13+ compositor::{Callback, Compositor},
1414 ui::{self, Popup, Prompt, PromptEvent},
1515};
1616
@@ -41,8 +41,8 @@ impl<'a> Context<'a> {
4141 }
4242
4343 #[inline]
44− pub fn doc(&mut self) -> &mut Document {
45− &mut self.editor.view_mut().doc
44+ pub fn doc(&mut self) -> std::cell::RefMut<Document> {
45+ self.editor.view().doc.borrow_mut()
4646 }
4747
4848 /// Push a new component onto the compositor.
@@ -72,7 +72,7 @@ pub type Command = fn(cx: &mut Context);
7272
7373pub fn move_char_left(cx: &mut Context) {
7474 let count = cx.count;
75− let doc = cx.doc();
75+ let mut doc = cx.doc();
7676 let text = doc.text().slice(..);
7777 let selection = doc.selection().transform(|range| {
7878 movement::move_horizontally(
@@ -88,7 +88,7 @@ pub fn move_char_left(cx: &mut Context) {
8888
8989pub fn move_char_right(cx: &mut Context) {
9090 let count = cx.count;
91− let doc = cx.doc();
91+ let mut doc = cx.doc();
9292 let text = doc.text().slice(..);
9393 let selection = doc.selection().transform(|range| {
9494 movement::move_horizontally(
@@ -104,7 +104,7 @@ pub fn move_char_right(cx: &mut Context) {
104104
105105pub fn move_line_up(cx: &mut Context) {
106106 let count = cx.count;
107− let doc = cx.doc();
107+ let mut doc = cx.doc();
108108 let text = doc.text().slice(..);
109109 let selection = doc.selection().transform(|range| {
110110 movement::move_vertically(
@@ -120,7 +120,7 @@ pub fn move_line_up(cx: &mut Context) {
120120
121121pub fn move_line_down(cx: &mut Context) {
122122 let count = cx.count;
123− let doc = cx.doc();
123+ let mut doc = cx.doc();
124124 let text = doc.text().slice(..);
125125 let selection = doc.selection().transform(|range| {
126126 movement::move_vertically(
@@ -135,7 +135,7 @@ pub fn move_line_down(cx: &mut Context) {
135135}
136136
137137pub fn move_line_end(cx: &mut Context) {
138− let doc = cx.doc();
138+ let mut doc = cx.doc();
139139 let lines = selection_lines(doc.text(), doc.selection());
140140
141141 let positions = lines
@@ -155,7 +155,7 @@ pub fn move_line_end(cx: &mut Context) {
155155}
156156
157157pub fn move_line_start(cx: &mut Context) {
158− let doc = cx.doc();
158+ let mut doc = cx.doc();
159159 let lines = selection_lines(doc.text(), doc.selection());
160160
161161 let positions = lines
@@ -177,7 +177,7 @@ pub fn move_line_start(cx: &mut Context) {
177177
178178pub fn move_next_word_start(cx: &mut Context) {
179179 let count = cx.count;
180− let doc = cx.doc();
180+ let mut doc = cx.doc();
181181 let text = doc.text().slice(..);
182182
183183 let selection = doc.selection().transform(|range| {
@@ -190,7 +190,7 @@ pub fn move_next_word_start(cx: &mut Context) {
190190
191191pub fn move_prev_word_start(cx: &mut Context) {
192192 let count = cx.count;
193− let doc = cx.doc();
193+ let mut doc = cx.doc();
194194 let text = doc.text().slice(..);
195195
196196 let selection = doc.selection().transform(|range| {
@@ -203,7 +203,7 @@ pub fn move_prev_word_start(cx: &mut Context) {
203203
204204pub fn move_next_word_end(cx: &mut Context) {
205205 let count = cx.count;
206− let doc = cx.doc();
206+ let mut doc = cx.doc();
207207 let text = doc.text().slice(..);
208208
209209 let selection = doc.selection().transform(|range| {
@@ -215,14 +215,14 @@ pub fn move_next_word_end(cx: &mut Context) {
215215}
216216
217217pub fn move_file_start(cx: &mut Context) {
218− let doc = cx.doc();
218+ let mut doc = cx.doc();
219219 doc.set_selection(Selection::point(0));
220220
221221 doc.mode = Mode::Normal;
222222}
223223
224224pub fn move_file_end(cx: &mut Context) {
225− let doc = cx.doc();
225+ let mut doc = cx.doc();
226226 let text = doc.text();
227227 let last_line = text.line_to_char(text.len_lines().saturating_sub(2));
228228 doc.set_selection(Selection::point(last_line));
@@ -232,7 +232,7 @@ pub fn move_file_end(cx: &mut Context) {
232232
233233pub fn extend_next_word_start(cx: &mut Context) {
234234 let count = cx.count;
235− let doc = cx.doc();
235+ let mut doc = cx.doc();
236236 let text = doc.text().slice(..);
237237
238238 let selection = doc.selection().transform(|mut range| {
@@ -245,7 +245,7 @@ pub fn extend_next_word_start(cx: &mut Context) {
245245
246246pub fn extend_prev_word_start(cx: &mut Context) {
247247 let count = cx.count;
248− let doc = cx.doc();
248+ let mut doc = cx.doc();
249249 let text = doc.text().slice(..);
250250
251251 let selection = doc.selection().transform(|mut range| {
@@ -257,7 +257,7 @@ pub fn extend_prev_word_start(cx: &mut Context) {
257257
258258pub fn extend_next_word_end(cx: &mut Context) {
259259 let count = cx.count;
260− let doc = cx.doc();
260+ let mut doc = cx.doc();
261261 let text = doc.text().slice(..);
262262
263263 let selection = doc.selection().transform(|mut range| {
@@ -286,7 +286,7 @@ where
286286 ..
287287 } = event
288288 {
289− let doc = cx.doc();
289+ let mut doc = cx.doc();
290290 let text = doc.text().slice(..);
291291
292292 let selection = doc.selection().transform(|mut range| {
@@ -382,11 +382,13 @@ pub fn extend_prev_char(cx: &mut Context) {
382382
383383fn scroll(view: &mut View, offset: usize, direction: Direction) {
384384 use Direction::*;
385− let text = view.doc.text().slice(..);
385+ // we use short lived borrows since view's methods read from doc too
386+ let doc = view.doc.borrow();
387+ let text = doc.text().slice(..);
388+ let cursor = coords_at_pos(doc.text().slice(..), doc.selection().cursor());
389+ let doc_last_line = doc.text().len_lines() - 1;
386390
387391 let last_line = view.last_line();
388− let cursor = coords_at_pos(text, view.doc.selection().cursor());
389− let doc_last_line = text.len_lines() - 1;
390392
391393 if direction == Backward && view.first_line == 0
392394 || direction == Forward && last_line == doc_last_line
@@ -412,7 +414,8 @@ fn scroll(view: &mut View, offset: usize, direction: Direction) {
412414 );
413415
414416 let pos = pos_at_coords(text, Position::new(line, cursor.col)); // this func will properly truncate to line end
415− view.doc.set_selection(Selection::point(pos));
417+ drop(doc); // upgrade to mutable borrow
418+ view.doc.borrow_mut().set_selection(Selection::point(pos));
416419}
417420
418421pub fn page_up(cx: &mut Context) {
@@ -437,7 +440,7 @@ pub fn half_page_down(cx: &mut Context) {
437440
438441pub fn extend_char_left(cx: &mut Context) {
439442 let count = cx.count;
440− let doc = cx.doc();
443+ let mut doc = cx.doc();
441444 let text = doc.text().slice(..);
442445 let selection = doc.selection().transform(|range| {
443446 movement::move_horizontally(
@@ -453,7 +456,7 @@ pub fn extend_char_left(cx: &mut Context) {
453456
454457pub fn extend_char_right(cx: &mut Context) {
455458 let count = cx.count;
456− let doc = cx.doc();
459+ let mut doc = cx.doc();
457460 let text = doc.text().slice(..);
458461 let selection = doc.selection().transform(|range| {
459462 movement::move_horizontally(
@@ -469,7 +472,7 @@ pub fn extend_char_right(cx: &mut Context) {
469472
470473pub fn extend_line_up(cx: &mut Context) {
471474 let count = cx.count;
472− let doc = cx.doc();
475+ let mut doc = cx.doc();
473476 let text = doc.text().slice(..);
474477 let selection = doc.selection().transform(|range| {
475478 movement::move_vertically(
@@ -485,7 +488,7 @@ pub fn extend_line_up(cx: &mut Context) {
485488
486489pub fn extend_line_down(cx: &mut Context) {
487490 let count = cx.count;
488− let doc = cx.doc();
491+ let mut doc = cx.doc();
489492 let text = doc.text().slice(..);
490493 let selection = doc.selection().transform(|range| {
491494 movement::move_vertically(
@@ -500,12 +503,10 @@ pub fn extend_line_down(cx: &mut Context) {
500503}
501504
502505pub fn select_all(cx: &mut Context) {
503− let doc = cx.doc();
506+ let mut doc = cx.doc();
504507
505− doc.set_selection(Selection::single(
506− 0,
507− doc.text().len_chars().saturating_sub(1),
508− ))
508+ let end = doc.text().len_chars().saturating_sub(1);
509+ doc.set_selection(Selection::single(0, end))
509510}
510511
511512pub fn select_regex(cx: &mut Context) {
@@ -530,7 +531,7 @@ pub fn split_selection(cx: &mut Context) {
530531}
531532
532533pub fn split_selection_on_newline(cx: &mut Context) {
533− let doc = cx.doc();
534+ let mut doc = cx.doc();
534535 let text = doc.text().slice(..);
535536 // only compile the regex once
536537 #[allow(clippy::trivial_regex)]
@@ -562,13 +563,14 @@ fn _search(doc: &mut Document, contents: &str, regex: &Regex) {
562563
563564// TODO: use one function for search vs extend
564565pub fn search(cx: &mut Context) {
565− let doc = cx.doc();
566+ let mut doc = cx.doc();
566567
567568 // TODO: could probably share with select_on_matches?
568569
569570 // HAXX: sadly we can't avoid allocating a single string for the whole buffer since we can't
570571 // feed chunks into the regex yet
571572 let contents = doc.text().slice(..).to_string();
573+ drop(doc);
572574
573575 let prompt = ui::regex_prompt(cx, "search:".to_string(), move |doc, regex| {
574576 let text = doc.text();
@@ -585,19 +587,20 @@ pub fn search(cx: &mut Context) {
585587pub fn search_next(cx: &mut Context) {
586588 if let Some(query) = register::get('\\') {
587589 let query = query.first().unwrap();
588− let doc = cx.doc();
590+ let mut doc = cx.doc();
589591 let contents = doc.text().slice(..).to_string();
590592 let regex = Regex::new(&query).unwrap();
591− _search(doc, &contents, &regex);
593+ _search(&mut doc, &contents, &regex);
592594 }
593595}
594596
595597pub fn search_selection(cx: &mut Context) {
596− let doc = cx.doc();
598+ let mut doc = cx.doc();
597599 let contents = doc.text().slice(..);
598600 let query = doc.selection().primary().fragment(contents);
599601 let regex = regex::escape(&query);
600602 register::set('\\', vec![regex]);
603+ drop(doc);
601604 search_next(cx);
602605}
603606
@@ -609,7 +612,7 @@ pub fn search_selection(cx: &mut Context) {
609612
610613pub fn select_line(cx: &mut Context) {
611614 let count = cx.count;
612− let doc = cx.doc();
615+ let mut doc = cx.doc();
613616
614617 let pos = doc.selection().primary();
615618 let text = doc.text();
@@ -622,7 +625,7 @@ pub fn select_line(cx: &mut Context) {
622625}
623626pub fn extend_line(cx: &mut Context) {
624627 let count = cx.count;
625− let doc = cx.doc();
628+ let mut doc = cx.doc();
626629
627630 let pos = doc.selection().primary();
628631 let text = doc.text();
@@ -650,20 +653,21 @@ fn _delete_selection(doc: &mut Document) {
650653}
651654
652655pub fn delete_selection(cx: &mut Context) {
653− let doc = cx.doc();
654− _delete_selection(doc);
656+ let mut doc = cx.doc();
657+ _delete_selection(&mut doc);
655658
656659 doc.append_changes_to_history();
657660}
658661
659662pub fn change_selection(cx: &mut Context) {
660− let doc = cx.doc();
661− _delete_selection(doc);
663+ let mut doc = cx.doc();
664+ _delete_selection(&mut doc);
665+ drop(doc);
662666 insert_mode(cx);
663667}
664668
665669pub fn collapse_selection(cx: &mut Context) {
666− let doc = cx.doc();
670+ let mut doc = cx.doc();
667671 let selection = doc
668672 .selection()
669673 .transform(|range| Range::new(range.head, range.head));
@@ -672,7 +676,7 @@ pub fn collapse_selection(cx: &mut Context) {
672676}
673677
674678pub fn flip_selections(cx: &mut Context) {
675− let doc = cx.doc();
679+ let mut doc = cx.doc();
676680 let selection = doc
677681 .selection()
678682 .transform(|range| Range::new(range.head, range.anchor));
@@ -686,8 +690,8 @@ fn enter_insert_mode(doc: &mut Document) {
686690
687691// inserts at the start of each selection
688692pub fn insert_mode(cx: &mut Context) {
689− let doc = cx.doc();
690− enter_insert_mode(doc);
693+ let mut doc = cx.doc();
694+ enter_insert_mode(&mut doc);
691695
692696 let selection = doc
693697 .selection()
@@ -697,8 +701,8 @@ pub fn insert_mode(cx: &mut Context) {
697701
698702// inserts at the end of each selection
699703pub fn append_mode(cx: &mut Context) {
700− let doc = cx.doc();
701− enter_insert_mode(doc);
704+ let mut doc = cx.doc();
705+ enter_insert_mode(&mut doc);
702706 doc.restore_cursor = true;
703707
704708 let text = doc.text().slice(..);
@@ -765,7 +769,7 @@ pub fn command_mode(cx: &mut Context) {
765769 }
766770 ["w"] | ["write"] => {
767771 // TODO: non-blocking via save() command
768− smol::block_on(editor.view_mut().doc.save());
772+ smol::block_on(editor.view().doc.borrow().save());
769773 }
770774
771775 _ => (),
@@ -800,16 +804,18 @@ fn selection_lines(doc: &Rope, selection: &Selection) -> Vec<usize> {
800804
801805// I inserts at the start of each line with a selection
802806pub fn prepend_to_line(cx: &mut Context) {
803− let doc = cx.doc();
804− enter_insert_mode(doc);
807+ let mut doc = cx.doc();
808+ enter_insert_mode(&mut doc);
809+ drop(doc);
805810
806811 move_line_start(cx);
807812}
808813
809814// A inserts at the end of each line with a selection
810815pub fn append_to_line(cx: &mut Context) {
811− let doc = cx.doc();
812− enter_insert_mode(doc);
816+ let mut doc = cx.doc();
817+ enter_insert_mode(&mut doc);
818+ drop(doc);
813819
814820 move_line_end(cx);
815821}
@@ -817,8 +823,8 @@ pub fn append_to_line(cx: &mut Context) {
817823// o inserts a new line after each line with a selection
818824pub fn open_below(cx: &mut Context) {
819825 let count = cx.count;
820− let doc = cx.doc();
821− enter_insert_mode(doc);
826+ let mut doc = cx.doc();
827+ enter_insert_mode(&mut doc);
822828
823829 let lines = selection_lines(doc.text(), doc.selection());
824830
@@ -875,7 +881,7 @@ pub fn open_below(cx: &mut Context) {
875881// O inserts a new line before each line with a selection
876882
877883pub fn normal_mode(cx: &mut Context) {
878− let doc = cx.doc();
884+ let mut doc = cx.doc();
879885
880886 doc.mode = Mode::Normal;
881887
@@ -909,14 +915,12 @@ pub fn exit_select_mode(cx: &mut Context) {
909915}
910916
911917fn goto(cx: &mut Context, locations: Vec<lsp::Location>) {
912− let doc = cx.doc();
913−
914− doc.mode = Mode::Normal;
918+ cx.doc().mode = Mode::Normal;
915919
916920 match locations.as_slice() {
917921 [location] => {
918922 cx.editor.open(PathBuf::from(location.uri.path()));
919− let doc = cx.doc();
923+ let mut doc = cx.doc();
920924 let definition_pos = location.range.start;
921925 let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text(), definition_pos);
922926 doc.set_selection(Selection::point(new_pos));
@@ -932,7 +936,8 @@ fn goto(cx: &mut Context, locations: Vec<lsp::Location>) {
932936 },
933937 move |editor: &mut Editor, item| {
934938 editor.open(PathBuf::from(item.uri.path()));
935− let mut doc = &mut editor.view_mut().doc;
939+ // TODO: issues with doc already being broo
940+ let mut doc = &mut editor.view().doc.borrow_mut();
936941 let definition_pos = item.range.start;
937942 let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text(), definition_pos);
938943 doc.set_selection(Selection::point(new_pos));
@@ -944,7 +949,7 @@ fn goto(cx: &mut Context, locations: Vec<lsp::Location>) {
944949}
945950
946951pub fn goto_definition(cx: &mut Context) {
947− let doc = cx.doc();
952+ let mut doc = cx.doc();
948953 let language_server = match doc.language_server() {
949954 Some(language_server) => language_server,
950955 None => return,
@@ -956,11 +961,12 @@ pub fn goto_definition(cx: &mut Context) {
956961 // TODO: handle fails
957962 let res =
958963 smol::block_on(language_server.goto_definition(doc.identifier(), pos)).unwrap_or_default();
964+ drop(doc);
959965 goto(cx, res);
960966}
961967
962968pub fn goto_type_definition(cx: &mut Context) {
963− let doc = cx.doc();
969+ let mut doc = cx.doc();
964970 let language_server = match doc.language_server() {
965971 Some(language_server) => language_server,
966972 None => return,
@@ -972,11 +978,12 @@ pub fn goto_type_definition(cx: &mut Context) {
972978 // TODO: handle fails
973979 let res = smol::block_on(language_server.goto_type_definition(doc.identifier(), pos))
974980 .unwrap_or_default();
981+ drop(doc);
975982 goto(cx, res);
976983}
977984
978985pub fn goto_implementation(cx: &mut Context) {
979− let doc = cx.doc();
986+ let mut doc = cx.doc();
980987 let language_server = match doc.language_server() {
981988 Some(language_server) => language_server,
982989 None => return,
@@ -988,11 +995,12 @@ pub fn goto_implementation(cx: &mut Context) {
988995 // TODO: handle fails
989996 let res = smol::block_on(language_server.goto_implementation(doc.identifier(), pos))
990997 .unwrap_or_default();
998+ drop(doc);
991999 goto(cx, res);
9921000}
9931001
9941002pub fn goto_reference(cx: &mut Context) {
995− let doc = cx.doc();
1003+ let mut doc = cx.doc();
9961004 let language_server = match doc.language_server() {
9971005 Some(language_server) => language_server,
9981006 None => return,
@@ -1004,6 +1012,7 @@ pub fn goto_reference(cx: &mut Context) {
10041012 // TODO: handle fails
10051013 let res =
10061014 smol::block_on(language_server.goto_reference(doc.identifier(), pos)).unwrap_or_default();
1015+ drop(doc);
10071016 goto(cx, res);
10081017}
10091018
@@ -1017,7 +1026,7 @@ pub mod insert {
10171026
10181027 // TODO: insert means add text just before cursor, on exit we should be on the last letter.
10191028 pub fn insert_char(cx: &mut Context, c: char) {
1020− let doc = cx.doc();
1029+ let mut doc = cx.doc();
10211030
10221031 // run through insert hooks, stopping on the first one that returns Some(t)
10231032 for hook in HOOKS {
@@ -1034,7 +1043,7 @@ pub mod insert {
10341043 }
10351044
10361045 pub fn insert_tab(cx: &mut Context) {
1037− let doc = cx.doc();
1046+ let mut doc = cx.doc();
10381047 // TODO: round out to nearest indentation level (for example a line with 3 spaces should
10391048 // indent by one to reach 4 spaces).
10401049
@@ -1044,7 +1053,7 @@ pub mod insert {
10441053 }
10451054
10461055 pub fn insert_newline(cx: &mut Context) {
1047− let doc = cx.doc();
1056+ let mut doc = cx.doc();
10481057 let text = doc.text().slice(..);
10491058 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
10501059 let indent_level =
@@ -1061,7 +1070,7 @@ pub mod insert {
10611070 // TODO: handle indent-aware delete
10621071 pub fn delete_char_backward(cx: &mut Context) {
10631072 let count = cx.count;
1064− let doc = cx.doc();
1073+ let mut doc = cx.doc();
10651074 let text = doc.text().slice(..);
10661075 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
10671076 (
@@ -1075,7 +1084,7 @@ pub mod insert {
10751084
10761085 pub fn delete_char_forward(cx: &mut Context) {
10771086 let count = cx.count;
1078− let doc = cx.doc();
1087+ let mut doc = cx.doc();
10791088 let text = doc.text().slice(..);
10801089 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
10811090 (
@@ -1105,7 +1114,7 @@ pub fn redo(cx: &mut Context) {
11051114
11061115pub fn yank(cx: &mut Context) {
11071116 // TODO: should selections be made end inclusive?
1108− let doc = cx.doc();
1117+ let mut doc = cx.doc();
11091118 let values = doc
11101119 .selection()
11111120 .fragments(doc.text().slice(..))
@@ -1146,7 +1155,7 @@ pub fn paste(cx: &mut Context) {
11461155
11471156 let mut values = values.into_iter().map(Tendril::from).chain(repeat);
11481157
1149− let doc = cx.doc();
1158+ let mut doc = cx.doc();
11501159
11511160 let transaction = if linewise {
11521161 // paste on the next line
@@ -1185,8 +1194,8 @@ fn get_lines(doc: &Document) -> Vec<usize> {
11851194}
11861195
11871196pub fn indent(cx: &mut Context) {
1188− let doc = cx.doc();
1189− let lines = get_lines(doc);
1197+ let mut doc = cx.doc();
1198+ let lines = get_lines(&mut doc);
11901199
11911200 // Indent by one level
11921201 let indent = Tendril::from(doc.indent_unit());
@@ -1203,8 +1212,8 @@ pub fn indent(cx: &mut Context) {
12031212}
12041213
12051214pub fn unindent(cx: &mut Context) {
1206− let doc = cx.doc();
1207− let lines = get_lines(doc);
1215+ let mut doc = cx.doc();
1216+ let lines = get_lines(&mut doc);
12081217 let mut changes = Vec::with_capacity(lines.len());
12091218 let tab_width = doc.tab_width();
12101219
@@ -1238,7 +1247,7 @@ pub fn unindent(cx: &mut Context) {
12381247
12391248pub fn format_selections(cx: &mut Context) {
12401249 use helix_lsp::lsp;
1241− let doc = cx.doc();
1250+ let mut doc = cx.doc();
12421251
12431252 // via lsp if available
12441253 // else via tree-sitter indentation calculations
@@ -1276,7 +1285,7 @@ pub fn format_selections(cx: &mut Context) {
12761285
12771286pub fn join_selections(cx: &mut Context) {
12781287 use movement::skip_over_next;
1279− let doc = cx.doc();
1288+ let mut doc = cx.doc();
12801289 let text = doc.text();
12811290 let slice = doc.text().slice(..);
12821291
@@ -1319,7 +1328,6 @@ pub fn join_selections(cx: &mut Context) {
13191328}
13201329
13211330pub fn keep_selections(cx: &mut Context) {
1322− let doc = cx.doc();
13231331 // keep selections matching regex
13241332 let prompt = ui::regex_prompt(cx, "keep:".to_string(), |doc, regex| {
13251333 let text = doc.text().slice(..);
@@ -1333,7 +1341,7 @@ pub fn keep_selections(cx: &mut Context) {
13331341}
13341342
13351343pub fn keep_primary_selection(cx: &mut Context) {
1336− let doc = cx.doc();
1344+ let mut doc = cx.doc();
13371345
13381346 let range = doc.selection().primary();
13391347 let selection = Selection::single(range.anchor, range.head);
@@ -1350,7 +1358,7 @@ pub fn save(cx: &mut Context) {
13501358}
13511359
13521360pub fn completion(cx: &mut Context) {
1353− let doc = cx.doc();
1361+ let mut doc = cx.doc();
13541362
13551363 let language_server = match doc.language_server() {
13561364 Some(language_server) => language_server,
@@ -1364,6 +1372,8 @@ pub fn completion(cx: &mut Context) {
13641372
13651373 let res = smol::block_on(language_server.completion(doc.identifier(), pos)).unwrap_or_default();
13661374
1375+ drop(doc);
1376+
13671377 // TODO: if no completion, show some message or something
13681378 if !res.is_empty() {
13691379 // let snapshot = doc.state.clone();
@@ -1379,11 +1389,11 @@ pub fn completion(cx: &mut Context) {
13791389 match event {
13801390 PromptEvent::Abort => {
13811391 // revert state
1382− let doc = &mut editor.view_mut().doc;
1392+ let doc = &mut editor.view().doc.borrow_mut();
13831393 // doc.state = snapshot.clone();
13841394 }
13851395 PromptEvent::Validate => {
1386− let doc = &mut editor.view_mut().doc;
1396+ let doc = &mut editor.view().doc.borrow_mut();
13871397
13881398 // revert state to what it was before the last update
13891399 // doc.state = snapshot.clone();
@@ -1449,7 +1459,7 @@ pub fn completion(cx: &mut Context) {
14491459pub fn hover(cx: &mut Context) {
14501460 use helix_lsp::lsp;
14511461
1452− let doc = cx.doc();
1462+ let mut doc = cx.doc();
14531463
14541464 let language_server = match doc.language_server() {
14551465 Some(language_server) => language_server,
@@ -1466,6 +1476,8 @@ pub fn hover(cx: &mut Context) {
14661476 let res = smol::block_on(language_server.text_document_hover(doc.identifier(), pos))
14671477 .unwrap_or_default();
14681478
1479+ drop(doc);
1480+
14691481 if let Some(hover) = res {
14701482 // hover.contents / .range <- used for visualizing
14711483 let contents = match hover.contents {
@@ -1491,12 +1503,12 @@ pub fn hover(cx: &mut Context) {
14911503
14921504// view movements
14931505pub fn next_view(cx: &mut Context) {
1494− cx.editor.tree.focus_next()
1506+ cx.editor.focus_next()
14951507}
14961508
14971509// comments
14981510pub fn toggle_comments(cx: &mut Context) {
1499− let doc = cx.doc();
1511+ let mut doc = cx.doc();
15001512 let transaction = comment::toggle_line_comments(doc.text(), doc.selection());
15011513
15021514 doc.apply(&transaction);
@@ -1506,7 +1518,7 @@ pub fn toggle_comments(cx: &mut Context) {
15061518// tree sitter node selection
15071519
15081520pub fn expand_selection(cx: &mut Context) {
1509− let doc = cx.doc();
1521+ let mut doc = cx.doc();
15101522
15111523 if let Some(syntax) = doc.syntax() {
15121524 let text = doc.text().slice(..);
helix-term/src/ui/editor.rs+11−10
@@ -57,7 +57,8 @@ impl EditorView {
5757 // TODO: this seems to prevent setting style later
5858 // surface.set_style(viewport, theme.get("ui.background"));
5959
60− self.render_diagnostics(&view.doc, area, surface, theme, is_focused);
60+ let doc = view.doc.borrow();
61+ self.render_diagnostics(&doc, area, surface, theme, is_focused);
6162
6263 let area = Rect::new(
6364 viewport.x,
@@ -65,7 +66,7 @@ impl EditorView {
6566 viewport.width,
6667 1,
6768 );
68− self.render_statusline(&view.doc, area, surface, theme, is_focused);
69+ self.render_statusline(&doc, area, surface, theme, is_focused);
6970 }
7071
7172 pub fn render_buffer(
@@ -76,7 +77,8 @@ impl EditorView {
7677 theme: &Theme,
7778 is_focused: bool,
7879 ) {
79− let text = view.doc.text();
80+ let doc = view.doc.borrow();
81+ let text = doc.text().slice(..);
8082
8183 let last_line = view.last_line();
8284
@@ -91,7 +93,7 @@ impl EditorView {
9193 // TODO: range doesn't actually restrict source, just highlight range
9294 // TODO: cache highlight results
9395 // TODO: only recalculate when state.doc is actually modified
94− let highlights: Vec<_> = match view.doc.syntax() {
96+ let highlights: Vec<_> = match doc.syntax() {
9597 Some(syntax) => {
9698 syntax
9799 .highlight_iter(text.slice(..), Some(range), None, |_| None)
@@ -105,7 +107,7 @@ impl EditorView {
105107 let mut spans = Vec::new();
106108 let mut visual_x = 0;
107109 let mut line = 0u16;
108− let tab_width = view.doc.tab_width();
110+ let tab_width = doc.tab_width();
109111
110112 'outer: for event in highlights {
111113 match event.unwrap() {
@@ -167,7 +169,7 @@ impl EditorView {
167169 let width = grapheme_width(&grapheme) as u16;
168170
169171 // ugh,interleave highlight spans with diagnostic spans
170− let is_diagnostic = view.doc.diagnostics.iter().any(|diagnostic| {
172+ let is_diagnostic = doc.diagnostics.iter().any(|diagnostic| {
171173 diagnostic.range.start <= char_index
172174 && diagnostic.range.end > char_index
173175 });
@@ -202,7 +204,6 @@ impl EditorView {
202204 let end = text.line_to_char(last_line + 1);
203205 Range::new(start, end)
204206 };
205− let text = text.slice(..);
206207 let cursor_style = Style::default()
207208 // .bg(Color::Rgb(255, 255, 255))
208209 .add_modifier(Modifier::REVERSED);
@@ -212,6 +213,7 @@ impl EditorView {
212213
213214 for selection in view
214215 .doc
216+ .borrow()
215217 .selection()
216218 .iter()
217219 .filter(|range| range.overlaps(&screen))
@@ -293,7 +295,7 @@ impl EditorView {
293295 let last_line = view.last_line();
294296 for (i, line) in (view.first_line..last_line).enumerate() {
295297 use helix_core::diagnostic::Severity;
296− if let Some(diagnostic) = view.doc.diagnostics.iter().find(|d| d.line == line) {
298+ if let Some(diagnostic) = doc.diagnostics.iter().find(|d| d.line == line) {
297299 surface.set_stringn(
298300 viewport.x - OFFSET,
299301 viewport.y + i as u16,
@@ -433,9 +435,8 @@ impl Component for EditorView {
433435 EventResult::Consumed(None)
434436 }
435437 Event::Key(event) => {
436− let view = cx.editor.view_mut();
438+ let mode = cx.editor.view().doc.borrow().mode();
437439
438− let mode = view.doc.mode();
439440 let mut cxt = commands::Context {
440441 editor: &mut cx.editor,
441442 count: 1,
helix-term/src/ui/mod.rs+5−4
@@ -39,9 +39,8 @@ pub fn regex_prompt(
3939 move |editor: &mut Editor, input: &str, event: PromptEvent| {
4040 match event {
4141 PromptEvent::Abort => {
42− // TODO: also revert doc
4342 // TODO: also revert text
44− let doc = &mut editor.view_mut().doc;
43+ let doc = &mut editor.view().doc.borrow_mut();
4544 doc.set_selection(snapshot.clone());
4645 }
4746 PromptEvent::Validate => {
@@ -56,13 +55,15 @@ pub fn regex_prompt(
5655 match Regex::new(input) {
5756 Ok(regex) => {
5857 let view = &mut editor.view_mut();
59− let doc = &mut view.doc;
58+ let mut doc = view.doc.borrow_mut();
6059
6160 // revert state to what it was before the last update
6261 // TODO: also revert text
6362 doc.set_selection(snapshot.clone());
6463
65− fun(doc, regex);
64+ fun(&mut doc, regex);
65+
66+ drop(doc);
6667
6768 view.ensure_cursor_in_view();
6869 }
helix-view/src/editor.rs+29−8
@@ -1,14 +1,15 @@
1−use crate::{theme::Theme, tree::Tree, Document, View};
1+use crate::{theme::Theme, tree::Tree, Document, DocumentId, View};
22
33use std::path::PathBuf;
4+use std::{cell::RefCell, rc::Rc};
45
5−use slotmap::DefaultKey as Key;
6+use slotmap::{DefaultKey as Key, SlotMap};
67
78use anyhow::Error;
89
910pub struct Editor {
1011 pub tree: Tree,
11− // pub documents: Vec<Document>,
12+ pub documents: SlotMap<DocumentId, Rc<RefCell<Document>>>,
1213 pub count: Option<usize>,
1314 pub theme: Theme,
1415 pub language_servers: helix_lsp::Registry,
@@ -25,6 +26,7 @@ impl Editor {
2526
2627 Self {
2728 tree: Tree::new(area),
29+ documents: SlotMap::with_key(),
2830 count: None,
2931 theme,
3032 language_servers,
@@ -33,10 +35,12 @@ impl Editor {
3335 }
3436
3537 pub fn open(&mut self, path: PathBuf) -> Result<(), Error> {
38+ // TODO: issues with doc already being borrowed if called from inside goto()
39+
3640 let existing_view = self
3741 .tree
3842 .views()
39− .find(|(view, _)| view.doc.path() == Some(&path));
43+ .find(|(view, _)| view.doc.borrow().path() == Some(&path));
4044
4145 if let Some((view, _)) = existing_view {
4246 self.tree.focus = view.id;
@@ -69,6 +73,10 @@ impl Editor {
6973 .unwrap();
7074 }
7175
76+ let doc = Rc::new(RefCell::new(doc));
77+ // TODO: store id as doc.id
78+ let id = self.documents.insert(doc.clone());
79+
7280 let view = View::new(doc)?;
7381 self.tree.insert(view);
7482 Ok(())
@@ -80,7 +88,7 @@ impl Editor {
8088 let language_servers = &mut self.language_servers;
8189 let executor = self.executor;
8290
83− let doc = &view.doc;
91+ let doc = view.doc.borrow();
8492
8593 let language_server = doc
8694 .language
@@ -90,7 +98,19 @@ impl Editor {
9098 if let Some(language_server) = language_server {
9199 smol::block_on(language_server.text_document_did_close(doc.identifier())).unwrap();
92100 }
93− self.tree.remove(id)
101+
102+ drop(doc); // to stop borrowing self.tree
103+
104+ // self.documents.remove(view.doc);
105+ self.tree.remove(id);
106+ }
107+
108+ pub fn resize(&mut self) {
109+ self.tree.focus_next();
110+ }
111+
112+ pub fn focus_next(&mut self) {
113+ self.tree.focus_next();
94114 }
95115
96116 pub fn should_close(&self) -> bool {
@@ -108,8 +128,9 @@ impl Editor {
108128 pub fn cursor_position(&self) -> Option<helix_core::Position> {
109129 const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter
110130 let view = self.view();
111− let cursor = view.doc.selection().cursor();
112− if let Some(mut pos) = view.screen_coords_at_pos(view.doc.text().slice(..), cursor) {
131+ let doc = view.doc.borrow();
132+ let cursor = doc.selection().cursor();
133+ if let Some(mut pos) = view.screen_coords_at_pos(doc.text().slice(..), cursor) {
113134 pos.col += view.area.x as usize + OFFSET as usize;
114135 pos.row += view.area.y as usize;
115136 return Some(pos);
helix-view/src/lib.rs+3
@@ -4,6 +4,9 @@ pub mod theme;
44pub mod tree;
55pub mod view;
66
7+use slotmap::new_key_type;
8+new_key_type! { pub struct DocumentId; }
9+
710pub use document::Document;
811pub use editor::Editor;
912pub use theme::Theme;
helix-view/src/view.rs+10−7
@@ -1,8 +1,9 @@
11use anyhow::Error;
22
33use std::borrow::Cow;
4+use std::{cell::RefCell, rc::Rc};
45
5−use crate::Document;
6+use crate::{Document, DocumentId};
67use helix_core::{
78 graphemes::{grapheme_width, RopeGraphemes},
89 Position, RopeSlice,
@@ -14,13 +15,13 @@ pub const PADDING: usize = 5;
1415
1516pub struct View {
1617 pub id: Key,
17− pub doc: Document,
18+ pub doc: Rc<RefCell<Document>>,
1819 pub first_line: usize,
1920 pub area: Rect,
2021}
2122
2223impl View {
23− pub fn new(doc: Document) -> Result<Self, Error> {
24+ pub fn new(doc: Rc<RefCell<Document>>) -> Result<Self, Error> {
2425 let view = Self {
2526 id: Key::default(),
2627 doc,
@@ -32,8 +33,9 @@ impl View {
3233 }
3334
3435 pub fn ensure_cursor_in_view(&mut self) {
35− let cursor = self.doc.selection().cursor();
36− let line = self.doc.text().char_to_line(cursor);
36+ let doc = self.doc.borrow();
37+ let cursor = doc.selection().cursor();
38+ let line = doc.text().char_to_line(cursor);
3739 let document_end = self.first_line + (self.area.height as usize).saturating_sub(2);
3840
3941 // TODO: side scroll
@@ -50,10 +52,11 @@ impl View {
5052 /// Calculates the last visible line on screen
5153 #[inline]
5254 pub fn last_line(&self) -> usize {
55+ let doc = self.doc.borrow();
5356 let height = self.area.height.saturating_sub(1); // - 1 for statusline
5457 std::cmp::min(
5558 self.first_line + height as usize,
56− self.doc.text().len_lines() - 1,
59+ doc.text().len_lines() - 1,
5760 )
5861 }
5962
@@ -71,7 +74,7 @@ impl View {
7174 let line_start = text.line_to_char(line);
7275 let line_slice = text.slice(line_start..pos);
7376 let mut col = 0;
74− let tab_width = self.doc.tab_width();
77+ let tab_width = self.doc.borrow().tab_width();
7578
7679 for grapheme in RopeGraphemes::new(line_slice) {
7780 if grapheme == "\t" {