Skip to main content

continuity_text/
selection.rs

1//! Selections: `(anchor, head, kind)`.
2//!
3//! Multi-cursor is the general case; a single cursor is a `Vec<Selection>`
4//! of length 1. Block (column) selection is a real selection kind because
5//! it preserves through edits in a way multi-cursors cannot.
6
7use serde::{Deserialize, Serialize};
8
9use crate::{Position, Range};
10
11/// The flavor of a selection.
12#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
13pub enum SelectionKind {
14    /// A caret with no extent (`anchor == head`).
15    Caret,
16    /// A line-wise selection (extends to whole lines).
17    LineWise,
18    /// A block / column selection.
19    BlockWise,
20}
21
22/// A single cursor or selection.
23#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
24pub struct Selection {
25    /// The anchor (the end that does not move when extending).
26    pub anchor: Position,
27    /// The head (the moving end / where the caret is rendered).
28    pub head: Position,
29    /// The selection kind.
30    pub kind: SelectionKind,
31}
32
33impl Selection {
34    /// Construct a selection from its anchor, head, and kind.
35    #[must_use]
36    pub const fn new(anchor: Position, head: Position, kind: SelectionKind) -> Self {
37        Self { anchor, head, kind }
38    }
39
40    /// A caret at `p` (no extent).
41    #[must_use]
42    pub const fn caret_at(p: Position) -> Self {
43        Self {
44            anchor: p,
45            head: p,
46            kind: SelectionKind::Caret,
47        }
48    }
49
50    /// `true` when the selection has zero extent.
51    #[must_use]
52    pub fn is_collapsed(&self) -> bool {
53        self.anchor == self.head
54    }
55
56    /// `true` when the selection is a caret (kind = Caret OR collapsed).
57    #[must_use]
58    pub fn is_caret(&self) -> bool {
59        matches!(self.kind, SelectionKind::Caret) && self.is_collapsed()
60    }
61
62    /// Return the character-wise range covered by this selection.
63    #[must_use]
64    pub fn range(&self) -> Range {
65        Range::new(self.anchor, self.head)
66    }
67
68    /// Return the covered range with `start <= end`.
69    #[must_use]
70    pub fn ordered_range(&self) -> Range {
71        self.range().ordered()
72    }
73}
74
75#[cfg(test)]
76mod tests {
77    use super::*;
78
79    #[test]
80    fn caret_at_origin_is_collapsed_and_caret() {
81        let s = Selection::caret_at(Position::ZERO);
82        assert!(s.is_collapsed());
83        assert!(s.is_caret());
84    }
85
86    #[test]
87    fn linewise_with_extent_is_not_caret() {
88        let s = Selection {
89            anchor: Position::ZERO,
90            head: Position::new(2, 0),
91            kind: SelectionKind::LineWise,
92        };
93        assert!(!s.is_collapsed());
94        assert!(!s.is_caret());
95    }
96
97    #[test]
98    fn caret_kind_with_extent_is_not_caret() {
99        let s = Selection {
100            anchor: Position::ZERO,
101            head: Position::new(0, 3),
102            kind: SelectionKind::Caret,
103        };
104        assert!(!s.is_collapsed());
105        assert!(!s.is_caret());
106        assert_eq!(s.ordered_range().start, Position::ZERO);
107        assert_eq!(s.ordered_range().end, Position::new(0, 3));
108    }
109}