on master help sign in
Store Document on the Editor type, make View reference it.
bf95ee2Blaž Hrastnik
helix-term/src/application.rs+12−11
@@ -125,15 +125,16 @@ impl Application {
125 Notification::PublishDiagnostics(params) => {
125 Notification::PublishDiagnostics(params) => {
126 let path = Some(params.uri.to_file_path().unwrap());
126 let path = Some(params.uri.to_file_path().unwrap());
127
127
128− let view = self
128+ let doc = self
129 .editor
129 .editor
130− .tree
130+ .documents
131− .views()
131+ .iter_mut()
132− .map(|(view, _key)| view)
133− .find(|view| view.doc.path() == path.as_ref());
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();
134
137
135− if let Some(view) = view {
136− let doc = view.doc.text();
137 let diagnostics = params
138 let diagnostics = params
138 .diagnostics
139 .diagnostics
139 .into_iter()
140 .into_iter()
@@ -144,8 +145,8 @@ impl Application {
144 };
145 };
145 use helix_lsp::{lsp, util::lsp_pos_to_pos};
146 use helix_lsp::{lsp, util::lsp_pos_to_pos};
146 use lsp::DiagnosticSeverity;
147 use lsp::DiagnosticSeverity;
147− let start = lsp_pos_to_pos(doc, diagnostic.range.start);
148+ let start = lsp_pos_to_pos(text, diagnostic.range.start);
148− let end = lsp_pos_to_pos(doc, diagnostic.range.end);
149+ let end = lsp_pos_to_pos(text, diagnostic.range.end);
149
150
150 Diagnostic {
151 Diagnostic {
151 range: Range { start, end },
152 range: Range { start, end },
@@ -165,8 +166,8 @@ impl Application {
165 })
166 })
166 .collect();
167 .collect();
167
168
168− view.doc.diagnostics = diagnostics;
169+ doc.diagnostics = diagnostics;
169−
170+ drop(doc);
170 // TODO: we want to process all the events in queue, then render. publishDiagnostic tends to send a whole bunch of events
171 // TODO: we want to process all the events in queue, then render. publishDiagnostic tends to send a whole bunch of events
171 self.render();
172 self.render();
172 }
173 }
helix-term/src/commands.rs+99−87
@@ -10,7 +10,7 @@ use helix_core::{
10use once_cell::sync::Lazy;
10use once_cell::sync::Lazy;
11
11
12use crate::{
12use crate::{
13− compositor::Compositor,
13+ compositor::{Callback, Compositor},
14 ui::{self, Popup, Prompt, PromptEvent},
14 ui::{self, Popup, Prompt, PromptEvent},
15};
15};
16
16
@@ -41,8 +41,8 @@ impl<'a> Context<'a> {
41 }
41 }
42
42
43 #[inline]
43 #[inline]
44− pub fn doc(&mut self) -> &mut Document {
44+ pub fn doc(&mut self) -> std::cell::RefMut<Document> {
45− &mut self.editor.view_mut().doc
45+ self.editor.view().doc.borrow_mut()
46 }
46 }
47
47
48 /// Push a new component onto the compositor.
48 /// Push a new component onto the compositor.
@@ -72,7 +72,7 @@ pub type Command = fn(cx: &mut Context);
72
72
73pub fn move_char_left(cx: &mut Context) {
73pub fn move_char_left(cx: &mut Context) {
74 let count = cx.count;
74 let count = cx.count;
75− let doc = cx.doc();
75+ let mut doc = cx.doc();
76 let text = doc.text().slice(..);
76 let text = doc.text().slice(..);
77 let selection = doc.selection().transform(|range| {
77 let selection = doc.selection().transform(|range| {
78 movement::move_horizontally(
78 movement::move_horizontally(
@@ -88,7 +88,7 @@ pub fn move_char_left(cx: &mut Context) {
88
88
89pub fn move_char_right(cx: &mut Context) {
89pub fn move_char_right(cx: &mut Context) {
90 let count = cx.count;
90 let count = cx.count;
91− let doc = cx.doc();
91+ let mut doc = cx.doc();
92 let text = doc.text().slice(..);
92 let text = doc.text().slice(..);
93 let selection = doc.selection().transform(|range| {
93 let selection = doc.selection().transform(|range| {
94 movement::move_horizontally(
94 movement::move_horizontally(
@@ -104,7 +104,7 @@ pub fn move_char_right(cx: &mut Context) {
104
104
105pub fn move_line_up(cx: &mut Context) {
105pub fn move_line_up(cx: &mut Context) {
106 let count = cx.count;
106 let count = cx.count;
107− let doc = cx.doc();
107+ let mut doc = cx.doc();
108 let text = doc.text().slice(..);
108 let text = doc.text().slice(..);
109 let selection = doc.selection().transform(|range| {
109 let selection = doc.selection().transform(|range| {
110 movement::move_vertically(
110 movement::move_vertically(
@@ -120,7 +120,7 @@ pub fn move_line_up(cx: &mut Context) {
120
120
121pub fn move_line_down(cx: &mut Context) {
121pub fn move_line_down(cx: &mut Context) {
122 let count = cx.count;
122 let count = cx.count;
123− let doc = cx.doc();
123+ let mut doc = cx.doc();
124 let text = doc.text().slice(..);
124 let text = doc.text().slice(..);
125 let selection = doc.selection().transform(|range| {
125 let selection = doc.selection().transform(|range| {
126 movement::move_vertically(
126 movement::move_vertically(
@@ -135,7 +135,7 @@ pub fn move_line_down(cx: &mut Context) {
135}
135}
136
136
137pub fn move_line_end(cx: &mut Context) {
137pub fn move_line_end(cx: &mut Context) {
138− let doc = cx.doc();
138+ let mut doc = cx.doc();
139 let lines = selection_lines(doc.text(), doc.selection());
139 let lines = selection_lines(doc.text(), doc.selection());
140
140
141 let positions = lines
141 let positions = lines
@@ -155,7 +155,7 @@ pub fn move_line_end(cx: &mut Context) {
155}
155}
156
156
157pub fn move_line_start(cx: &mut Context) {
157pub fn move_line_start(cx: &mut Context) {
158− let doc = cx.doc();
158+ let mut doc = cx.doc();
159 let lines = selection_lines(doc.text(), doc.selection());
159 let lines = selection_lines(doc.text(), doc.selection());
160
160
161 let positions = lines
161 let positions = lines
@@ -177,7 +177,7 @@ pub fn move_line_start(cx: &mut Context) {
177
177
178pub fn move_next_word_start(cx: &mut Context) {
178pub fn move_next_word_start(cx: &mut Context) {
179 let count = cx.count;
179 let count = cx.count;
180− let doc = cx.doc();
180+ let mut doc = cx.doc();
181 let text = doc.text().slice(..);
181 let text = doc.text().slice(..);
182
182
183 let selection = doc.selection().transform(|range| {
183 let selection = doc.selection().transform(|range| {
@@ -190,7 +190,7 @@ pub fn move_next_word_start(cx: &mut Context) {
190
190
191pub fn move_prev_word_start(cx: &mut Context) {
191pub fn move_prev_word_start(cx: &mut Context) {
192 let count = cx.count;
192 let count = cx.count;
193− let doc = cx.doc();
193+ let mut doc = cx.doc();
194 let text = doc.text().slice(..);
194 let text = doc.text().slice(..);
195
195
196 let selection = doc.selection().transform(|range| {
196 let selection = doc.selection().transform(|range| {
@@ -203,7 +203,7 @@ pub fn move_prev_word_start(cx: &mut Context) {
203
203
204pub fn move_next_word_end(cx: &mut Context) {
204pub fn move_next_word_end(cx: &mut Context) {
205 let count = cx.count;
205 let count = cx.count;
206− let doc = cx.doc();
206+ let mut doc = cx.doc();
207 let text = doc.text().slice(..);
207 let text = doc.text().slice(..);
208
208
209 let selection = doc.selection().transform(|range| {
209 let selection = doc.selection().transform(|range| {
@@ -215,14 +215,14 @@ pub fn move_next_word_end(cx: &mut Context) {
215}
215}
216
216
217pub fn move_file_start(cx: &mut Context) {
217pub fn move_file_start(cx: &mut Context) {
218− let doc = cx.doc();
218+ let mut doc = cx.doc();
219 doc.set_selection(Selection::point(0));
219 doc.set_selection(Selection::point(0));
220
220
221 doc.mode = Mode::Normal;
221 doc.mode = Mode::Normal;
222}
222}
223
223
224pub fn move_file_end(cx: &mut Context) {
224pub fn move_file_end(cx: &mut Context) {
225− let doc = cx.doc();
225+ let mut doc = cx.doc();
226 let text = doc.text();
226 let text = doc.text();
227 let last_line = text.line_to_char(text.len_lines().saturating_sub(2));
227 let last_line = text.line_to_char(text.len_lines().saturating_sub(2));
228 doc.set_selection(Selection::point(last_line));
228 doc.set_selection(Selection::point(last_line));
@@ -232,7 +232,7 @@ pub fn move_file_end(cx: &mut Context) {
232
232
233pub fn extend_next_word_start(cx: &mut Context) {
233pub fn extend_next_word_start(cx: &mut Context) {
234 let count = cx.count;
234 let count = cx.count;
235− let doc = cx.doc();
235+ let mut doc = cx.doc();
236 let text = doc.text().slice(..);
236 let text = doc.text().slice(..);
237
237
238 let selection = doc.selection().transform(|mut range| {
238 let selection = doc.selection().transform(|mut range| {
@@ -245,7 +245,7 @@ pub fn extend_next_word_start(cx: &mut Context) {
245
245
246pub fn extend_prev_word_start(cx: &mut Context) {
246pub fn extend_prev_word_start(cx: &mut Context) {
247 let count = cx.count;
247 let count = cx.count;
248− let doc = cx.doc();
248+ let mut doc = cx.doc();
249 let text = doc.text().slice(..);
249 let text = doc.text().slice(..);
250
250
251 let selection = doc.selection().transform(|mut range| {
251 let selection = doc.selection().transform(|mut range| {
@@ -257,7 +257,7 @@ pub fn extend_prev_word_start(cx: &mut Context) {
257
257
258pub fn extend_next_word_end(cx: &mut Context) {
258pub fn extend_next_word_end(cx: &mut Context) {
259 let count = cx.count;
259 let count = cx.count;
260− let doc = cx.doc();
260+ let mut doc = cx.doc();
261 let text = doc.text().slice(..);
261 let text = doc.text().slice(..);
262
262
263 let selection = doc.selection().transform(|mut range| {
263 let selection = doc.selection().transform(|mut range| {
@@ -286,7 +286,7 @@ where
286 ..
286 ..
287 } = event
287 } = event
288 {
288 {
289− let doc = cx.doc();
289+ let mut doc = cx.doc();
290 let text = doc.text().slice(..);
290 let text = doc.text().slice(..);
291
291
292 let selection = doc.selection().transform(|mut range| {
292 let selection = doc.selection().transform(|mut range| {
@@ -382,11 +382,13 @@ pub fn extend_prev_char(cx: &mut Context) {
382
382
383fn scroll(view: &mut View, offset: usize, direction: Direction) {
383fn scroll(view: &mut View, offset: usize, direction: Direction) {
384 use Direction::*;
384 use Direction::*;
385+ // we use short lived borrows since view's methods read from doc too
385− let text = view.doc.text().slice(..);
386+ let doc = view.doc.borrow();
387+ let text = doc.text().slice(..);
388+ let cursor = coords_at_pos(doc.text().slice(..), doc.selection().cursor());
389+ let doc_last_line = doc.text().len_lines() - 1;
386
390
387 let last_line = view.last_line();
391 let last_line = view.last_line();
388− let cursor = coords_at_pos(text, view.doc.selection().cursor());
389− let doc_last_line = text.len_lines() - 1;
390
392
391 if direction == Backward && view.first_line == 0
393 if direction == Backward && view.first_line == 0
392 || direction == Forward && last_line == doc_last_line
394 || direction == Forward && last_line == doc_last_line
@@ -412,7 +414,8 @@ fn scroll(view: &mut View, offset: usize, direction: Direction) {
412 );
414 );
413
415
414 let pos = pos_at_coords(text, Position::new(line, cursor.col)); // this func will properly truncate to line end
416 let pos = pos_at_coords(text, Position::new(line, cursor.col)); // this func will properly truncate to line end
417+ drop(doc); // upgrade to mutable borrow
415− view.doc.set_selection(Selection::point(pos));
418+ view.doc.borrow_mut().set_selection(Selection::point(pos));
416}
419}
417
420
418pub fn page_up(cx: &mut Context) {
421pub fn page_up(cx: &mut Context) {
@@ -437,7 +440,7 @@ pub fn half_page_down(cx: &mut Context) {
437
440
438pub fn extend_char_left(cx: &mut Context) {
441pub fn extend_char_left(cx: &mut Context) {
439 let count = cx.count;
442 let count = cx.count;
440− let doc = cx.doc();
443+ let mut doc = cx.doc();
441 let text = doc.text().slice(..);
444 let text = doc.text().slice(..);
442 let selection = doc.selection().transform(|range| {
445 let selection = doc.selection().transform(|range| {
443 movement::move_horizontally(
446 movement::move_horizontally(
@@ -453,7 +456,7 @@ pub fn extend_char_left(cx: &mut Context) {
453
456
454pub fn extend_char_right(cx: &mut Context) {
457pub fn extend_char_right(cx: &mut Context) {
455 let count = cx.count;
458 let count = cx.count;
456− let doc = cx.doc();
459+ let mut doc = cx.doc();
457 let text = doc.text().slice(..);
460 let text = doc.text().slice(..);
458 let selection = doc.selection().transform(|range| {
461 let selection = doc.selection().transform(|range| {
459 movement::move_horizontally(
462 movement::move_horizontally(
@@ -469,7 +472,7 @@ pub fn extend_char_right(cx: &mut Context) {
469
472
470pub fn extend_line_up(cx: &mut Context) {
473pub fn extend_line_up(cx: &mut Context) {
471 let count = cx.count;
474 let count = cx.count;
472− let doc = cx.doc();
475+ let mut doc = cx.doc();
473 let text = doc.text().slice(..);
476 let text = doc.text().slice(..);
474 let selection = doc.selection().transform(|range| {
477 let selection = doc.selection().transform(|range| {
475 movement::move_vertically(
478 movement::move_vertically(
@@ -485,7 +488,7 @@ pub fn extend_line_up(cx: &mut Context) {
485
488
486pub fn extend_line_down(cx: &mut Context) {
489pub fn extend_line_down(cx: &mut Context) {
487 let count = cx.count;
490 let count = cx.count;
488− let doc = cx.doc();
491+ let mut doc = cx.doc();
489 let text = doc.text().slice(..);
492 let text = doc.text().slice(..);
490 let selection = doc.selection().transform(|range| {
493 let selection = doc.selection().transform(|range| {
491 movement::move_vertically(
494 movement::move_vertically(
@@ -500,12 +503,10 @@ pub fn extend_line_down(cx: &mut Context) {
500}
503}
501
504
502pub fn select_all(cx: &mut Context) {
505pub fn select_all(cx: &mut Context) {
503− let doc = cx.doc();
506+ let mut doc = cx.doc();
504
507
508+ let end = doc.text().len_chars().saturating_sub(1);
505− doc.set_selection(Selection::single(
509+ doc.set_selection(Selection::single(0, end))
506− 0,
507− doc.text().len_chars().saturating_sub(1),
508− ))
509}
510}
510
511
511pub fn select_regex(cx: &mut Context) {
512pub fn select_regex(cx: &mut Context) {
@@ -530,7 +531,7 @@ pub fn split_selection(cx: &mut Context) {
530}
531}
531
532
532pub fn split_selection_on_newline(cx: &mut Context) {
533pub fn split_selection_on_newline(cx: &mut Context) {
533− let doc = cx.doc();
534+ let mut doc = cx.doc();
534 let text = doc.text().slice(..);
535 let text = doc.text().slice(..);
535 // only compile the regex once
536 // only compile the regex once
536 #[allow(clippy::trivial_regex)]
537 #[allow(clippy::trivial_regex)]
@@ -562,13 +563,14 @@ fn _search(doc: &mut Document, contents: &str, regex: &Regex) {
562
563
563// TODO: use one function for search vs extend
564// TODO: use one function for search vs extend
564pub fn search(cx: &mut Context) {
565pub fn search(cx: &mut Context) {
565− let doc = cx.doc();
566+ let mut doc = cx.doc();
566
567
567 // TODO: could probably share with select_on_matches?
568 // TODO: could probably share with select_on_matches?
568
569
569 // HAXX: sadly we can't avoid allocating a single string for the whole buffer since we can't
570 // HAXX: sadly we can't avoid allocating a single string for the whole buffer since we can't
570 // feed chunks into the regex yet
571 // feed chunks into the regex yet
571 let contents = doc.text().slice(..).to_string();
572 let contents = doc.text().slice(..).to_string();
573+ drop(doc);
572
574
573 let prompt = ui::regex_prompt(cx, "search:".to_string(), move |doc, regex| {
575 let prompt = ui::regex_prompt(cx, "search:".to_string(), move |doc, regex| {
574 let text = doc.text();
576 let text = doc.text();
@@ -585,19 +587,20 @@ pub fn search(cx: &mut Context) {
585pub fn search_next(cx: &mut Context) {
587pub fn search_next(cx: &mut Context) {
586 if let Some(query) = register::get('\\') {
588 if let Some(query) = register::get('\\') {
587 let query = query.first().unwrap();
589 let query = query.first().unwrap();
588− let doc = cx.doc();
590+ let mut doc = cx.doc();
589 let contents = doc.text().slice(..).to_string();
591 let contents = doc.text().slice(..).to_string();
590 let regex = Regex::new(&query).unwrap();
592 let regex = Regex::new(&query).unwrap();
591− _search(doc, &contents, &regex);
593+ _search(&mut doc, &contents, &regex);
592 }
594 }
593}
595}
594
596
595pub fn search_selection(cx: &mut Context) {
597pub fn search_selection(cx: &mut Context) {
596− let doc = cx.doc();
598+ let mut doc = cx.doc();
597 let contents = doc.text().slice(..);
599 let contents = doc.text().slice(..);
598 let query = doc.selection().primary().fragment(contents);
600 let query = doc.selection().primary().fragment(contents);
599 let regex = regex::escape(&query);
601 let regex = regex::escape(&query);
600 register::set('\\', vec![regex]);
602 register::set('\\', vec![regex]);
603+ drop(doc);
601 search_next(cx);
604 search_next(cx);
602}
605}
603
606
@@ -609,7 +612,7 @@ pub fn search_selection(cx: &mut Context) {
609
612
610pub fn select_line(cx: &mut Context) {
613pub fn select_line(cx: &mut Context) {
611 let count = cx.count;
614 let count = cx.count;
612− let doc = cx.doc();
615+ let mut doc = cx.doc();
613
616
614 let pos = doc.selection().primary();
617 let pos = doc.selection().primary();
615 let text = doc.text();
618 let text = doc.text();
@@ -622,7 +625,7 @@ pub fn select_line(cx: &mut Context) {
622}
625}
623pub fn extend_line(cx: &mut Context) {
626pub fn extend_line(cx: &mut Context) {
624 let count = cx.count;
627 let count = cx.count;
625− let doc = cx.doc();
628+ let mut doc = cx.doc();
626
629
627 let pos = doc.selection().primary();
630 let pos = doc.selection().primary();
628 let text = doc.text();
631 let text = doc.text();
@@ -650,20 +653,21 @@ fn _delete_selection(doc: &mut Document) {
650}
653}
651
654
652pub fn delete_selection(cx: &mut Context) {
655pub fn delete_selection(cx: &mut Context) {
653− let doc = cx.doc();
656+ let mut doc = cx.doc();
654− _delete_selection(doc);
657+ _delete_selection(&mut doc);
655
658
656 doc.append_changes_to_history();
659 doc.append_changes_to_history();
657}
660}
658
661
659pub fn change_selection(cx: &mut Context) {
662pub fn change_selection(cx: &mut Context) {
660− let doc = cx.doc();
663+ let mut doc = cx.doc();
661− _delete_selection(doc);
664+ _delete_selection(&mut doc);
665+ drop(doc);
662 insert_mode(cx);
666 insert_mode(cx);
663}
667}
664
668
665pub fn collapse_selection(cx: &mut Context) {
669pub fn collapse_selection(cx: &mut Context) {
666− let doc = cx.doc();
670+ let mut doc = cx.doc();
667 let selection = doc
671 let selection = doc
668 .selection()
672 .selection()
669 .transform(|range| Range::new(range.head, range.head));
673 .transform(|range| Range::new(range.head, range.head));
@@ -672,7 +676,7 @@ pub fn collapse_selection(cx: &mut Context) {
672}
676}
673
677
674pub fn flip_selections(cx: &mut Context) {
678pub fn flip_selections(cx: &mut Context) {
675− let doc = cx.doc();
679+ let mut doc = cx.doc();
676 let selection = doc
680 let selection = doc
677 .selection()
681 .selection()
678 .transform(|range| Range::new(range.head, range.anchor));
682 .transform(|range| Range::new(range.head, range.anchor));
@@ -686,8 +690,8 @@ fn enter_insert_mode(doc: &mut Document) {
686
690
687// inserts at the start of each selection
691// inserts at the start of each selection
688pub fn insert_mode(cx: &mut Context) {
692pub fn insert_mode(cx: &mut Context) {
689− let doc = cx.doc();
693+ let mut doc = cx.doc();
690− enter_insert_mode(doc);
694+ enter_insert_mode(&mut doc);
691
695
692 let selection = doc
696 let selection = doc
693 .selection()
697 .selection()
@@ -697,8 +701,8 @@ pub fn insert_mode(cx: &mut Context) {
697
701
698// inserts at the end of each selection
702// inserts at the end of each selection
699pub fn append_mode(cx: &mut Context) {
703pub fn append_mode(cx: &mut Context) {
700− let doc = cx.doc();
704+ let mut doc = cx.doc();
701− enter_insert_mode(doc);
705+ enter_insert_mode(&mut doc);
702 doc.restore_cursor = true;
706 doc.restore_cursor = true;
703
707
704 let text = doc.text().slice(..);
708 let text = doc.text().slice(..);
@@ -765,7 +769,7 @@ pub fn command_mode(cx: &mut Context) {
765 }
769 }
766 ["w"] | ["write"] => {
770 ["w"] | ["write"] => {
767 // TODO: non-blocking via save() command
771 // TODO: non-blocking via save() command
768− smol::block_on(editor.view_mut().doc.save());
772+ smol::block_on(editor.view().doc.borrow().save());
769 }
773 }
770
774
771 _ => (),
775 _ => (),
@@ -800,16 +804,18 @@ fn selection_lines(doc: &Rope, selection: &Selection) -> Vec<usize> {
800
804
801// I inserts at the start of each line with a selection
805// I inserts at the start of each line with a selection
802pub fn prepend_to_line(cx: &mut Context) {
806pub fn prepend_to_line(cx: &mut Context) {
803− let doc = cx.doc();
807+ let mut doc = cx.doc();
804− enter_insert_mode(doc);
808+ enter_insert_mode(&mut doc);
809+ drop(doc);
805
810
806 move_line_start(cx);
811 move_line_start(cx);
807}
812}
808
813
809// A inserts at the end of each line with a selection
814// A inserts at the end of each line with a selection
810pub fn append_to_line(cx: &mut Context) {
815pub fn append_to_line(cx: &mut Context) {
811− let doc = cx.doc();
816+ let mut doc = cx.doc();
812− enter_insert_mode(doc);
817+ enter_insert_mode(&mut doc);
818+ drop(doc);
813
819
814 move_line_end(cx);
820 move_line_end(cx);
815}
821}
@@ -817,8 +823,8 @@ pub fn append_to_line(cx: &mut Context) {
817// o inserts a new line after each line with a selection
823// o inserts a new line after each line with a selection
818pub fn open_below(cx: &mut Context) {
824pub fn open_below(cx: &mut Context) {
819 let count = cx.count;
825 let count = cx.count;
820− let doc = cx.doc();
826+ let mut doc = cx.doc();
821− enter_insert_mode(doc);
827+ enter_insert_mode(&mut doc);
822
828
823 let lines = selection_lines(doc.text(), doc.selection());
829 let lines = selection_lines(doc.text(), doc.selection());
824
830
@@ -875,7 +881,7 @@ pub fn open_below(cx: &mut Context) {
875// O inserts a new line before each line with a selection
881// O inserts a new line before each line with a selection
876
882
877pub fn normal_mode(cx: &mut Context) {
883pub fn normal_mode(cx: &mut Context) {
878− let doc = cx.doc();
884+ let mut doc = cx.doc();
879
885
880 doc.mode = Mode::Normal;
886 doc.mode = Mode::Normal;
881
887
@@ -909,14 +915,12 @@ pub fn exit_select_mode(cx: &mut Context) {
909}
915}
910
916
911fn goto(cx: &mut Context, locations: Vec<lsp::Location>) {
917fn 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;
915
919
916 match locations.as_slice() {
920 match locations.as_slice() {
917 [location] => {
921 [location] => {
918 cx.editor.open(PathBuf::from(location.uri.path()));
922 cx.editor.open(PathBuf::from(location.uri.path()));
919− let doc = cx.doc();
923+ let mut doc = cx.doc();
920 let definition_pos = location.range.start;
924 let definition_pos = location.range.start;
921 let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text(), definition_pos);
925 let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text(), definition_pos);
922 doc.set_selection(Selection::point(new_pos));
926 doc.set_selection(Selection::point(new_pos));
@@ -932,7 +936,8 @@ fn goto(cx: &mut Context, locations: Vec<lsp::Location>) {
932 },
936 },
933 move |editor: &mut Editor, item| {
937 move |editor: &mut Editor, item| {
934 editor.open(PathBuf::from(item.uri.path()));
938 editor.open(PathBuf::from(item.uri.path()));
939+ // TODO: issues with doc already being broo
935− let mut doc = &mut editor.view_mut().doc;
940+ let mut doc = &mut editor.view().doc.borrow_mut();
936 let definition_pos = item.range.start;
941 let definition_pos = item.range.start;
937 let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text(), definition_pos);
942 let new_pos = helix_lsp::util::lsp_pos_to_pos(doc.text(), definition_pos);
938 doc.set_selection(Selection::point(new_pos));
943 doc.set_selection(Selection::point(new_pos));
@@ -944,7 +949,7 @@ fn goto(cx: &mut Context, locations: Vec<lsp::Location>) {
944}
949}
945
950
946pub fn goto_definition(cx: &mut Context) {
951pub fn goto_definition(cx: &mut Context) {
947− let doc = cx.doc();
952+ let mut doc = cx.doc();
948 let language_server = match doc.language_server() {
953 let language_server = match doc.language_server() {
949 Some(language_server) => language_server,
954 Some(language_server) => language_server,
950 None => return,
955 None => return,
@@ -956,11 +961,12 @@ pub fn goto_definition(cx: &mut Context) {
956 // TODO: handle fails
961 // TODO: handle fails
957 let res =
962 let res =
958 smol::block_on(language_server.goto_definition(doc.identifier(), pos)).unwrap_or_default();
963 smol::block_on(language_server.goto_definition(doc.identifier(), pos)).unwrap_or_default();
964+ drop(doc);
959 goto(cx, res);
965 goto(cx, res);
960}
966}
961
967
962pub fn goto_type_definition(cx: &mut Context) {
968pub fn goto_type_definition(cx: &mut Context) {
963− let doc = cx.doc();
969+ let mut doc = cx.doc();
964 let language_server = match doc.language_server() {
970 let language_server = match doc.language_server() {
965 Some(language_server) => language_server,
971 Some(language_server) => language_server,
966 None => return,
972 None => return,
@@ -972,11 +978,12 @@ pub fn goto_type_definition(cx: &mut Context) {
972 // TODO: handle fails
978 // TODO: handle fails
973 let res = smol::block_on(language_server.goto_type_definition(doc.identifier(), pos))
979 let res = smol::block_on(language_server.goto_type_definition(doc.identifier(), pos))
974 .unwrap_or_default();
980 .unwrap_or_default();
981+ drop(doc);
975 goto(cx, res);
982 goto(cx, res);
976}
983}
977
984
978pub fn goto_implementation(cx: &mut Context) {
985pub fn goto_implementation(cx: &mut Context) {
979− let doc = cx.doc();
986+ let mut doc = cx.doc();
980 let language_server = match doc.language_server() {
987 let language_server = match doc.language_server() {
981 Some(language_server) => language_server,
988 Some(language_server) => language_server,
982 None => return,
989 None => return,
@@ -988,11 +995,12 @@ pub fn goto_implementation(cx: &mut Context) {
988 // TODO: handle fails
995 // TODO: handle fails
989 let res = smol::block_on(language_server.goto_implementation(doc.identifier(), pos))
996 let res = smol::block_on(language_server.goto_implementation(doc.identifier(), pos))
990 .unwrap_or_default();
997 .unwrap_or_default();
998+ drop(doc);
991 goto(cx, res);
999 goto(cx, res);
992}
1000}
993
1001
994pub fn goto_reference(cx: &mut Context) {
1002pub fn goto_reference(cx: &mut Context) {
995− let doc = cx.doc();
1003+ let mut doc = cx.doc();
996 let language_server = match doc.language_server() {
1004 let language_server = match doc.language_server() {
997 Some(language_server) => language_server,
1005 Some(language_server) => language_server,
998 None => return,
1006 None => return,
@@ -1004,6 +1012,7 @@ pub fn goto_reference(cx: &mut Context) {
1004 // TODO: handle fails
1012 // TODO: handle fails
1005 let res =
1013 let res =
1006 smol::block_on(language_server.goto_reference(doc.identifier(), pos)).unwrap_or_default();
1014 smol::block_on(language_server.goto_reference(doc.identifier(), pos)).unwrap_or_default();
1015+ drop(doc);
1007 goto(cx, res);
1016 goto(cx, res);
1008}
1017}
1009
1018
@@ -1017,7 +1026,7 @@ pub mod insert {
1017
1026
1018 // TODO: insert means add text just before cursor, on exit we should be on the last letter.
1027 // TODO: insert means add text just before cursor, on exit we should be on the last letter.
1019 pub fn insert_char(cx: &mut Context, c: char) {
1028 pub fn insert_char(cx: &mut Context, c: char) {
1020− let doc = cx.doc();
1029+ let mut doc = cx.doc();
1021
1030
1022 // run through insert hooks, stopping on the first one that returns Some(t)
1031 // run through insert hooks, stopping on the first one that returns Some(t)
1023 for hook in HOOKS {
1032 for hook in HOOKS {
@@ -1034,7 +1043,7 @@ pub mod insert {
1034 }
1043 }
1035
1044
1036 pub fn insert_tab(cx: &mut Context) {
1045 pub fn insert_tab(cx: &mut Context) {
1037− let doc = cx.doc();
1046+ let mut doc = cx.doc();
1038 // TODO: round out to nearest indentation level (for example a line with 3 spaces should
1047 // TODO: round out to nearest indentation level (for example a line with 3 spaces should
1039 // indent by one to reach 4 spaces).
1048 // indent by one to reach 4 spaces).
1040
1049
@@ -1044,7 +1053,7 @@ pub mod insert {
1044 }
1053 }
1045
1054
1046 pub fn insert_newline(cx: &mut Context) {
1055 pub fn insert_newline(cx: &mut Context) {
1047− let doc = cx.doc();
1056+ let mut doc = cx.doc();
1048 let text = doc.text().slice(..);
1057 let text = doc.text().slice(..);
1049 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
1058 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
1050 let indent_level =
1059 let indent_level =
@@ -1061,7 +1070,7 @@ pub mod insert {
1061 // TODO: handle indent-aware delete
1070 // TODO: handle indent-aware delete
1062 pub fn delete_char_backward(cx: &mut Context) {
1071 pub fn delete_char_backward(cx: &mut Context) {
1063 let count = cx.count;
1072 let count = cx.count;
1064− let doc = cx.doc();
1073+ let mut doc = cx.doc();
1065 let text = doc.text().slice(..);
1074 let text = doc.text().slice(..);
1066 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
1075 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
1067 (
1076 (
@@ -1075,7 +1084,7 @@ pub mod insert {
1075
1084
1076 pub fn delete_char_forward(cx: &mut Context) {
1085 pub fn delete_char_forward(cx: &mut Context) {
1077 let count = cx.count;
1086 let count = cx.count;
1078− let doc = cx.doc();
1087+ let mut doc = cx.doc();
1079 let text = doc.text().slice(..);
1088 let text = doc.text().slice(..);
1080 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
1089 let transaction = Transaction::change_by_selection(doc.text(), doc.selection(), |range| {
1081 (
1090 (
@@ -1105,7 +1114,7 @@ pub fn redo(cx: &mut Context) {
1105
1114
1106pub fn yank(cx: &mut Context) {
1115pub fn yank(cx: &mut Context) {
1107 // TODO: should selections be made end inclusive?
1116 // TODO: should selections be made end inclusive?
1108− let doc = cx.doc();
1117+ let mut doc = cx.doc();
1109 let values = doc
1118 let values = doc
1110 .selection()
1119 .selection()
1111 .fragments(doc.text().slice(..))
1120 .fragments(doc.text().slice(..))
@@ -1146,7 +1155,7 @@ pub fn paste(cx: &mut Context) {
1146
1155
1147 let mut values = values.into_iter().map(Tendril::from).chain(repeat);
1156 let mut values = values.into_iter().map(Tendril::from).chain(repeat);
1148
1157
1149− let doc = cx.doc();
1158+ let mut doc = cx.doc();
1150
1159
1151 let transaction = if linewise {
1160 let transaction = if linewise {
1152 // paste on the next line
1161 // paste on the next line
@@ -1185,8 +1194,8 @@ fn get_lines(doc: &Document) -> Vec<usize> {
1185}
1194}
1186
1195
1187pub fn indent(cx: &mut Context) {
1196pub fn indent(cx: &mut Context) {
1188− let doc = cx.doc();
1197+ let mut doc = cx.doc();
1189− let lines = get_lines(doc);
1198+ let lines = get_lines(&mut doc);
1190
1199
1191 // Indent by one level
1200 // Indent by one level
1192 let indent = Tendril::from(doc.indent_unit());
1201 let indent = Tendril::from(doc.indent_unit());
@@ -1203,8 +1212,8 @@ pub fn indent(cx: &mut Context) {
1203}
1212}
1204
1213
1205pub fn unindent(cx: &mut Context) {
1214pub fn unindent(cx: &mut Context) {
1206− let doc = cx.doc();
1215+ let mut doc = cx.doc();
1207− let lines = get_lines(doc);
1216+ let lines = get_lines(&mut doc);
1208 let mut changes = Vec::with_capacity(lines.len());
1217 let mut changes = Vec::with_capacity(lines.len());
1209 let tab_width = doc.tab_width();
1218 let tab_width = doc.tab_width();
1210
1219
@@ -1238,7 +1247,7 @@ pub fn unindent(cx: &mut Context) {
1238
1247
1239pub fn format_selections(cx: &mut Context) {
1248pub fn format_selections(cx: &mut Context) {
1240 use helix_lsp::lsp;
1249 use helix_lsp::lsp;
1241− let doc = cx.doc();
1250+ let mut doc = cx.doc();
1242
1251
1243 // via lsp if available
1252 // via lsp if available
1244 // else via tree-sitter indentation calculations
1253 // else via tree-sitter indentation calculations
@@ -1276,7 +1285,7 @@ pub fn format_selections(cx: &mut Context) {
1276
1285
1277pub fn join_selections(cx: &mut Context) {
1286pub fn join_selections(cx: &mut Context) {
1278 use movement::skip_over_next;
1287 use movement::skip_over_next;
1279− let doc = cx.doc();
1288+ let mut doc = cx.doc();
1280 let text = doc.text();
1289 let text = doc.text();
1281 let slice = doc.text().slice(..);
1290 let slice = doc.text().slice(..);
1282
1291
@@ -1319,7 +1328,6 @@ pub fn join_selections(cx: &mut Context) {
1319}
1328}
1320
1329
1321pub fn keep_selections(cx: &mut Context) {
1330pub fn keep_selections(cx: &mut Context) {
1322− let doc = cx.doc();
1323 // keep selections matching regex
1331 // keep selections matching regex
1324 let prompt = ui::regex_prompt(cx, "keep:".to_string(), |doc, regex| {
1332 let prompt = ui::regex_prompt(cx, "keep:".to_string(), |doc, regex| {
1325 let text = doc.text().slice(..);
1333 let text = doc.text().slice(..);
@@ -1333,7 +1341,7 @@ pub fn keep_selections(cx: &mut Context) {
1333}
1341}
1334
1342
1335pub fn keep_primary_selection(cx: &mut Context) {
1343pub fn keep_primary_selection(cx: &mut Context) {
1336− let doc = cx.doc();
1344+ let mut doc = cx.doc();
1337
1345
1338 let range = doc.selection().primary();
1346 let range = doc.selection().primary();
1339 let selection = Selection::single(range.anchor, range.head);
1347 let selection = Selection::single(range.anchor, range.head);
@@ -1350,7 +1358,7 @@ pub fn save(cx: &mut Context) {
1350}
1358}
1351
1359
1352pub fn completion(cx: &mut Context) {
1360pub fn completion(cx: &mut Context) {
1353− let doc = cx.doc();
1361+ let mut doc = cx.doc();
1354
1362
1355 let language_server = match doc.language_server() {
1363 let language_server = match doc.language_server() {
1356 Some(language_server) => language_server,
1364 Some(language_server) => language_server,
@@ -1364,6 +1372,8 @@ pub fn completion(cx: &mut Context) {
1364
1372
1365 let res = smol::block_on(language_server.completion(doc.identifier(), pos)).unwrap_or_default();
1373 let res = smol::block_on(language_server.completion(doc.identifier(), pos)).unwrap_or_default();
1366
1374
1375+ drop(doc);
1376+
1367 // TODO: if no completion, show some message or something
1377 // TODO: if no completion, show some message or something
1368 if !res.is_empty() {
1378 if !res.is_empty() {
1369 // let snapshot = doc.state.clone();
1379 // let snapshot = doc.state.clone();
@@ -1379,11 +1389,11 @@ pub fn completion(cx: &mut Context) {
1379 match event {
1389 match event {
1380 PromptEvent::Abort => {
1390 PromptEvent::Abort => {
1381 // revert state
1391 // revert state
1382− let doc = &mut editor.view_mut().doc;
1392+ let doc = &mut editor.view().doc.borrow_mut();
1383 // doc.state = snapshot.clone();
1393 // doc.state = snapshot.clone();
1384 }
1394 }
1385 PromptEvent::Validate => {
1395 PromptEvent::Validate => {
1386− let doc = &mut editor.view_mut().doc;
1396+ let doc = &mut editor.view().doc.borrow_mut();
1387
1397
1388 // revert state to what it was before the last update
1398 // revert state to what it was before the last update
1389 // doc.state = snapshot.clone();
1399 // doc.state = snapshot.clone();
@@ -1449,7 +1459,7 @@ pub fn completion(cx: &mut Context) {
1449pub fn hover(cx: &mut Context) {
1459pub fn hover(cx: &mut Context) {
1450 use helix_lsp::lsp;
1460 use helix_lsp::lsp;
1451
1461
1452− let doc = cx.doc();
1462+ let mut doc = cx.doc();
1453
1463
1454 let language_server = match doc.language_server() {
1464 let language_server = match doc.language_server() {
1455 Some(language_server) => language_server,
1465 Some(language_server) => language_server,
@@ -1466,6 +1476,8 @@ pub fn hover(cx: &mut Context) {
1466 let res = smol::block_on(language_server.text_document_hover(doc.identifier(), pos))
1476 let res = smol::block_on(language_server.text_document_hover(doc.identifier(), pos))
1467 .unwrap_or_default();
1477 .unwrap_or_default();
1468
1478
1479+ drop(doc);
1480+
1469 if let Some(hover) = res {
1481 if let Some(hover) = res {
1470 // hover.contents / .range <- used for visualizing
1482 // hover.contents / .range <- used for visualizing
1471 let contents = match hover.contents {
1483 let contents = match hover.contents {
@@ -1491,12 +1503,12 @@ pub fn hover(cx: &mut Context) {
1491
1503
1492// view movements
1504// view movements
1493pub fn next_view(cx: &mut Context) {
1505pub fn next_view(cx: &mut Context) {
1494− cx.editor.tree.focus_next()
1506+ cx.editor.focus_next()
1495}
1507}
1496
1508
1497// comments
1509// comments
1498pub fn toggle_comments(cx: &mut Context) {
1510pub fn toggle_comments(cx: &mut Context) {
1499− let doc = cx.doc();
1511+ let mut doc = cx.doc();
1500 let transaction = comment::toggle_line_comments(doc.text(), doc.selection());
1512 let transaction = comment::toggle_line_comments(doc.text(), doc.selection());
1501
1513
1502 doc.apply(&transaction);
1514 doc.apply(&transaction);
@@ -1506,7 +1518,7 @@ pub fn toggle_comments(cx: &mut Context) {
1506// tree sitter node selection
1518// tree sitter node selection
1507
1519
1508pub fn expand_selection(cx: &mut Context) {
1520pub fn expand_selection(cx: &mut Context) {
1509− let doc = cx.doc();
1521+ let mut doc = cx.doc();
1510
1522
1511 if let Some(syntax) = doc.syntax() {
1523 if let Some(syntax) = doc.syntax() {
1512 let text = doc.text().slice(..);
1524 let text = doc.text().slice(..);
helix-term/src/ui/editor.rs+11−10
@@ -57,7 +57,8 @@ impl EditorView {
57 // TODO: this seems to prevent setting style later
57 // TODO: this seems to prevent setting style later
58 // surface.set_style(viewport, theme.get("ui.background"));
58 // surface.set_style(viewport, theme.get("ui.background"));
59
59
60+ let doc = view.doc.borrow();
60− self.render_diagnostics(&view.doc, area, surface, theme, is_focused);
61+ self.render_diagnostics(&doc, area, surface, theme, is_focused);
61
62
62 let area = Rect::new(
63 let area = Rect::new(
63 viewport.x,
64 viewport.x,
@@ -65,7 +66,7 @@ impl EditorView {
65 viewport.width,
66 viewport.width,
66 1,
67 1,
67 );
68 );
68− self.render_statusline(&view.doc, area, surface, theme, is_focused);
69+ self.render_statusline(&doc, area, surface, theme, is_focused);
69 }
70 }
70
71
71 pub fn render_buffer(
72 pub fn render_buffer(
@@ -76,7 +77,8 @@ impl EditorView {
76 theme: &Theme,
77 theme: &Theme,
77 is_focused: bool,
78 is_focused: bool,
78 ) {
79 ) {
79− let text = view.doc.text();
80+ let doc = view.doc.borrow();
81+ let text = doc.text().slice(..);
80
82
81 let last_line = view.last_line();
83 let last_line = view.last_line();
82
84
@@ -91,7 +93,7 @@ impl EditorView {
91 // TODO: range doesn't actually restrict source, just highlight range
93 // TODO: range doesn't actually restrict source, just highlight range
92 // TODO: cache highlight results
94 // TODO: cache highlight results
93 // TODO: only recalculate when state.doc is actually modified
95 // TODO: only recalculate when state.doc is actually modified
94− let highlights: Vec<_> = match view.doc.syntax() {
96+ let highlights: Vec<_> = match doc.syntax() {
95 Some(syntax) => {
97 Some(syntax) => {
96 syntax
98 syntax
97 .highlight_iter(text.slice(..), Some(range), None, |_| None)
99 .highlight_iter(text.slice(..), Some(range), None, |_| None)
@@ -105,7 +107,7 @@ impl EditorView {
105 let mut spans = Vec::new();
107 let mut spans = Vec::new();
106 let mut visual_x = 0;
108 let mut visual_x = 0;
107 let mut line = 0u16;
109 let mut line = 0u16;
108− let tab_width = view.doc.tab_width();
110+ let tab_width = doc.tab_width();
109
111
110 'outer: for event in highlights {
112 'outer: for event in highlights {
111 match event.unwrap() {
113 match event.unwrap() {
@@ -167,7 +169,7 @@ impl EditorView {
167 let width = grapheme_width(&grapheme) as u16;
169 let width = grapheme_width(&grapheme) as u16;
168
170
169 // ugh,interleave highlight spans with diagnostic spans
171 // 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| {
171 diagnostic.range.start <= char_index
173 diagnostic.range.start <= char_index
172 && diagnostic.range.end > char_index
174 && diagnostic.range.end > char_index
173 });
175 });
@@ -202,7 +204,6 @@ impl EditorView {
202 let end = text.line_to_char(last_line + 1);
204 let end = text.line_to_char(last_line + 1);
203 Range::new(start, end)
205 Range::new(start, end)
204 };
206 };
205− let text = text.slice(..);
206 let cursor_style = Style::default()
207 let cursor_style = Style::default()
207 // .bg(Color::Rgb(255, 255, 255))
208 // .bg(Color::Rgb(255, 255, 255))
208 .add_modifier(Modifier::REVERSED);
209 .add_modifier(Modifier::REVERSED);
@@ -212,6 +213,7 @@ impl EditorView {
212
213
213 for selection in view
214 for selection in view
214 .doc
215 .doc
216+ .borrow()
215 .selection()
217 .selection()
216 .iter()
218 .iter()
217 .filter(|range| range.overlaps(&screen))
219 .filter(|range| range.overlaps(&screen))
@@ -293,7 +295,7 @@ impl EditorView {
293 let last_line = view.last_line();
295 let last_line = view.last_line();
294 for (i, line) in (view.first_line..last_line).enumerate() {
296 for (i, line) in (view.first_line..last_line).enumerate() {
295 use helix_core::diagnostic::Severity;
297 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) {
297 surface.set_stringn(
299 surface.set_stringn(
298 viewport.x - OFFSET,
300 viewport.x - OFFSET,
299 viewport.y + i as u16,
301 viewport.y + i as u16,
@@ -433,9 +435,8 @@ impl Component for EditorView {
433 EventResult::Consumed(None)
435 EventResult::Consumed(None)
434 }
436 }
435 Event::Key(event) => {
437 Event::Key(event) => {
436− let view = cx.editor.view_mut();
438+ let mode = cx.editor.view().doc.borrow().mode();
437
439
438− let mode = view.doc.mode();
439 let mut cxt = commands::Context {
440 let mut cxt = commands::Context {
440 editor: &mut cx.editor,
441 editor: &mut cx.editor,
441 count: 1,
442 count: 1,
helix-term/src/ui/mod.rs+5−4
@@ -39,9 +39,8 @@ pub fn regex_prompt(
39 move |editor: &mut Editor, input: &str, event: PromptEvent| {
39 move |editor: &mut Editor, input: &str, event: PromptEvent| {
40 match event {
40 match event {
41 PromptEvent::Abort => {
41 PromptEvent::Abort => {
42− // TODO: also revert doc
43 // TODO: also revert text
42 // TODO: also revert text
44− let doc = &mut editor.view_mut().doc;
43+ let doc = &mut editor.view().doc.borrow_mut();
45 doc.set_selection(snapshot.clone());
44 doc.set_selection(snapshot.clone());
46 }
45 }
47 PromptEvent::Validate => {
46 PromptEvent::Validate => {
@@ -56,13 +55,15 @@ pub fn regex_prompt(
56 match Regex::new(input) {
55 match Regex::new(input) {
57 Ok(regex) => {
56 Ok(regex) => {
58 let view = &mut editor.view_mut();
57 let view = &mut editor.view_mut();
59− let doc = &mut view.doc;
58+ let mut doc = view.doc.borrow_mut();
60
59
61 // revert state to what it was before the last update
60 // revert state to what it was before the last update
62 // TODO: also revert text
61 // TODO: also revert text
63 doc.set_selection(snapshot.clone());
62 doc.set_selection(snapshot.clone());
64
63
65− fun(doc, regex);
64+ fun(&mut doc, regex);
65+
66+ drop(doc);
66
67
67 view.ensure_cursor_in_view();
68 view.ensure_cursor_in_view();
68 }
69 }
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};
2
2
3use std::path::PathBuf;
3use std::path::PathBuf;
4+use std::{cell::RefCell, rc::Rc};
4
5
5−use slotmap::DefaultKey as Key;
6+use slotmap::{DefaultKey as Key, SlotMap};
6
7
7use anyhow::Error;
8use anyhow::Error;
8
9
9pub struct Editor {
10pub struct Editor {
10 pub tree: Tree,
11 pub tree: Tree,
11− // pub documents: Vec<Document>,
12+ pub documents: SlotMap<DocumentId, Rc<RefCell<Document>>>,
12 pub count: Option<usize>,
13 pub count: Option<usize>,
13 pub theme: Theme,
14 pub theme: Theme,
14 pub language_servers: helix_lsp::Registry,
15 pub language_servers: helix_lsp::Registry,
@@ -25,6 +26,7 @@ impl Editor {
25
26
26 Self {
27 Self {
27 tree: Tree::new(area),
28 tree: Tree::new(area),
29+ documents: SlotMap::with_key(),
28 count: None,
30 count: None,
29 theme,
31 theme,
30 language_servers,
32 language_servers,
@@ -33,10 +35,12 @@ impl Editor {
33 }
35 }
34
36
35 pub fn open(&mut self, path: PathBuf) -> Result<(), Error> {
37 pub fn open(&mut self, path: PathBuf) -> Result<(), Error> {
38+ // TODO: issues with doc already being borrowed if called from inside goto()
39+
36 let existing_view = self
40 let existing_view = self
37 .tree
41 .tree
38 .views()
42 .views()
39− .find(|(view, _)| view.doc.path() == Some(&path));
43+ .find(|(view, _)| view.doc.borrow().path() == Some(&path));
40
44
41 if let Some((view, _)) = existing_view {
45 if let Some((view, _)) = existing_view {
42 self.tree.focus = view.id;
46 self.tree.focus = view.id;
@@ -69,6 +73,10 @@ impl Editor {
69 .unwrap();
73 .unwrap();
70 }
74 }
71
75
76+ let doc = Rc::new(RefCell::new(doc));
77+ // TODO: store id as doc.id
78+ let id = self.documents.insert(doc.clone());
79+
72 let view = View::new(doc)?;
80 let view = View::new(doc)?;
73 self.tree.insert(view);
81 self.tree.insert(view);
74 Ok(())
82 Ok(())
@@ -80,7 +88,7 @@ impl Editor {
80 let language_servers = &mut self.language_servers;
88 let language_servers = &mut self.language_servers;
81 let executor = self.executor;
89 let executor = self.executor;
82
90
83− let doc = &view.doc;
91+ let doc = view.doc.borrow();
84
92
85 let language_server = doc
93 let language_server = doc
86 .language
94 .language
@@ -90,7 +98,19 @@ impl Editor {
90 if let Some(language_server) = language_server {
98 if let Some(language_server) = language_server {
91 smol::block_on(language_server.text_document_did_close(doc.identifier())).unwrap();
99 smol::block_on(language_server.text_document_did_close(doc.identifier())).unwrap();
92 }
100 }
101+
102+ drop(doc); // to stop borrowing self.tree
103+
104+ // self.documents.remove(view.doc);
93− self.tree.remove(id)
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();
94 }
114 }
95
115
96 pub fn should_close(&self) -> bool {
116 pub fn should_close(&self) -> bool {
@@ -108,8 +128,9 @@ impl Editor {
108 pub fn cursor_position(&self) -> Option<helix_core::Position> {
128 pub fn cursor_position(&self) -> Option<helix_core::Position> {
109 const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter
129 const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter
110 let view = self.view();
130 let view = self.view();
131+ let doc = view.doc.borrow();
111− let cursor = view.doc.selection().cursor();
132+ let cursor = doc.selection().cursor();
112− if let Some(mut pos) = view.screen_coords_at_pos(view.doc.text().slice(..), cursor) {
133+ if let Some(mut pos) = view.screen_coords_at_pos(doc.text().slice(..), cursor) {
113 pos.col += view.area.x as usize + OFFSET as usize;
134 pos.col += view.area.x as usize + OFFSET as usize;
114 pos.row += view.area.y as usize;
135 pos.row += view.area.y as usize;
115 return Some(pos);
136 return Some(pos);
helix-view/src/lib.rs+3
@@ -4,6 +4,9 @@ pub mod theme;
4pub mod tree;
4pub mod tree;
5pub mod view;
5pub mod view;
6
6
7+use slotmap::new_key_type;
8+new_key_type! { pub struct DocumentId; }
9+
7pub use document::Document;
10pub use document::Document;
8pub use editor::Editor;
11pub use editor::Editor;
9pub use theme::Theme;
12pub use theme::Theme;
helix-view/src/view.rs+10−7
@@ -1,8 +1,9 @@
1use anyhow::Error;
1use anyhow::Error;
2
2
3use std::borrow::Cow;
3use std::borrow::Cow;
4+use std::{cell::RefCell, rc::Rc};
4
5
5−use crate::Document;
6+use crate::{Document, DocumentId};
6use helix_core::{
7use helix_core::{
7 graphemes::{grapheme_width, RopeGraphemes},
8 graphemes::{grapheme_width, RopeGraphemes},
8 Position, RopeSlice,
9 Position, RopeSlice,
@@ -14,13 +15,13 @@ pub const PADDING: usize = 5;
14
15
15pub struct View {
16pub struct View {
16 pub id: Key,
17 pub id: Key,
17− pub doc: Document,
18+ pub doc: Rc<RefCell<Document>>,
18 pub first_line: usize,
19 pub first_line: usize,
19 pub area: Rect,
20 pub area: Rect,
20}
21}
21
22
22impl View {
23impl View {
23− pub fn new(doc: Document) -> Result<Self, Error> {
24+ pub fn new(doc: Rc<RefCell<Document>>) -> Result<Self, Error> {
24 let view = Self {
25 let view = Self {
25 id: Key::default(),
26 id: Key::default(),
26 doc,
27 doc,
@@ -32,8 +33,9 @@ impl View {
32 }
33 }
33
34
34 pub fn ensure_cursor_in_view(&mut self) {
35 pub fn ensure_cursor_in_view(&mut self) {
36+ let doc = self.doc.borrow();
35− let cursor = self.doc.selection().cursor();
37+ let cursor = doc.selection().cursor();
36− let line = self.doc.text().char_to_line(cursor);
38+ let line = doc.text().char_to_line(cursor);
37 let document_end = self.first_line + (self.area.height as usize).saturating_sub(2);
39 let document_end = self.first_line + (self.area.height as usize).saturating_sub(2);
38
40
39 // TODO: side scroll
41 // TODO: side scroll
@@ -50,10 +52,11 @@ impl View {
50 /// Calculates the last visible line on screen
52 /// Calculates the last visible line on screen
51 #[inline]
53 #[inline]
52 pub fn last_line(&self) -> usize {
54 pub fn last_line(&self) -> usize {
55+ let doc = self.doc.borrow();
53 let height = self.area.height.saturating_sub(1); // - 1 for statusline
56 let height = self.area.height.saturating_sub(1); // - 1 for statusline
54 std::cmp::min(
57 std::cmp::min(
55 self.first_line + height as usize,
58 self.first_line + height as usize,
56− self.doc.text().len_lines() - 1,
59+ doc.text().len_lines() - 1,
57 )
60 )
58 }
61 }
59
62
@@ -71,7 +74,7 @@ impl View {
71 let line_start = text.line_to_char(line);
74 let line_start = text.line_to_char(line);
72 let line_slice = text.slice(line_start..pos);
75 let line_slice = text.slice(line_start..pos);
73 let mut col = 0;
76 let mut col = 0;
74− let tab_width = self.doc.tab_width();
77+ let tab_width = self.doc.borrow().tab_width();
75
78
76 for grapheme in RopeGraphemes::new(line_slice) {
79 for grapheme in RopeGraphemes::new(line_slice) {
77 if grapheme == "\t" {
80 if grapheme == "\t" {