Skip to main content

lattice_host/
prompt_line_mode.rs

1//! `prompt-line-mode`: the generic one-line minibuffer text prompt
2//! backing `Effect::OpenPrompt`.
3//!
4//! Structurally mirrors [`crate::command_line_mode::CommandLineMode`]
5//! / [`crate::search_line_mode::SearchLineMode`] (buffer-backed,
6//! Insert-editable, focus-swapped via `Editor::focus_editing_buffer`)
7//! but is generic rather than tied to one purpose: the caller
8//! supplies the prompt label, initial text, and which `action:*`
9//! handler fires on submit (`Effect::OpenPrompt`'s fields), so this
10//! mode's own keymap only needs submit/cancel — no history, no
11//! completion, no purpose-specific logic. See
12//! `Editor::open_prompt_line` / `do_prompt_line_submit`.
13
14use lattice_core::BufferKind;
15use lattice_mode::{Keymap, KeymapEntry, LifecycleFuture, Mode, ModeContext, ModeId, ModeKind};
16
17/// Default synthetic name used when `Effect::OpenPrompt`'s
18/// `buffer_name` is `None`.
19pub const PROMPT_LINE_BUFFER_NAME_DEFAULT: &str = "*prompt*";
20
21pub const PROMPT_LINE_SUBMIT: &str = "action:prompt-line-submit";
22pub const PROMPT_LINE_CANCEL: &str = "action:prompt-line-cancel";
23
24pub struct PromptLineMode;
25
26impl PromptLineMode {
27    pub fn mode_id() -> ModeId {
28        ModeId::new("prompt-line-mode")
29    }
30}
31
32impl Mode for PromptLineMode {
33    type Guard = ();
34
35    fn id(&self) -> ModeId {
36        Self::mode_id()
37    }
38
39    fn kind(&self) -> ModeKind {
40        ModeKind::Major
41    }
42
43    fn target_buffer_kind(&self) -> Option<BufferKind> {
44        None
45    }
46
47    fn options(&self) -> lattice_config::OptionOverrideSet {
48        lattice_config::overrides! {
49            lattice_config::NoFile = true,
50            lattice_config::Wrap = false,
51            lattice_config::Number = false,
52            lattice_config::SignColumnOption = lattice_config::SignColumn::No,
53            lattice_config::CursorLine = false,
54        }
55    }
56
57    fn keymap(&self) -> Keymap {
58        Keymap::from_entries(prompt_line_entries())
59    }
60
61    fn on_activate(&self, _ctx: ModeContext) -> LifecycleFuture<'_, Self::Guard> {
62        Box::pin(async { Ok(()) })
63    }
64}
65
66fn prompt_line_entries() -> &'static [KeymapEntry] {
67    use std::sync::OnceLock;
68    static ENTRIES: OnceLock<Vec<KeymapEntry>> = OnceLock::new();
69    ENTRIES.get_or_init(|| {
70        vec![
71            lattice_mode::keymap_entry! { mode: Prompt, chord: "<CR>", doc: "Submit the prompt", cmd: "action:prompt-line-submit" },
72            lattice_mode::keymap_entry! { mode: Prompt, chord: "<Esc>", doc: "Cancel the prompt", cmd: "action:prompt-line-cancel" },
73            lattice_mode::keymap_entry! { mode: Prompt, chord: "<C-c>", doc: "Cancel the prompt", cmd: "action:prompt-line-cancel" },
74        ]
75    })
76}