@@ -36,6 +36,15 @@ impl<'a> Context<'a> {
3636 pub fn doc(&mut self) -> &mut Document {
3737 &mut self.editor.view_mut().doc
3838 }
39+
40+ /// Push a new component onto the compositor.
41+ pub fn push_layer(&mut self, component: Box<dyn crate::compositor::Component>) {
42+ self.callback = Some(Box::new(
43+ |compositor: &mut Compositor, editor: &mut Editor| {
44+ compositor.push(component);
45+ },
46+ ));
47+ }
3948}
4049
4150/// A command is a function that takes the current state and a count, and does a side-effect on the
@@ -44,42 +53,43 @@ pub type Command = fn(cx: &mut Context);
4453
4554pub fn move_char_left(cx: &mut Context) {
4655 let count = cx.count;
47− let selection =
48− cx.doc()
49− .state
50− .move_selection(Direction::Backward, Granularity::Character, count);
51− cx.doc().set_selection(selection);
56+ let doc = cx.doc();
57+ let selection = doc
58+ .state
59+ .move_selection(Direction::Backward, Granularity::Character, count);
60+ doc.set_selection(selection);
5261}
5362
5463pub fn move_char_right(cx: &mut Context) {
5564 let count = cx.count;
56− let selection =
57− cx.doc()
58− .state
59− .move_selection(Direction::Forward, Granularity::Character, count);
60− cx.doc().set_selection(selection);
65+ let doc = cx.doc();
66+ let selection = doc
67+ .state
68+ .move_selection(Direction::Forward, Granularity::Character, count);
69+ doc.set_selection(selection);
6170}
6271
6372pub fn move_line_up(cx: &mut Context) {
6473 let count = cx.count;
65− let selection = cx
66− .doc()
74+ let doc = cx.doc();
75+ let selection = doc
6776 .state
6877 .move_selection(Direction::Backward, Granularity::Line, count);
69− cx.doc().set_selection(selection);
78+ doc.set_selection(selection);
7079}
7180
7281pub fn move_line_down(cx: &mut Context) {
7382 let count = cx.count;
74− let selection = cx
75− .doc()
83+ let doc = cx.doc();
84+ let selection = doc
7685 .state
7786 .move_selection(Direction::Forward, Granularity::Line, count);
78− cx.doc().set_selection(selection);
87+ doc.set_selection(selection);
7988}
8089
8190pub fn move_line_end(cx: &mut Context) {
82− let lines = selection_lines(&cx.doc().state);
91+ let doc = cx.doc();
92+ let lines = selection_lines(&doc.state);
8393
8494 let positions = lines
8595 .into_iter()
@@ -88,29 +98,30 @@ pub fn move_line_end(cx: &mut Context) {
8898
8999 // Line end is pos at the start of next line - 1
90100 // subtract another 1 because the line ends with \n
91− cx.doc().text().line_to_char(index + 1).saturating_sub(2)
101+ doc.text().line_to_char(index + 1).saturating_sub(2)
92102 })
93103 .map(|pos| Range::new(pos, pos));
94104
95105 let selection = Selection::new(positions.collect(), 0);
96106
97− cx.doc().set_selection(selection);
107+ doc.set_selection(selection);
98108}
99109
100110pub fn move_line_start(cx: &mut Context) {
101− let lines = selection_lines(&cx.doc().state);
111+ let doc = cx.doc();
112+ let lines = selection_lines(&doc.state);
102113
103114 let positions = lines
104115 .into_iter()
105116 .map(|index| {
106117 // adjust all positions to the start of the line.
107− cx.doc().text().line_to_char(index)
118+ doc.text().line_to_char(index)
108119 })
109120 .map(|pos| Range::new(pos, pos));
110121
111122 let selection = Selection::new(positions.collect(), 0);
112123
113− cx.doc().set_selection(selection);
124+ doc.set_selection(selection);
114125}
115126
116127pub fn move_next_word_start(cx: &mut Context) {
@@ -139,17 +150,19 @@ pub fn move_next_word_end(cx: &mut Context) {
139150}
140151
141152pub fn move_file_start(cx: &mut Context) {
142− cx.doc().set_selection(Selection::point(0));
153+ let doc = cx.doc();
154+ doc.set_selection(Selection::point(0));
143155
144− cx.doc().mode = Mode::Normal;
156+ doc.mode = Mode::Normal;
145157}
146158
147159pub fn move_file_end(cx: &mut Context) {
148− let text = &cx.doc().text();
160+ let doc = cx.doc();
161+ let text = doc.text();
149162 let last_line = text.line_to_char(text.len_lines().saturating_sub(2));
150− cx.doc().set_selection(Selection::point(last_line));
163+ doc.set_selection(Selection::point(last_line));
151164
152− cx.doc().mode = Mode::Normal;
165+ doc.mode = Mode::Normal;
153166}
154167
155168pub fn extend_next_word_start(cx: &mut Context) {
@@ -161,7 +174,7 @@ pub fn extend_next_word_start(cx: &mut Context) {
161174 range
162175 }); // TODO: count
163176
164− cx.doc().set_selection(selection);
177+ doc.set_selection(selection);
165178}
166179
167180pub fn extend_prev_word_start(cx: &mut Context) {
@@ -172,7 +185,7 @@ pub fn extend_prev_word_start(cx: &mut Context) {
172185 range.head = pos;
173186 range
174187 }); // TODO: count
175− cx.doc().set_selection(selection);
188+ doc.set_selection(selection);
176189}
177190
178191pub fn extend_next_word_end(cx: &mut Context) {
@@ -184,7 +197,7 @@ pub fn extend_next_word_end(cx: &mut Context) {
184197 range
185198 }); // TODO: count
186199
187− cx.doc().set_selection(selection);
200+ doc.set_selection(selection);
188201}
189202
190203pub fn check_cursor_in_view(view: &View) -> bool {
@@ -309,11 +322,8 @@ pub fn select_regex(cx: &mut Context) {
309322 selection::select_on_matches(text, doc.selection(), ®ex).expect("no matches");
310323 doc.set_selection(selection);
311324 });
312− cx.callback = Some(Box::new(
313− move |compositor: &mut Compositor, editor: &mut Editor| {
314− compositor.push(Box::new(prompt));
315− },
316− ));
325+
326+ cx.push_layer(Box::new(prompt));
317327}
318328
319329pub fn split_selection(cx: &mut Context) {
@@ -336,11 +346,7 @@ pub fn split_selection(cx: &mut Context) {
336346 doc.set_selection(selection);
337347 });
338348
339− cx.callback = Some(Box::new(
340− move |compositor: &mut Compositor, editor: &mut Editor| {
341− compositor.push(Box::new(prompt));
342− },
343− ));
349+ cx.push_layer(Box::new(prompt));
344350}
345351
346352pub fn split_selection_on_newline(cx: &mut Context) {
@@ -393,11 +399,7 @@ pub fn search(cx: &mut Context) {
393399 register::set('\\', vec![regex.as_str().to_string()]);
394400 });
395401
396− cx.callback = Some(Box::new(
397− move |compositor: &mut Compositor, editor: &mut Editor| {
398− compositor.push(Box::new(prompt));
399− },
400− ));
402+ cx.push_layer(Box::new(prompt));
401403}
402404
403405pub fn search_next(cx: &mut Context) {
@@ -418,74 +420,76 @@ pub fn search_next(cx: &mut Context) {
418420
419421pub fn select_line(cx: &mut Context) {
420422 // TODO: count
421− let pos = cx.doc().selection().primary();
422− let text = cx.doc().text();
423+ let doc = cx.doc();
424+ let pos = doc.selection().primary();
425+ let text = doc.text();
423426 let line = text.char_to_line(pos.head);
424427 let start = text.line_to_char(line);
425428 let end = text.line_to_char(line + 1).saturating_sub(1);
426429
427− cx.doc().set_selection(Selection::single(start, end));
430+ doc.set_selection(Selection::single(start, end));
428431}
429432
430433// heuristic: append changes to history after each command, unless we're in insert mode
431434
432−fn _delete_selection(cx: &mut Context) {
433− let transaction = Transaction::change_by_selection(&cx.doc().state, |range| {
434− (range.from(), range.to() + 1, None)
435− });
436− cx.doc().apply(&transaction);
435+fn _delete_selection(doc: &mut Document) {
436+ let transaction =
437+ Transaction::change_by_selection(&doc.state, |range| (range.from(), range.to() + 1, None));
438+ doc.apply(&transaction);
437439}
438440
439441pub fn delete_selection(cx: &mut Context) {
440− _delete_selection(cx);
442+ let doc = cx.doc();
443+ _delete_selection(doc);
441444
442− append_changes_to_history(cx);
445+ append_changes_to_history(doc);
443446}
444447
445448pub fn change_selection(cx: &mut Context) {
446− _delete_selection(cx);
449+ let doc = cx.doc();
450+ _delete_selection(doc);
447451 insert_mode(cx);
448452}
449453
450454pub fn collapse_selection(cx: &mut Context) {
451− let selection = cx
452− .doc()
455+ let doc = cx.doc();
456+ let selection = doc
453457 .selection()
454458 .transform(|range| Range::new(range.head, range.head));
455459
456− cx.doc().set_selection(selection);
460+ doc.set_selection(selection);
457461}
458462
459463pub fn flip_selections(cx: &mut Context) {
460− let selection = cx
461− .doc()
464+ let doc = cx.doc();
465+ let selection = doc
462466 .selection()
463467 .transform(|range| Range::new(range.head, range.anchor));
464468
465− cx.doc().set_selection(selection);
469+ doc.set_selection(selection);
466470}
467471
468−fn enter_insert_mode(cx: &mut Context) {
469− cx.doc().mode = Mode::Insert;
472+fn enter_insert_mode(doc: &mut Document) {
473+ doc.mode = Mode::Insert;
470474
471475 // TODO: store selection for undo
472476}
473477
474478// inserts at the start of each selection
475479pub fn insert_mode(cx: &mut Context) {
476− enter_insert_mode(cx);
480+ let doc = cx.doc();
481+ enter_insert_mode(doc);
477482
478− let selection = cx
479− .doc()
483+ let selection = doc
480484 .selection()
481485 .transform(|range| Range::new(range.to(), range.from()));
482− cx.doc().set_selection(selection);
486+ doc.set_selection(selection);
483487}
484488
485489// inserts at the end of each selection
486490pub fn append_mode(cx: &mut Context) {
487− enter_insert_mode(cx);
488491 let doc = cx.doc();
492+ enter_insert_mode(doc);
489493 doc.restore_cursor = true;
490494
491495 // TODO: as transaction
@@ -503,98 +507,56 @@ pub fn append_mode(cx: &mut Context) {
503507// TODO: I, A, o and O can share a lot of the primitives.
504508pub fn command_mode(cx: &mut Context) {
505509 let executor = cx.executor;
506− cx.callback = Some(Box::new(
507− move |compositor: &mut Compositor, editor: &mut Editor| {
508− let prompt = Prompt::new(
509− ":".to_owned(),
510− |_input: &str| {
511− // TODO: i need this duplicate list right now to avoid borrow checker issues
512− let command_list = vec![
513− "q".to_string(),
514− "o".to_string(),
515− "w".to_string(),
516− // String::from("q"),
517− // String::from("aaa"),
518− // String::from("bbb"),
519− // String::from("ccc"),
520− // String::from("ddd"),
521− // String::from("eee"),
522− // String::from("averylongcommandaverylongcommandaverylongcommandaverylongcommandaverylongcommand"),
523− // String::from("q"),
524− // String::from("aaa"),
525− // String::from("bbb"),
526− // String::from("ccc"),
527− // String::from("ddd"),
528− // String::from("eee"),
529− // String::from("q"),
530− // String::from("aaa"),
531− // String::from("bbb"),
532− // String::from("ccc"),
533− // String::from("ddd"),
534− // String::from("eee"),
535− // String::from("q"),
536− // String::from("aaa"),
537− // String::from("bbb"),
538− // String::from("ccc"),
539− // String::from("ddd"),
540− // String::from("eee"),
541− // String::from("q"),
542− // String::from("aaa"),
543− // String::from("bbb"),
544− // String::from("ccc"),
545− // String::from("ddd"),
546− // String::from("eee"),
547− ];
548− command_list
549− .into_iter()
550− .filter(|command| command.contains(_input))
551− .collect()
552− }, // completion
553− move |editor: &mut Editor, input: &str, event: PromptEvent| {
554− if event != PromptEvent::Validate {
555− return;
556− }
557−
558− let parts = input.split_ascii_whitespace().collect::<Vec<&str>>();
559−
560− match *parts.as_slice() {
561− ["q"] => {
562− editor.tree.remove(editor.view().id);
563− // editor.should_close = true,
564− }
565− ["o", path] => {
566− editor.open(path.into(), executor);
567− }
568− ["w"] => {
569− // TODO: non-blocking via save() command
570− smol::block_on(editor.view_mut().doc.save());
571− }
510+ let prompt = Prompt::new(
511+ ":".to_owned(),
512+ |_input: &str| {
513+ // TODO: i need this duplicate list right now to avoid borrow checker issues
514+ let command_list = vec![
515+ "q".to_string(),
516+ "o".to_string(),
517+ "w".to_string(),
518+ // String::from("q"),
519+ ];
520+ command_list
521+ .into_iter()
522+ .filter(|command| command.contains(_input))
523+ .collect()
524+ }, // completion
525+ move |editor: &mut Editor, input: &str, event: PromptEvent| {
526+ if event != PromptEvent::Validate {
527+ return;
528+ }
572529
573− _ => (),
574− }
575− },
576− );
577− compositor.push(Box::new(prompt));
530+ let parts = input.split_ascii_whitespace().collect::<Vec<&str>>();
531+
532+ match *parts.as_slice() {
533+ ["q"] => {
534+ editor.tree.remove(editor.view().id);
535+ // editor.should_close = true,
536+ }
537+ ["o", path] => {
538+ editor.open(path.into(), executor);
539+ }
540+ ["w"] => {
541+ // TODO: non-blocking via save() command
542+ smol::block_on(editor.view_mut().doc.save());
543+ }
544+
545+ _ => (),
546+ }
578547 },
579− ));
548+ );
549+ cx.push_layer(Box::new(prompt));
580550}
581551pub fn file_picker(cx: &mut Context) {
582552 let picker = ui::file_picker("./", cx.executor);
583− cx.callback = Some(Box::new(
584− |compositor: &mut Compositor, editor: &mut Editor| {
585− compositor.push(Box::new(picker));
586− },
587− ));
553+ cx.push_layer(Box::new(picker));
588554}
589555
590556pub fn buffer_picker(cx: &mut Context) {
591557 unimplemented!()
592− // cx.callback = Some(Box::new(
593− // |compositor: &mut Compositor, editor: &mut Editor| {
594− // let picker = ui::buffer_picker(&editor.views, editor.focus);
595− // compositor.push(Box::new(picker));
596− // },
597− // ));
558+ // let picker = ui::buffer_picker(&editor.views, editor.focus);
559+ // cx.push_layer(Box::new(picker));
598560}
599561
600562// calculate line numbers for each selection range
@@ -614,23 +576,24 @@ fn selection_lines(state: &State) -> Vec<usize> {
614576
615577// I inserts at the start of each line with a selection
616578pub fn prepend_to_line(cx: &mut Context) {
617− enter_insert_mode(cx);
579+ let doc = cx.doc();
580+ enter_insert_mode(doc);
618581
619582 move_line_start(cx);
620583}
621584
622585// A inserts at the end of each line with a selection
623586pub fn append_to_line(cx: &mut Context) {
624− enter_insert_mode(cx);
587+ let doc = cx.doc();
588+ enter_insert_mode(doc);
625589
626590 move_line_end(cx);
627591}
628592
629593// o inserts a new line after each line with a selection
630594pub fn open_below(cx: &mut Context) {
631− enter_insert_mode(cx);
632−
633595 let doc = cx.doc();
596+ enter_insert_mode(doc);
634597
635598 let lines = selection_lines(&doc.state);
636599
@@ -687,40 +650,40 @@ pub fn open_below(cx: &mut Context) {
687650
688651// O inserts a new line before each line with a selection
689652
690−fn append_changes_to_history(cx: &mut Context) {
691− if cx.doc().changes.is_empty() {
653+fn append_changes_to_history(doc: &mut Document) {
654+ if doc.changes.is_empty() {
692655 return;
693656 }
694657
695658 // TODO: change -> change -> undo -> change -> change fails, probably old_state needs reset
696659
697− let new_changeset = ChangeSet::new(cx.doc().text());
698− let changes = std::mem::replace(&mut cx.doc().changes, new_changeset);
660+ let new_changeset = ChangeSet::new(doc.text());
661+ let changes = std::mem::replace(&mut doc.changes, new_changeset);
699662 // Instead of doing this messy merge we could always commit, and based on transaction
700663 // annotations either add a new layer or compose into the previous one.
701− let transaction = Transaction::from(changes).with_selection(cx.doc().selection().clone());
664+ let transaction = Transaction::from(changes).with_selection(doc.selection().clone());
702665
703666 // increment document version
704667 // TODO: needs to happen on undo/redo too
705− cx.doc().version += 1;
668+ doc.version += 1;
706669
707670 // TODO: trigger lsp/documentDidChange with changes
708671
709672 // HAXX: we need to reconstruct the state as it was before the changes..
710− let old_state = cx.doc().old_state.take().expect("no old_state available");
673+ let old_state = doc.old_state.take().expect("no old_state available");
711674
712675 // TODO: take transaction by value?
713− cx.doc().history.commit_revision(&transaction, &old_state);
676+ doc.history.commit_revision(&transaction, &old_state);
714677
715678 // TODO: notify LSP of changes
716679}
717680
718681pub fn normal_mode(cx: &mut Context) {
719− cx.doc().mode = Mode::Normal;
682+ let doc = cx.doc();
720683
721− append_changes_to_history(cx);
684+ doc.mode = Mode::Normal;
722685
723− let doc = cx.doc();
686+ append_changes_to_history(doc);
724687
725688 // if leaving append mode, move cursor back by 1
726689 if doc.restore_cursor {
@@ -746,10 +709,11 @@ pub mod insert {
746709 use super::*;
747710 // TODO: insert means add text just before cursor, on exit we should be on the last letter.
748711 pub fn insert_char(cx: &mut Context, c: char) {
712+ let doc = cx.doc();
749713 let c = Tendril::from_char(c);
750− let transaction = Transaction::insert(&cx.doc().state, c);
714+ let transaction = Transaction::insert(&doc.state, c);
751715
752− cx.doc().apply(&transaction);
716+ doc.apply(&transaction);
753717 }
754718
755719 pub fn insert_tab(cx: &mut Context) {
@@ -884,7 +848,7 @@ pub fn paste(cx: &mut Context) {
884848 };
885849
886850 doc.apply(&transaction);
887− append_changes_to_history(cx);
851+ append_changes_to_history(doc);
888852 }
889853}
890854
@@ -919,8 +883,8 @@ pub fn indent(cx: &mut Context) {
919883 (pos, pos, Some(indent.clone()))
920884 }),
921885 );
922− cx.doc().apply(&transaction);
923− append_changes_to_history(cx);
886+ doc.apply(&transaction);
887+ append_changes_to_history(doc);
924888}
925889
926890pub fn unindent(cx: &mut Context) {
@@ -953,7 +917,7 @@ pub fn unindent(cx: &mut Context) {
953917 let transaction = Transaction::change(&doc.state, changes.into_iter());
954918
955919 doc.apply(&transaction);
956− append_changes_to_history(cx);
920+ append_changes_to_history(doc);
957921}
958922
959923//
@@ -979,12 +943,11 @@ pub fn completion(cx: &mut Context) {
979943 let pos = helix_lsp::util::pos_to_lsp_pos(doc.text().slice(..), doc.selection().cursor());
980944
981945 // TODO: handle fails
982− let res =
983− smol::block_on(language_server.completion(cx.doc().identifier(), pos)).unwrap_or_default();
946+ let res = smol::block_on(language_server.completion(doc.identifier(), pos)).unwrap_or_default();
984947
985948 // TODO: if no completion, show some message or something
986949 if !res.is_empty() {
987− let snapshot = cx.doc().state.clone();
950+ let snapshot = doc.state.clone();
988951 let mut menu = ui::Menu::new(
989952 res,
990953 |item| {
@@ -1047,7 +1010,7 @@ pub fn completion(cx: &mut Context) {
10471010 let transaction =
10481011 util::generate_transaction_from_edits(&doc.state, vec![edit]);
10491012 doc.apply(&transaction);
1050− // TODO: append_changes_to_history(cx); if not in insert mode?
1013+ // TODO: append_changes_to_history(doc); if not in insert mode?
10511014 }
10521015 _ => (),
10531016 };