1use super::{ExcerptId, MultiBufferSnapshot, ToOffset, ToPoint};
2use anyhow::{anyhow, Result};
3use std::{
4 cmp::Ordering,
5 ops::{Range, Sub},
6};
7use sum_tree::Bias;
8use text::{rope::TextDimension, Point};
9
10#[derive(Clone, Eq, PartialEq, Debug, Hash)]
11pub struct Anchor {
12 pub(crate) excerpt_id: ExcerptId,
13 pub(crate) text_anchor: text::Anchor,
14}
15
16impl Anchor {
17 pub fn min() -> Self {
18 Self {
19 excerpt_id: ExcerptId::min(),
20 text_anchor: text::Anchor::min(),
21 }
22 }
23
24 pub fn max() -> Self {
25 Self {
26 excerpt_id: ExcerptId::max(),
27 text_anchor: text::Anchor::max(),
28 }
29 }
30
31 pub fn cmp<'a>(&self, other: &Anchor, snapshot: &MultiBufferSnapshot) -> Result<Ordering> {
32 let excerpt_id_cmp = self.excerpt_id.cmp(&other.excerpt_id);
33 if excerpt_id_cmp.is_eq() {
34 self.text_anchor.cmp(
35 &other.text_anchor,
36 snapshot
37 .buffer_snapshot_for_excerpt(&self.excerpt_id)
38 .ok_or_else(|| anyhow!("excerpt {:?} not found", self.excerpt_id))?,
39 )
40 } else {
41 return Ok(excerpt_id_cmp);
42 }
43 }
44
45 pub fn bias_left(&self, snapshot: &MultiBufferSnapshot) -> Anchor {
46 if self.text_anchor.bias != Bias::Left {
47 if let Some(buffer_snapshot) = snapshot.buffer_snapshot_for_excerpt(&self.excerpt_id) {
48 return Self {
49 excerpt_id: self.excerpt_id.clone(),
50 text_anchor: self.text_anchor.bias_left(buffer_snapshot),
51 };
52 }
53 }
54 self.clone()
55 }
56
57 pub fn bias_right(&self, snapshot: &MultiBufferSnapshot) -> Anchor {
58 if self.text_anchor.bias != Bias::Right {
59 if let Some(buffer_snapshot) = snapshot.buffer_snapshot_for_excerpt(&self.excerpt_id) {
60 return Self {
61 excerpt_id: self.excerpt_id.clone(),
62 text_anchor: self.text_anchor.bias_right(buffer_snapshot),
63 };
64 }
65 }
66 self.clone()
67 }
68
69 pub fn summary<D>(&self, snapshot: &MultiBufferSnapshot) -> D
70 where
71 D: TextDimension + Ord + Sub<D, Output = D>,
72 {
73 snapshot.summary_for_anchor(self)
74 }
75}
76
77impl ToOffset for Anchor {
78 fn to_offset<'a>(&self, snapshot: &MultiBufferSnapshot) -> usize {
79 self.summary(snapshot)
80 }
81}
82
83impl ToPoint for Anchor {
84 fn to_point<'a>(&self, snapshot: &MultiBufferSnapshot) -> Point {
85 self.summary(snapshot)
86 }
87}
88
89pub trait AnchorRangeExt {
90 fn cmp(&self, b: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> Result<Ordering>;
91 fn to_offset(&self, content: &MultiBufferSnapshot) -> Range<usize>;
92 fn to_point(&self, content: &MultiBufferSnapshot) -> Range<Point>;
93}
94
95impl AnchorRangeExt for Range<Anchor> {
96 fn cmp(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> Result<Ordering> {
97 Ok(match self.start.cmp(&other.start, buffer)? {
98 Ordering::Equal => other.end.cmp(&self.end, buffer)?,
99 ord @ _ => ord,
100 })
101 }
102
103 fn to_offset(&self, content: &MultiBufferSnapshot) -> Range<usize> {
104 self.start.to_offset(&content)..self.end.to_offset(&content)
105 }
106
107 fn to_point(&self, content: &MultiBufferSnapshot) -> Range<Point> {
108 self.start.to_point(&content)..self.end.to_point(&content)
109 }
110}