euv-core 0.48.1

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Character-range queries and text extraction helpers for `SelectionState`.
impl SelectionState {
    /// Constructs a single-node character-range selection on `node_id`.
    ///
    /// # Arguments
    ///
    /// - `NodeId` - Target `NodeKind::Text` node handle.
    /// - `usize` - Anchor character index.
    /// - `usize` - Focus character index.
    ///
    /// # Returns
    ///
    /// - `Self` - Single-node selection state.
    #[must_use]
    #[inline(always)]
    pub const fn single(node_id: NodeId, anchor_char: usize, focus_char: usize) -> Self {
        Self {
            anchor_node: node_id,
            anchor_char,
            focus_node: node_id,
            focus_char,
        }
    }

    /// Returns the anchor `NodeId` (matching the single selected node when `is_single_node()` holds).
    ///
    /// # Arguments
    ///
    /// - `&Self` - Selection state reference.
    ///
    /// # Returns
    ///
    /// - `NodeId` - Anchor text node handle.
    #[must_use]
    #[inline(always)]
    pub const fn get_node_id(&self) -> NodeId {
        let Self { anchor_node, .. } = *self;
        anchor_node
    }

    /// Returns `true` when both endpoints of the selection reside on the same `NodeId`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Selection state reference.
    ///
    /// # Returns
    ///
    /// - `bool` - Whether `anchor_node == focus_node`.
    #[must_use]
    #[inline(always)]
    pub fn is_single_node(&self) -> bool {
        let Self {
            anchor_node,
            focus_node,
            ..
        } = *self;
        anchor_node == focus_node
    }

    /// Returns the normalized `(start_char, end_char)` range with `start_char <= end_char` for single-node selections.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Selection state reference.
    ///
    /// # Returns
    ///
    /// - `(usize, usize)` - Ordered character index bounds.
    #[must_use]
    #[inline(always)]
    pub const fn ordered_range(&self) -> (usize, usize) {
        let Self {
            anchor_char,
            focus_char,
            ..
        } = *self;
        if anchor_char <= focus_char {
            (anchor_char, focus_char)
        } else {
            (focus_char, anchor_char)
        }
    }

    /// Returns `true` when the selection is collapsed (same node and equal character offset).
    ///
    /// # Arguments
    ///
    /// - `&Self` - Selection state reference.
    ///
    /// # Returns
    ///
    /// - `bool` - Whether `anchor_node == focus_node && anchor_char == focus_char`.
    #[must_use]
    #[inline(always)]
    pub fn is_empty(&self) -> bool {
        let Self {
            anchor_char,
            focus_char,
            ..
        } = *self;
        self.is_single_node() && anchor_char == focus_char
    }

    /// Extracts the selected UTF-8 slice from `text` for a single-node selection, safely clamping to valid character boundaries.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Selection state reference.
    /// - `&'a str` - Source UTF-8 text string of the selected node.
    ///
    /// # Returns
    ///
    /// - `&'a str` - Selected substring slice (empty when collapsed).
    #[must_use]
    pub fn extract_from<'a>(&self, text: &'a str) -> &'a str {
        let (start_char, end_char): (usize, usize) = self.ordered_range();
        extract_char_range(text, start_char, end_char)
    }
}