vorto 0.14.0

A Vim-flavored modal terminal editor with batteries included: tree-sitter, LSP, fuzzy pickers, vim-surround, multi-cursor, and optional Copilot.
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
//! Apply a `Cmd` stream to `App`.
//!
//! The third stage of the input pipeline: where `handle_expr` produced a
//! list of non-buffer state changes, `run_cmds` actually performs them.
//! Most variants are thin shims over existing `App` helpers
//! (`enter_mode`, `open_prompt`, `buffer_cycle`, the `lsp_*` methods,
//! …); this module is the dispatcher, not the implementer.

use std::path::Path;
use std::time::Duration;

use anyhow::Result;

use super::eval::word_under_cursor;
use super::{App, Toast, root_cause};
use crate::effect::{Cmd, ScrollAnchor};
use crate::lsp;

/// Upper bound on how long save waits for `textDocument/formatting`
/// before giving up and writing the un-formatted buffer. Generous
/// enough for rust-analyzer's first-format-after-startup; short enough
/// that a wedged server doesn't strand the user.
const LSP_FORMAT_TIMEOUT: Duration = Duration::from_secs(3);

impl App {
    pub(super) fn run_cmds(&mut self, cmds: Vec<Cmd>) -> Result<()> {
        for cmd in cmds {
            self.run_cmd(cmd)?;
        }
        Ok(())
    }

    fn run_cmd(&mut self, cmd: Cmd) -> Result<()> {
        match cmd {
            Cmd::EnterMode(m) => self.enter_mode(m),
            Cmd::ToastInfo(s) => self.push_toast(Toast::info(s)),
            Cmd::ToastError(s) => self.push_toast(Toast::error(s)),
            Cmd::OpenPrompt(k) => self.open_prompt(k),
            Cmd::OpenRenamePrompt => self.open_rename_prompt(),
            Cmd::SetSearch { pattern, forward } => self.search.set(pattern, forward),
            Cmd::JumpSearch { reverse } => {
                let forward = self.search.last_forward ^ reverse;
                self.run_jump_search(forward);
            }
            Cmd::SearchSelectMatch { reverse } => {
                let forward = self.search.last_forward ^ reverse;
                self.run_search_select(forward);
            }
            Cmd::SetLastFind(lf) => self.last_find = Some(lf),
            Cmd::Scroll(anchor) => self.run_scroll(anchor),
            Cmd::Save {
                path,
                then_quit,
                force,
            } => self.run_save(path.as_deref(), then_quit, force),
            Cmd::OpenPath(path) => self.open_path(&path)?,
            Cmd::Reload => self.run_reload(),
            Cmd::ReloadAll => self.run_reload_all(),
            Cmd::LspJump { method, label } => self.lsp_jump(method, label),
            Cmd::LspFindReferences => self.lsp_find_references(),
            Cmd::LspCodeAction => self.lsp_code_action(),
            Cmd::LspHover => self.lsp_hover(),
            Cmd::OpenLspStatus { all } => self.open_lsp_status(all),
            Cmd::GotoDiagnostic { forward, count } => self.run_goto_diagnostic(forward, count),
            Cmd::GotoConflict { forward, count } => self.run_goto_conflict(forward, count),
            Cmd::BufferCycle { forward } => self.buffer_cycle(forward)?,
            Cmd::BufferDelete { force } => self.buffer_delete(force)?,
            Cmd::BufferDeleteAll => self.buffer_delete_all()?,
            Cmd::NewScratchBuffer => {
                // Always mint a fresh id: `:new` is documented as "give
                // me a new buffer", which would feel broken if it kept
                // reusing the single Scratch(0) slot.
                let id = self.mint_scratch_id();
                self.switch_to_buffer(crate::buffer_ref::BufferRef::Scratch(id))?;
            }
            Cmd::Quit => self.should_quit = true,
            Cmd::StartJumpLabel => self.start_jump_label(),
            Cmd::JumpBack { count } => self.jump_back(count),
            Cmd::JumpForward { count } => self.jump_forward(count),
            Cmd::SelectWholeBuffer => self.run_select_whole_buffer(),
            Cmd::SyncYank => self.sync_yank_to_clipboard(),
            Cmd::SplitWindow { dir } => self.split_window(dir),
            Cmd::CloseWindow => self.close_window(),
            Cmd::FocusWindow { dir } => self.focus_window(dir),
            Cmd::CycleWindow => self.cycle_window(),
        }
        Ok(())
    }

    /// Push the current `Buffer.yank` onto the OS clipboard. Initializes
    /// the `arboard` handle on first use; both init failure and a failed
    /// `set_text` are swallowed silently so that headless / sandboxed
    /// environments don't surface a noisy error on every yank — the
    /// internal register keeps working and `p` paste-in-vorto is
    /// unaffected.
    pub(super) fn sync_yank_to_clipboard(&mut self) {
        if self.clipboard.is_none() {
            self.clipboard = arboard::Clipboard::new().ok();
        }
        let yank = self.active_doc().yank.clone();
        if let Some(cb) = self.clipboard.as_mut() {
            let _ = cb.set_text(yank);
        }
    }

    /// `gA` — select every line in the buffer. Sets the visual anchor
    /// at (0, 0) directly rather than going through `enter_mode`, since
    /// the latter only pins the anchor on a Normal→Visual transition
    /// and we want a fresh selection even if we're already in some
    /// visual mode.
    fn run_select_whole_buffer(&mut self) {
        let last = self.active_doc().lines.len().saturating_sub(1);
        self.visual_anchor = Some(crate::editor::Cursor { row: 0, col: 0 });
        self.editor.mode = crate::mode::Mode::VisualLine;
        self.editor.cursor = crate::editor::Cursor { row: last, col: 0 };
    }

    /// `]d` / `[d` — move the cursor to the next/previous LSP
    /// diagnostic in the current buffer. Diagnostics are sorted by
    /// `(line, column)` upstream (in `lsp_coordinator`), so the walk
    /// is just a linear scan. Wraps at the buffer boundary so repeated
    /// presses cycle. Centers the viewport on the landing position
    /// and surfaces the diagnostic message as an info toast — the
    /// inline marker would otherwise be the only feedback that the
    /// jump fired, and that's easy to miss after a long jump.
    fn run_goto_diagnostic(&mut self, forward: bool, count: u32) {
        let Some(diags) = self.current_diagnostics() else {
            self.push_toast(Toast::info("no diagnostics"));
            return;
        };
        if diags.is_empty() {
            self.push_toast(Toast::info("no diagnostics"));
            return;
        }
        let n = diags.len();
        let cur = self.editor.cursor;
        // Index of the next match in `forward` direction strictly past
        // the cursor; falls back to wrap-around when the cursor sits
        // at/after the last (forward) or at/before the first (back).
        let start = if forward {
            diags
                .iter()
                .position(|d| {
                    let p = d.range.start;
                    (p.line as usize, p.character as usize) > (cur.row, cur.col)
                })
                .unwrap_or(0)
        } else {
            diags
                .iter()
                .rposition(|d| {
                    let p = d.range.start;
                    (p.line as usize, p.character as usize) < (cur.row, cur.col)
                })
                .unwrap_or(n - 1)
        };
        // Count: walk `count - 1` more steps in the same direction,
        // wrapping. count == 0 shouldn't reach here (the parser maps
        // bare `]d` to count == 1) but guard with max(1) just in case.
        let steps = count.max(1) as usize - 1;
        let idx = if forward {
            (start + steps) % n
        } else {
            // (start - steps) mod n, computed without going negative.
            (start + n - (steps % n)) % n
        };
        let target = &diags[idx];
        // Clamp against the live buffer — servers occasionally publish
        // diagnostics that point past EOF (in-flight edits race against
        // the next `publishDiagnostics`).
        let last_row = self.active_doc().lines.len().saturating_sub(1);
        let row = (target.range.start.line as usize).min(last_row);
        let col = target.range.start.character as usize;
        self.record_jump();
        self.editor.cursor = crate::editor::Cursor { row, col };
        ed_op_ref!(self, clamp_col(false));
        self.run_scroll(ScrollAnchor::Center);
        self.push_toast(Toast::info(target.message.clone()));
    }

    /// Body of `]c` / `[c`. Lands the cursor on the `<<<<<<<` line of the
    /// next / previous conflict, wrapping at
    /// the buffer boundary so repeated presses cycle. Centers the
    /// viewport and toasts the position (`conflict 2/3`) since a marker
    /// far off-screen would otherwise give no sign the jump fired.
    pub(super) fn run_goto_conflict(&mut self, forward: bool, count: u32) {
        let hunks = self.active_doc().conflict_hunks();
        if hunks.is_empty() {
            self.push_toast(Toast::info("no conflict markers in this buffer"));
            return;
        }
        let n = hunks.len();
        let row = self.editor.cursor.row;
        // First hunk strictly past the cursor (forward) or before it
        // (back); wrap to the far end when the cursor is already beyond
        // the last / first one.
        let start = if forward {
            hunks.iter().position(|h| h.start > row).unwrap_or(0)
        } else {
            hunks.iter().rposition(|h| h.start < row).unwrap_or(n - 1)
        };
        let steps = count.max(1) as usize - 1;
        let idx = if forward {
            (start + steps) % n
        } else {
            (start + n - (steps % n)) % n
        };
        let target = hunks[idx].start;
        self.record_jump();
        self.editor.cursor = crate::editor::Cursor {
            row: target,
            col: 0,
        };
        ed_op_ref!(self, clamp_col(false));
        self.run_scroll(ScrollAnchor::Center);
        self.push_toast(Toast::info(format!(
            "conflict {}/{} at line {}",
            idx + 1,
            n,
            target + 1
        )));
    }

    fn run_jump_search(&mut self, forward: bool) {
        if let Some(c) = self
            .search
            .find_next(&self.editor, self.active_doc(), forward)
        {
            self.record_jump();
            self.editor.cursor = c;
        } else {
            self.push_toast(Toast::error("pattern not found"));
        }
    }

    /// Body of `gn` / `gN`. Looks up the next match in the requested
    /// direction; in Normal mode, drop the cursor on the match start
    /// and enter Visual (which pins the anchor there); in Visual,
    /// keep the existing anchor and only extend the active end. Either
    /// way, the cursor lands on the match's last char so the selection
    /// covers the whole match. Shared with Visual-mode key handling.
    pub(super) fn run_search_select(&mut self, forward: bool) {
        let Some((start, end_incl)) =
            self.search
                .find_match_range(&self.editor, self.active_doc(), forward)
        else {
            self.push_toast(Toast::error("pattern not found"));
            return;
        };
        if !self.editor.mode.is_visual() {
            self.record_jump();
            self.editor.cursor = start;
            self.enter_mode(crate::mode::Mode::Visual);
        }
        self.editor.cursor = end_incl;
    }

    /// Visual mode's `*` / `#` — extract the word under the cursor,
    /// seed the search state, then jump. The Normal-mode counterpart
    /// goes through `Cmd::SetSearch` + `Cmd::JumpSearch` from
    /// `handle_motion`; visual mode bypasses the Cmd pipeline so this
    /// shim collapses both into one call.
    pub(super) fn search_word_under_cursor(&mut self, forward: bool) {
        let Some(word) = word_under_cursor(&self.editor, self.active_doc()) else {
            self.push_toast(Toast::error("no word under cursor"));
            return;
        };
        self.search.set(word, forward);
        self.run_jump_search(forward);
    }

    pub(super) fn run_scroll(&mut self, anchor: ScrollAnchor) {
        let height = self.active_doc().viewport_height.get();
        if height == 0 {
            // Viewport size isn't known yet (most often: we just thawed
            // a sleeping buffer, which resets `viewport_height` to 0 by
            // design — see `SleepingBuffer::thaw`). Defer to the next
            // draw via `pending_center`; the sticky scroll path in
            // `compute_scroll` reads and clears it.
            if matches!(anchor, ScrollAnchor::Center) {
                self.active_doc().pending_center.set(true);
            }
            return;
        }
        let cur = self.editor.cursor.row;
        let last = self.active_doc().lines.len().saturating_sub(1);
        let scroll = match anchor {
            ScrollAnchor::Top => cur,
            ScrollAnchor::Center => cur.saturating_sub(height / 2),
            ScrollAnchor::Bottom => cur + 1 - height.min(cur + 1),
        };
        let max_scroll = last.saturating_sub(height.saturating_sub(1));
        self.active_doc().scroll.set(scroll.min(max_scroll));
    }

    /// Persist the active buffer to disk and, when `then_quit`, set
    /// `should_quit` only if the write succeeded. A failed save
    /// (no file name, missing parent dir, permission denied, …)
    /// surfaces as a toast and the editor stays open — propagating
    /// the error would tear down the run loop, which is the wrong
    /// response to a fat-fingered path. `force` enables `:w!`
    /// semantics: missing parent directories are created instead of
    /// reported as an error.
    fn run_save(&mut self, path: Option<&Path>, then_quit: bool, force: bool) {
        // Format-on-save runs only for in-place saves (not `:w <path>`):
        // for a save-as, the buffer's current language is ambiguous
        // with respect to the new path, and we'd rather avoid surprising
        // the user by rewriting their text right before changing where
        // it lives. In-place saves go through the formatter step,
        // which is no-op when no formatter is configured and no LSP
        // is attached.
        if path.is_none() && self.active_doc().path.is_some() {
            self.run_format_on_save();
        }

        let target = path
            .map(|p| p.to_path_buf())
            .or_else(|| self.active_doc().path.clone());
        let Some(target) = target else {
            self.push_toast(Toast::error("no file name (use :w <path>)"));
            return;
        };

        // External-edit guard: only for in-place saves where we have a
        // baseline `disk_meta` from load/reload. `:w <path>` deliberately
        // skips this — saving *to* a different file shouldn't be gated
        // on the original file's drift state. `:w!` bypasses too, since
        // forcing a write past a missing parent dir already implies
        // "I know what I'm doing." If the file has since vanished we
        // also refuse (without force) — silently re-creating it would
        // mask an external `rm`.
        if path.is_none()
            && !force
            && let Some(expected) = self.active_doc().disk_meta
        {
            match crate::editor::FileMeta::of(&target) {
                Some(actual) if actual != expected => {
                    self.push_toast(Toast::error(
                        "file changed on disk since open (use :w! or :reload)",
                    ));
                    return;
                }
                None => {
                    self.push_toast(Toast::error("file no longer on disk (use :w! to recreate)"));
                    return;
                }
                _ => {}
            }
        }

        let parent_missing = target
            .parent()
            .map(|p| !p.as_os_str().is_empty() && !p.exists())
            .unwrap_or(false);
        if parent_missing {
            if !force {
                self.push_toast(Toast::error(format!(
                    "no such directory: {} (use :w!)",
                    target.parent().unwrap().display()
                )));
                return;
            }
            if let Err(e) = std::fs::create_dir_all(target.parent().unwrap()) {
                self.push_toast(Toast::error(format!(
                    "mkdir {}: {}",
                    target.parent().unwrap().display(),
                    e
                )));
                return;
            }
        }

        let result = if path.is_some() {
            self.active_doc_mut().save_as(&target)
        } else {
            self.active_doc_mut().save()
        };
        let wrote = match result {
            Ok(()) => {
                let msg = if path.is_some() {
                    format!("written to {}", target.display())
                } else {
                    "written".to_string()
                };
                self.push_toast(Toast::info(msg));
                true
            }
            Err(e) => {
                self.push_toast(Toast::error(format!("save: {}", root_cause(&e))));
                false
            }
        };
        if wrote {
            // Many servers (rust-analyzer in particular) only re-run
            // their full checker on save, so this notify is what makes
            // fresh diagnostics arrive.
            self.run_notify_lsp_save();
            if then_quit {
                self.should_quit = true;
            }
        }
    }

    /// External formatter > LSP `textDocument/formatting` > no-op.
    /// Errors surface as toasts but never abort the save: the user
    /// asked to save and we'd rather write the un-formatted bytes
    /// than refuse the action. Format failures during save (e.g.
    /// rustfmt rejecting a syntax error) are common enough that
    /// blocking the save would be hostile.
    fn run_format_on_save(&mut self) {
        let eff = self.effective_editor();
        if !eff.format_on_save {
            return;
        }
        let language = self
            .active_doc()
            .path
            .as_deref()
            .and_then(|p| self.config.languages.by_path(p));

        // External formatter wins when configured: it's the user's
        // explicit choice, and the LSP would typically just shell out
        // to the same tool anyway (gopls → gofmt, rust-analyzer →
        // rustfmt).
        if let Some(lang) = language
            && let Some(formatter) = lang.formatter.clone()
        {
            let cwd = self
                .active_doc()
                .path
                .as_ref()
                .and_then(|p| p.parent())
                .map(|p| p.to_path_buf())
                .unwrap_or_else(|| self.lsp.startup_cwd().to_path_buf());
            let text = self.active_doc().lines.join("\n");
            match crate::format::run_external(&formatter, &text, &cwd) {
                Ok(formatted) => self.apply_formatted_text(formatted),
                Err(e) => {
                    self.push_toast(Toast::fatal(format!(
                        "format `{}`: {}",
                        formatter.command,
                        root_cause(&e)
                    )));
                }
            }
            return;
        }

        // Fall through to LSP. `format_first_client` returns Ok(None)
        // when no client is attached — quietly do nothing in that
        // case so saves on plain-text buffers don't surface noise.
        let options = self.formatting_options();
        match self.lsp.format_first_client(options, LSP_FORMAT_TIMEOUT) {
            Ok(Some(edits)) if !edits.is_empty() => self.apply_format_edits(edits),
            Ok(_) => {}
            Err(e) => {
                self.push_toast(Toast::fatal(format!("lsp format: {}", root_cause(&e))));
            }
        }
    }

    /// Replace the buffer's text wholesale with the external
    /// formatter's stdout. Snapshots first so undo lands on the
    /// pre-format state. Cursor is clamped — the formatter typically
    /// only adds/removes whitespace so the row is usually still valid,
    /// but a wholesale rewrite is allowed to break that.
    fn apply_formatted_text(&mut self, formatted: String) {
        let new_lines: Vec<String> = formatted.split('\n').map(|s| s.to_string()).collect();
        let new_lines = if new_lines.is_empty() {
            vec![String::new()]
        } else {
            // External formatters typically end output with a trailing
            // newline, which `split('\n')` turns into a stray empty
            // last element. Drop it so the buffer doesn't grow an
            // extra blank line on every save.
            let mut v = new_lines;
            if v.len() > 1 && v.last().map(|s| s.is_empty()).unwrap_or(false) {
                v.pop();
            }
            v
        };
        if new_lines == self.active_doc().lines {
            return;
        }
        ed_op!(self, snapshot());
        let doc = self.active_doc_mut();
        doc.lines = new_lines;
        doc.bump_version();
        doc.dirty = true;
        self.clamp_cursor_to_buffer();
    }

    /// Apply a list of LSP `TextEdit`s to the buffer. Snapshots first
    /// so undo lands on the pre-format state; bumps the version so
    /// the highlighter re-runs against the rewritten text.
    fn apply_format_edits(&mut self, edits: Vec<lsp::TextEdit>) {
        ed_op!(self, snapshot());
        let doc = self.active_doc_mut();
        let mut lines = std::mem::take(&mut doc.lines);
        lsp::apply_text_edits(&mut lines, edits);
        if lines.is_empty() {
            lines.push(String::new());
        }
        let doc = self.active_doc_mut();
        doc.lines = lines;
        doc.bump_version();
        doc.dirty = true;
        self.clamp_cursor_to_buffer();
    }

    /// Pin the cursor inside the (possibly shrunken) buffer after a
    /// format rewrite. Conservative: just clamps row/col without
    /// trying to track the cursor's logical position through the
    /// edit — formatters mostly preserve structure, and the user
    /// can scroll back if the cursor lands somewhere unexpected.
    fn clamp_cursor_to_buffer(&mut self) {
        let last_row = self.active_doc().lines.len().saturating_sub(1);
        if self.editor.cursor.row > last_row {
            self.editor.cursor.row = last_row;
        }
        let row_len = self
            .active_doc()
            .lines
            .get(self.editor.cursor.row)
            .map(|s| s.chars().count())
            .unwrap_or(0);
        if self.editor.cursor.col > row_len {
            self.editor.cursor.col = row_len;
        }
    }

    /// LSP `FormattingOptions` derived from the buffer's effective
    /// editor settings. Servers honour these to pick tab vs. space
    /// (gopls in particular needs `insertSpaces: false`).
    fn formatting_options(&self) -> serde_json::Value {
        let eff = self.effective_editor();
        serde_json::json!({
            "tabSize": eff.indent_width,
            "insertSpaces": !eff.use_tabs,
            "trimTrailingWhitespace": true,
            "insertFinalNewline": true,
            "trimFinalNewlines": true,
        })
    }

    /// `:reload` — re-read the active buffer's backing file from
    /// disk. Always reloads: `reload_from_disk` takes an undo
    /// snapshot before the content is replaced, so unsaved edits
    /// are recoverable with `u`. The version bump triggers the
    /// normal `didChange` sync so the LSP picks up the new content
    /// on the next tick.
    fn run_reload(&mut self) {
        let Some(path) = self.active_doc().path.clone() else {
            self.push_toast(Toast::error("no file name"));
            return;
        };
        match ed_op!(self, reload_from_disk()) {
            Ok(true) => self.push_toast(Toast::info(format!("reloaded {}", path.display()))),
            Ok(false) => self.push_toast(Toast::info("reloaded (no change)")),
            Err(e) => self.push_toast(Toast::error(format!("reload: {}", root_cause(&e)))),
        }
    }

    /// `:reload-all` — re-read every file-backed buffer.
    ///
    /// - Active and parked buffers always reload: `reload_from_disk`
    ///   snapshots first, so unsaved edits are recoverable via `u`
    ///   in each pane.
    /// - Clean sleeping entries are dropped so the next visit
    ///   triggers a fresh load; cheaper than thawing them just to
    ///   reload them back.
    /// - Dirty sleeping entries are left alone — their unsaved
    ///   edits live only inside the frozen snapshot, with no
    ///   undo stack reachable from the active pane, so dropping
    ///   would be unrecoverable data loss. They surface unchanged
    ///   on the next visit.
    fn run_reload_all(&mut self) {
        let mut reloaded = 0usize;
        let mut unchanged = 0usize;
        let mut errors: Vec<String> = Vec::new();

        if self.active_doc().path.is_some() {
            match ed_op!(self, reload_from_disk()) {
                Ok(true) => reloaded += 1,
                Ok(false) => unchanged += 1,
                Err(e) => errors.push(format!("active: {}", root_cause(&e))),
            }
        }

        // Inactive panes' sessions, each resolving its own document.
        // Two panes sharing one document reload it twice — the second
        // pass sees disk == buffer and reports "unchanged", so it's a
        // harmless no-op (no double snapshot).
        let inactive_panes: Vec<crate::app::PaneId> = self
            .pane_content
            .iter()
            .filter_map(|(id, c)| match c {
                crate::app::PaneContent::Editor(_) => Some(*id),
                crate::app::PaneContent::Agent => None,
            })
            .collect();
        for pane in inactive_panes {
            let doc_ref = match self.pane_content.get(&pane) {
                Some(crate::app::PaneContent::Editor(ed)) => ed.doc.clone(),
                _ => continue,
            };
            let has_path = self
                .documents
                .get(&doc_ref)
                .map(|d| d.path.is_some())
                .unwrap_or(false);
            if !has_path {
                continue;
            }
            // Disjoint fields: the pane's editor and its pooled document.
            let Some(crate::app::PaneContent::Editor(mut ed)) = self.pane_content.remove(&pane)
            else {
                continue;
            };
            let result = {
                let doc = self
                    .documents
                    .get_mut(&doc_ref)
                    .expect("pane doc present in pool");
                ed.reload_from_disk(doc)
            };
            self.pane_content
                .insert(pane, crate::app::PaneContent::Editor(ed));
            match result {
                Ok(true) => reloaded += 1,
                Ok(false) => unchanged += 1,
                Err(e) => {
                    let label = match &doc_ref {
                        crate::buffer_ref::BufferRef::File(p) => p.display().to_string(),
                        crate::buffer_ref::BufferRef::Scratch(id) => {
                            crate::buffer_ref::BufferRef::scratch_label(*id)
                        }
                    };
                    errors.push(format!("{label}: {}", root_cause(&e)));
                }
            }
        }

        let clean_keys: Vec<_> = self
            .sleeping
            .iter()
            .filter_map(|(k, s)| match k {
                crate::buffer_ref::BufferRef::File(_) if !s.dirty => Some(k.clone()),
                _ => None,
            })
            .collect();
        let preserved_dirty = self
            .sleeping
            .iter()
            .filter(|(k, s)| matches!(k, crate::buffer_ref::BufferRef::File(_)) && s.dirty)
            .count();
        for k in &clean_keys {
            self.sleeping.remove(k);
            reloaded += 1;
        }

        let mut parts = Vec::new();
        if reloaded > 0 {
            parts.push(format!("reloaded {reloaded}"));
        }
        if unchanged > 0 {
            parts.push(format!("{unchanged} unchanged"));
        }
        if preserved_dirty > 0 {
            parts.push(format!("{preserved_dirty} dirty kept"));
        }
        if parts.is_empty() && errors.is_empty() {
            self.push_toast(Toast::info("no file-backed buffers"));
        } else if errors.is_empty() {
            self.push_toast(Toast::info(parts.join(", ")));
        } else {
            let summary = if parts.is_empty() {
                String::from("reload-all failed")
            } else {
                parts.join(", ")
            };
            self.push_toast(Toast::error(format!(
                "{summary}; errors: {}",
                errors.join("; ")
            )));
        }
    }

    fn run_notify_lsp_save(&mut self) {
        let text = self.active_doc().lines.join("\n");
        if let Err(e) = self.lsp.did_save(&text) {
            self.push_toast(Toast::error(format!("lsp didSave: {}", root_cause(&e))));
        }
    }
}