Skip to main content

redox_core/
lib.rs

1//! Core editor primitives.
2//!
3//! This crate provides a rope-based text buffer (via `ropey`) and small, focused
4//! navigation utilities suitable for implementing a Vim-like editor.
5//!
6//! Notes on indexing
7//! - `ropey::Rope` is UTF-8 text stored as a rope.
8//! - Most editing operations are most naturally expressed in **char indices**
9//!   (`usize` counts of Unicode scalar values), because `ropey` exposes many APIs
10//!   in terms of `char` offsets.
11//! - The UI may need **byte indices** for interoperability with external data,
12//!   but those are not used as the primary index type in this crate.
13
14pub mod buffer;
15pub mod io;
16pub mod logic;
17pub mod motion;
18pub mod session;
19pub mod text;
20
21// Prefer using the rope-backed buffer implementation from `buffer`.
22// Re-export the common types here for ergonomic access by downstream crates.
23pub use buffer::{Edit, EditBatchSummary, Pos, Selection, TextBuffer, VisualSelectionEditPlan};
24pub use session::{
25    BufferId, BufferKind, BufferLoadPhase, BufferLoadStatus, BufferMeta, BufferSummary,
26    EditorSession,
27};
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn empty_buffer_has_one_line() {
35        let b = TextBuffer::new();
36        assert_eq!(b.len_lines(), 1);
37        assert_eq!(b.len_chars(), 0);
38    }
39
40    #[test]
41    fn insert_and_delete_selection_smoke() {
42        let mut b = TextBuffer::from_str("ab");
43
44        let sel = Selection::empty(Pos::new(0, 2));
45        let new_cursor = b.insert(sel.cursor, "c");
46        assert_eq!(b.to_string(), "abc");
47        assert_eq!(new_cursor, Pos::new(0, 3));
48
49        let sel2 = Selection::new(Pos::new(0, 1), Pos::new(0, 2));
50        let (cur, did) = b.delete_selection(sel2);
51        assert!(did);
52        assert_eq!(cur, Pos::new(0, 1));
53        assert_eq!(b.to_string(), "ac");
54    }
55}