Skip to main content

hjkl_buffer/
lib.rs

1//! # hjkl-buffer
2//!
3//! Rope-backed text buffer with vim-shaped semantics: charwise/linewise/
4//! blockwise selection, motions matching vim edge cases (no `h` wrap, `$`
5//! clamp, sticky col on `j`/`k`), folds, viewport, and search.
6//!
7//! Extracted from `sqeel-buffer` with full git history.
8//!
9//! ## Pre-1.0 stability
10//!
11//! Pre-1.0: signatures may shift between patch versions. The invariants
12//! documented on each type and function are the load-bearing semantics — they
13//! will not silently change without a CHANGELOG entry and a deliberate version
14//! bump.
15//!
16//! ## Why so many invariants?
17//!
18//! Most of them follow from one rule: **the engine layer treats
19//! [`Buffer`] as the source of truth for text content**. Any divergence
20//! between cached state (engine-side selections, undo stacks, search matches)
21//! and the buffer's `lines()` is a bug. The invariants documented on each type
22//! are the contract that lets the engine cache aggressively without risking
23//! that divergence.
24//!
25//! Open issues: <https://github.com/kryptic-sh/hjkl/issues>.
26//!
27//! ## Testing your `Buffer` use
28//!
29//! Property tests are encouraged for any non-trivial caller. The crate ships
30//! its own test suite; reuse [`Buffer::from_str`] to construct fixtures from
31//! inline strings.
32//!
33//! Things worth proving:
34//!
35//! - After any sequence of valid edits + their inverses, the buffer returns to
36//!   its original `lines()`.
37//! - For any valid [`Position`] and motion call, the resulting cursor is itself
38//!   valid.
39//! - [`Buffer::dirty_gen`] strictly increases across mutations and stays
40//!   constant across read-only queries.
41
42#![deny(unsafe_op_in_unsafe_fn)]
43
44mod buffer;
45pub mod content;
46mod edit;
47mod folds;
48pub mod geom;
49pub mod listchars;
50mod motion;
51mod position;
52mod selection;
53mod span;
54mod viewport;
55pub mod wrap;
56
57pub use buffer::Buffer;
58pub use buffer::{rope_line_bytes, rope_line_str};
59pub use content::Content;
60pub use edit::{Edit, MotionKind};
61pub use folds::Fold;
62pub use geom::visual_col_to_char_col;
63pub use listchars::{ListChars, apply_listchars};
64pub use motion::is_keyword_char;
65pub use position::Position;
66pub use selection::{RowSpan, Selection};
67pub use span::Span;
68pub use viewport::{Viewport, is_big_viewport_jump};
69pub use wrap::Wrap;
70
71/// Stable per-buffer identifier carried through async pipelines
72/// (syntax, git-signs, format-worker) so workers can multiplex per-buffer
73/// state without holding buffer references.
74///
75/// Assigned by the application layer; 0 is a valid test sentinel.
76///
77/// # Example
78///
79/// ```
80/// use hjkl_buffer::BufferId;
81/// let id: BufferId = 42;
82/// assert_eq!(id, 42);
83/// ```
84pub type BufferId = u64;