Skip to main content

euv_core/scene/selection/
impl.rs

1use super::*;
2
3/// Character-range queries and text extraction helpers for `SelectionState`.
4impl SelectionState {
5    /// Constructs a single-node character-range selection on `node_id`.
6    ///
7    /// # Arguments
8    ///
9    /// - `NodeId` - Target `NodeKind::Text` node handle.
10    /// - `usize` - Anchor character index.
11    /// - `usize` - Focus character index.
12    ///
13    /// # Returns
14    ///
15    /// - `Self` - Single-node selection state.
16    #[must_use]
17    #[inline(always)]
18    pub const fn single(node_id: NodeId, anchor_char: usize, focus_char: usize) -> Self {
19        Self {
20            anchor_node: node_id,
21            anchor_char,
22            focus_node: node_id,
23            focus_char,
24        }
25    }
26
27    /// Returns the anchor `NodeId` (matching the single selected node when `is_single_node()` holds).
28    ///
29    /// # Arguments
30    ///
31    /// - `&Self` - Selection state reference.
32    ///
33    /// # Returns
34    ///
35    /// - `NodeId` - Anchor text node handle.
36    #[must_use]
37    #[inline(always)]
38    pub const fn get_node_id(&self) -> NodeId {
39        let Self { anchor_node, .. } = *self;
40        anchor_node
41    }
42
43    /// Returns `true` when both endpoints of the selection reside on the same `NodeId`.
44    ///
45    /// # Arguments
46    ///
47    /// - `&Self` - Selection state reference.
48    ///
49    /// # Returns
50    ///
51    /// - `bool` - Whether `anchor_node == focus_node`.
52    #[must_use]
53    #[inline(always)]
54    pub fn is_single_node(&self) -> bool {
55        let Self {
56            anchor_node,
57            focus_node,
58            ..
59        } = *self;
60        anchor_node == focus_node
61    }
62
63    /// Returns the normalized `(start_char, end_char)` range with `start_char <= end_char` for single-node selections.
64    ///
65    /// # Arguments
66    ///
67    /// - `&Self` - Selection state reference.
68    ///
69    /// # Returns
70    ///
71    /// - `(usize, usize)` - Ordered character index bounds.
72    #[must_use]
73    #[inline(always)]
74    pub const fn ordered_range(&self) -> (usize, usize) {
75        let Self {
76            anchor_char,
77            focus_char,
78            ..
79        } = *self;
80        if anchor_char <= focus_char {
81            (anchor_char, focus_char)
82        } else {
83            (focus_char, anchor_char)
84        }
85    }
86
87    /// Returns `true` when the selection is collapsed (same node and equal character offset).
88    ///
89    /// # Arguments
90    ///
91    /// - `&Self` - Selection state reference.
92    ///
93    /// # Returns
94    ///
95    /// - `bool` - Whether `anchor_node == focus_node && anchor_char == focus_char`.
96    #[must_use]
97    #[inline(always)]
98    pub fn is_empty(&self) -> bool {
99        let Self {
100            anchor_char,
101            focus_char,
102            ..
103        } = *self;
104        self.is_single_node() && anchor_char == focus_char
105    }
106
107    /// Extracts the selected UTF-8 slice from `text` for a single-node selection, safely clamping to valid character boundaries.
108    ///
109    /// # Arguments
110    ///
111    /// - `&Self` - Selection state reference.
112    /// - `&'a str` - Source UTF-8 text string of the selected node.
113    ///
114    /// # Returns
115    ///
116    /// - `&'a str` - Selected substring slice (empty when collapsed).
117    #[must_use]
118    pub fn extract_from<'a>(&self, text: &'a str) -> &'a str {
119        let (start_char, end_char): (usize, usize) = self.ordered_range();
120        extract_char_range(text, start_char, end_char)
121    }
122}