1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
//! Owns the bottom-line prompt (`:cmd`, `/search`, fuzzy pickers,
//! rename input) and translates key events into outcomes the App
//! reacts to.
use std::path::{Path, PathBuf};
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use crate::buffer_ref::BufferRef;
use crate::finder::{ExplorerMode, ExplorerState, Finder, FuzzyKind, IgnoreOpts};
use crate::lsp::{CodeAction, Location};
mod completion;
mod line_input;
pub use completion::{CommandPrompt, CompletionKind};
pub use line_input::LineInput;
pub(crate) use line_input::apply_line_key;
/// Active prompt state. Mirrors the four ways the user can interact
/// with the bottom-line input: `:` command line, `/` (or `?`) search,
/// fuzzy pickers, and rename.
pub enum Prompt {
None,
Command(CommandPrompt),
Search {
forward: bool,
query: LineInput,
},
Fuzzy(Finder),
/// `<space>r` — text input for the new identifier. The cursor and
/// URI captured at open-time aren't stored here: the LSP rename
/// request is built against the live cursor at submit, which
/// matches what the user sees (the cursor is locked while the
/// prompt is up because Normal-mode input is suspended).
Rename(LineInput),
/// `<space>a` — popup menu of LSP code actions, anchored just under
/// the buffer cursor. Up/Down navigate, Enter submits, Esc cancels.
/// Filtering is intentionally omitted: action lists are short and
/// users want to read titles, not type query strings.
CodeActionMenu {
actions: Vec<CodeAction>,
selected: usize,
},
/// `K` — read-only popup showing `textDocument/hover` content
/// anchored at the cursor. j/k/Up/Down/PageUp/PageDown scroll the
/// content; any other key (including Enter and Esc) closes it.
Hover {
content: String,
scroll: usize,
},
/// `:lsp` — read-only, screen-centered modal listing every
/// configured LSP server and whether it's currently running.
/// Same key model as [`Self::Hover`]: scroll keys page the
/// content, any other key dismisses.
LspStatus {
content: String,
scroll: usize,
},
/// Copilot device-flow signin: screen-centered modal showing the
/// user code and verification URL. The clipboard already holds
/// `code` (the open path sets that up); the modal exists so the
/// info doesn't scroll away in the toast queue while the user is
/// in the browser. Any key dismisses; signin_confirm continues to
/// run in the background.
CopilotSignin {
code: String,
url: String,
},
/// `<space>e` — tree file explorer. Owns the full node list, the
/// expand state, and the live fuzzy query. Submit on a file
/// produces `OpenRelativeFile`; submit on a dir toggles its
/// expand state in-place and stays open.
Explorer(ExplorerState),
/// `:grammar` — interactive list of tree-sitter grammars with their
/// install status. Unlike the read-only modals above, this one
/// drives actions: `j`/`k` move the selection, Enter installs the
/// selected grammar (asynchronously — the row flips to
/// [`GrammarState::Installing`] and the worker reports back via
/// [`crate::event::AppEvent::GrammarInstalled`]), and `d` removes an
/// installed one. The modal stays open across installs so the user
/// can queue several.
GrammarList {
rows: Vec<GrammarRow>,
selected: usize,
},
/// Shown at file-open time when the active language has an install
/// recipe (built-in or `[grammars.*]`) but its parser/queries aren't
/// fully installed yet — offered in place of the old "highlight
/// failed" error toast. `y`/Enter installs (async); `n`/Esc/any
/// other key dismisses and the buffer stays plain text. We only ask
/// once per grammar per session (see [`App::asked_grammars`]).
GrammarInstallConfirm {
/// Recipe/grammar name to install on accept.
grammar: String,
/// Language name, for the prompt message.
language: String,
},
}
/// One grammar entry in the `:grammar` modal.
#[derive(Clone)]
pub struct GrammarRow {
/// Grammar name — the `.so` stem and the `grammar install <name>`
/// handle.
pub name: String,
pub state: GrammarState,
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum GrammarState {
/// Library present on disk.
Installed,
/// No library yet — Enter installs it.
Missing,
/// Install worker is in flight.
Installing,
}
impl Prompt {
pub fn is_open(&self) -> bool {
!matches!(self, Prompt::None)
}
}
/// What a key event produced. `Nothing` means "input absorbed, prompt
/// stays open"; everything else closes the prompt and asks the caller
/// to act.
pub enum PromptOutcome {
Nothing,
/// User pressed Esc / Ctrl-C — prompt closed, no action.
Cancelled,
/// `:cmd` submitted. Caller parses and dispatches.
RunCommand(String),
/// `/` or `?` submitted.
Search {
forward: bool,
query: String,
},
/// Fuzzy file picker submission. The path is relative to
/// `startup_cwd` — re-anchored by the caller.
OpenRelativeFile(String),
/// Fuzzy line picker submission. 0-based row in the active buffer.
GotoLine(usize),
/// Fuzzy references picker submission.
JumpToLocation(Location),
/// Fuzzy buffer picker submission. The caller maps the
/// [`BufferRef`] back to an actual buffer load (`Scratch` →
/// fresh empty buffer, `File(path)` → `open_path`).
OpenBuffer(BufferRef),
/// Rename submitted with the new identifier.
SubmitRename(String),
/// Code action picker selection. The caller either applies the
/// embedded `WorkspaceEdit` or sends a `codeAction/resolve` round
/// trip first when `edit` is `None`.
SelectCodeAction(CodeAction),
/// Explorer rename/move — a file or directory was relocated on
/// disk from `old` to `new` (both absolute). The caller rewrites
/// any open or sleeping buffer whose path falls under `old` so the
/// next save lands at the new location instead of recreating the
/// source. The explorer prompt stays open.
PathMoved {
old: PathBuf,
new: PathBuf,
},
/// `:grammar` modal — Enter on a missing grammar. The caller spawns
/// the install worker; the modal stays open with the row already
/// flipped to [`GrammarState::Installing`].
InstallGrammar(String),
/// `:grammar` modal — `d` on an installed grammar. The caller
/// deletes the library; the modal stays open with the row flipped
/// back to [`GrammarState::Missing`].
RemoveGrammar(String),
}
pub struct PromptController {
pub state: Prompt,
/// Side-channel for `Fuzzy(Locations)` pickers — `locations[idx]`
/// matches the picker's `items[idx]`. Cleared on submit or cancel.
locations: Vec<Location>,
/// Side-channel for `Fuzzy(Buffers)` pickers — `buffer_paths[idx]`
/// is the buffer to open when the user submits the matching item.
/// Cleared on submit or cancel.
buffer_paths: Vec<BufferRef>,
}
impl PromptController {
pub fn new() -> Self {
Self {
state: Prompt::None,
locations: Vec::new(),
buffer_paths: Vec::new(),
}
}
pub fn is_open(&self) -> bool {
self.state.is_open()
}
/// Side-channel `Location`s that mirror the active `Locations` picker.
/// Returns `&[]` for any other prompt state. The UI uses this to read
/// `locations[idx]` for preview rendering.
pub fn locations(&self) -> &[Location] {
&self.locations
}
pub fn open_command(&mut self) {
self.state = Prompt::Command(CommandPrompt::new());
}
pub fn open_search(&mut self, forward: bool) {
self.state = Prompt::Search {
forward,
query: LineInput::new(),
};
}
pub fn open_files(
&mut self,
startup_cwd: &Path,
ignore: IgnoreOpts,
hidden_patterns: &[String],
max_items: usize,
) {
self.state = Prompt::Fuzzy(Finder::files(
startup_cwd,
ignore,
hidden_patterns,
max_items,
));
}
/// `<space>e` — tree file explorer. All dirs start collapsed; the
/// user expands by pressing Enter / Right on a dir row. Typing
/// into the query box fuzzy-filters files and auto-expands their
/// ancestor dirs so matches stay reachable.
pub fn open_explorer(
&mut self,
startup_cwd: &Path,
ignore: IgnoreOpts,
hidden_patterns: Vec<String>,
max_items: usize,
compact: bool,
) {
self.state = Prompt::Explorer(ExplorerState::new(
startup_cwd,
ignore,
hidden_patterns,
max_items,
compact,
));
}
pub fn open_lines(&mut self, lines: &[String]) {
self.state = Prompt::Fuzzy(Finder::lines(lines));
}
pub fn open_locations(&mut self, items: Vec<String>, locations: Vec<Location>) {
self.locations = locations;
self.state = Prompt::Fuzzy(Finder::locations(items));
}
/// `<space>d` / `<space>D` — diagnostics picker. Same `Location`
/// side-channel as references; only the picker kind (and therefore
/// the title / formatting) differs.
pub fn open_diagnostics(
&mut self,
items: Vec<String>,
locations: Vec<Location>,
workspace: bool,
) {
self.locations = locations;
self.state = Prompt::Fuzzy(Finder::diagnostics(items, workspace));
}
/// `<space>/` — workspace-wide content picker. One candidate per
/// file; on each keystroke, the Finder scans every line of every
/// file and exposes the matched line numbers via
/// `MatchItem::line_hits`. Submit jumps to the file's best-scoring
/// match.
///
/// `locations[i]` is the *base* `Location` for `items[i]`: same
/// URI, line 0. Submit clones it and overrides the line with
/// `selection.line_hits[0]`.
pub fn open_workspace_search(
&mut self,
items: Vec<String>,
file_lines: Vec<Vec<String>>,
locations: Vec<Location>,
) {
self.locations = locations;
self.state = Prompt::Fuzzy(Finder::workspace_search(items, file_lines));
}
/// Open a fuzzy buffer picker. `items` are the display strings;
/// `refs` are the matching [`BufferRef`]s in parallel order —
/// the controller stores them and produces an `OpenBuffer(…)`
/// outcome on submit.
pub fn open_buffers(&mut self, items: Vec<String>, refs: Vec<BufferRef>) {
self.buffer_paths = refs;
self.state = Prompt::Fuzzy(Finder::buffers(items));
}
/// Read-only view of the buffer-picker side-channel, mirroring
/// [`Self::locations`]. The UI uses this for preview rendering.
pub fn buffer_paths(&self) -> &[BufferRef] {
&self.buffer_paths
}
pub fn open_rename(&mut self) {
self.state = Prompt::Rename(LineInput::new());
}
/// Open the cursor-anchored code-actions popup. `actions` is consumed
/// — we own them while the menu is up so submit can hand a fully-
/// owned `CodeAction` to the caller without an extra clone.
pub fn open_code_actions(&mut self, actions: Vec<CodeAction>) {
self.state = Prompt::CodeActionMenu {
actions,
selected: 0,
};
}
/// Open a hover popup with the given content. Cursor position is
/// captured by the renderer at draw time, so `App` doesn't need to
/// store it.
pub fn open_hover(&mut self, content: String) {
self.state = Prompt::Hover { content, scroll: 0 };
}
/// Open the `:lsp` status modal with pre-formatted content.
pub fn open_lsp_status(&mut self, content: String) {
self.state = Prompt::LspStatus { content, scroll: 0 };
}
/// Open the `:grammar` modal with the given rows. Selection starts
/// at the top.
pub fn open_grammar_list(&mut self, rows: Vec<GrammarRow>) {
self.state = Prompt::GrammarList { rows, selected: 0 };
}
/// Update a grammar row's state in place when the modal is open and
/// still showing this grammar. No-op otherwise (the user may have
/// closed the modal before the install worker reported back). Called
/// from the `GrammarInstalled` handler.
pub fn grammar_set_state(&mut self, name: &str, state: GrammarState) {
if let Prompt::GrammarList { rows, .. } = &mut self.state
&& let Some(row) = rows.iter_mut().find(|r| r.name == name)
{
row.state = state;
}
}
/// Open the open-time "install this grammar?" confirmation modal.
pub fn open_grammar_install_prompt(&mut self, grammar: String, language: String) {
self.state = Prompt::GrammarInstallConfirm { grammar, language };
}
/// Open the Copilot signin modal. Any prior modal is replaced.
pub fn open_copilot_signin(&mut self, code: String, url: String) {
self.state = Prompt::CopilotSignin { code, url };
}
pub fn handle_key(&mut self, key: KeyEvent, root: &Path) -> PromptOutcome {
let ctrl_c =
key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('c');
// Explorer steals Esc/Enter ahead of the generic close/submit
// path so its sub-modes can carry their own meaning: Esc may
// pop back to selection (from filter / pending input) rather
// than closing the prompt, and Enter routes to file ops when a
// pending mode is up.
if let Prompt::Explorer(state) = &mut self.state {
if key.code == KeyCode::Esc || ctrl_c {
if matches!(state.mode, ExplorerMode::Selection) {
self.close();
return PromptOutcome::Cancelled;
}
state.cancel_pending();
return PromptOutcome::Nothing;
}
if key.code == KeyCode::Enter {
return self.handle_explorer_enter();
}
// `y` confirms a pending delete; the explorer's apply_key
// treats anything else (besides Enter/Esc above) as a
// cancel.
if matches!(state.mode, ExplorerMode::PendingDelete)
&& matches!(key.code, KeyCode::Char('y') | KeyCode::Char('Y'))
{
return self.run_explorer_delete();
}
state.apply_key(key);
return PromptOutcome::Nothing;
}
// `:grammar` modal owns Enter (install) and `d` (remove) ahead
// of the generic submit/close path, since Enter must *not* close
// the modal — the user queues an install and keeps browsing.
// Esc/Ctrl-C still close (handled below, after navigation).
if let Prompt::GrammarList { rows, selected } = &mut self.state {
let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
if key.code == KeyCode::Esc || ctrl_c {
self.close();
return PromptOutcome::Cancelled;
}
let last = rows.len().saturating_sub(1);
match key.code {
KeyCode::Up | KeyCode::Char('k') => *selected = selected.saturating_sub(1),
KeyCode::Char('p') if ctrl => *selected = selected.saturating_sub(1),
KeyCode::Down | KeyCode::Char('j') => *selected = (*selected + 1).min(last),
KeyCode::Char('n') if ctrl => *selected = (*selected + 1).min(last),
KeyCode::Enter | KeyCode::Char('i') => {
// Only missing grammars are installable; an already-
// installed or in-flight row is a no-op.
if let Some(row) = rows.get_mut(*selected)
&& row.state == GrammarState::Missing
{
row.state = GrammarState::Installing;
return PromptOutcome::InstallGrammar(row.name.clone());
}
}
KeyCode::Char('d') | KeyCode::Char('x') => {
if let Some(row) = rows.get_mut(*selected)
&& row.state == GrammarState::Installed
{
row.state = GrammarState::Missing;
return PromptOutcome::RemoveGrammar(row.name.clone());
}
}
_ => {}
}
return PromptOutcome::Nothing;
}
// Open-time "install this grammar?" confirmation. `y`/Enter
// accepts; anything else (Esc/Ctrl-C/`n`/stray key) declines.
// Either way the modal closes — the App has already recorded the
// grammar in `asked_grammars`, so it won't re-prompt this session.
if let Prompt::GrammarInstallConfirm { grammar, .. } = &self.state {
let accept = matches!(
key.code,
KeyCode::Char('y') | KeyCode::Char('Y') | KeyCode::Enter
) && !ctrl_c;
let grammar = grammar.clone();
self.close();
return if accept {
PromptOutcome::InstallGrammar(grammar)
} else {
PromptOutcome::Cancelled
};
}
if key.code == KeyCode::Esc || ctrl_c {
self.close();
return PromptOutcome::Cancelled;
}
if key.code == KeyCode::Enter {
return self.submit();
}
match &mut self.state {
Prompt::None => PromptOutcome::Nothing,
Prompt::Command(cp) => {
match key.code {
KeyCode::Tab => cp.tab(1, root),
KeyCode::BackTab => cp.tab(-1, root),
_ => {
cp.completion = None;
apply_line_key(&mut cp.input, key);
}
}
PromptOutcome::Nothing
}
Prompt::Rename(input) => {
apply_line_key(input, key);
PromptOutcome::Nothing
}
Prompt::Search { query, .. } => {
apply_line_key(query, key);
PromptOutcome::Nothing
}
Prompt::Fuzzy(finder) => {
let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
match key.code {
KeyCode::Up => finder.prev(),
KeyCode::Down => finder.next(),
KeyCode::Char('n') if ctrl => finder.next(),
KeyCode::Char('p') if ctrl => finder.prev(),
_ => finder.apply_line_key(key),
}
PromptOutcome::Nothing
}
Prompt::CodeActionMenu { actions, selected } => {
let last = actions.len().saturating_sub(1);
match key.code {
KeyCode::Up => *selected = selected.saturating_sub(1),
KeyCode::Char('k') => *selected = selected.saturating_sub(1),
KeyCode::Char('p') if key.modifiers.contains(KeyModifiers::CONTROL) => {
*selected = selected.saturating_sub(1)
}
KeyCode::Down => *selected = (*selected + 1).min(last),
KeyCode::Char('j') => *selected = (*selected + 1).min(last),
KeyCode::Char('n') if key.modifiers.contains(KeyModifiers::CONTROL) => {
*selected = (*selected + 1).min(last)
}
_ => {}
}
PromptOutcome::Nothing
}
Prompt::Explorer(_)
| Prompt::GrammarList { .. }
| Prompt::GrammarInstallConfirm { .. } => {
// All three are fully handled by their early intercepts
// above (they own Esc/Enter and per-key dispatch).
// Reaching this arm means the early branch didn't match —
// which shouldn't happen — but stay defensive.
PromptOutcome::Nothing
}
Prompt::Hover { scroll, .. } | Prompt::LspStatus { scroll, .. } => {
// Read-only popup. Esc/Ctrl-C/Enter are intercepted by
// the top of `handle_key`, so here we only see scroll
// keys and "anything else" (which we treat as dismiss).
match key.code {
KeyCode::Up | KeyCode::Char('k') => {
*scroll = scroll.saturating_sub(1);
}
KeyCode::Down | KeyCode::Char('j') => {
*scroll = scroll.saturating_add(1);
}
KeyCode::PageUp => {
*scroll = scroll.saturating_sub(5);
}
KeyCode::PageDown => {
*scroll = scroll.saturating_add(5);
}
_ => {
self.close();
return PromptOutcome::Cancelled;
}
}
PromptOutcome::Nothing
}
Prompt::CopilotSignin { .. } => {
// No scrollable body — any non-Esc/Ctrl-C/Enter key
// dismisses too. Esc/Ctrl-C handled at the top.
self.close();
PromptOutcome::Cancelled
}
}
}
/// Enter handler for the Explorer prompt. The meaning depends on
/// the current mode:
/// * `Selection` / `Filter` — on a dir, toggle expand; on a file,
/// close the prompt and signal `OpenRelativeFile`.
/// * `PendingCreate` / `PendingRename` / `PendingMove` — run the
/// filesystem op against the input buffer.
/// * `PendingDelete` — Enter on the confirmation is treated as
/// "yes" (matching the shell convention for default-yes when
/// the user explicitly pressed `d` already; we still default
/// the chord to N for any other key).
fn handle_explorer_enter(&mut self) -> PromptOutcome {
let Prompt::Explorer(state) = &mut self.state else {
return PromptOutcome::Nothing;
};
match state.mode {
ExplorerMode::Selection | ExplorerMode::Filter => {
let Some(node) = state.selection() else {
return PromptOutcome::Nothing;
};
if node.is_dir {
state.toggle_selected();
state.refilter();
return PromptOutcome::Nothing;
}
let rel = node.rel_path.clone();
self.close();
PromptOutcome::OpenRelativeFile(rel)
}
ExplorerMode::PendingCreate => self.run_explorer_create(),
ExplorerMode::PendingRename => self.run_explorer_rename(),
ExplorerMode::PendingMove => self.run_explorer_move(),
ExplorerMode::PendingDelete => self.run_explorer_delete(),
}
}
fn run_explorer_create(&mut self) -> PromptOutcome {
let Prompt::Explorer(state) = &mut self.state else {
return PromptOutcome::Nothing;
};
let Some(input) = state.action.as_ref() else {
return PromptOutcome::Nothing;
};
let raw = input.text.clone();
match state.perform_create(&raw) {
Ok(_) => {
state.cancel_pending();
PromptOutcome::Nothing
}
Err(msg) => {
state.error = Some(msg);
PromptOutcome::Nothing
}
}
}
fn run_explorer_rename(&mut self) -> PromptOutcome {
let Prompt::Explorer(state) = &mut self.state else {
return PromptOutcome::Nothing;
};
let Some(node) = state.selection() else {
return PromptOutcome::Nothing;
};
let old_rel = node.rel_path.clone();
let Some(input) = state.action.as_ref() else {
return PromptOutcome::Nothing;
};
let new_name = input.text.clone();
match state.perform_rename(&old_rel, &new_name) {
Ok(new_rel) => {
let old_abs = state.root.join(&old_rel);
let new_abs = state.root.join(&new_rel);
state.cancel_pending();
PromptOutcome::PathMoved {
old: old_abs,
new: new_abs,
}
}
Err(msg) => {
state.error = Some(msg);
PromptOutcome::Nothing
}
}
}
fn run_explorer_move(&mut self) -> PromptOutcome {
let Prompt::Explorer(state) = &mut self.state else {
return PromptOutcome::Nothing;
};
let Some(node) = state.selection() else {
return PromptOutcome::Nothing;
};
let old_rel = node.rel_path.clone();
let Some(input) = state.action.as_ref() else {
return PromptOutcome::Nothing;
};
let new_rel = input.text.clone();
match state.perform_move(&old_rel, &new_rel) {
Ok(new_rel) => {
let old_abs = state.root.join(&old_rel);
let new_abs = state.root.join(&new_rel);
state.cancel_pending();
PromptOutcome::PathMoved {
old: old_abs,
new: new_abs,
}
}
Err(msg) => {
state.error = Some(msg);
PromptOutcome::Nothing
}
}
}
fn run_explorer_delete(&mut self) -> PromptOutcome {
let Prompt::Explorer(state) = &mut self.state else {
return PromptOutcome::Nothing;
};
let Some(node) = state.selection() else {
return PromptOutcome::Nothing;
};
let rel = node.rel_path.clone();
match state.perform_delete(&rel) {
Ok(()) => {
state.cancel_pending();
PromptOutcome::Nothing
}
Err(msg) => {
state.error = Some(msg);
PromptOutcome::Nothing
}
}
}
fn close(&mut self) {
self.state = Prompt::None;
self.locations.clear();
self.buffer_paths.clear();
}
fn submit(&mut self) -> PromptOutcome {
let prompt = std::mem::replace(&mut self.state, Prompt::None);
match prompt {
Prompt::None => PromptOutcome::Nothing,
Prompt::Command(cp) => PromptOutcome::RunCommand(cp.input.as_str().trim().to_string()),
Prompt::Search { forward, query } => PromptOutcome::Search {
forward,
query: query.into_string(),
},
Prompt::Rename(new_name) => PromptOutcome::SubmitRename(new_name.into_string()),
Prompt::Fuzzy(finder) => self.submit_fuzzy(finder),
// Explorer's and the grammar modal's submits are short-
// circuited inside `handle_key` (their early intercepts own
// Enter), so these branches are only reached through a
// future caller that bypasses the key path — no-op.
Prompt::Explorer(_)
| Prompt::GrammarList { .. }
| Prompt::GrammarInstallConfirm { .. } => PromptOutcome::Nothing,
Prompt::CodeActionMenu {
mut actions,
selected,
} => {
if selected < actions.len() {
PromptOutcome::SelectCodeAction(actions.swap_remove(selected))
} else {
PromptOutcome::Nothing
}
}
// Read-only popups — Enter just dismisses them.
Prompt::Hover { .. } | Prompt::LspStatus { .. } | Prompt::CopilotSignin { .. } => {
PromptOutcome::Cancelled
}
}
}
fn submit_fuzzy(&mut self, finder: Finder) -> PromptOutcome {
let Some(sel) = finder.selection() else {
self.locations.clear();
return PromptOutcome::Nothing;
};
match finder.kind {
FuzzyKind::Files { .. } => {
PromptOutcome::OpenRelativeFile(finder.items[sel.idx].clone())
}
FuzzyKind::Lines => PromptOutcome::GotoLine(sel.idx),
FuzzyKind::Locations | FuzzyKind::Diagnostics { .. } => {
let loc = self.locations.get(sel.idx).cloned();
self.locations.clear();
match loc {
Some(loc) => PromptOutcome::JumpToLocation(loc),
None => PromptOutcome::Nothing,
}
}
FuzzyKind::WorkspaceSearch => {
// Workspace search: `locations[idx]` is the file's base
// (line 0, char 0); the actual jump target lives on the
// match item — the matched row, and the column where
// the substring starts, so the cursor lands on the hit.
let target_line = sel.line_hits.first().copied().unwrap_or(0) as u32;
let target_col = sel.match_col;
let loc = self.locations.get(sel.idx).cloned().map(|mut l| {
l.range.start.line = target_line;
l.range.start.character = target_col;
l.range.end.line = target_line;
l.range.end.character = target_col;
l
});
self.locations.clear();
match loc {
Some(loc) => PromptOutcome::JumpToLocation(loc),
None => PromptOutcome::Nothing,
}
}
FuzzyKind::Buffers => {
let r = self.buffer_paths.get(sel.idx).cloned();
self.buffer_paths.clear();
match r {
Some(r) => PromptOutcome::OpenBuffer(r),
None => PromptOutcome::Nothing,
}
}
}
}
}
impl Default for PromptController {
fn default() -> Self {
Self::new()
}
}