Skip to main content

lattice_host/
command_line_expand_mode.rs

1//! MB.2 (rich minibuffer, tier 2): `command-line-expand-mode`.
2//!
3//! When the user presses `<C-x><C-e>` in the `:` line, the one-row
4//! `*command-line*` buffer grows into a full-modal mini-buffer band.
5//! This mode replaces `command-line-mode` as the buffer's major for
6//! the duration of the expand, giving the expanded band its own isolated
7//! option stack (`Number = false`, `SignColumn = No`, etc.) and keymap
8//! surface.
9//!
10//! Collapsing (`<C-x><C-e>` again) reactivates `command-line-mode`.
11//!
12//! The mode is intentionally a separate `ModeId` so that:
13//! - option overrides (gutter, line numbers, wrap) are scoped to the
14//!   expanded band and never leak to the document pane;
15//! - future expand-specific features (full-text syntax highlighting,
16//!   per-line decorations, band-shaped render path) have a dedicated
17//!   mode to attach to.
18
19use lattice_core::BufferKind;
20use lattice_mode::{
21    Keymap, KeymapEntry, LifecycleFuture, Mode, ModeContext, ModeId, ModeKind, keymap_entry,
22};
23
24/// `command-line-expand-mode`: the major mode of the `*command-line*`
25/// buffer while expanded into the full-modal band.
26pub struct CommandLineExpandMode;
27
28impl CommandLineExpandMode {
29    pub fn mode_id() -> ModeId {
30        ModeId::new("command-line-expand-mode")
31    }
32}
33
34impl Mode for CommandLineExpandMode {
35    type Guard = ();
36
37    fn id(&self) -> ModeId {
38        Self::mode_id()
39    }
40
41    fn kind(&self) -> ModeKind {
42        ModeKind::Major
43    }
44
45    fn target_buffer_kind(&self) -> Option<BufferKind> {
46        None
47    }
48
49    /// Same option overrides as `command-line-mode`: gutterless, no
50    /// line numbers, no wrap, no cursor line. These are scoped to
51    /// the `*command-line*` buffer only (per-buffer option cascade).
52    fn options(&self) -> lattice_config::OptionOverrideSet {
53        lattice_config::overrides! {
54            lattice_config::NoFile = true,
55            lattice_config::Wrap = false,
56            lattice_config::Number = false,
57            lattice_config::SignColumnOption = lattice_config::SignColumn::No,
58            lattice_config::CursorLine = false,
59        }
60    }
61
62    fn keymap(&self) -> Keymap {
63        Keymap::from_entries(expand_entries())
64    }
65
66    fn on_activate(&self, _ctx: ModeContext) -> LifecycleFuture<'_, Self::Guard> {
67        Box::pin(async { Ok(()) })
68    }
69}
70
71/// Static entry table for `command-line-expand-mode`. Mirrors
72/// `command-line-mode`'s keymap: the same Insert-layer chords
73/// (submit/cancel/history/completion/describe) plus the Normal-layer
74/// `<C-x><C-e>` for collapse.
75fn expand_entries() -> &'static [KeymapEntry] {
76    use std::sync::OnceLock;
77    static ENTRIES: OnceLock<Vec<KeymapEntry>> = OnceLock::new();
78    ENTRIES.get_or_init(|| {
79        vec![
80            keymap_entry! { mode: Command, chord: "<CR>", doc: "Submit the command line", cmd: "action:command-line-submit" },
81            keymap_entry! { mode: Command, chord: "<Esc>", doc: "Cancel the command line", cmd: "action:command-line-cancel" },
82            keymap_entry! { mode: Command, chord: "<C-c>", doc: "Cancel the command line", cmd: "action:command-line-cancel" },
83            keymap_entry! { mode: Command, chord: "<C-p>", doc: "Previous history entry", cmd: "action:command-line-history-prev" },
84            keymap_entry! { mode: Command, chord: "<C-n>", doc: "Next history entry", cmd: "action:command-line-history-next" },
85            keymap_entry! { mode: Command, chord: "<Up>", doc: "Previous history entry", cmd: "action:command-line-history-prev" },
86            keymap_entry! { mode: Command, chord: "<Down>", doc: "Next history entry", cmd: "action:command-line-history-next" },
87            keymap_entry! { mode: Command, chord: "<Tab>", doc: "Complete / next candidate", cmd: "action:command-line-complete" },
88            keymap_entry! { mode: Command, chord: "<S-Tab>", doc: "Previous candidate", cmd: "action:command-line-complete-prev" },
89            keymap_entry! { mode: Command, chord: "<C-h>", doc: "Describe command / arg under cursor", cmd: "action:command-line-describe-under-cursor" },
90            keymap_entry! { mode: Command, chord: "<C-x><C-e>", doc: "Collapse the mini-buffer band back to the one-row `:` line", cmd: "action:command-line-toggle-expand" },
91            keymap_entry! { mode: Normal, chord: "<C-x><C-e>", doc: "Collapse the mini-buffer band back to the one-row `:` line", cmd: "action:command-line-toggle-expand" },
92        ]
93    })
94}