1#![allow(unused)]
2// TODO kb
3
4use std::ops::{Add, AddAssign, Range, Sub};
5
6use crate::MultiBufferSnapshot;
7
8use super::{
9 suggestion_map::{
10 SuggestionBufferRows, SuggestionChunks, SuggestionEdit, SuggestionOffset, SuggestionPoint,
11 SuggestionSnapshot,
12 },
13 TextHighlights,
14};
15use gpui::fonts::HighlightStyle;
16use language::{Chunk, Edit, Point, Rope, TextSummary};
17use parking_lot::Mutex;
18use project::InlayHint;
19use rand::Rng;
20use sum_tree::Bias;
21
22pub struct EditorAdditionMap(Mutex<EditorAdditionSnapshot>);
23
24#[derive(Clone)]
25pub struct EditorAdditionSnapshot {
26 // TODO kb merge these two together
27 pub suggestion_snapshot: SuggestionSnapshot,
28 pub version: usize,
29 hints: Vec<InlayHintToRender>,
30}
31
32pub type EditorAdditionEdit = Edit<EditorAdditionOffset>;
33
34#[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialOrd, PartialEq)]
35pub struct EditorAdditionOffset(pub usize);
36
37impl Add for EditorAdditionOffset {
38 type Output = Self;
39
40 fn add(self, rhs: Self) -> Self::Output {
41 Self(self.0 + rhs.0)
42 }
43}
44
45impl Sub for EditorAdditionOffset {
46 type Output = Self;
47
48 fn sub(self, rhs: Self) -> Self::Output {
49 Self(self.0 - rhs.0)
50 }
51}
52
53impl AddAssign for EditorAdditionOffset {
54 fn add_assign(&mut self, rhs: Self) {
55 self.0 += rhs.0;
56 }
57}
58
59#[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialOrd, PartialEq)]
60pub struct EditorAdditionPoint(pub Point);
61
62#[derive(Clone)]
63pub struct EditorAdditionBufferRows<'a> {
64 suggestion_rows: SuggestionBufferRows<'a>,
65}
66
67pub struct EditorAdditionChunks<'a> {
68 suggestion_chunks: SuggestionChunks<'a>,
69}
70
71#[derive(Clone)]
72pub struct InlayHintToRender {
73 pub(super) position: EditorAdditionPoint,
74 pub(super) text: Rope,
75}
76
77impl<'a> Iterator for EditorAdditionChunks<'a> {
78 type Item = Chunk<'a>;
79
80 fn next(&mut self) -> Option<Self::Item> {
81 self.suggestion_chunks.next()
82 }
83}
84
85impl<'a> Iterator for EditorAdditionBufferRows<'a> {
86 type Item = Option<u32>;
87
88 fn next(&mut self) -> Option<Self::Item> {
89 self.suggestion_rows.next()
90 }
91}
92
93impl EditorAdditionPoint {
94 pub fn new(row: u32, column: u32) -> Self {
95 Self(Point::new(row, column))
96 }
97
98 pub fn row(self) -> u32 {
99 self.0.row
100 }
101
102 pub fn column(self) -> u32 {
103 self.0.column
104 }
105}
106
107impl EditorAdditionMap {
108 pub fn new(suggestion_snapshot: SuggestionSnapshot) -> (Self, EditorAdditionSnapshot) {
109 let snapshot = EditorAdditionSnapshot {
110 suggestion_snapshot: suggestion_snapshot.clone(),
111 version: 0,
112 hints: Vec::new(),
113 };
114 (Self(Mutex::new(snapshot.clone())), snapshot)
115 }
116
117 pub fn sync(
118 &self,
119 suggestion_snapshot: SuggestionSnapshot,
120 suggestion_edits: Vec<SuggestionEdit>,
121 ) -> (EditorAdditionSnapshot, Vec<EditorAdditionEdit>) {
122 let mut snapshot = self.0.lock();
123
124 if snapshot.suggestion_snapshot.version != suggestion_snapshot.version {
125 snapshot.version += 1;
126 }
127
128 let mut editor_addition_edits = Vec::new();
129 for suggestion_edit in suggestion_edits {
130 let old = suggestion_edit.old;
131 let new = suggestion_edit.new;
132 // TODO kb copied from suggestion_map
133 editor_addition_edits.push(EditorAdditionEdit {
134 old: EditorAdditionOffset(old.start.0)..EditorAdditionOffset(old.end.0),
135 new: EditorAdditionOffset(old.start.0)..EditorAdditionOffset(new.end.0),
136 })
137 }
138
139 snapshot.suggestion_snapshot = suggestion_snapshot;
140
141 (snapshot.clone(), editor_addition_edits)
142 }
143
144 pub fn set_inlay_hints(&self, new_hints: Vec<InlayHintToRender>) {
145 self.0.lock().hints = new_hints;
146 }
147}
148
149impl EditorAdditionSnapshot {
150 pub fn buffer_snapshot(&self) -> &MultiBufferSnapshot {
151 // TODO kb copied from suggestion_map
152 self.suggestion_snapshot.buffer_snapshot()
153 }
154
155 pub fn to_point(&self, offset: EditorAdditionOffset) -> EditorAdditionPoint {
156 // TODO kb copied from suggestion_map
157 self.to_editor_addition_point(
158 self.suggestion_snapshot
159 .to_point(super::suggestion_map::SuggestionOffset(offset.0)),
160 )
161 }
162
163 pub fn max_point(&self) -> EditorAdditionPoint {
164 // TODO kb copied from suggestion_map
165 self.to_editor_addition_point(self.suggestion_snapshot.max_point())
166 }
167
168 pub fn to_offset(&self, point: EditorAdditionPoint) -> EditorAdditionOffset {
169 // TODO kb copied from suggestion_map
170 EditorAdditionOffset(
171 self.suggestion_snapshot
172 .to_offset(self.to_suggestion_point(point, Bias::Left))
173 .0,
174 )
175 }
176
177 pub fn chars_at(&self, start: EditorAdditionPoint) -> impl '_ + Iterator<Item = char> {
178 self.suggestion_snapshot
179 .chars_at(self.to_suggestion_point(start, Bias::Left))
180 }
181
182 // TODO kb what to do with bias?
183 pub fn to_suggestion_point(&self, point: EditorAdditionPoint, _: Bias) -> SuggestionPoint {
184 SuggestionPoint(point.0)
185 }
186
187 pub fn to_editor_addition_point(&self, point: SuggestionPoint) -> EditorAdditionPoint {
188 EditorAdditionPoint(point.0)
189 }
190
191 pub fn clip_point(&self, point: EditorAdditionPoint, bias: Bias) -> EditorAdditionPoint {
192 // TODO kb copied from suggestion_map
193 self.to_editor_addition_point(
194 self.suggestion_snapshot
195 .clip_point(self.to_suggestion_point(point, bias), bias),
196 )
197 }
198
199 pub fn text_summary_for_range(&self, range: Range<EditorAdditionPoint>) -> TextSummary {
200 // TODO kb copied from suggestion_map
201 self.suggestion_snapshot.text_summary_for_range(
202 self.to_suggestion_point(range.start, Bias::Left)
203 ..self.to_suggestion_point(range.end, Bias::Left),
204 )
205 }
206
207 pub fn buffer_rows<'a>(&'a self, row: u32) -> EditorAdditionBufferRows<'a> {
208 EditorAdditionBufferRows {
209 suggestion_rows: self.suggestion_snapshot.buffer_rows(row),
210 }
211 }
212
213 pub fn line_len(&self, row: u32) -> u32 {
214 // TODO kb copied from suggestion_map
215 self.suggestion_snapshot.line_len(row)
216 }
217
218 pub fn chunks<'a>(
219 &'a self,
220 range: Range<EditorAdditionOffset>,
221 language_aware: bool,
222 text_highlights: Option<&'a TextHighlights>,
223 suggestion_highlight: Option<HighlightStyle>,
224 ) -> EditorAdditionChunks<'a> {
225 // TODO kb copied from suggestion_map
226 EditorAdditionChunks {
227 suggestion_chunks: self.suggestion_snapshot.chunks(
228 SuggestionOffset(range.start.0)..SuggestionOffset(range.end.0),
229 language_aware,
230 text_highlights,
231 suggestion_highlight,
232 ),
233 }
234 }
235
236 #[cfg(test)]
237 pub fn text(&self) -> String {
238 // TODO kb copied from suggestion_map
239 self.suggestion_snapshot.text()
240 }
241}