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 if self.excerpt_id == ExcerptId::min() || self.excerpt_id == ExcerptId::max() {
35 Ok(Ordering::Equal)
36 } else {
37 self.text_anchor.cmp(
38 &other.text_anchor,
39 snapshot
40 .buffer_snapshot_for_excerpt(&self.excerpt_id)
41 .ok_or_else(|| anyhow!("excerpt {:?} not found", self.excerpt_id))?,
42 )
43 }
44 } else {
45 return Ok(excerpt_id_cmp);
46 }
47 }
48
49 pub fn bias_left(&self, snapshot: &MultiBufferSnapshot) -> Anchor {
50 if self.text_anchor.bias != Bias::Left {
51 if let Some(buffer_snapshot) = snapshot.buffer_snapshot_for_excerpt(&self.excerpt_id) {
52 return Self {
53 excerpt_id: self.excerpt_id.clone(),
54 text_anchor: self.text_anchor.bias_left(buffer_snapshot),
55 };
56 }
57 }
58 self.clone()
59 }
60
61 pub fn bias_right(&self, snapshot: &MultiBufferSnapshot) -> Anchor {
62 if self.text_anchor.bias != Bias::Right {
63 if let Some(buffer_snapshot) = snapshot.buffer_snapshot_for_excerpt(&self.excerpt_id) {
64 return Self {
65 excerpt_id: self.excerpt_id.clone(),
66 text_anchor: self.text_anchor.bias_right(buffer_snapshot),
67 };
68 }
69 }
70 self.clone()
71 }
72
73 pub fn summary<D>(&self, snapshot: &MultiBufferSnapshot) -> D
74 where
75 D: TextDimension + Ord + Sub<D, Output = D>,
76 {
77 snapshot.summary_for_anchor(self)
78 }
79}
80
81impl ToOffset for Anchor {
82 fn to_offset<'a>(&self, snapshot: &MultiBufferSnapshot) -> usize {
83 self.summary(snapshot)
84 }
85}
86
87impl ToPoint for Anchor {
88 fn to_point<'a>(&self, snapshot: &MultiBufferSnapshot) -> Point {
89 self.summary(snapshot)
90 }
91}
92
93pub trait AnchorRangeExt {
94 fn cmp(&self, b: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> Result<Ordering>;
95 fn to_offset(&self, content: &MultiBufferSnapshot) -> Range<usize>;
96 fn to_point(&self, content: &MultiBufferSnapshot) -> Range<Point>;
97}
98
99impl AnchorRangeExt for Range<Anchor> {
100 fn cmp(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> Result<Ordering> {
101 Ok(match self.start.cmp(&other.start, buffer)? {
102 Ordering::Equal => other.end.cmp(&self.end, buffer)?,
103 ord @ _ => ord,
104 })
105 }
106
107 fn to_offset(&self, content: &MultiBufferSnapshot) -> Range<usize> {
108 self.start.to_offset(&content)..self.end.to_offset(&content)
109 }
110
111 fn to_point(&self, content: &MultiBufferSnapshot) -> Range<Point> {
112 self.start.to_point(&content)..self.end.to_point(&content)
113 }
114}