justerm_core/selection.rs
1//! Engine-owned selection state (see `docs/architecture.md` "Selection").
2//!
3//! Anchors are stored in **absolute buffer coordinates** — a line index into the
4//! concatenated `[scrollback ++ screen]` stream, counted from the oldest line.
5//! This coordinate is stable under a normal top-anchored scroll (the evicted
6//! line entering scrollback grows `scrollback.len()` by exactly the screen
7//! shift, so existing content keeps its absolute index); the only places it
8//! moves are cap eviction, in-screen region/RI scrolls, and reflow — each
9//! handled explicitly by `Term`. The cell-aware logic (text extraction, range
10//! clipping) lives in `term/selection.rs` — the `Term` half of this model, moved out
11//! of `term.rs` in #587.
12
13/// What a selection covers.
14///
15/// **Deliberately exhaustive (#843), on convergence rather than on traffic.**
16///
17/// An earlier draft of that sweep argued *"a consumer cannot fall back on a
18/// neighbour for one it does not know"* — which describes matching traffic this
19/// API does not have. Measured: this type appears in exactly one public
20/// signature, as a **parameter** to [`crate::Engine::selection_begin`], and
21/// nothing hands one outward. So the attribute would cost a consumer nothing,
22/// and that argument cannot be what keeps it off.
23///
24/// What keeps it off is that the set really is closed: **alacritty arrives at the
25/// same four modes independently**, under different names —
26/// `Simple` / `Semantic` / `Lines` / `Block`
27/// (`alacritty_terminal/src/selection.rs:93`), where its own doc glosses `simple`
28/// as tracking cells "without any expansion" ([`Char`](Self::Char)) and
29/// `semantic` as expanding "to the nearest semantic escape char"
30/// ([`Word`](Self::Word)). Two implementations landing on one partition of the
31/// space is the non-arbitrariness signal, and it is a stronger ground than the
32/// one it replaces.
33#[derive(Clone, Copy, PartialEq, Eq, Debug)]
34pub enum SelectionType {
35 /// Contiguous run, wrapping line to line.
36 Char,
37 /// Expanded to word boundaries.
38 Word,
39 /// Whole lines.
40 Line,
41 /// Rectangular column range on every line.
42 Block,
43}
44
45/// Which half of a cell an anchor sits on — the left or right edge. Lets a drag
46/// include or exclude the cell under the pointer (mouse precision).
47///
48/// **Deliberately exhaustive (#843).** Closed by geometry — there is no third side.
49/// Left exhaustive on purpose, not by omission.
50#[derive(Clone, Copy, PartialEq, Eq, Debug)]
51pub enum Side {
52 Left,
53 Right,
54}
55
56/// One highlighted run on a single **viewport** row: columns `left..=right`
57/// (both inclusive). `selection_range` returns one per visible row the selection
58/// touches — the renderer paints these. Off-screen rows are not emitted.
59#[derive(Clone, Copy, PartialEq, Eq, Debug)]
60pub struct SelectionSpan {
61 pub row: usize,
62 pub left: usize,
63 pub right: usize,
64}
65
66/// A point in absolute buffer coordinates: `line` indexes `[scrollback ++ screen]`
67/// from the oldest line, `col` is the column.
68#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
69pub(crate) struct BufferPoint {
70 pub line: usize,
71 pub col: usize,
72}
73
74/// A selection endpoint: a buffer point plus which side of the cell it touches.
75#[derive(Clone, Copy, PartialEq, Eq, Debug)]
76pub(crate) struct Anchor {
77 pub point: BufferPoint,
78 pub side: Side,
79}
80
81/// The live selection: where the drag began (`anchor`) and where it currently
82/// reaches (`focus`). Either may be the earlier point — `ordered` sorts them.
83pub(crate) struct Selection {
84 pub ty: SelectionType,
85 pub anchor: Anchor,
86 pub focus: Anchor,
87}
88
89impl Selection {
90 /// The two anchors sorted so the first is the earlier buffer point.
91 pub fn ordered(&self) -> (Anchor, Anchor) {
92 if self.anchor.point <= self.focus.point {
93 (self.anchor, self.focus)
94 } else {
95 (self.focus, self.anchor)
96 }
97 }
98}