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 fuzzy;
16pub mod io;
17pub mod logic;
18pub mod motion;
19pub mod session;
20pub mod text;
21
22// Prefer using the rope-backed buffer implementation from `buffer`.
23// Re-export the common types here for ergonomic access by downstream crates.
24pub use buffer::{
25    DelimiterKind, Edit, EditBatchSummary, Pos, Selection, TextBuffer, TextObjectEditPlan,
26    TextObjectKind, TextObjectScope, TextObjectSpec, VisualModeKind, VisualSelectionEditPlan,
27};
28pub use fuzzy::{
29    FuzzyMatch, FuzzyQuery, PathMatchScore, compare_path_match_scores, fuzzy_match_ranges,
30    path_match_score,
31};
32pub use session::{
33    BufferId, BufferKind, BufferLoadPhase, BufferLoadStatus, BufferMeta, BufferSummary,
34    EditorSession,
35};
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn empty_buffer_has_one_line() {
43        let b = TextBuffer::new();
44        assert_eq!(b.len_lines(), 1);
45        assert_eq!(b.len_chars(), 0);
46    }
47
48    #[test]
49    fn insert_and_delete_selection_smoke() {
50        let mut b = TextBuffer::from_str("ab");
51
52        let sel = Selection::empty(Pos::new(0, 2));
53        let new_cursor = b.insert(sel.cursor, "c");
54        assert_eq!(b.to_string(), "abc");
55        assert_eq!(new_cursor, Pos::new(0, 3));
56
57        let sel2 = Selection::new(Pos::new(0, 1), Pos::new(0, 2));
58        let (cur, did) = b.delete_selection(sel2);
59        assert!(did);
60        assert_eq!(cur, Pos::new(0, 1));
61        assert_eq!(b.to_string(), "ac");
62    }
63}