Skip to main content

sivtr_core/selection/
mod.rs

1pub mod extract;
2pub mod mode;
3
4use crate::buffer::cursor::Cursor;
5
6/// Selection mode matching Vim visual modes.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum SelectionMode {
9    /// Character-wise selection (v).
10    Visual,
11    /// Line-wise selection (V).
12    VisualLine,
13    /// Block/column selection (Ctrl-V).
14    VisualBlock,
15}
16
17/// A selection region defined by an anchor and the current cursor.
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19pub struct Selection {
20    pub mode: SelectionMode,
21    /// The position where selection started.
22    pub anchor: Cursor,
23}
24
25impl Selection {
26    pub fn new(mode: SelectionMode, anchor: Cursor) -> Self {
27        Self { mode, anchor }
28    }
29
30    /// Get the ordered row range (top, bottom) given the current cursor.
31    pub fn row_range(&self, cursor: &Cursor) -> (usize, usize) {
32        let r1 = self.anchor.row;
33        let r2 = cursor.row;
34        (r1.min(r2), r1.max(r2))
35    }
36
37    /// Get the ordered column range (left, right) given the current cursor.
38    /// Only meaningful for Visual and VisualBlock modes.
39    pub fn col_range(&self, cursor: &Cursor) -> (usize, usize) {
40        let c1 = self.anchor.col;
41        let c2 = cursor.col;
42        (c1.min(c2), c1.max(c2))
43    }
44}