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///
60/// **No `#[non_exhaustive]` (#844).** 57 out-of-crate literal sites, the most of any type here —
61/// and `{row, left, right}` is closed geometry, so there is no growth cause to defend against.
62/// Declining costs nothing that can be measured.
63#[derive(Clone, Copy, PartialEq, Eq, Debug)]
64pub struct SelectionSpan {
65 pub row: usize,
66 pub left: usize,
67 pub right: usize,
68}
69
70/// A point in absolute buffer coordinates: `line` indexes `[scrollback ++ screen]`
71/// from the oldest line, `col` is the column.
72#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
73pub(crate) struct BufferPoint {
74 pub line: usize,
75 pub col: usize,
76}
77
78/// A selection endpoint: a buffer point plus which side of the cell it touches.
79#[derive(Clone, Copy, PartialEq, Eq, Debug)]
80pub(crate) struct Anchor {
81 pub point: BufferPoint,
82 pub side: Side,
83}
84
85/// The live selection: where the drag began (`anchor`) and where it currently
86/// reaches (`focus`). Either may be the earlier point — `ordered` sorts them.
87pub(crate) struct Selection {
88 pub ty: SelectionType,
89 pub anchor: Anchor,
90 pub focus: Anchor,
91}
92
93impl Selection {
94 /// The two anchors sorted so the first is the earlier buffer point.
95 pub fn ordered(&self) -> (Anchor, Anchor) {
96 if self.anchor.point <= self.focus.point {
97 (self.anchor, self.focus)
98 } else {
99 (self.focus, self.anchor)
100 }
101 }
102}