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