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#[derive(Clone, Copy, PartialEq, Eq, Debug)]
15pub enum SelectionType {
16 /// Contiguous run, wrapping line to line.
17 Char,
18 /// Expanded to word boundaries.
19 Word,
20 /// Whole lines.
21 Line,
22 /// Rectangular column range on every line.
23 Block,
24}
25
26/// Which half of a cell an anchor sits on — the left or right edge. Lets a drag
27/// include or exclude the cell under the pointer (mouse precision).
28#[derive(Clone, Copy, PartialEq, Eq, Debug)]
29pub enum Side {
30 Left,
31 Right,
32}
33
34/// One highlighted run on a single **viewport** row: columns `left..=right`
35/// (both inclusive). `selection_range` returns one per visible row the selection
36/// touches — the renderer paints these. Off-screen rows are not emitted.
37#[derive(Clone, Copy, PartialEq, Eq, Debug)]
38pub struct SelectionSpan {
39 pub row: usize,
40 pub left: usize,
41 pub right: usize,
42}
43
44/// A point in absolute buffer coordinates: `line` indexes `[scrollback ++ screen]`
45/// from the oldest line, `col` is the column.
46#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
47pub(crate) struct BufferPoint {
48 pub line: usize,
49 pub col: usize,
50}
51
52/// A selection endpoint: a buffer point plus which side of the cell it touches.
53#[derive(Clone, Copy, PartialEq, Eq, Debug)]
54pub(crate) struct Anchor {
55 pub point: BufferPoint,
56 pub side: Side,
57}
58
59/// The live selection: where the drag began (`anchor`) and where it currently
60/// reaches (`focus`). Either may be the earlier point — `ordered` sorts them.
61pub(crate) struct Selection {
62 pub ty: SelectionType,
63 pub anchor: Anchor,
64 pub focus: Anchor,
65}
66
67impl Selection {
68 /// The two anchors sorted so the first is the earlier buffer point.
69 pub fn ordered(&self) -> (Anchor, Anchor) {
70 if self.anchor.point <= self.focus.point {
71 (self.anchor, self.focus)
72 } else {
73 (self.focus, self.anchor)
74 }
75 }
76}