vorto 0.15.3

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
//! `Expr::Direct` evaluation — the kitchen-sink dispatch for keystrokes
//! that don't fit the motion / operator framework (mode transitions,
//! prompt opens, buffer-wide ops, ex-style commands, multi-cursor and
//! window controls). Buffer mutations happen inline; everything else is
//! emitted as a `Cmd` for the runtime to apply.

use std::path::PathBuf;

use super::push_word_search;
use crate::action::{Ctx, DirectKind, PromptKind};
use crate::app::App;
use crate::app::eval::format_dirty_list;
use crate::buffer_ref::BufferRef;
use crate::editor::Cursor;
use crate::effect::{Cmd, ScrollAnchor};
use crate::mode::Mode;

pub(super) fn handle_direct(app: &mut App, kind: DirectKind, count: u32, ctx: Ctx) -> Vec<Cmd> {
    use DirectKind as D;
    let mut cmds = Vec::new();
    match kind {
        D::EnterMode(m) => cmds.push(Cmd::EnterMode(m)),
        D::OpenPrompt(k) => cmds.push(Cmd::OpenPrompt(k)),
        D::OpenLineBelow => {
            let indent = app.indent_settings();
            ed_op!(app, insert_line_below(indent));
            cmds.push(Cmd::EnterMode(Mode::Insert));
        }
        D::OpenLineAbove => {
            let indent = app.indent_settings();
            ed_op!(app, insert_line_above(indent));
            cmds.push(Cmd::EnterMode(Mode::Insert));
        }
        D::AppendAfterCursor => {
            // Past-the-end is allowed in Insert, so step right with
            // that permission rather than the Normal-mode clamp.
            ed_op_ref!(app, move_right(true));
            cmds.push(Cmd::EnterMode(Mode::Insert));
        }
        D::AppendAtLineEnd => {
            app.editor.cursor.col = ed_op_ref!(app, current_line_len());
            cmds.push(Cmd::EnterMode(Mode::Insert));
        }
        D::InsertAtLineStart => {
            let col = {
                let line = ed_op_ref!(app, current_line());
                line.chars().position(|c| !c.is_whitespace()).unwrap_or(0)
            };
            app.editor.cursor.col = col;
            cmds.push(Cmd::EnterMode(Mode::Insert));
        }
        D::ChangeToEol => {
            ed_op!(app, delete_to_eol());
            cmds.push(Cmd::EnterMode(Mode::Insert));
        }
        D::DeleteToEol => ed_op!(app, delete_to_eol()),
        D::YankLine => {
            // `NY` yanks N whole lines as one run — capture the span in a
            // single call so the register holds every line, not just the
            // last.
            let start = app.editor.cursor.row;
            let last = app.active_doc().lines.len().saturating_sub(1);
            let end = start.saturating_add(count.max(1) as usize - 1).min(last);
            app.active_doc_mut().yank_lines(start, end);
            cmds.push(Cmd::SyncYank);
            cmds.push(Cmd::ToastInfo("yanked".into()));
        }
        D::JoinLines => {
            for _ in 0..count {
                ed_op!(app, join_next_line());
            }
        }
        D::ToggleCase => {
            for _ in 0..count {
                ed_op!(app, toggle_case_under_cursor());
            }
        }
        D::SubstituteChar => {
            for _ in 0..count {
                ed_op!(app, delete_char_under_cursor());
            }
            cmds.push(Cmd::EnterMode(Mode::Insert));
        }
        D::SubstituteLine => {
            ed_op!(app, clear_current_line());
            cmds.push(Cmd::EnterMode(Mode::Insert));
        }
        D::ReplaceChar { ch } => {
            for _ in 0..count {
                ed_op!(app, replace_char(ch));
                // After each replacement, vim leaves the cursor on
                // the replaced char; a count > 1 walks forward one
                // step per replacement.
                ed_op_ref!(app, move_right(false));
            }
            // Final cursor: vim leaves it on the LAST replaced
            // char, not past it.
            app.editor.move_left();
        }
        D::ViewportCenter => cmds.push(Cmd::Scroll(ScrollAnchor::Center)),
        D::ViewportTopAtCursor => cmds.push(Cmd::Scroll(ScrollAnchor::Top)),
        D::ViewportBottomAtCursor => cmds.push(Cmd::Scroll(ScrollAnchor::Bottom)),
        D::FoldToggle => app.fold_toggle_at_cursor(),
        D::FoldOpen => app.fold_open_at_cursor(),
        D::FoldClose => app.fold_close_at_cursor(),
        D::FoldOpenAll => app.fold_open_all(),
        D::FoldCloseAll => app.fold_close_all(),
        D::Paste => {
            // `Np` repeats the register as one contiguous block, so the
            // count goes *into* the paste rather than re-running it (which
            // would interleave a multi-line register).
            ed_op!(app, paste_after(count as usize));
        }
        D::Undo => {
            if !ed_op!(app, undo()) {
                cmds.push(Cmd::ToastError("already at oldest change".into()));
            }
        }
        D::Redo => {
            if !ed_op!(app, redo()) {
                cmds.push(Cmd::ToastError("already at newest change".into()));
            }
        }
        D::DeleteCharUnderCursor => {
            for _ in 0..count {
                ed_op!(app, delete_char_under_cursor());
            }
        }
        D::Quit => cmds.push(plan_quit(app)),
        D::QuitForce => cmds.push(Cmd::Quit),
        D::BufferNext => cmds.push(Cmd::BufferCycle { forward: true }),
        D::BufferPrev => cmds.push(Cmd::BufferCycle { forward: false }),
        D::BufferDelete => cmds.push(Cmd::BufferDelete { force: false }),
        D::BufferDeleteForce => cmds.push(Cmd::BufferDelete { force: true }),
        D::BufferDeleteAll => cmds.push(Cmd::BufferDeleteAll),
        D::BufferList => {
            cmds.push(Cmd::OpenPrompt(PromptKind::Fuzzy(
                crate::finder::FuzzyKind::Buffers,
            )));
        }
        D::NewScratchBuffer => cmds.push(Cmd::NewScratchBuffer),
        D::SaveAndQuit => cmds.push(Cmd::Save {
            path: parse_save_path(ctx.rest),
            then_quit: true,
            force: false,
        }),
        D::Save => cmds.push(Cmd::Save {
            path: parse_save_path(ctx.rest),
            then_quit: false,
            force: false,
        }),
        D::SaveForce => cmds.push(Cmd::Save {
            path: parse_save_path(ctx.rest),
            then_quit: false,
            force: true,
        }),
        D::SaveAll => cmds.push(Cmd::SaveAll {
            then_quit: false,
            force: false,
        }),
        D::SaveAllForce => cmds.push(Cmd::SaveAll {
            then_quit: false,
            force: true,
        }),
        D::SaveAllAndQuit => cmds.push(Cmd::SaveAll {
            then_quit: true,
            force: false,
        }),
        D::SaveAllAndQuitForce => cmds.push(Cmd::SaveAll {
            then_quit: true,
            force: true,
        }),
        D::Open => {
            if ctx.rest.is_empty() {
                cmds.push(Cmd::ToastError("missing path".into()));
            } else {
                cmds.push(Cmd::OpenPath(PathBuf::from(ctx.rest)));
            }
        }
        D::OpenLog => match crate::log::default_path() {
            Some(p) => cmds.push(Cmd::OpenPath(p)),
            None => cmds.push(Cmd::ToastError("log path unresolved".into())),
        },
        D::Reload => cmds.push(Cmd::Reload),
        D::ReloadAll => cmds.push(Cmd::ReloadAll),
        D::ConfigReload => cmds.push(Cmd::ConfigReload),
        D::GotoLine => match ctx.rest.parse::<usize>() {
            Ok(n) if n >= 1 => app.goto_line_n_pure(n),
            _ => cmds.push(Cmd::ToastError("usage: :goto <line>".into())),
        },
        D::GotoDefinition => cmds.push(Cmd::LspJump {
            method: "textDocument/definition",
            label: "definition",
        }),
        D::GotoDeclaration => cmds.push(Cmd::LspJump {
            method: "textDocument/declaration",
            label: "declaration",
        }),
        D::GotoImplementation => cmds.push(Cmd::LspJump {
            method: "textDocument/implementation",
            label: "implementation",
        }),
        D::FindReferences => cmds.push(Cmd::LspFindReferences),
        D::Rename => cmds.push(Cmd::OpenRenamePrompt),
        D::CodeAction => cmds.push(Cmd::LspCodeAction),
        D::Hover => cmds.push(Cmd::LspHover),
        D::LspStatus => {
            // `:lsp all` / `:lsp a` widens the scope to every
            // configured language. Anything else (including the
            // common no-arg case) is buffer-scoped — `open_lsp_status`
            // filters down to the active file's language.
            let all = matches!(ctx.rest.trim(), "all" | "a");
            cmds.push(Cmd::OpenLspStatus { all });
        }
        D::GotoDiagnostic { forward } => {
            cmds.push(Cmd::GotoDiagnostic { forward, count });
        }
        D::GotoConflict { forward } => {
            cmds.push(Cmd::GotoConflict { forward, count });
        }
        // Intercepted by `App::evaluate` before reaching here.
        D::RepeatLast => unreachable!("RepeatLast handled in App::evaluate"),
        D::SearchSelectNext { reverse } => {
            cmds.push(Cmd::SearchSelectMatch { reverse });
        }
        D::SearchWordKeep { forward } => {
            push_word_search(app, &mut cmds, forward, false);
        }
        D::ClearSearch => {
            cmds.push(Cmd::SetSearch {
                pattern: String::new(),
                forward: true,
            });
        }
        D::Substitute => run_substitute(app, ctx.rest, &mut cmds),
        D::MultiCursorAddNext => add_next_cursor(app, &mut cmds),
        D::MultiCursorAddBelow => add_cursor_below(app, &mut cmds),
        D::MultiCursorPop => {
            if let Some(c) = app.editor.extra_cursors.pop() {
                app.editor.cursor = c;
            } else {
                cmds.push(Cmd::ToastInfo("no extra cursor to remove".into()));
            }
        }
        D::MultiCursorClear => {
            if app.editor.extra_cursors.is_empty() {
                cmds.push(Cmd::ToastInfo("no extra cursors".into()));
            } else {
                let n = app.editor.extra_cursors.len();
                app.editor.extra_cursors.clear();
                cmds.push(Cmd::ToastInfo(format!("cleared {n} extra cursors")));
            }
        }
        D::BookmarkAdd => app.add_current_bookmark(),
        D::BookmarkRemoveCurrent => app.remove_current_bookmark(),
        D::JumpLabel => cmds.push(Cmd::StartJumpLabel),
        D::SelectWholeBuffer => cmds.push(Cmd::SelectWholeBuffer),
        D::SplitWindowHorizontal => cmds.push(Cmd::SplitWindow {
            dir: crate::app::SplitDir::Horizontal,
        }),
        D::SplitWindowVertical => cmds.push(Cmd::SplitWindow {
            dir: crate::app::SplitDir::Vertical,
        }),
        D::CloseWindow => cmds.push(Cmd::CloseWindow),
        D::FocusWindow { dir } => cmds.push(Cmd::FocusWindow { dir }),
        D::CycleWindow => cmds.push(Cmd::CycleWindow),
        D::JumpBack => cmds.push(Cmd::JumpBack { count }),
        D::JumpForward => cmds.push(Cmd::JumpForward { count }),
        D::JumpList => {
            cmds.push(Cmd::OpenPrompt(PromptKind::Fuzzy(
                crate::finder::FuzzyKind::Jumps,
            )));
        }
    }
    cmds
}

/// Plan the response to a bare `:q`. Refuses with an error status
/// while there are unsaved edits in the active or any sleeping
/// buffer; otherwise emits the actual quit command.
fn plan_quit(app: &App) -> Cmd {
    if app.active_doc().dirty {
        return Cmd::ToastError("unsaved changes (use :q!)".into());
    }
    // Non-active buffers live either frozen in `sleeping` (shown in no
    // pane) or still pooled in `documents` (shown in an inactive split
    // pane). Check both so a dirty buffer in another pane still blocks
    // the quit instead of being dropped on the floor.
    let active = &app.editor.doc;
    let mut dirty: Vec<&BufferRef> = app
        .documents
        .iter()
        .filter(|(r, b)| *r != active && b.dirty)
        .map(|(r, _)| r)
        .collect();
    dirty.extend(app.sleeping.iter().filter(|(_, b)| b.dirty).map(|(r, _)| r));
    if !dirty.is_empty() {
        return Cmd::ToastError(format!(
            "unsaved changes in {} (use :q!)",
            format_dirty_list(&dirty)
        ));
    }
    Cmd::Quit
}

/// `<C-n>` body. Pulls the word under the cursor, finds its next
/// occurrence forward from primary (wrapping around the buffer), and
/// pushes primary as a new extra cursor before jumping primary to the
/// match. Also seeds `App.search` via `Cmd::SetSearch` so `n` / `N`
/// keep working on the same pattern after the user is done adding
/// cursors. When the cursor isn't on a word (e.g. sitting on `[`),
/// drops into Visual mode at the current position instead of erroring
/// — the user can extend the selection and try again, or just operate
/// on the highlighted char. No-ops with a status message when the
/// next match would land on a cursor that's already tracked.
fn add_next_cursor(app: &mut App, cmds: &mut Vec<Cmd>) {
    let Some(word) = crate::app::eval::word_under_cursor(&app.editor, app.active_doc()) else {
        app.enter_mode(Mode::Visual);
        return;
    };
    // Use a throwaway SearchState for the lookup so we can act on the
    // result this turn — `Cmd::SetSearch` is only applied after
    // `handle_expr` returns, so reading `app.search` here would see
    // the pre-Ctrl-N pattern.
    let mut tmp = crate::editor::SearchState::default();
    tmp.set(word.clone(), true);
    let Some(next) = tmp.find_next(&app.editor, app.active_doc(), true) else {
        cmds.push(Cmd::ToastError("no further match".into()));
        return;
    };
    let primary = app.editor.cursor;
    if next == primary || app.editor.extra_cursors.contains(&next) {
        cmds.push(Cmd::ToastInfo("no further match".into()));
        return;
    }
    app.editor.extra_cursors.push(primary);
    app.editor.cursor = next;
    cmds.push(Cmd::SetSearch {
        pattern: word,
        forward: true,
    });
    let n = app.editor.extra_cursors.len() + 1;
    cmds.push(Cmd::ToastInfo(format!("{n} cursors")));
}

/// Push the current primary into `extra_cursors` and move primary one
/// row down at the same column (clamped to the new line's length).
/// No-ops with a toast when already on the last line, or when the
/// landing cursor would collide with primary or an existing extra.
fn add_cursor_below(app: &mut App, cmds: &mut Vec<Cmd>) {
    let primary = app.editor.cursor;
    if primary.row + 1 >= app.active_doc().lines.len() {
        cmds.push(Cmd::ToastError("no line below".into()));
        return;
    }
    let next_row = primary.row + 1;
    let next_line_len = app.active_doc().lines[next_row].chars().count();
    let next_col = primary.col.min(next_line_len.saturating_sub(1));
    let next = Cursor {
        row: next_row,
        col: next_col,
    };
    if next == primary || app.editor.extra_cursors.contains(&next) {
        cmds.push(Cmd::ToastInfo("cursor already there".into()));
        return;
    }
    app.editor.extra_cursors.push(primary);
    app.editor.cursor = next;
    let n = app.editor.extra_cursors.len() + 1;
    cmds.push(Cmd::ToastInfo(format!("{n} cursors")));
}

fn parse_save_path(rest: &str) -> Option<PathBuf> {
    if rest.is_empty() {
        None
    } else {
        Some(PathBuf::from(rest))
    }
}

/// Body of `:s/pat/repl/[g]` and `:%s/...`. Parses the raw command
/// string off `ctx.rest`, falls back to the active search pattern when
/// the user passed an empty pattern (vim convention: `:%s//new/g`
/// after a `/old`), applies the substitution against the buffer, and
/// pushes a status toast plus a `SetSearch` so `n`/`hlsearch` track
/// what was replaced.
fn run_substitute(app: &mut App, raw: &str, cmds: &mut Vec<Cmd>) {
    let Some(parsed) = crate::editor::parse_substitute(raw) else {
        cmds.push(Cmd::ToastError("usage: :s/pat/repl/[g]".into()));
        return;
    };
    let args = match parsed {
        Ok(a) => a,
        Err(msg) => {
            cmds.push(Cmd::ToastError(msg.into()));
            return;
        }
    };

    // Empty pattern → reuse the last search pattern. Saves typing
    // after `/foo<CR>:%s//bar/g`.
    let fallback;
    let pattern = if args.pattern.is_empty() {
        if app.search.query.is_empty() {
            cmds.push(Cmd::ToastError("no previous search pattern".into()));
            return;
        }
        fallback = app.search.query.clone();
        fallback.as_str()
    } else {
        args.pattern
    };

    let resolved = crate::editor::SubsArgs {
        range: args.range,
        pattern,
        replacement: args.replacement,
        global: args.global,
    };
    let outcome = ed_op!(app, substitute(&resolved));

    if outcome.matches == 0 {
        cmds.push(Cmd::ToastError(format!("pattern not found: {}", pattern)));
        return;
    }
    cmds.push(Cmd::SetSearch {
        pattern: pattern.to_string(),
        forward: true,
    });
    cmds.push(Cmd::ToastInfo(format!(
        "{} substitution{} on {} line{}",
        outcome.matches,
        if outcome.matches == 1 { "" } else { "s" },
        outcome.lines_changed,
        if outcome.lines_changed == 1 { "" } else { "s" },
    )));
}