Skip to main content

nativeview_engine/editing/
mod.rs

1//! Platform-agnostic text editing: UAX #29 grapheme/word/paragraph stepping, BiDi visual line
2//! layout (`VisualGraphemeCluster`, `ShapedLineLayout`), and `TextFocusState` (caret, selection,
3//! IME composition).
4
5pub mod bidi;
6pub mod focus;
7pub mod grapheme;
8
9pub use bidi::{ShapedLineLayout, VisualGraphemeCluster};
10pub use focus::TextFocusState;
11pub use grapheme::{
12    clamp_grapheme_boundary, grapheme_boundaries, grapheme_count, is_grapheme_boundary,
13    next_grapheme_boundary, normalize_line_endings, paragraph_bounds_at, prev_grapheme_boundary,
14    word_bounds_at,
15};
16
17#[cfg(test)]
18mod tests {
19    use super::*;
20
21    #[test]
22    fn grapheme_zwj_flag_combining_and_crlf_boundaries() {
23        let zwj = "a๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆb";
24        assert_eq!(grapheme_count(zwj), 3);
25        assert_eq!(next_grapheme_boundary(zwj, 0), 1);
26        assert_eq!(next_grapheme_boundary(zwj, 1), zwj.len() - 1);
27        assert_eq!(prev_grapheme_boundary(zwj, zwj.len() - 1), 1);
28        assert_eq!(clamp_grapheme_boundary(zwj, 4), 1);
29
30        let flag = "๐Ÿ‡ฉ๐Ÿ‡ชx";
31        assert_eq!(grapheme_count(flag), 2);
32        assert_eq!(next_grapheme_boundary(flag, 0), flag.len() - 1);
33
34        assert_eq!(normalize_line_endings("a\r\nb\rc"), "a\nb\nc");
35    }
36
37    #[test]
38    fn focus_state_selection_range_and_reset() {
39        let mut st = TextFocusState::default();
40        assert_eq!(st.selection_range(), None);
41        st.selection_anchor = 5;
42        st.caret_byte_index = 2;
43        assert_eq!(st.selection_range(), Some((2, 5)));
44        assert!(st.selection_range().is_some());
45    }
46}