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
//! Vim-style line editor implementation.
//!
//! Owns the `VimLineEditor` struct, its `Mode` / `Operator` state, cursor-
//! motion and edit helpers, the five mode-specific key handlers, and the
//! `LineEditor` trait implementation.
use crate::{Action, EditResult, Key, KeyCode, LineEditor, TextEdit};
use std::ops::Range;
mod edits;
mod motions;
/// Vim editing mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub(crate) enum Mode {
#[default]
Normal,
Insert,
OperatorPending(Operator),
Visual,
/// Waiting for a character to replace the one under cursor (r command)
ReplaceChar,
}
/// Operators that wait for a motion.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Operator {
Delete,
Change,
Yank,
}
/// A vim-style line editor.
///
/// Implements modal editing with Normal, Insert, Visual, and OperatorPending modes.
/// Designed for single "one-shot" inputs that may span multiple lines.
#[derive(Debug, Clone)]
pub struct VimLineEditor {
pub(in crate::vim) cursor: usize,
pub(in crate::vim) mode: Mode,
/// Anchor point for visual selection (cursor is the other end).
pub(in crate::vim) visual_anchor: Option<usize>,
/// Last yanked text (for paste).
pub(in crate::vim) yank_buffer: String,
}
impl Default for VimLineEditor {
fn default() -> Self {
Self::new()
}
}
impl VimLineEditor {
/// Create a new editor in Normal mode.
pub fn new() -> Self {
Self {
cursor: 0,
mode: Mode::Normal,
visual_anchor: None,
yank_buffer: String::new(),
}
}
/// Current mode — test-only accessor.
#[cfg(test)]
fn mode(&self) -> Mode {
self.mode
}
/// Clamp cursor to valid range for the given text.
fn clamp_cursor(&mut self, text: &str) {
self.cursor = self.cursor.min(text.len());
}
/// Move cursor left by one character.
fn move_left(&mut self, text: &str) {
self.cursor = motions::move_left(self.cursor, text);
}
/// Move cursor right by one character.
fn move_right(&mut self, text: &str) {
self.cursor = motions::move_right(self.cursor, text);
}
/// Move cursor to start of line (0).
fn move_line_start(&mut self, text: &str) {
self.cursor = motions::move_line_start(self.cursor, text);
}
/// Move cursor to first non-whitespace of line (^).
fn move_first_non_blank(&mut self, text: &str) {
self.cursor = motions::move_first_non_blank(self.cursor, text);
}
/// Move cursor to end of line (Normal mode — stays on last char).
fn move_line_end(&mut self, text: &str) {
self.cursor = motions::move_line_end(self.cursor, text);
}
/// Move cursor past end of line (Insert mode).
fn move_line_end_insert(&mut self, text: &str) {
self.cursor = motions::move_line_end_insert(self.cursor, text);
}
/// Move cursor forward by word (w).
fn move_word_forward(&mut self, text: &str) {
self.cursor = motions::move_word_forward(self.cursor, text);
}
/// Move cursor backward by word (b).
fn move_word_backward(&mut self, text: &str) {
self.cursor = motions::move_word_backward(self.cursor, text);
}
/// Move cursor to end of word (e).
fn move_word_end(&mut self, text: &str) {
self.cursor = motions::move_word_end(self.cursor, text);
}
/// Move cursor up one line (k).
fn move_up(&mut self, text: &str) {
self.cursor = motions::move_up(self.cursor, text);
}
/// Move cursor down one line (j).
fn move_down(&mut self, text: &str) {
self.cursor = motions::move_down(self.cursor, text);
}
/// Move cursor to matching bracket (%).
/// Supports (), [], {}, and <>.
fn move_to_matching_bracket(&mut self, text: &str) {
self.cursor = motions::move_to_matching_bracket(self.cursor, text);
}
/// Dispatch a shared motion key (h/l/j/k/0/$/^/w/b/e/%/Left/Right/Home/End)
/// to the appropriate cursor helper. Returns `true` when the key was
/// recognized as a motion, `false` otherwise.
///
/// Up/Down arrow keys are intentionally NOT handled here — Normal mode
/// treats them as history navigation, not motion.
///
/// Called by Normal and Visual handlers so each can delegate motion
/// interpretation to one place and then wrap the result in its own way.
/// OperatorPending has extra `c`/`cw`/`ce` quirks and handles motion
/// itself.
fn dispatch_motion(&mut self, code: KeyCode, text: &str) -> bool {
match code {
KeyCode::Char('h') | KeyCode::Left => self.move_left(text),
KeyCode::Char('l') | KeyCode::Right => self.move_right(text),
KeyCode::Char('j') => self.move_down(text),
KeyCode::Char('k') => self.move_up(text),
KeyCode::Char('0') | KeyCode::Home => self.move_line_start(text),
KeyCode::Char('^') => self.move_first_non_blank(text),
KeyCode::Char('$') | KeyCode::End => self.move_line_end(text),
KeyCode::Char('w') => self.move_word_forward(text),
KeyCode::Char('b') => self.move_word_backward(text),
KeyCode::Char('e') => self.move_word_end(text),
KeyCode::Char('%') => self.move_to_matching_bracket(text),
_ => return false,
}
true
}
/// Handle key in Normal mode.
fn handle_normal(&mut self, key: Key, text: &str) -> EditResult {
// Shared motions (h/l/j/k/0/$/^/w/b/e/%/Left/Right/Home/End).
// Up/Down are NOT motions in Normal — they're history navigation below.
if self.dispatch_motion(key.code, text) {
return EditResult::cursor_only();
}
match key.code {
// Mode switching
KeyCode::Char('i') => {
self.mode = Mode::Insert;
EditResult::none()
}
KeyCode::Char('a') => {
self.mode = Mode::Insert;
self.move_right(text);
EditResult::none()
}
KeyCode::Char('A') => {
self.mode = Mode::Insert;
self.move_line_end_insert(text);
EditResult::none()
}
KeyCode::Char('I') => {
self.mode = Mode::Insert;
self.move_first_non_blank(text);
EditResult::none()
}
KeyCode::Char('o') => {
self.mode = Mode::Insert;
self.move_line_end(text);
let pos = self.cursor;
self.cursor = pos + 1;
EditResult::edit(TextEdit::Insert {
at: pos,
text: "\n".to_string(),
})
}
KeyCode::Char('O') => {
self.mode = Mode::Insert;
self.move_line_start(text);
let pos = self.cursor;
EditResult::edit(TextEdit::Insert {
at: pos,
text: "\n".to_string(),
})
}
// Visual mode
KeyCode::Char('v') => {
self.mode = Mode::Visual;
self.visual_anchor = Some(self.cursor);
EditResult::none()
}
// Cancel (Ctrl+C)
KeyCode::Char('c') if key.ctrl => EditResult::action(Action::Cancel),
// Operators (enter pending mode)
KeyCode::Char('d') => {
self.mode = Mode::OperatorPending(Operator::Delete);
EditResult::none()
}
KeyCode::Char('c') => {
self.mode = Mode::OperatorPending(Operator::Change);
EditResult::none()
}
KeyCode::Char('y') => {
self.mode = Mode::OperatorPending(Operator::Yank);
EditResult::none()
}
// Direct deletions
KeyCode::Char('x') => self.delete_char(text),
KeyCode::Char('D') => self.delete_to_end(text),
KeyCode::Char('C') => {
self.mode = Mode::Insert;
self.delete_to_end(text)
}
// Replace character (r)
KeyCode::Char('r') => {
self.mode = Mode::ReplaceChar;
EditResult::none()
}
// Paste
KeyCode::Char('p') => self.paste_after(text),
KeyCode::Char('P') => self.paste_before(text),
// History (arrows only)
KeyCode::Up => EditResult::action(Action::HistoryPrev),
KeyCode::Down => EditResult::action(Action::HistoryNext),
// Submit
KeyCode::Enter if !key.shift => EditResult::action(Action::Submit),
// Newline (Shift+Enter)
KeyCode::Enter if key.shift => {
self.mode = Mode::Insert;
let pos = self.cursor;
self.cursor = pos + 1;
EditResult::edit(TextEdit::Insert {
at: pos,
text: "\n".to_string(),
})
}
// Escape in Normal mode is a no-op (safe to spam like in vim)
// Use Ctrl+C to cancel/quit
KeyCode::Escape => EditResult::none(),
_ => EditResult::none(),
}
}
/// Handle key in Insert mode.
fn handle_insert(&mut self, key: Key, text: &str) -> EditResult {
match key.code {
KeyCode::Escape => {
self.mode = Mode::Normal;
// Move cursor left like vim does when exiting insert
if self.cursor > 0 {
self.move_left(text);
}
EditResult::none()
}
// Ctrl+C exits insert mode
KeyCode::Char('c') if key.ctrl => {
self.mode = Mode::Normal;
EditResult::none()
}
KeyCode::Char(c) if !key.ctrl && !key.alt => {
let pos = self.cursor;
self.cursor = pos + c.len_utf8();
EditResult::edit(TextEdit::Insert {
at: pos,
text: c.to_string(),
})
}
KeyCode::Backspace => {
if self.cursor == 0 {
return EditResult::none();
}
let mut start = self.cursor - 1;
while start > 0 && !text.is_char_boundary(start) {
start -= 1;
}
let end = self.cursor; // Save original cursor before updating
self.cursor = start;
EditResult::edit(TextEdit::Delete { start, end })
}
KeyCode::Delete => self.delete_char(text),
KeyCode::Left => {
self.move_left(text);
EditResult::cursor_only()
}
KeyCode::Right => {
self.move_right(text);
EditResult::cursor_only()
}
KeyCode::Up => {
self.move_up(text);
EditResult::cursor_only()
}
KeyCode::Down => {
self.move_down(text);
EditResult::cursor_only()
}
KeyCode::Home => {
self.move_line_start(text);
EditResult::cursor_only()
}
KeyCode::End => {
// In Insert mode, cursor can go past the last character
self.move_line_end_insert(text);
EditResult::cursor_only()
}
// Enter inserts newline in insert mode
KeyCode::Enter => {
let pos = self.cursor;
self.cursor = pos + 1;
EditResult::edit(TextEdit::Insert {
at: pos,
text: "\n".to_string(),
})
}
_ => EditResult::none(),
}
}
/// Handle key in OperatorPending mode.
fn handle_operator_pending(&mut self, op: Operator, key: Key, text: &str) -> EditResult {
// First, handle escape to cancel
if key.code == KeyCode::Escape {
self.mode = Mode::Normal;
return EditResult::none();
}
// Handle doubled operator (dd, cc, yy) - operates on whole line
let is_line_op = matches!(
(op, key.code),
(Operator::Delete, KeyCode::Char('d'))
| (Operator::Change, KeyCode::Char('c'))
| (Operator::Yank, KeyCode::Char('y'))
);
if is_line_op {
self.mode = Mode::Normal;
return self.apply_operator_line(op, text);
}
// Handle motion
let start = self.cursor;
match key.code {
KeyCode::Char('w') => {
// Special case: cw behaves like ce (change to end of word, not including space)
// This is a vim quirk for historical compatibility
if op == Operator::Change {
self.move_word_end(text);
// Include the character at cursor
if self.cursor < text.len() {
self.cursor += 1;
}
} else {
self.move_word_forward(text);
}
}
KeyCode::Char('b') => self.move_word_backward(text),
KeyCode::Char('e') => {
self.move_word_end(text);
// Include the character at cursor for delete/change
if self.cursor < text.len() {
self.cursor += 1;
}
}
KeyCode::Char('0') | KeyCode::Home => self.move_line_start(text),
KeyCode::Char('$') | KeyCode::End => self.move_line_end(text),
KeyCode::Char('^') => self.move_first_non_blank(text),
KeyCode::Char('h') | KeyCode::Left => self.move_left(text),
KeyCode::Char('l') | KeyCode::Right => self.move_right(text),
KeyCode::Char('j') => self.move_down(text),
KeyCode::Char('k') => self.move_up(text),
_ => {
// Unknown motion, cancel
self.mode = Mode::Normal;
return EditResult::none();
}
}
let end = self.cursor;
self.mode = Mode::Normal;
if start == end {
return EditResult::none();
}
let (range_start, range_end) = if start < end {
(start, end)
} else {
(end, start)
};
self.apply_operator(op, range_start, range_end, text)
}
/// Handle key in Visual mode.
fn handle_visual(&mut self, key: Key, text: &str) -> EditResult {
match key.code {
KeyCode::Escape => {
self.mode = Mode::Normal;
self.visual_anchor = None;
EditResult::none()
}
// Motions extend selection (note: `^` and `%` are intentionally
// not wired here to preserve original behavior — tracked for a
// separate behavior-change PR).
KeyCode::Char('h') | KeyCode::Left => {
self.move_left(text);
EditResult::cursor_only()
}
KeyCode::Char('l') | KeyCode::Right => {
self.move_right(text);
EditResult::cursor_only()
}
KeyCode::Char('j') => {
self.move_down(text);
EditResult::cursor_only()
}
KeyCode::Char('k') => {
self.move_up(text);
EditResult::cursor_only()
}
KeyCode::Char('w') => {
self.move_word_forward(text);
EditResult::cursor_only()
}
KeyCode::Char('b') => {
self.move_word_backward(text);
EditResult::cursor_only()
}
KeyCode::Char('e') => {
self.move_word_end(text);
EditResult::cursor_only()
}
KeyCode::Char('0') | KeyCode::Home => {
self.move_line_start(text);
EditResult::cursor_only()
}
KeyCode::Char('$') | KeyCode::End => {
self.move_line_end(text);
EditResult::cursor_only()
}
// Operators on selection
KeyCode::Char('d') | KeyCode::Char('x') => {
let (start, end) = self.selection_range();
self.mode = Mode::Normal;
self.visual_anchor = None;
self.apply_operator(Operator::Delete, start, end, text)
}
KeyCode::Char('c') => {
let (start, end) = self.selection_range();
self.mode = Mode::Normal;
self.visual_anchor = None;
self.apply_operator(Operator::Change, start, end, text)
}
KeyCode::Char('y') => {
let (start, end) = self.selection_range();
self.mode = Mode::Normal;
self.visual_anchor = None;
self.apply_operator(Operator::Yank, start, end, text)
}
_ => EditResult::none(),
}
}
/// Handle key in ReplaceChar mode (waiting for character after 'r').
fn handle_replace_char(&mut self, key: Key, text: &str) -> EditResult {
self.mode = Mode::Normal;
match key.code {
KeyCode::Escape => EditResult::none(),
KeyCode::Char(c) if !key.ctrl && !key.alt => {
// Replace character at cursor
if self.cursor >= text.len() {
return EditResult::none();
}
// Find the end of the current character
let mut end = self.cursor + 1;
while end < text.len() && !text.is_char_boundary(end) {
end += 1;
}
// Delete current char and insert new one
// Note: edits are applied in reverse order, so Insert comes first in vec
EditResult {
edits: vec![
TextEdit::Insert {
at: self.cursor,
text: c.to_string(),
},
TextEdit::Delete {
start: self.cursor,
end,
},
],
..Default::default()
}
}
_ => EditResult::none(),
}
}
/// Get the selection range (ordered).
fn selection_range(&self) -> (usize, usize) {
let anchor = self.visual_anchor.unwrap_or(self.cursor);
if self.cursor < anchor {
(self.cursor, anchor)
} else {
(anchor, self.cursor + 1) // Include cursor position
}
}
}
impl LineEditor for VimLineEditor {
fn handle_key(&mut self, key: Key, text: &str) -> EditResult {
self.clamp_cursor(text);
let result = match self.mode {
Mode::Normal => self.handle_normal(key, text),
Mode::Insert => self.handle_insert(key, text),
Mode::OperatorPending(op) => self.handle_operator_pending(op, key, text),
Mode::Visual => self.handle_visual(key, text),
Mode::ReplaceChar => self.handle_replace_char(key, text),
};
// Store yanked text
if let Some(ref yanked) = result.yanked {
self.yank_buffer = yanked.clone();
}
result
}
fn cursor(&self) -> usize {
self.cursor
}
fn status(&self) -> &str {
match self.mode {
Mode::Normal => "NORMAL",
Mode::Insert => "INSERT",
Mode::OperatorPending(Operator::Delete) => "d...",
Mode::OperatorPending(Operator::Change) => "c...",
Mode::OperatorPending(Operator::Yank) => "y...",
Mode::Visual => "VISUAL",
Mode::ReplaceChar => "r...",
}
}
fn selection(&self) -> Option<Range<usize>> {
if self.mode == Mode::Visual {
let (start, end) = self.selection_range();
Some(start..end)
} else {
None
}
}
fn reset(&mut self) {
self.cursor = 0;
self.mode = Mode::Normal;
self.visual_anchor = None;
// Keep yank buffer across resets
}
fn set_cursor(&mut self, pos: usize, text: &str) {
// Clamp to text length and ensure we're at a char boundary
let pos = pos.min(text.len());
self.cursor = if text.is_char_boundary(pos) {
pos
} else {
// Walk backwards to find a valid boundary
let mut p = pos;
while p > 0 && !text.is_char_boundary(p) {
p -= 1;
}
p
};
}
}
#[cfg(test)]
mod tests;