vim-navigation 0.2.1

Reusable Vim-compatible motion and modal text-input state
Documentation
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
//! Read-only Vim cursor and viewport motion.
//!
//! This module owns the coordinate types and pure movement vocabulary used by
//! both direct callers and the crate's optional editable buffer. [`apply`]
//! borrows a logical line snapshot, clamps the supplied [`Cursor`] to a valid
//! UTF-8 boundary, applies one completed [`Motion`], and updates [`Viewport`]
//! so the result remains visible. It never changes the input text.
//!
//! # Coordinates
//!
//! [`Cursor::byte_column`] is a zero-based UTF-8 byte offset, not a character
//! index or screen column. [`Viewport::left`] and [`Viewport::width`] are
//! terminal display cells. A tab advances to the next four-cell tab stop;
//! wide and combining Unicode characters use their terminal widths. Empty
//! input and empty lines have the single valid cursor position `(line, 0)`.
//!
//! Count parsing, incomplete commands, and character-search repetition belong
//! to [`crate::command`]. Text mutation and Insert-mode cursor rules belong to
//! [`crate::editor`]. Search histories, marks, jump lists, terminal keys, and
//! application resources remain caller-owned.
//!
//! # Example
//!
//! ```
//! use vim_navigation::motion::{Cursor, Motion, MotionKind, Viewport, apply};
//!
//! let lines = ["\t界"];
//! let mut viewport = Viewport::new(0, 0, 1, 8, 0);
//! let cursor = apply(
//!     &lines,
//!     Cursor::new(99, 2), // invalid line and inside the three-byte `界`
//!     &mut viewport,
//!     Motion::new(MotionKind::LineEnd),
//! );
//! assert_eq!(cursor, Cursor::new(0, 1));
//! ```

mod buffer;
mod engine;
mod line;
#[cfg(test)]
mod tests;

pub(crate) use engine::reveal_cursor_line;
pub use engine::{apply, reveal};

pub(crate) const FULL_PERCENT: usize = 100;

/// A zero-based text position whose column is a UTF-8 byte offset.
///
/// Construction is deliberately unchecked so adapters can store a cursor
/// before their next text snapshot is available. [`apply`] and [`reveal`]
/// clamp an out-of-range line to the final logical line and round a byte inside
/// a UTF-8 scalar backward to its start. On non-empty lines a byte column at or
/// beyond the line length clamps to the final scalar; empty lines use column
/// zero.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub struct Cursor {
    line: usize,
    byte_column: usize,
}

impl Cursor {
    /// Creates a cursor without consulting any text.
    ///
    /// The next call to [`apply`] or [`reveal`] performs the documented
    /// boundary clamping.
    #[must_use]
    pub const fn new(line: usize, byte_column: usize) -> Self {
        Self { line, byte_column }
    }

    /// Returns the zero-based line.
    #[must_use]
    pub const fn line(self) -> usize {
        self.line
    }

    /// Returns the zero-based UTF-8 byte column.
    #[must_use]
    pub const fn byte_column(self) -> usize {
        self.byte_column
    }
}

/// Origin of a motion count, including the semantically distinct implicit one.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum CountSource {
    /// No decimal count was supplied; the effective count defaults to one.
    Implicit,
    /// The user supplied a decimal count, including an explicit one.
    Explicit,
}

impl CountSource {
    /// Reports whether the count was explicitly supplied.
    #[must_use]
    pub const fn is_explicit(self) -> bool {
        matches!(self, Self::Explicit)
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) enum Repetition {
    Original,
    Repeated,
}

/// A completed Vim movement with its count and optional character argument.
///
/// Use [`crate::MotionState`] when counts and character arguments arrive as
/// separate key events. Constructing a `Motion` directly is useful when an
/// adapter has already resolved its command language. A target is meaningful
/// only to character find/till, unmatched-delimiter, and method motions; other
/// motions safely ignore it. Method motions use target `'M'` for closing braces
/// (`[M`/`]M`) and otherwise select opening braces (`[m`/`]m`).
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Motion {
    kind: MotionKind,
    count: usize,
    count_source: CountSource,
    target: Option<char>,
    repetition: Repetition,
}

impl Motion {
    /// Creates an uncounted movement template with an effective count of one.
    #[must_use]
    pub const fn new(kind: MotionKind) -> Self {
        Self {
            kind,
            count: 1,
            count_source: CountSource::Implicit,
            target: None,
            repetition: Repetition::Original,
        }
    }

    /// Returns the operation.
    #[must_use]
    pub const fn kind(self) -> MotionKind {
        self.kind
    }

    /// Returns the normalized count, which is always at least one.
    #[must_use]
    pub const fn count(self) -> usize {
        self.count
    }

    /// Reports whether the user supplied a count.
    ///
    /// This is semantically distinct from [`Self::count`]: Vim interprets an
    /// explicit `1G`, `1%`, or `1z` command differently from its uncounted
    /// form even though both carry the numeric value one.
    #[must_use]
    pub const fn has_explicit_count(self) -> bool {
        self.count_source.is_explicit()
    }

    /// Returns the character argument for find, till, delimiter, or method movement.
    #[must_use]
    pub const fn target(self) -> Option<char> {
        self.target
    }

    /// Attaches a normalized count and its explicit or implicit [`CountSource`].
    ///
    /// A zero count is normalized to one. Counts produced from decimal input
    /// should normally come from [`crate::MotionState`], which also saturates
    /// overflow and applies Vim's counted-command reinterpretations.
    #[must_use]
    pub const fn counted(mut self, count: usize, source: CountSource) -> Self {
        self.count = if count == 0 { 1 } else { count };
        self.count_source = source;
        self
    }

    /// Attaches a Unicode scalar character or delimiter argument.
    #[must_use]
    pub const fn targeting(mut self, target: char) -> Self {
        self.target = Some(target);
        self
    }

    /// Marks a find/till movement as a `;` or `,` repetition.
    ///
    /// Repeated till motions have a Vim-specific adjacent-match exception;
    /// callers should set this through [`crate::MotionState`] rather than
    /// emulating it by issuing the original motion again.
    #[must_use]
    pub const fn repeating(mut self) -> Self {
        self.repetition = Repetition::Repeated;
        self
    }

    /// Reports whether this movement repeats a prior character search.
    #[must_use]
    pub const fn is_repeated(self) -> bool {
        matches!(self.repetition, Repetition::Repeated)
    }

    pub(super) const fn repetition(self) -> Repetition {
        self.repetition
    }
}

/// Vim's standalone cursor and viewport movement vocabulary.
///
/// Most variants are handled entirely by [`apply`]. Search and mark variants
/// are vocabulary signals for adapters because their resources intentionally
/// remain outside this crate; direct `apply` calls leave the cursor unchanged
/// for those variants. [`crate::MotionState`] resolves the two character-search
/// repetition variants to a concrete find/till motion before application.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum MotionKind {
    /// `h` or Left.
    Left,
    /// Backspace or Ctrl-H when `'whichwrap'` permits crossing lines.
    LeftWrap,
    /// `l` or Right.
    Right,
    /// Space when `'whichwrap'` permits crossing lines.
    RightWrap,
    /// `k`, Up, or Ctrl-P.
    Up,
    /// `j`, Down, Ctrl-J, Ctrl-N, or newline.
    Down,
    /// `0` or Home.
    LineStart,
    /// `^`.
    FirstNonBlank,
    /// `$` or End.
    LineEnd,
    /// `g_`.
    LastNonBlank,
    /// `g0`.
    ScreenLineStart,
    /// `g^`.
    ScreenFirstNonBlank,
    /// `g$`.
    ScreenLineEnd,
    /// `g<End>`.
    ScreenLastNonBlank,
    /// `gm`.
    ScreenMiddle,
    /// `gM`.
    LineMiddle,
    /// `|`.
    Column,
    /// `go`.
    ByteOffset,
    /// `w`.
    WordForward,
    /// `W`.
    BigWordForward,
    /// `e`.
    WordEndForward,
    /// `E`.
    BigWordEndForward,
    /// `b`.
    WordBackward,
    /// `B`.
    BigWordBackward,
    /// `ge`.
    WordEndBackward,
    /// `gE`.
    BigWordEndBackward,
    /// `f{char}`.
    FindForward,
    /// `F{char}`.
    FindBackward,
    /// `t{char}`.
    TillForward,
    /// `T{char}`.
    TillBackward,
    /// `;`, resolved by [`crate::MotionState`].
    RepeatCharacterSearch,
    /// `,`, resolved by [`crate::MotionState`].
    ReverseCharacterSearch,
    /// `-`.
    PreviousLineFirstNonBlank,
    /// `+` or Enter.
    NextLineFirstNonBlank,
    /// `_`.
    CountedLineFirstNonBlank,
    /// `gg` or Ctrl-Home.
    BufferTop,
    /// `G`.
    BufferBottom,
    /// Ctrl-End.
    BufferBottomEnd,
    /// `{count}%`.
    BufferPercentage,
    /// `H`.
    WindowTop,
    /// `M`.
    WindowMiddle,
    /// `L`.
    WindowBottom,
    /// `(`.
    SentenceBackward,
    /// `)`.
    SentenceForward,
    /// `{`.
    ParagraphBackward,
    /// `}`.
    ParagraphForward,
    /// `[[`.
    SectionStartBackward,
    /// `]]`.
    SectionStartForward,
    /// `[]`.
    SectionEndBackward,
    /// `][`.
    SectionEndForward,
    /// `%` without a count.
    MatchingPair,
    /// `g%`.
    MatchingPairBackward,
    /// `[(` or `[{`; the delimiter is stored in [`Motion::target`].
    UnmatchedOpenBackward,
    /// `])` or `]}`; the delimiter is stored in [`Motion::target`].
    UnmatchedCloseForward,
    /// `[m` or `[M`.
    MethodBackward,
    /// `]m` or `]M`.
    MethodForward,
    /// `[#`.
    PreprocessorBackward,
    /// `]#`.
    PreprocessorForward,
    /// `[*` or `[/`.
    CommentBackward,
    /// `]*` or `]/`.
    CommentForward,
    /// `[c` in diff text.
    DiffChangeBackward,
    /// `]c` in diff text.
    DiffChangeForward,
    /// `n`, applied by the caller's retained search state.
    SearchNext,
    /// `N`, applied by the caller's retained search state.
    SearchPrevious,
    /// `*`, applied by the caller's retained search state.
    SearchWordForward,
    /// `#`, applied by the caller's retained search state.
    SearchWordBackward,
    /// `g*`, applied by the caller's retained search state.
    SearchPartialWordForward,
    /// `g#`, applied by the caller's retained search state.
    SearchPartialWordBackward,
    /// `['`, applied by the caller's resource-aware mark store.
    PreviousMarkLine,
    /// `` [` ``, applied by the caller's resource-aware mark store.
    PreviousMarkExact,
    /// `]'`, applied by the caller's resource-aware mark store.
    NextMarkLine,
    /// `` ]` ``, applied by the caller's resource-aware mark store.
    NextMarkExact,
    /// Ctrl-D.
    HalfPageDown,
    /// Ctrl-U.
    HalfPageUp,
    /// Ctrl-F or `PageDown`.
    PageDown,
    /// Ctrl-B or `PageUp`.
    PageUp,
    /// Ctrl-E.
    ScrollLineDown,
    /// Ctrl-Y.
    ScrollLineUp,
    /// `zt`.
    CursorToWindowTop,
    /// `z<CR>`.
    CursorToWindowTopFirstNonBlank,
    /// `zz`.
    CursorToWindowMiddle,
    /// `z.`.
    CursorToWindowMiddleFirstNonBlank,
    /// `zb`.
    CursorToWindowBottom,
    /// `z-`.
    CursorToWindowBottomFirstNonBlank,
    /// `z+`.
    NextWindowTop,
    /// `z^`.
    PreviousWindowBottom,
    /// `zh`.
    ScrollColumnLeft,
    /// `zl`.
    ScrollColumnRight,
    /// `zH`.
    ScrollHalfScreenLeft,
    /// `zL`.
    ScrollHalfScreenRight,
    /// `zs`.
    CursorToWindowLeft,
    /// `ze`.
    CursorToWindowRight,
}

/// The visible part of a text pane, including any rendered gutter columns.
///
/// `top` is a zero-based logical line. `left`, `width`, `gutter`, and the
/// retained desired column are terminal display cells, not UTF-8 byte columns.
/// The viewport does not own wrapping or rendered text: callers supply the
/// same logical line snapshot to [`apply`] that they display.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Viewport {
    top: usize,
    left: usize,
    height: usize,
    width: usize,
    gutter: usize,
    desired_column: Option<usize>,
}

impl Viewport {
    /// Creates a viewport in terminal display cells.
    ///
    /// Zero `height` or `width` is normalized to one so reveal calculations
    /// remain total. `top` and `left` may initially be beyond the supplied
    /// text; motion application clamps or adjusts them as needed.
    #[must_use]
    pub fn new(top: usize, left: usize, height: usize, width: usize, gutter: usize) -> Self {
        Self {
            top,
            left,
            height: height.max(1),
            width: width.max(1),
            gutter,
            desired_column: None,
        }
    }

    /// Restores the desired display column retained by vertical motions.
    ///
    /// `None` derives the first desired column from the current cursor. After
    /// each motion, [`apply`] stores the column needed for a later `j` or `k`;
    /// callers preserving a viewport between calls normally do not need to set
    /// this themselves.
    #[must_use]
    pub fn with_desired_column(mut self, desired_column: Option<usize>) -> Self {
        self.desired_column = desired_column;
        self
    }

    /// Returns the zero-based first visible logical line.
    #[must_use]
    pub const fn top(&self) -> usize {
        self.top
    }

    /// Returns the first visible terminal column, including the gutter.
    #[must_use]
    pub const fn left(&self) -> usize {
        self.left
    }

    /// Returns the viewport height in logical lines (always at least one).
    #[must_use]
    pub const fn height(&self) -> usize {
        self.height
    }

    /// Returns the viewport width in terminal display cells (always at least one).
    #[must_use]
    pub const fn width(&self) -> usize {
        self.width
    }

    /// Returns the rendered gutter width in terminal display cells.
    #[must_use]
    pub const fn gutter(&self) -> usize {
        self.gutter
    }

    /// Returns the retained source-text display column used by vertical motions.
    #[must_use]
    pub const fn desired_column(&self) -> Option<usize> {
        self.desired_column
    }
}