@@ -36,6 +36,15 @@ impl<'a> Context<'a> {
36 pub fn doc(&mut self) -> &mut Document {
36 pub fn doc(&mut self) -> &mut Document {
37 &mut self.editor.view_mut().doc
37 &mut self.editor.view_mut().doc
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);
41/// A command is a function that takes the current state and a count, and does a side-effect on the
50/// 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);
45pub fn move_char_left(cx: &mut Context) {
54pub fn move_char_left(cx: &mut Context) {
46 let count = cx.count;
55 let count = cx.count;
48− cx.doc()
56+ let doc = cx.doc();
50− .move_selection(Direction::Backward, Granularity::Character, count);
59+ .move_selection(Direction::Backward, Granularity::Character, count);
51− cx.doc().set_selection(selection);
60+ doc.set_selection(selection);
54pub fn move_char_right(cx: &mut Context) {
63pub fn move_char_right(cx: &mut Context) {
55 let count = cx.count;
64 let count = cx.count;
57− cx.doc()
65+ let doc = cx.doc();
59− .move_selection(Direction::Forward, Granularity::Character, count);
68+ .move_selection(Direction::Forward, Granularity::Character, count);
60− cx.doc().set_selection(selection);
69+ doc.set_selection(selection);
63pub fn move_line_up(cx: &mut Context) {
72pub fn move_line_up(cx: &mut Context) {
64 let count = cx.count;
73 let count = cx.count;
66− .doc()
74+ let doc = cx.doc();
68 .move_selection(Direction::Backward, Granularity::Line, count);
77 .move_selection(Direction::Backward, Granularity::Line, count);
69− cx.doc().set_selection(selection);
78+ doc.set_selection(selection);
72pub fn move_line_down(cx: &mut Context) {
81pub fn move_line_down(cx: &mut Context) {
73 let count = cx.count;
82 let count = cx.count;
75− .doc()
83+ let doc = cx.doc();
77 .move_selection(Direction::Forward, Granularity::Line, count);
86 .move_selection(Direction::Forward, Granularity::Line, count);
78− cx.doc().set_selection(selection);
87+ doc.set_selection(selection);
81pub fn move_line_end(cx: &mut Context) {
90pub fn move_line_end(cx: &mut Context) {
82− let lines = selection_lines(&cx.doc().state);
92+ let lines = selection_lines(&doc.state);
84 let positions = lines
94 let positions = lines
85 .into_iter()
95 .into_iter()
@@ -88,29 +98,30 @@ pub fn move_line_end(cx: &mut Context) {
89 // Line end is pos at the start of next line - 1
99 // Line end is pos at the start of next line - 1
90 // subtract another 1 because the line ends with \n
100 // 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)
93 .map(|pos| Range::new(pos, pos));
103 .map(|pos| Range::new(pos, pos));
95 let selection = Selection::new(positions.collect(), 0);
105 let selection = Selection::new(positions.collect(), 0);
97− cx.doc().set_selection(selection);
107+ doc.set_selection(selection);
100pub fn move_line_start(cx: &mut Context) {
110pub fn move_line_start(cx: &mut Context) {
101− let lines = selection_lines(&cx.doc().state);
112+ let lines = selection_lines(&doc.state);
103 let positions = lines
114 let positions = lines
104 .into_iter()
115 .into_iter()
105 .map(|index| {
116 .map(|index| {
106 // adjust all positions to the start of the line.
117 // adjust all positions to the start of the line.
107− cx.doc().text().line_to_char(index)
118+ doc.text().line_to_char(index)
109 .map(|pos| Range::new(pos, pos));
120 .map(|pos| Range::new(pos, pos));
111 let selection = Selection::new(positions.collect(), 0);
122 let selection = Selection::new(positions.collect(), 0);
113− cx.doc().set_selection(selection);
124+ doc.set_selection(selection);
116pub fn move_next_word_start(cx: &mut Context) {
127pub fn move_next_word_start(cx: &mut Context) {
@@ -139,17 +150,19 @@ pub fn move_next_word_end(cx: &mut Context) {
141pub fn move_file_start(cx: &mut Context) {
152pub fn move_file_start(cx: &mut Context) {
142− cx.doc().set_selection(Selection::point(0));
154+ doc.set_selection(Selection::point(0));
144− cx.doc().mode = Mode::Normal;
156+ doc.mode = Mode::Normal;
147pub fn move_file_end(cx: &mut Context) {
159pub fn move_file_end(cx: &mut Context) {
148− let text = &cx.doc().text();
160+ let doc = cx.doc();
161+ let text = doc.text();
149 let last_line = text.line_to_char(text.len_lines().saturating_sub(2));
162 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));
152− cx.doc().mode = Mode::Normal;
165+ doc.mode = Mode::Normal;
155pub fn extend_next_word_start(cx: &mut Context) {
168pub fn extend_next_word_start(cx: &mut Context) {
@@ -161,7 +174,7 @@ pub fn extend_next_word_start(cx: &mut Context) {
162 }); // TODO: count
175 }); // TODO: count
164− cx.doc().set_selection(selection);
177+ doc.set_selection(selection);
167pub fn extend_prev_word_start(cx: &mut Context) {
180pub fn extend_prev_word_start(cx: &mut Context) {
@@ -172,7 +185,7 @@ pub fn extend_prev_word_start(cx: &mut Context) {
172 range.head = pos;
185 range.head = pos;
174 }); // TODO: count
187 }); // TODO: count
175− cx.doc().set_selection(selection);
188+ doc.set_selection(selection);
178pub fn extend_next_word_end(cx: &mut Context) {
191pub fn extend_next_word_end(cx: &mut Context) {
@@ -184,7 +197,7 @@ pub fn extend_next_word_end(cx: &mut Context) {
185 }); // TODO: count
198 }); // TODO: count
187− cx.doc().set_selection(selection);
200+ doc.set_selection(selection);
190pub fn check_cursor_in_view(view: &View) -> bool {
203pub fn check_cursor_in_view(view: &View) -> bool {
@@ -309,11 +322,8 @@ pub fn select_regex(cx: &mut Context) {
309 selection::select_on_matches(text, doc.selection(), ®ex).expect("no matches");
322 selection::select_on_matches(text, doc.selection(), ®ex).expect("no matches");
310 doc.set_selection(selection);
323 doc.set_selection(selection);
312− cx.callback = Some(Box::new(
325+
313− move |compositor: &mut Compositor, editor: &mut Editor| {
314− compositor.push(Box::new(prompt));
326+ cx.push_layer(Box::new(prompt));
319pub fn split_selection(cx: &mut Context) {
329pub fn split_selection(cx: &mut Context) {
@@ -336,11 +346,7 @@ pub fn split_selection(cx: &mut Context) {
336 doc.set_selection(selection);
346 doc.set_selection(selection);
339− cx.callback = Some(Box::new(
340− move |compositor: &mut Compositor, editor: &mut Editor| {
341− compositor.push(Box::new(prompt));
349+ cx.push_layer(Box::new(prompt));
346pub fn split_selection_on_newline(cx: &mut Context) {
352pub fn split_selection_on_newline(cx: &mut Context) {
@@ -393,11 +399,7 @@ pub fn search(cx: &mut Context) {
393 register::set('\\', vec![regex.as_str().to_string()]);
399 register::set('\\', vec![regex.as_str().to_string()]);
396− cx.callback = Some(Box::new(
397− move |compositor: &mut Compositor, editor: &mut Editor| {
398− compositor.push(Box::new(prompt));
402+ cx.push_layer(Box::new(prompt));
403pub fn search_next(cx: &mut Context) {
405pub fn search_next(cx: &mut Context) {
@@ -418,74 +420,76 @@ pub fn search_next(cx: &mut Context) {
419pub fn select_line(cx: &mut Context) {
421pub fn select_line(cx: &mut Context) {
420 // TODO: count
422 // TODO: count
421− let pos = cx.doc().selection().primary();
424+ let pos = doc.selection().primary();
422− let text = cx.doc().text();
425+ let text = doc.text();
423 let line = text.char_to_line(pos.head);
426 let line = text.char_to_line(pos.head);
424 let start = text.line_to_char(line);
427 let start = text.line_to_char(line);
425 let end = text.line_to_char(line + 1).saturating_sub(1);
428 let end = text.line_to_char(line + 1).saturating_sub(1);
427− cx.doc().set_selection(Selection::single(start, end));
430+ doc.set_selection(Selection::single(start, end));
430// heuristic: append changes to history after each command, unless we're in insert mode
433// heuristic: append changes to history after each command, unless we're in insert mode
432−fn _delete_selection(cx: &mut Context) {
435+fn _delete_selection(doc: &mut Document) {
433− let transaction = Transaction::change_by_selection(&cx.doc().state, |range| {
437+ Transaction::change_by_selection(&doc.state, |range| (range.from(), range.to() + 1, None));
434− (range.from(), range.to() + 1, None)
436− cx.doc().apply(&transaction);
438+ doc.apply(&transaction);
439pub fn delete_selection(cx: &mut Context) {
441pub fn delete_selection(cx: &mut Context) {
440− _delete_selection(cx);
443+ _delete_selection(doc);
442− append_changes_to_history(cx);
445+ append_changes_to_history(doc);
445pub fn change_selection(cx: &mut Context) {
448pub fn change_selection(cx: &mut Context) {
446− _delete_selection(cx);
450+ _delete_selection(doc);
447 insert_mode(cx);
451 insert_mode(cx);
450pub fn collapse_selection(cx: &mut Context) {
454pub fn collapse_selection(cx: &mut Context) {
452− .doc()
455+ let doc = cx.doc();
453 .selection()
457 .selection()
454 .transform(|range| Range::new(range.head, range.head));
458 .transform(|range| Range::new(range.head, range.head));
456− cx.doc().set_selection(selection);
460+ doc.set_selection(selection);
459pub fn flip_selections(cx: &mut Context) {
463pub fn flip_selections(cx: &mut Context) {
461− .doc()
464+ let doc = cx.doc();
462 .selection()
466 .selection()
463 .transform(|range| Range::new(range.head, range.anchor));
467 .transform(|range| Range::new(range.head, range.anchor));
465− cx.doc().set_selection(selection);
469+ doc.set_selection(selection);
468−fn enter_insert_mode(cx: &mut Context) {
472+fn enter_insert_mode(doc: &mut Document) {
469− cx.doc().mode = Mode::Insert;
473+ doc.mode = Mode::Insert;
471 // TODO: store selection for undo
475 // TODO: store selection for undo
474// inserts at the start of each selection
478// inserts at the start of each selection
475pub fn insert_mode(cx: &mut Context) {
479pub fn insert_mode(cx: &mut Context) {
476− enter_insert_mode(cx);
481+ enter_insert_mode(doc);
478− let selection = cx
483+ let selection = doc
480 .selection()
484 .selection()
481 .transform(|range| Range::new(range.to(), range.from()));
485 .transform(|range| Range::new(range.to(), range.from()));
482− cx.doc().set_selection(selection);
486+ doc.set_selection(selection);
485// inserts at the end of each selection
489// inserts at the end of each selection
486pub fn append_mode(cx: &mut Context) {
490pub fn append_mode(cx: &mut Context) {
487− enter_insert_mode(cx);
488 let doc = cx.doc();
491 let doc = cx.doc();
492+ enter_insert_mode(doc);
489 doc.restore_cursor = true;
493 doc.restore_cursor = true;
491 // TODO: as transaction
495 // TODO: as transaction
@@ -503,98 +507,56 @@ pub fn append_mode(cx: &mut Context) {
503// TODO: I, A, o and O can share a lot of the primitives.
507// TODO: I, A, o and O can share a lot of the primitives.
504pub fn command_mode(cx: &mut Context) {
508pub fn command_mode(cx: &mut Context) {
505 let executor = cx.executor;
509 let executor = cx.executor;
506− cx.callback = Some(Box::new(
507− move |compositor: &mut Compositor, editor: &mut Editor| {
508− let prompt = Prompt::new(
510+ let prompt = Prompt::new(
509− ":".to_owned(),
511+ ":".to_owned(),
510− |_input: &str| {
512+ |_input: &str| {
511− // TODO: i need this duplicate list right now to avoid borrow checker issues
513+ // TODO: i need this duplicate list right now to avoid borrow checker issues
512− let command_list = vec![
514+ let command_list = vec![
513− "q".to_string(),
515+ "q".to_string(),
514− "o".to_string(),
516+ "o".to_string(),
515− "w".to_string(),
517+ "w".to_string(),
516− // String::from("q"),
518+ // 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"),
548− command_list
520+ command_list
549− .into_iter()
521+ .into_iter()
550− .filter(|command| command.contains(_input))
522+ .filter(|command| command.contains(_input))
551− .collect()
523+ .collect()
552− }, // completion
524+ }, // completion
553− move |editor: &mut Editor, input: &str, event: PromptEvent| {
525+ move |editor: &mut Editor, input: &str, event: PromptEvent| {
554− if event != PromptEvent::Validate {
526+ if event != PromptEvent::Validate {
558− let parts = input.split_ascii_whitespace().collect::<Vec<&str>>();
560− match *parts.as_slice() {
562− editor.tree.remove(editor.view().id);
563− // editor.should_close = true,
566− editor.open(path.into(), executor);
569− // TODO: non-blocking via save() command
570− smol::block_on(editor.view_mut().doc.save());
573− _ => (),
530+ let parts = input.split_ascii_whitespace().collect::<Vec<&str>>();
532+ match *parts.as_slice() {
534+ editor.tree.remove(editor.view().id);
535+ // editor.should_close = true,
576− );
537+ ["o", path] => {
577− compositor.push(Box::new(prompt));
538+ editor.open(path.into(), executor);
541+ // TODO: non-blocking via save() command
542+ smol::block_on(editor.view_mut().doc.save());
549+ cx.push_layer(Box::new(prompt));
581pub fn file_picker(cx: &mut Context) {
551pub fn file_picker(cx: &mut Context) {
582 let picker = ui::file_picker("./", cx.executor);
552 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));
553+ cx.push_layer(Box::new(picker));
590pub fn buffer_picker(cx: &mut Context) {
556pub fn buffer_picker(cx: &mut Context) {
591 unimplemented!()
557 unimplemented!()
592− // cx.callback = Some(Box::new(
593− // |compositor: &mut Compositor, editor: &mut Editor| {
594− // let picker = ui::buffer_picker(&editor.views, editor.focus);
558+ // let picker = ui::buffer_picker(&editor.views, editor.focus);
595− // compositor.push(Box::new(picker));
559+ // cx.push_layer(Box::new(picker));
600// calculate line numbers for each selection range
562// calculate line numbers for each selection range
@@ -614,23 +576,24 @@ fn selection_lines(state: &State) -> Vec<usize> {
615// I inserts at the start of each line with a selection
577// I inserts at the start of each line with a selection
616pub fn prepend_to_line(cx: &mut Context) {
578pub fn prepend_to_line(cx: &mut Context) {
617− enter_insert_mode(cx);
580+ enter_insert_mode(doc);
619 move_line_start(cx);
582 move_line_start(cx);
622// A inserts at the end of each line with a selection
585// A inserts at the end of each line with a selection
623pub fn append_to_line(cx: &mut Context) {
586pub fn append_to_line(cx: &mut Context) {
624− enter_insert_mode(cx);
588+ enter_insert_mode(doc);
626 move_line_end(cx);
590 move_line_end(cx);
629// o inserts a new line after each line with a selection
593// o inserts a new line after each line with a selection
630pub fn open_below(cx: &mut Context) {
594pub fn open_below(cx: &mut Context) {
631− enter_insert_mode(cx);
633 let doc = cx.doc();
595 let doc = cx.doc();
596+ enter_insert_mode(doc);
635 let lines = selection_lines(&doc.state);
598 let lines = selection_lines(&doc.state);
@@ -687,40 +650,40 @@ pub fn open_below(cx: &mut Context) {
688// O inserts a new line before each line with a selection
651// O inserts a new line before each line with a selection
690−fn append_changes_to_history(cx: &mut Context) {
653+fn append_changes_to_history(doc: &mut Document) {
691− if cx.doc().changes.is_empty() {
654+ if doc.changes.is_empty() {
695 // TODO: change -> change -> undo -> change -> change fails, probably old_state needs reset
658 // TODO: change -> change -> undo -> change -> change fails, probably old_state needs reset
697− let new_changeset = ChangeSet::new(cx.doc().text());
660+ let new_changeset = ChangeSet::new(doc.text());
698− let changes = std::mem::replace(&mut cx.doc().changes, new_changeset);
661+ let changes = std::mem::replace(&mut doc.changes, new_changeset);
699 // Instead of doing this messy merge we could always commit, and based on transaction
662 // Instead of doing this messy merge we could always commit, and based on transaction
700 // annotations either add a new layer or compose into the previous one.
663 // 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());
703 // increment document version
666 // increment document version
704 // TODO: needs to happen on undo/redo too
667 // TODO: needs to happen on undo/redo too
705− cx.doc().version += 1;
668+ doc.version += 1;
707 // TODO: trigger lsp/documentDidChange with changes
670 // TODO: trigger lsp/documentDidChange with changes
709 // HAXX: we need to reconstruct the state as it was before the changes..
672 // 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");
712 // TODO: take transaction by value?
675 // TODO: take transaction by value?
713− cx.doc().history.commit_revision(&transaction, &old_state);
676+ doc.history.commit_revision(&transaction, &old_state);
715 // TODO: notify LSP of changes
678 // TODO: notify LSP of changes
718pub fn normal_mode(cx: &mut Context) {
681pub fn normal_mode(cx: &mut Context) {
719− cx.doc().mode = Mode::Normal;
682+ let doc = cx.doc();
721− append_changes_to_history(cx);
684+ doc.mode = Mode::Normal;
723− let doc = cx.doc();
686+ append_changes_to_history(doc);
725 // if leaving append mode, move cursor back by 1
688 // if leaving append mode, move cursor back by 1
726 if doc.restore_cursor {
689 if doc.restore_cursor {
@@ -746,10 +709,11 @@ pub mod insert {
746 use super::*;
709 use super::*;
747 // TODO: insert means add text just before cursor, on exit we should be on the last letter.
710 // TODO: insert means add text just before cursor, on exit we should be on the last letter.
748 pub fn insert_char(cx: &mut Context, c: char) {
711 pub fn insert_char(cx: &mut Context, c: char) {
749 let c = Tendril::from_char(c);
713 let c = Tendril::from_char(c);
750− let transaction = Transaction::insert(&cx.doc().state, c);
714+ let transaction = Transaction::insert(&doc.state, c);
752− cx.doc().apply(&transaction);
716+ doc.apply(&transaction);
755 pub fn insert_tab(cx: &mut Context) {
719 pub fn insert_tab(cx: &mut Context) {
@@ -884,7 +848,7 @@ pub fn paste(cx: &mut Context) {
886 doc.apply(&transaction);
850 doc.apply(&transaction);
887− append_changes_to_history(cx);
851+ append_changes_to_history(doc);
@@ -919,8 +883,8 @@ pub fn indent(cx: &mut Context) {
919 (pos, pos, Some(indent.clone()))
883 (pos, pos, Some(indent.clone()))
922− cx.doc().apply(&transaction);
886+ doc.apply(&transaction);
923− append_changes_to_history(cx);
887+ append_changes_to_history(doc);
926pub fn unindent(cx: &mut Context) {
890pub fn unindent(cx: &mut Context) {
@@ -953,7 +917,7 @@ pub fn unindent(cx: &mut Context) {
953 let transaction = Transaction::change(&doc.state, changes.into_iter());
917 let transaction = Transaction::change(&doc.state, changes.into_iter());
955 doc.apply(&transaction);
919 doc.apply(&transaction);
956− append_changes_to_history(cx);
920+ append_changes_to_history(doc);
@@ -979,12 +943,11 @@ pub fn completion(cx: &mut Context) {
979 let pos = helix_lsp::util::pos_to_lsp_pos(doc.text().slice(..), doc.selection().cursor());
943 let pos = helix_lsp::util::pos_to_lsp_pos(doc.text().slice(..), doc.selection().cursor());
981 // TODO: handle fails
945 // TODO: handle fails
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();
985 // TODO: if no completion, show some message or something
948 // TODO: if no completion, show some message or something
986 if !res.is_empty() {
949 if !res.is_empty() {
987− let snapshot = cx.doc().state.clone();
950+ let snapshot = doc.state.clone();
988 let mut menu = ui::Menu::new(
951 let mut menu = ui::Menu::new(
@@ -1047,7 +1010,7 @@ pub fn completion(cx: &mut Context) {
1047 let transaction =
1010 let transaction =
1048 util::generate_transaction_from_edits(&doc.state, vec![edit]);
1011 util::generate_transaction_from_edits(&doc.state, vec![edit]);
1049 doc.apply(&transaction);
1012 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?
1052 _ => (),
1015 _ => (),