@@ -52,27 +52,6 @@ pub struct Context<'a> {
5252}
5353
5454impl<'a> Context<'a> {
55− #[inline]
56− pub fn view(&mut self) -> &mut View {
57− self.editor.view_mut()
58− }
59−
60− #[inline]
61− pub fn doc(&mut self) -> &mut Document {
62− let id = self.editor.view().doc;
63− &mut self.editor.documents[id]
64− }
65−
66− #[inline]
67− pub fn current(&mut self) -> (&mut View, &mut Document) {
68− self.editor.current()
69− }
70−
71− #[inline]
72− pub fn current_with_registers(&mut self) -> (&mut View, &mut Document, &mut Registers) {
73− self.editor.current_with_registers()
74− }
75−
7655 /// Push a new component onto the compositor.
7756 pub fn push_layer(&mut self, mut component: Box<dyn Component>) {
7857 self.callback = Some(Box::new(|compositor: &mut Compositor| {
@@ -275,7 +254,7 @@ impl Command {
275254
276255fn move_char_left(cx: &mut Context) {
277256 let count = cx.count();
278− let (view, doc) = cx.current();
257+ let (view, doc) = current!(cx.editor);
279258 let text = doc.text().slice(..);
280259 let selection = doc.selection(view.id).transform(|range| {
281260 movement::move_horizontally(text, range, Direction::Backward, count, Movement::Move)
@@ -285,7 +264,7 @@ fn move_char_left(cx: &mut Context) {
285264
286265fn move_char_right(cx: &mut Context) {
287266 let count = cx.count();
288− let (view, doc) = cx.current();
267+ let (view, doc) = current!(cx.editor);
289268 let text = doc.text().slice(..);
290269 let selection = doc.selection(view.id).transform(|range| {
291270 movement::move_horizontally(text, range, Direction::Forward, count, Movement::Move)
@@ -295,7 +274,7 @@ fn move_char_right(cx: &mut Context) {
295274
296275fn move_line_up(cx: &mut Context) {
297276 let count = cx.count();
298− let (view, doc) = cx.current();
277+ let (view, doc) = current!(cx.editor);
299278 let text = doc.text().slice(..);
300279 let selection = doc.selection(view.id).transform(|range| {
301280 movement::move_vertically(text, range, Direction::Backward, count, Movement::Move)
@@ -305,7 +284,7 @@ fn move_line_up(cx: &mut Context) {
305284
306285fn move_line_down(cx: &mut Context) {
307286 let count = cx.count();
308− let (view, doc) = cx.current();
287+ let (view, doc) = current!(cx.editor);
309288 let text = doc.text().slice(..);
310289 let selection = doc.selection(view.id).transform(|range| {
311290 movement::move_vertically(text, range, Direction::Forward, count, Movement::Move)
@@ -314,7 +293,7 @@ fn move_line_down(cx: &mut Context) {
314293}
315294
316295fn move_line_end(cx: &mut Context) {
317− let (view, doc) = cx.current();
296+ let (view, doc) = current!(cx.editor);
318297
319298 let selection = doc.selection(view.id).transform(|range| {
320299 let text = doc.text();
@@ -330,7 +309,7 @@ fn move_line_end(cx: &mut Context) {
330309}
331310
332311fn move_line_start(cx: &mut Context) {
333− let (view, doc) = cx.current();
312+ let (view, doc) = current!(cx.editor);
334313
335314 let selection = doc.selection(view.id).transform(|range| {
336315 let text = doc.text();
@@ -345,7 +324,7 @@ fn move_line_start(cx: &mut Context) {
345324}
346325
347326fn move_first_nonwhitespace(cx: &mut Context) {
348− let (view, doc) = cx.current();
327+ let (view, doc) = current!(cx.editor);
349328
350329 let selection = doc.selection(view.id).transform(|range| {
351330 let text = doc.text();
@@ -368,7 +347,7 @@ fn move_first_nonwhitespace(cx: &mut Context) {
368347
369348fn move_next_word_start(cx: &mut Context) {
370349 let count = cx.count();
371− let (view, doc) = cx.current();
350+ let (view, doc) = current!(cx.editor);
372351 let text = doc.text().slice(..);
373352
374353 let selection = doc
@@ -380,7 +359,7 @@ fn move_next_word_start(cx: &mut Context) {
380359
381360fn move_prev_word_start(cx: &mut Context) {
382361 let count = cx.count();
383− let (view, doc) = cx.current();
362+ let (view, doc) = current!(cx.editor);
384363 let text = doc.text().slice(..);
385364
386365 let selection = doc
@@ -392,7 +371,7 @@ fn move_prev_word_start(cx: &mut Context) {
392371
393372fn move_next_word_end(cx: &mut Context) {
394373 let count = cx.count();
395− let (view, doc) = cx.current();
374+ let (view, doc) = current!(cx.editor);
396375 let text = doc.text().slice(..);
397376
398377 let selection = doc
@@ -404,13 +383,13 @@ fn move_next_word_end(cx: &mut Context) {
404383
405384fn move_file_start(cx: &mut Context) {
406385 push_jump(cx.editor);
407− let (view, doc) = cx.current();
386+ let (view, doc) = current!(cx.editor);
408387 doc.set_selection(view.id, Selection::point(0));
409388}
410389
411390fn move_file_end(cx: &mut Context) {
412391 push_jump(cx.editor);
413− let (view, doc) = cx.current();
392+ let (view, doc) = current!(cx.editor);
414393 let text = doc.text();
415394 let last_line = text.line_to_char(text.len_lines().saturating_sub(2));
416395 doc.set_selection(view.id, Selection::point(last_line));
@@ -418,7 +397,7 @@ fn move_file_end(cx: &mut Context) {
418397
419398fn extend_next_word_start(cx: &mut Context) {
420399 let count = cx.count();
421− let (view, doc) = cx.current();
400+ let (view, doc) = current!(cx.editor);
422401 let text = doc.text().slice(..);
423402
424403 let selection = doc.selection(view.id).transform(|mut range| {
@@ -432,7 +411,7 @@ fn extend_next_word_start(cx: &mut Context) {
432411
433412fn extend_prev_word_start(cx: &mut Context) {
434413 let count = cx.count();
435− let (view, doc) = cx.current();
414+ let (view, doc) = current!(cx.editor);
436415 let text = doc.text().slice(..);
437416
438417 let selection = doc.selection(view.id).transform(|mut range| {
@@ -445,7 +424,7 @@ fn extend_prev_word_start(cx: &mut Context) {
445424
446425fn extend_next_word_end(cx: &mut Context) {
447426 let count = cx.count();
448− let (view, doc) = cx.current();
427+ let (view, doc) = current!(cx.editor);
449428 let text = doc.text().slice(..);
450429
451430 let selection = doc.selection(view.id).transform(|mut range| {
@@ -482,7 +461,7 @@ where
482461 _ => return,
483462 };
484463
485− let (view, doc) = cx.current();
464+ let (view, doc) = current!(cx.editor);
486465 let text = doc.text().slice(..);
487466
488467 let selection = doc.selection(view.id).transform(|mut range| {
@@ -574,7 +553,7 @@ fn extend_prev_char(cx: &mut Context) {
574553}
575554
576555fn extend_first_nonwhitespace(cx: &mut Context) {
577− let (view, doc) = cx.current();
556+ let (view, doc) = current!(cx.editor);
578557
579558 let selection = doc.selection(view.id).transform(|range| {
580559 let text = doc.text();
@@ -607,7 +586,7 @@ fn replace(cx: &mut Context) {
607586 };
608587
609588 if let Some(ch) = ch {
610− let (view, doc) = cx.current();
589+ let (view, doc) = current!(cx.editor);
611590
612591 let transaction =
613592 Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
@@ -631,7 +610,7 @@ fn replace(cx: &mut Context) {
631610
632611fn scroll(cx: &mut Context, offset: usize, direction: Direction) {
633612 use Direction::*;
634− let (view, doc) = cx.current();
613+ let (view, doc) = current!(cx.editor);
635614 let cursor = coords_at_pos(doc.text().slice(..), doc.selection(view.id).cursor());
636615 let doc_last_line = doc.text().len_lines() - 1;
637616
@@ -672,32 +651,32 @@ fn scroll(cx: &mut Context, offset: usize, direction: Direction) {
672651}
673652
674653fn page_up(cx: &mut Context) {
675− let view = cx.view();
654+ let view = view!(cx.editor);
676655 let offset = view.area.height as usize;
677656 scroll(cx, offset, Direction::Backward);
678657}
679658
680659fn page_down(cx: &mut Context) {
681− let view = cx.view();
660+ let view = view!(cx.editor);
682661 let offset = view.area.height as usize;
683662 scroll(cx, offset, Direction::Forward);
684663}
685664
686665fn half_page_up(cx: &mut Context) {
687− let view = cx.view();
666+ let view = view!(cx.editor);
688667 let offset = view.area.height as usize / 2;
689668 scroll(cx, offset, Direction::Backward);
690669}
691670
692671fn half_page_down(cx: &mut Context) {
693− let view = cx.view();
672+ let view = view!(cx.editor);
694673 let offset = view.area.height as usize / 2;
695674 scroll(cx, offset, Direction::Forward);
696675}
697676
698677fn extend_char_left(cx: &mut Context) {
699678 let count = cx.count();
700− let (view, doc) = cx.current();
679+ let (view, doc) = current!(cx.editor);
701680 let text = doc.text().slice(..);
702681 let selection = doc.selection(view.id).transform(|range| {
703682 movement::move_horizontally(text, range, Direction::Backward, count, Movement::Extend)
@@ -707,7 +686,7 @@ fn extend_char_left(cx: &mut Context) {
707686
708687fn extend_char_right(cx: &mut Context) {
709688 let count = cx.count();
710− let (view, doc) = cx.current();
689+ let (view, doc) = current!(cx.editor);
711690 let text = doc.text().slice(..);
712691 let selection = doc.selection(view.id).transform(|range| {
713692 movement::move_horizontally(text, range, Direction::Forward, count, Movement::Extend)
@@ -717,7 +696,7 @@ fn extend_char_right(cx: &mut Context) {
717696
718697fn extend_line_up(cx: &mut Context) {
719698 let count = cx.count();
720− let (view, doc) = cx.current();
699+ let (view, doc) = current!(cx.editor);
721700 let text = doc.text().slice(..);
722701 let selection = doc.selection(view.id).transform(|range| {
723702 movement::move_vertically(text, range, Direction::Backward, count, Movement::Extend)
@@ -727,7 +706,7 @@ fn extend_line_up(cx: &mut Context) {
727706
728707fn extend_line_down(cx: &mut Context) {
729708 let count = cx.count();
730− let (view, doc) = cx.current();
709+ let (view, doc) = current!(cx.editor);
731710 let text = doc.text().slice(..);
732711 let selection = doc.selection(view.id).transform(|range| {
733712 movement::move_vertically(text, range, Direction::Forward, count, Movement::Extend)
@@ -736,7 +715,7 @@ fn extend_line_down(cx: &mut Context) {
736715}
737716
738717fn extend_line_end(cx: &mut Context) {
739− let (view, doc) = cx.current();
718+ let (view, doc) = current!(cx.editor);
740719
741720 let selection = doc.selection(view.id).transform(|range| {
742721 let text = doc.text();
@@ -752,7 +731,7 @@ fn extend_line_end(cx: &mut Context) {
752731}
753732
754733fn extend_line_start(cx: &mut Context) {
755− let (view, doc) = cx.current();
734+ let (view, doc) = current!(cx.editor);
756735
757736 let selection = doc.selection(view.id).transform(|range| {
758737 let text = doc.text();
@@ -767,7 +746,7 @@ fn extend_line_start(cx: &mut Context) {
767746}
768747
769748fn select_all(cx: &mut Context) {
770− let (view, doc) = cx.current();
749+ let (view, doc) = current!(cx.editor);
771750
772751 let end = doc.text().len_chars().saturating_sub(1);
773752 doc.set_selection(view.id, Selection::single(0, end))
@@ -796,7 +775,7 @@ fn split_selection(cx: &mut Context) {
796775}
797776
798777fn split_selection_on_newline(cx: &mut Context) {
799− let (view, doc) = cx.current();
778+ let (view, doc) = current!(cx.editor);
800779 let text = doc.text().slice(..);
801780 // only compile the regex once
802781 #[allow(clippy::trivial_regex)]
@@ -848,7 +827,7 @@ fn search_impl(doc: &mut Document, view: &mut View, contents: &str, regex: &Rege
848827
849828// TODO: use one function for search vs extend
850829fn search(cx: &mut Context) {
851− let (view, doc) = cx.current();
830+ let (view, doc) = current!(cx.editor);
852831
853832 // TODO: could probably share with select_on_matches?
854833
@@ -871,7 +850,8 @@ fn search(cx: &mut Context) {
871850}
872851
873852fn search_next_impl(cx: &mut Context, extend: bool) {
874− let (view, doc, registers) = cx.current_with_registers();
853+ let (view, doc) = current!(cx.editor);
854+ let registers = &mut cx.editor.registers;
875855 if let Some(query) = registers.read('\\') {
876856 let query = query.first().unwrap();
877857 let contents = doc.text().slice(..).to_string();
@@ -889,7 +869,7 @@ fn extend_search_next(cx: &mut Context) {
889869}
890870
891871fn search_selection(cx: &mut Context) {
892− let (view, doc) = cx.current();
872+ let (view, doc) = current!(cx.editor);
893873 let contents = doc.text().slice(..);
894874 let query = doc.selection(view.id).primary().fragment(contents);
895875 let regex = regex::escape(&query);
@@ -905,7 +885,7 @@ fn search_selection(cx: &mut Context) {
905885
906886fn select_line(cx: &mut Context) {
907887 let count = cx.count();
908− let (view, doc) = cx.current();
888+ let (view, doc) = current!(cx.editor);
909889
910890 let pos = doc.selection(view.id).primary();
911891 let text = doc.text();
@@ -920,7 +900,7 @@ fn select_line(cx: &mut Context) {
920900}
921901fn extend_line(cx: &mut Context) {
922902 let count = cx.count();
923− let (view, doc) = cx.current();
903+ let (view, doc) = current!(cx.editor);
924904
925905 let pos = doc.selection(view.id).primary();
926906 let text = doc.text();
@@ -962,7 +942,8 @@ fn delete_selection_impl(reg: &mut Register, doc: &mut Document, view_id: ViewId
962942
963943fn delete_selection(cx: &mut Context) {
964944 let reg_name = cx.selected_register.name();
965− let (view, doc, registers) = cx.current_with_registers();
945+ let (view, doc) = current!(cx.editor);
946+ let registers = &mut cx.editor.registers;
966947 let reg = registers.get_or_insert(reg_name);
967948 delete_selection_impl(reg, doc, view.id);
968949
@@ -974,14 +955,15 @@ fn delete_selection(cx: &mut Context) {
974955
975956fn change_selection(cx: &mut Context) {
976957 let reg_name = cx.selected_register.name();
977− let (view, doc, registers) = cx.current_with_registers();
958+ let (view, doc) = current!(cx.editor);
959+ let registers = &mut cx.editor.registers;
978960 let reg = registers.get_or_insert(reg_name);
979961 delete_selection_impl(reg, doc, view.id);
980962 enter_insert_mode(doc);
981963}
982964
983965fn collapse_selection(cx: &mut Context) {
984− let (view, doc) = cx.current();
966+ let (view, doc) = current!(cx.editor);
985967 let selection = doc
986968 .selection(view.id)
987969 .transform(|range| Range::new(range.head, range.head));
@@ -990,7 +972,7 @@ fn collapse_selection(cx: &mut Context) {
990972}
991973
992974fn flip_selections(cx: &mut Context) {
993− let (view, doc) = cx.current();
975+ let (view, doc) = current!(cx.editor);
994976 let selection = doc
995977 .selection(view.id)
996978 .transform(|range| Range::new(range.head, range.anchor));
@@ -1004,7 +986,7 @@ fn enter_insert_mode(doc: &mut Document) {
1004986
1005987// inserts at the start of each selection
1006988fn insert_mode(cx: &mut Context) {
1007− let (view, doc) = cx.current();
989+ let (view, doc) = current!(cx.editor);
1008990 enter_insert_mode(doc);
1009991
1010992 let selection = doc
@@ -1015,7 +997,7 @@ fn insert_mode(cx: &mut Context) {
1015997
1016998// inserts at the end of each selection
1017999fn append_mode(cx: &mut Context) {
1018− let (view, doc) = cx.current();
1000+ let (view, doc) = current!(cx.editor);
10191001 enter_insert_mode(doc);
10201002 doc.restore_cursor = true;
10211003
@@ -1062,11 +1044,11 @@ mod cmd {
10621044 if editor.tree.views().count() == 1 && buffers_remaining_impl(editor) {
10631045 return;
10641046 }
1065− editor.close(editor.view().id, /* close_buffer */ false);
1047+ editor.close(view!(editor).id, /* close_buffer */ false);
10661048 }
10671049
10681050 fn force_quit(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1069− editor.close(editor.view().id, /* close_buffer */ false);
1051+ editor.close(view!(editor).id, /* close_buffer */ false);
10701052 }
10711053
10721054 fn open(editor: &mut Editor, args: &[&str], event: PromptEvent) {
@@ -1108,7 +1090,7 @@ mod cmd {
11081090 }
11091091
11101092 fn write(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1111− let (view, doc) = editor.current();
1093+ let (view, doc) = current!(editor);
11121094 if let Err(e) = write_impl(view, doc, args.first()) {
11131095 editor.set_error(e.to_string());
11141096 };
@@ -1119,7 +1101,7 @@ mod cmd {
11191101 }
11201102
11211103 fn format(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1122− let (view, doc) = editor.current();
1104+ let (view, doc) = current!(editor);
11231105
11241106 doc.format(view.id)
11251107 }
@@ -1129,7 +1111,7 @@ mod cmd {
11291111
11301112 // If no argument, report current indent style.
11311113 if args.is_empty() {
1132− let style = editor.current().1.indent_style;
1114+ let style = current!(editor).1.indent_style;
11331115 editor.set_status(match style {
11341116 Tabs => "tabs".into(),
11351117 Spaces(1) => "1 space".into(),
@@ -1152,7 +1134,7 @@ mod cmd {
11521134 };
11531135
11541136 if let Some(s) = style {
1155− let (_, doc) = editor.current();
1137+ let doc = doc_mut!(editor);
11561138 doc.indent_style = s;
11571139 } else {
11581140 // Invalid argument.
@@ -1168,7 +1150,7 @@ mod cmd {
11681150 return;
11691151 }
11701152 };
1171− let (view, doc) = editor.current();
1153+ let (view, doc) = current!(editor);
11721154 doc.earlier(view.id, uk)
11731155 }
11741156
@@ -1180,12 +1162,12 @@ mod cmd {
11801162 return;
11811163 }
11821164 };
1183− let (view, doc) = editor.current();
1165+ let (view, doc) = current!(editor);
11841166 doc.later(view.id, uk)
11851167 }
11861168
11871169 fn write_quit(editor: &mut Editor, args: &[&str], event: PromptEvent) {
1188− let (view, doc) = editor.current();
1170+ let (view, doc) = current!(editor);
11891171 if let Err(e) = write_impl(view, doc, args.first()) {
11901172 editor.set_error(e.to_string());
11911173 return;
@@ -1499,7 +1481,7 @@ fn file_picker(cx: &mut Context) {
14991481
15001482fn buffer_picker(cx: &mut Context) {
15011483 use std::path::{Path, PathBuf};
1502− let current = cx.editor.view().doc;
1484+ let current = view!(cx.editor).doc;
15031485
15041486 let picker = Picker::new(
15051487 cx.editor
@@ -1547,7 +1529,7 @@ fn symbol_picker(cx: &mut Context) {
15471529 nested_to_flat(list, file, child);
15481530 }
15491531 }
1550− let (view, doc) = cx.current();
1532+ let (view, doc) = current!(cx.editor);
15511533
15521534 let language_server = match doc.language_server() {
15531535 Some(language_server) => language_server,
@@ -1568,7 +1550,7 @@ fn symbol_picker(cx: &mut Context) {
15681550 let symbols = match symbols {
15691551 lsp::DocumentSymbolResponse::Flat(symbols) => symbols,
15701552 lsp::DocumentSymbolResponse::Nested(symbols) => {
1571− let (_view, doc) = editor.current();
1553+ let (_view, doc) = current!(editor);
15721554 let mut flat_symbols = Vec::new();
15731555 for symbol in symbols {
15741556 nested_to_flat(&mut flat_symbols, &doc.identifier(), symbol)
@@ -1582,7 +1564,7 @@ fn symbol_picker(cx: &mut Context) {
15821564 |symbol| (&symbol.name).into(),
15831565 move |editor: &mut Editor, symbol, _action| {
15841566 push_jump(editor);
1585− let (view, doc) = editor.current();
1567+ let (view, doc) = current!(editor);
15861568
15871569 if let Some(range) =
15881570 lsp_range_to_range(doc.text(), symbol.location.range, offset_encoding)
@@ -1601,13 +1583,13 @@ fn symbol_picker(cx: &mut Context) {
16011583// I inserts at the first nonwhitespace character of each line with a selection
16021584fn prepend_to_line(cx: &mut Context) {
16031585 move_first_nonwhitespace(cx);
1604− let doc = cx.doc();
1586+ let doc = doc_mut!(cx.editor);
16051587 enter_insert_mode(doc);
16061588}
16071589
16081590// A inserts at the end of each line with a selection
16091591fn append_to_line(cx: &mut Context) {
1610− let (view, doc) = cx.current();
1592+ let (view, doc) = current!(cx.editor);
16111593 enter_insert_mode(doc);
16121594
16131595 let selection = doc.selection(view.id).transform(|range| {
@@ -1627,7 +1609,7 @@ enum Open {
16271609
16281610fn open(cx: &mut Context, open: Open) {
16291611 let count = cx.count();
1630− let (view, doc) = cx.current();
1612+ let (view, doc) = current!(cx.editor);
16311613 enter_insert_mode(doc);
16321614
16331615 let text = doc.text().slice(..);
@@ -1695,7 +1677,7 @@ fn open_above(cx: &mut Context) {
16951677}
16961678
16971679fn normal_mode(cx: &mut Context) {
1698− let (view, doc) = cx.current();
1680+ let (view, doc) = current!(cx.editor);
16991681
17001682 doc.mode = Mode::Normal;
17011683
@@ -1718,13 +1700,13 @@ fn normal_mode(cx: &mut Context) {
17181700
17191701// Store a jump on the jumplist.
17201702fn push_jump(editor: &mut Editor) {
1721− let (view, doc) = editor.current();
1703+ let (view, doc) = current!(editor);
17221704 let jump = (doc.id(), doc.selection(view.id).clone());
17231705 view.jumps.push(jump);
17241706}
17251707
17261708fn switch_to_last_accessed_file(cx: &mut Context) {
1727− let alternate_file = cx.view().last_accessed_doc;
1709+ let alternate_file = view!(cx.editor).last_accessed_doc;
17281710 if let Some(alt) = alternate_file {
17291711 cx.editor.switch(alt, Action::Replace);
17301712 } else {
@@ -1736,7 +1718,7 @@ fn goto_mode(cx: &mut Context) {
17361718 if let Some(count) = cx.count {
17371719 push_jump(cx.editor);
17381720
1739− let (view, doc) = cx.current();
1721+ let (view, doc) = current!(cx.editor);
17401722 let line_idx = std::cmp::min(count.get() - 1, doc.text().len_lines().saturating_sub(2));
17411723 let pos = doc.text().line_to_char(line_idx);
17421724 doc.set_selection(view.id, Selection::point(pos));
@@ -1750,7 +1732,8 @@ fn goto_mode(cx: &mut Context) {
17501732 } = event
17511733 {
17521734 // TODO: temporarily show GOTO in the mode list
1753− match (cx.doc().mode, ch) {
1735+ let doc = doc_mut!(cx.editor);
1736+ match (doc.mode, ch) {
17541737 (_, 'g') => move_file_start(cx),
17551738 (_, 'e') => move_file_end(cx),
17561739 (_, 'a') => switch_to_last_accessed_file(cx),
@@ -1766,7 +1749,7 @@ fn goto_mode(cx: &mut Context) {
17661749 (Mode::Select, 's') => extend_first_nonwhitespace(cx),
17671750
17681751 (_, 't') | (_, 'm') | (_, 'b') => {
1769− let (view, doc) = cx.current();
1752+ let (view, doc) = current!(cx.editor);
17701753
17711754 let pos = doc.selection(view.id).cursor();
17721755 let line = doc.text().char_to_line(pos);
@@ -1794,11 +1777,11 @@ fn goto_mode(cx: &mut Context) {
17941777}
17951778
17961779fn select_mode(cx: &mut Context) {
1797− cx.doc().mode = Mode::Select;
1780+ doc_mut!(cx.editor).mode = Mode::Select;
17981781}
17991782
18001783fn exit_select_mode(cx: &mut Context) {
1801− cx.doc().mode = Mode::Normal;
1784+ doc_mut!(cx.editor).mode = Mode::Normal;
18021785}
18031786
18041787fn goto_impl(
@@ -1820,7 +1803,7 @@ fn goto_impl(
18201803 let id = editor
18211804 .open(PathBuf::from(location.uri.path()), action)
18221805 .expect("editor.open failed");
1823− let (view, doc) = editor.current();
1806+ let (view, doc) = current!(editor);
18241807 let definition_pos = location.range.start;
18251808 // TODO: convert inside server
18261809 let new_pos =
@@ -1858,7 +1841,7 @@ fn goto_impl(
18581841}
18591842
18601843fn goto_definition(cx: &mut Context) {
1861− let (view, doc) = cx.current();
1844+ let (view, doc) = current!(cx.editor);
18621845 let language_server = match doc.language_server() {
18631846 Some(language_server) => language_server,
18641847 None => return,
@@ -1895,7 +1878,7 @@ fn goto_definition(cx: &mut Context) {
18951878}
18961879
18971880fn goto_type_definition(cx: &mut Context) {
1898− let (view, doc) = cx.current();
1881+ let (view, doc) = current!(cx.editor);
18991882 let language_server = match doc.language_server() {
19001883 Some(language_server) => language_server,
19011884 None => return,
@@ -1932,7 +1915,7 @@ fn goto_type_definition(cx: &mut Context) {
19321915}
19331916
19341917fn goto_implementation(cx: &mut Context) {
1935− let (view, doc) = cx.current();
1918+ let (view, doc) = current!(cx.editor);
19361919 let language_server = match doc.language_server() {
19371920 Some(language_server) => language_server,
19381921 None => return,
@@ -1969,7 +1952,7 @@ fn goto_implementation(cx: &mut Context) {
19691952}
19701953
19711954fn goto_reference(cx: &mut Context) {
1972− let (view, doc) = cx.current();
1955+ let (view, doc) = current!(cx.editor);
19731956 let language_server = match doc.language_server() {
19741957 Some(language_server) => language_server,
19751958 None => return,
@@ -2000,7 +1983,7 @@ fn goto_reference(cx: &mut Context) {
20001983fn goto_pos(editor: &mut Editor, pos: usize) {
20011984 push_jump(editor);
20021985
2003− let (view, doc) = editor.current();
1986+ let (view, doc) = current!(editor);
20041987
20051988 doc.set_selection(view.id, Selection::point(pos));
20061989 align_view(doc, view, Align::Center);
@@ -2008,7 +1991,7 @@ fn goto_pos(editor: &mut Editor, pos: usize) {
20081991
20091992fn goto_first_diag(cx: &mut Context) {
20101993 let editor = &mut cx.editor;
2011− let (view, doc) = editor.current();
1994+ let (view, doc) = current!(editor);
20121995
20131996 let cursor_pos = doc.selection(view.id).cursor();
20141997 let diag = if let Some(diag) = doc.diagnostics().first() {
@@ -2022,7 +2005,7 @@ fn goto_first_diag(cx: &mut Context) {
20222005
20232006fn goto_last_diag(cx: &mut Context) {
20242007 let editor = &mut cx.editor;
2025− let (view, doc) = editor.current();
2008+ let (view, doc) = current!(editor);
20262009
20272010 let cursor_pos = doc.selection(view.id).cursor();
20282011 let diag = if let Some(diag) = doc.diagnostics().last() {
@@ -2036,7 +2019,7 @@ fn goto_last_diag(cx: &mut Context) {
20362019
20372020fn goto_next_diag(cx: &mut Context) {
20382021 let editor = &mut cx.editor;
2039− let (view, doc) = editor.current();
2022+ let (view, doc) = current!(editor);
20402023
20412024 let cursor_pos = doc.selection(view.id).cursor();
20422025 let diag = if let Some(diag) = doc
@@ -2057,7 +2040,7 @@ fn goto_next_diag(cx: &mut Context) {
20572040
20582041fn goto_prev_diag(cx: &mut Context) {
20592042 let editor = &mut cx.editor;
2060− let (view, doc) = editor.current();
2043+ let (view, doc) = current!(editor);
20612044
20622045 let cursor_pos = doc.selection(view.id).cursor();
20632046 let diag = if let Some(diag) = doc
@@ -2078,7 +2061,7 @@ fn goto_prev_diag(cx: &mut Context) {
20782061}
20792062
20802063fn signature_help(cx: &mut Context) {
2081− let (view, doc) = cx.current();
2064+ let (view, doc) = current!(cx.editor);
20822065
20832066 let language_server = match doc.language_server() {
20842067 Some(language_server) => language_server,
@@ -2124,7 +2107,7 @@ pub mod insert {
21242107
21252108 fn completion(cx: &mut Context, ch: char) {
21262109 // if ch matches completion char, trigger completion
2127− let doc = cx.doc();
2110+ let doc = doc_mut!(cx.editor);
21282111 let language_server = match doc.language_server() {
21292112 Some(language_server) => language_server,
21302113 None => return,
@@ -2152,7 +2135,7 @@ pub mod insert {
21522135
21532136 fn signature_help(cx: &mut Context, ch: char) {
21542137 // if ch matches signature_help char, trigger
2155− let doc = cx.doc();
2138+ let doc = doc_mut!(cx.editor);
21562139 let language_server = match doc.language_server() {
21572140 Some(language_server) => language_server,
21582141 None => return,
@@ -2207,7 +2190,7 @@ pub mod insert {
22072190 const POST_HOOKS: &[PostHook] = &[completion, signature_help];
22082191
22092192 pub fn insert_char(cx: &mut Context, c: char) {
2210− let (view, doc) = cx.current();
2193+ let (view, doc) = current!(cx.editor);
22112194
22122195 let text = doc.text();
22132196 let selection = doc.selection(view.id);
@@ -2229,7 +2212,7 @@ pub mod insert {
22292212 }
22302213
22312214 pub fn insert_tab(cx: &mut Context) {
2232− let (view, doc) = cx.current();
2215+ let (view, doc) = current!(cx.editor);
22332216 // TODO: round out to nearest indentation level (for example a line with 3 spaces should
22342217 // indent by one to reach 4 spaces).
22352218
@@ -2239,7 +2222,7 @@ pub mod insert {
22392222 }
22402223
22412224 pub fn insert_newline(cx: &mut Context) {
2242− let (view, doc) = cx.current();
2225+ let (view, doc) = current!(cx.editor);
22432226 let text = doc.text().slice(..);
22442227
22452228 let contents = doc.text();
@@ -2308,7 +2291,7 @@ pub mod insert {
23082291 // TODO: handle indent-aware delete
23092292 pub fn delete_char_backward(cx: &mut Context) {
23102293 let count = cx.count();
2311− let (view, doc) = cx.current();
2294+ let (view, doc) = current!(cx.editor);
23122295 let text = doc.text().slice(..);
23132296 let transaction =
23142297 Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
@@ -2323,7 +2306,7 @@ pub mod insert {
23232306
23242307 pub fn delete_char_forward(cx: &mut Context) {
23252308 let count = cx.count();
2326− let (view, doc) = cx.current();
2309+ let (view, doc) = current!(cx.editor);
23272310 let text = doc.text().slice(..);
23282311 let transaction =
23292312 Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
@@ -2338,7 +2321,7 @@ pub mod insert {
23382321
23392322 pub fn delete_word_backward(cx: &mut Context) {
23402323 let count = cx.count();
2341− let (view, doc) = cx.current();
2324+ let (view, doc) = current!(cx.editor);
23422325 let text = doc.text().slice(..);
23432326 let selection = doc
23442327 .selection(view.id)
@@ -2354,20 +2337,22 @@ pub mod insert {
23542337// storing it?
23552338
23562339fn undo(cx: &mut Context) {
2357− let view_id = cx.view().id;
2358− cx.doc().undo(view_id);
2340+ let (view, doc) = current!(cx.editor);
2341+ let view_id = view.id;
2342+ doc.undo(view_id);
23592343}
23602344
23612345fn redo(cx: &mut Context) {
2362− let view_id = cx.view().id;
2363− cx.doc().redo(view_id);
2346+ let (view, doc) = current!(cx.editor);
2347+ let view_id = view.id;
2348+ doc.redo(view_id);
23642349}
23652350
23662351// Yank / Paste
23672352
23682353fn yank(cx: &mut Context) {
23692354 // TODO: should selections be made end inclusive?
2370− let (view, doc) = cx.current();
2355+ let (view, doc) = current!(cx.editor);
23712356 let values: Vec<String> = doc
23722357 .selection(view.id)
23732358 .fragments(doc.text().slice(..))
@@ -2432,7 +2417,8 @@ fn paste_impl(
24322417
24332418fn replace_with_yanked(cx: &mut Context) {
24342419 let reg_name = cx.selected_register.name();
2435− let (view, doc, registers) = cx.current_with_registers();
2420+ let (view, doc) = current!(cx.editor);
2421+ let registers = &mut cx.editor.registers;
24362422
24372423 if let Some(values) = registers.read(reg_name) {
24382424 if let Some(yank) = values.first() {
@@ -2461,7 +2447,8 @@ fn replace_with_yanked(cx: &mut Context) {
24612447
24622448fn paste_after(cx: &mut Context) {
24632449 let reg_name = cx.selected_register.name();
2464− let (view, doc, registers) = cx.current_with_registers();
2450+ let (view, doc) = current!(cx.editor);
2451+ let registers = &mut cx.editor.registers;
24652452
24662453 if let Some(transaction) = registers
24672454 .read(reg_name)
@@ -2474,7 +2461,8 @@ fn paste_after(cx: &mut Context) {
24742461
24752462fn paste_before(cx: &mut Context) {
24762463 let reg_name = cx.selected_register.name();
2477− let (view, doc, registers) = cx.current_with_registers();
2464+ let (view, doc) = current!(cx.editor);
2465+ let registers = &mut cx.editor.registers;
24782466
24792467 if let Some(transaction) = registers
24802468 .read(reg_name)
@@ -2504,7 +2492,7 @@ fn get_lines(doc: &Document, view_id: ViewId) -> Vec<usize> {
25042492
25052493fn indent(cx: &mut Context) {
25062494 let count = cx.count();
2507− let (view, doc) = cx.current();
2495+ let (view, doc) = current!(cx.editor);
25082496 let lines = get_lines(doc, view.id);
25092497
25102498 // Indent by one level
@@ -2523,7 +2511,7 @@ fn indent(cx: &mut Context) {
25232511
25242512fn unindent(cx: &mut Context) {
25252513 let count = cx.count();
2526− let (view, doc) = cx.current();
2514+ let (view, doc) = current!(cx.editor);
25272515 let lines = get_lines(doc, view.id);
25282516 let mut changes = Vec::with_capacity(lines.len());
25292517 let tab_width = doc.tab_width();
@@ -2562,7 +2550,7 @@ fn unindent(cx: &mut Context) {
25622550}
25632551
25642552fn format_selections(cx: &mut Context) {
2565− let (view, doc) = cx.current();
2553+ let (view, doc) = current!(cx.editor);
25662554
25672555 // via lsp if available
25682556 // else via tree-sitter indentation calculations
@@ -2609,7 +2597,7 @@ fn format_selections(cx: &mut Context) {
26092597
26102598fn join_selections(cx: &mut Context) {
26112599 use movement::skip_while;
2612− let (view, doc) = cx.current();
2600+ let (view, doc) = current!(cx.editor);
26132601 let text = doc.text();
26142602 let slice = doc.text().slice(..);
26152603
@@ -2665,7 +2653,7 @@ fn keep_selections(cx: &mut Context) {
26652653}
26662654
26672655fn keep_primary_selection(cx: &mut Context) {
2668− let (view, doc) = cx.current();
2656+ let (view, doc) = current!(cx.editor);
26692657
26702658 let range = doc.selection(view.id).primary();
26712659 let selection = Selection::single(range.anchor, range.head);
@@ -2678,7 +2666,7 @@ fn save(cx: &mut Context) {
26782666 // Spawns an async task to actually do the saving. This way we prevent blocking.
26792667
26802668 // TODO: handle save errors somehow?
2681− tokio::spawn(cx.doc().save());
2669+ tokio::spawn(doc_mut!(cx.editor).save());
26822670}
26832671
26842672fn completion(cx: &mut Context) {
@@ -2718,7 +2706,7 @@ fn completion(cx: &mut Context) {
27182706 // The prefix still has to satisfy `company-minimum-prefix-length' before that
27192707 // happens. The value of nil means no idle completion."
27202708
2721− let (view, doc) = cx.current();
2709+ let (view, doc) = current!(cx.editor);
27222710
27232711 let language_server = match doc.language_server() {
27242712 Some(language_server) => language_server,
@@ -2772,7 +2760,7 @@ fn completion(cx: &mut Context) {
27722760}
27732761
27742762fn hover(cx: &mut Context) {
2775− let (view, doc) = cx.current();
2763+ let (view, doc) = current!(cx.editor);
27762764
27772765 let language_server = match doc.language_server() {
27782766 Some(language_server) => language_server,
@@ -2822,7 +2810,7 @@ fn hover(cx: &mut Context) {
28222810
28232811// comments
28242812fn toggle_comments(cx: &mut Context) {
2825− let (view, doc) = cx.current();
2813+ let (view, doc) = current!(cx.editor);
28262814 let transaction = comment::toggle_line_comments(doc.text(), doc.selection(view.id));
28272815
28282816 doc.apply(&transaction, view.id);
@@ -2832,7 +2820,7 @@ fn toggle_comments(cx: &mut Context) {
28322820// tree sitter node selection
28332821
28342822fn expand_selection(cx: &mut Context) {
2835− let (view, doc) = cx.current();
2823+ let (view, doc) = current!(cx.editor);
28362824
28372825 if let Some(syntax) = doc.syntax() {
28382826 let text = doc.text().slice(..);
@@ -2842,7 +2830,7 @@ fn expand_selection(cx: &mut Context) {
28422830}
28432831
28442832fn match_brackets(cx: &mut Context) {
2845− let (view, doc) = cx.current();
2833+ let (view, doc) = current!(cx.editor);
28462834
28472835 if let Some(syntax) = doc.syntax() {
28482836 let pos = doc.selection(view.id).cursor();
@@ -2857,12 +2845,12 @@ fn match_brackets(cx: &mut Context) {
28572845
28582846fn jump_forward(cx: &mut Context) {
28592847 let count = cx.count();
2860− let (view, doc) = cx.current();
2848+ let (view, doc) = current!(cx.editor);
28612849
28622850 if let Some((id, selection)) = view.jumps.forward(count) {
28632851 view.doc = *id;
28642852 let selection = selection.clone();
2865− let (view, doc) = cx.current(); // refetch doc
2853+ let (view, doc) = current!(cx.editor); // refetch doc
28662854 doc.set_selection(view.id, selection);
28672855
28682856 align_view(doc, view, Align::Center);
@@ -2871,7 +2859,7 @@ fn jump_forward(cx: &mut Context) {
28712859
28722860fn jump_backward(cx: &mut Context) {
28732861 let count = cx.count();
2874− let (view, doc) = cx.current();
2862+ let (view, doc) = current!(cx.editor);
28752863
28762864 if let Some((id, selection)) = view.jumps.backward(view.id, doc, count) {
28772865 // manually set the alternate_file as we cannot use the Editor::switch function here.
@@ -2880,7 +2868,7 @@ fn jump_backward(cx: &mut Context) {
28802868 }
28812869 view.doc = *id;
28822870 let selection = selection.clone();
2883− let (view, doc) = cx.current(); // refetch doc
2871+ let (view, doc) = current!(cx.editor); // refetch doc
28842872 doc.set_selection(view.id, selection);
28852873
28862874 align_view(doc, view, Align::Center);
@@ -2915,7 +2903,7 @@ use helix_view::editor::Action;
29152903use self::cmd::TypableCommand;
29162904fn split(cx: &mut Context, action: Action) {
29172905 use helix_view::editor::Action;
2918− let (view, doc) = cx.current();
2906+ let (view, doc) = current!(cx.editor);
29192907 let id = doc.id();
29202908 let selection = doc.selection(view.id).clone();
29212909 let first_line = view.first_line;
@@ -2923,7 +2911,7 @@ fn split(cx: &mut Context, action: Action) {
29232911 cx.editor.switch(id, action);
29242912
29252913 // match the selection in the previous view
2926− let (view, doc) = cx.current();
2914+ let (view, doc) = current!(cx.editor);
29272915 view.first_line = first_line;
29282916 doc.set_selection(view.id, selection);
29292917}
@@ -2937,7 +2925,7 @@ fn vsplit(cx: &mut Context) {
29372925}
29382926
29392927fn wclose(cx: &mut Context) {
2940− let view_id = cx.view().id;
2928+ let view_id = view!(cx.editor).id;
29412929 // close current split
29422930 cx.editor.close(view_id, /* close_buffer */ false);
29432931}
@@ -2992,7 +2980,7 @@ fn view_mode(cx: &mut Context) {
29922980 | 't'
29932981 // bottom
29942982 | 'b' => {
2995− let (view, doc) = cx.current();
2983+ let (view, doc) = current!(cx.editor);
29962984
29972985 align_view(doc, view, match ch {
29982986 'z' | 'c' => Align::Center,
@@ -3002,7 +2990,7 @@ fn view_mode(cx: &mut Context) {
30022990 });
30032991 }
30042992 'm' => {
3005− let (view, doc) = cx.current();
2993+ let (view, doc) = current!(cx.editor);
30062994 let pos = doc.selection(view.id).cursor();
30072995 let pos = coords_at_pos(doc.text().slice(..), pos);
30082996