1use super::{
2 suggestion_map::{self, SuggestionChunks, SuggestionEdit, SuggestionPoint, SuggestionSnapshot},
3 TextHighlights,
4};
5use crate::MultiBufferSnapshot;
6use gpui::fonts::HighlightStyle;
7use language::{Chunk, Point};
8use parking_lot::Mutex;
9use std::{cmp, mem, num::NonZeroU32, ops::Range};
10use sum_tree::Bias;
11
12pub struct TabMap(Mutex<TabSnapshot>);
13
14impl TabMap {
15 pub fn new(input: SuggestionSnapshot, tab_size: NonZeroU32) -> (Self, TabSnapshot) {
16 let snapshot = TabSnapshot {
17 suggestion_snapshot: input,
18 tab_size,
19 version: 0,
20 };
21 (Self(Mutex::new(snapshot.clone())), snapshot)
22 }
23
24 pub fn sync(
25 &self,
26 suggestion_snapshot: SuggestionSnapshot,
27 mut suggestion_edits: Vec<SuggestionEdit>,
28 tab_size: NonZeroU32,
29 ) -> (TabSnapshot, Vec<TabEdit>) {
30 let mut old_snapshot = self.0.lock();
31 let mut new_snapshot = TabSnapshot {
32 suggestion_snapshot,
33 tab_size,
34 version: old_snapshot.version,
35 };
36
37 if old_snapshot.suggestion_snapshot.version != new_snapshot.suggestion_snapshot.version {
38 new_snapshot.version += 1;
39 }
40
41 let old_max_offset = old_snapshot.suggestion_snapshot.len();
42 let mut tab_edits = Vec::with_capacity(suggestion_edits.len());
43
44 if old_snapshot.tab_size == new_snapshot.tab_size {
45 for suggestion_edit in &mut suggestion_edits {
46 let mut delta = 0;
47 for chunk in old_snapshot.suggestion_snapshot.chunks(
48 suggestion_edit.old.end..old_max_offset,
49 false,
50 None,
51 None,
52 ) {
53 let patterns: &[_] = &['\t', '\n'];
54 if let Some(ix) = chunk.text.find(patterns) {
55 if &chunk.text[ix..ix + 1] == "\t" {
56 suggestion_edit.old.end.0 += delta + ix + 1;
57 suggestion_edit.new.end.0 += delta + ix + 1;
58 }
59
60 break;
61 }
62
63 delta += chunk.text.len();
64 }
65 }
66
67 let mut ix = 1;
68 while ix < suggestion_edits.len() {
69 let (prev_edits, next_edits) = suggestion_edits.split_at_mut(ix);
70 let prev_edit = prev_edits.last_mut().unwrap();
71 let edit = &next_edits[0];
72 if prev_edit.old.end >= edit.old.start {
73 prev_edit.old.end = edit.old.end;
74 prev_edit.new.end = edit.new.end;
75 suggestion_edits.remove(ix);
76 } else {
77 ix += 1;
78 }
79 }
80
81 for suggestion_edit in suggestion_edits {
82 let old_start = old_snapshot
83 .suggestion_snapshot
84 .to_point(suggestion_edit.old.start);
85 let old_end = old_snapshot
86 .suggestion_snapshot
87 .to_point(suggestion_edit.old.end);
88 let new_start = new_snapshot
89 .suggestion_snapshot
90 .to_point(suggestion_edit.new.start);
91 let new_end = new_snapshot
92 .suggestion_snapshot
93 .to_point(suggestion_edit.new.end);
94 tab_edits.push(TabEdit {
95 old: old_snapshot.to_tab_point(old_start)..old_snapshot.to_tab_point(old_end),
96 new: new_snapshot.to_tab_point(new_start)..new_snapshot.to_tab_point(new_end),
97 });
98 }
99 } else {
100 new_snapshot.version += 1;
101 tab_edits.push(TabEdit {
102 old: TabPoint::zero()..old_snapshot.max_point(),
103 new: TabPoint::zero()..new_snapshot.max_point(),
104 });
105 }
106
107 *old_snapshot = new_snapshot;
108 (old_snapshot.clone(), tab_edits)
109 }
110}
111
112#[derive(Clone)]
113pub struct TabSnapshot {
114 pub suggestion_snapshot: SuggestionSnapshot,
115 pub tab_size: NonZeroU32,
116 pub version: usize,
117}
118
119impl TabSnapshot {
120 pub fn buffer_snapshot(&self) -> &MultiBufferSnapshot {
121 self.suggestion_snapshot.buffer_snapshot()
122 }
123
124 pub fn line_len(&self, row: u32) -> u32 {
125 let max_point = self.max_point();
126 if row < max_point.row() {
127 self.chunks(
128 TabPoint::new(row, 0)..TabPoint::new(row + 1, 0),
129 false,
130 None,
131 None,
132 )
133 .map(|chunk| chunk.text.len() as u32)
134 .sum::<u32>()
135 - 1
136 } else {
137 max_point.column()
138 }
139 }
140
141 pub fn text_summary(&self) -> TextSummary {
142 self.text_summary_for_range(TabPoint::zero()..self.max_point())
143 }
144
145 pub fn text_summary_for_range(&self, range: Range<TabPoint>) -> TextSummary {
146 let input_start = self.to_suggestion_point(range.start, Bias::Left).0;
147 let input_end = self.to_suggestion_point(range.end, Bias::Right).0;
148 let input_summary = self
149 .suggestion_snapshot
150 .text_summary_for_range(input_start..input_end);
151
152 let mut first_line_chars = 0;
153 let line_end = if range.start.row() == range.end.row() {
154 range.end
155 } else {
156 self.max_point()
157 };
158 for c in self
159 .chunks(range.start..line_end, false, None, None)
160 .flat_map(|chunk| chunk.text.chars())
161 {
162 if c == '\n' {
163 break;
164 }
165 first_line_chars += 1;
166 }
167
168 let mut last_line_chars = 0;
169 if range.start.row() == range.end.row() {
170 last_line_chars = first_line_chars;
171 } else {
172 for _ in self
173 .chunks(
174 TabPoint::new(range.end.row(), 0)..range.end,
175 false,
176 None,
177 None,
178 )
179 .flat_map(|chunk| chunk.text.chars())
180 {
181 last_line_chars += 1;
182 }
183 }
184
185 TextSummary {
186 lines: range.end.0 - range.start.0,
187 first_line_chars,
188 last_line_chars,
189 longest_row: input_summary.longest_row,
190 longest_row_chars: input_summary.longest_row_chars,
191 }
192 }
193
194 pub fn chunks<'a>(
195 &'a self,
196 range: Range<TabPoint>,
197 language_aware: bool,
198 text_highlights: Option<&'a TextHighlights>,
199 suggestion_highlight: Option<HighlightStyle>,
200 ) -> TabChunks<'a> {
201 let (input_start, expanded_char_column, to_next_stop) =
202 self.to_suggestion_point(range.start, Bias::Left);
203 let input_start = self.suggestion_snapshot.to_offset(input_start);
204 let input_end = self
205 .suggestion_snapshot
206 .to_offset(self.to_suggestion_point(range.end, Bias::Right).0);
207 let to_next_stop = if range.start.0 + Point::new(0, to_next_stop as u32) > range.end.0 {
208 (range.end.column() - range.start.column()) as usize
209 } else {
210 to_next_stop
211 };
212
213 TabChunks {
214 suggestion_chunks: self.suggestion_snapshot.chunks(
215 input_start..input_end,
216 language_aware,
217 text_highlights,
218 suggestion_highlight,
219 ),
220 column: expanded_char_column,
221 output_position: range.start.0,
222 max_output_position: range.end.0,
223 tab_size: self.tab_size,
224 chunk: Chunk {
225 text: &SPACES[0..to_next_stop],
226 ..Default::default()
227 },
228 skip_leading_tab: to_next_stop > 0,
229 }
230 }
231
232 pub fn buffer_rows(&self, row: u32) -> suggestion_map::SuggestionBufferRows {
233 self.suggestion_snapshot.buffer_rows(row)
234 }
235
236 #[cfg(test)]
237 pub fn text(&self) -> String {
238 self.chunks(TabPoint::zero()..self.max_point(), false, None, None)
239 .map(|chunk| chunk.text)
240 .collect()
241 }
242
243 pub fn max_point(&self) -> TabPoint {
244 self.to_tab_point(self.suggestion_snapshot.max_point())
245 }
246
247 pub fn clip_point(&self, point: TabPoint, bias: Bias) -> TabPoint {
248 self.to_tab_point(
249 self.suggestion_snapshot
250 .clip_point(self.to_suggestion_point(point, bias).0, bias),
251 )
252 }
253
254 pub fn to_tab_point(&self, input: SuggestionPoint) -> TabPoint {
255 let chars = self
256 .suggestion_snapshot
257 .chars_at(SuggestionPoint::new(input.row(), 0));
258 let expanded = Self::expand_tabs(chars, input.column() as usize, self.tab_size);
259 TabPoint::new(input.row(), expanded as u32)
260 }
261
262 pub fn to_suggestion_point(
263 &self,
264 output: TabPoint,
265 bias: Bias,
266 ) -> (SuggestionPoint, usize, usize) {
267 let chars = self
268 .suggestion_snapshot
269 .chars_at(SuggestionPoint::new(output.row(), 0));
270 let expanded = output.column() as usize;
271 let (collapsed, expanded_char_column, to_next_stop) =
272 Self::collapse_tabs(chars, expanded, bias, self.tab_size);
273 (
274 SuggestionPoint::new(output.row(), collapsed as u32),
275 expanded_char_column,
276 to_next_stop,
277 )
278 }
279
280 pub fn make_tab_point(&self, point: Point, bias: Bias) -> TabPoint {
281 let fold_point = self
282 .suggestion_snapshot
283 .fold_snapshot
284 .to_fold_point(point, bias);
285 let suggestion_point = self.suggestion_snapshot.to_suggestion_point(fold_point);
286 self.to_tab_point(suggestion_point)
287 }
288
289 pub fn to_point(&self, point: TabPoint, bias: Bias) -> Point {
290 let suggestion_point = self.to_suggestion_point(point, bias).0;
291 let fold_point = self.suggestion_snapshot.to_fold_point(suggestion_point);
292 fold_point.to_buffer_point(&self.suggestion_snapshot.fold_snapshot)
293 }
294
295 pub fn expand_tabs(
296 chars: impl Iterator<Item = char>,
297 column: usize,
298 tab_size: NonZeroU32,
299 ) -> usize {
300 let mut expanded_chars = 0;
301 let mut expanded_bytes = 0;
302 let mut collapsed_bytes = 0;
303 for c in chars {
304 if collapsed_bytes == column {
305 break;
306 }
307 if c == '\t' {
308 let tab_size = tab_size.get() as usize;
309 let tab_len = tab_size - expanded_chars % tab_size;
310 expanded_bytes += tab_len;
311 expanded_chars += tab_len;
312 } else {
313 expanded_bytes += c.len_utf8();
314 expanded_chars += 1;
315 }
316 collapsed_bytes += c.len_utf8();
317 }
318 expanded_bytes
319 }
320
321 fn collapse_tabs(
322 chars: impl Iterator<Item = char>,
323 column: usize,
324 bias: Bias,
325 tab_size: NonZeroU32,
326 ) -> (usize, usize, usize) {
327 let mut expanded_bytes = 0;
328 let mut expanded_chars = 0;
329 let mut collapsed_bytes = 0;
330 for c in chars {
331 if expanded_bytes >= column {
332 break;
333 }
334
335 if c == '\t' {
336 let tab_size = tab_size.get() as usize;
337 let tab_len = tab_size - (expanded_chars % tab_size);
338 expanded_chars += tab_len;
339 expanded_bytes += tab_len;
340 if expanded_bytes > column {
341 expanded_chars -= expanded_bytes - column;
342 return match bias {
343 Bias::Left => (collapsed_bytes, expanded_chars, expanded_bytes - column),
344 Bias::Right => (collapsed_bytes + 1, expanded_chars, 0),
345 };
346 }
347 } else {
348 expanded_chars += 1;
349 expanded_bytes += c.len_utf8();
350 }
351
352 if expanded_bytes > column && matches!(bias, Bias::Left) {
353 expanded_chars -= 1;
354 break;
355 }
356
357 collapsed_bytes += c.len_utf8();
358 }
359 (collapsed_bytes, expanded_chars, 0)
360 }
361}
362
363#[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialOrd, PartialEq)]
364pub struct TabPoint(pub Point);
365
366impl TabPoint {
367 pub fn new(row: u32, column: u32) -> Self {
368 Self(Point::new(row, column))
369 }
370
371 pub fn zero() -> Self {
372 Self::new(0, 0)
373 }
374
375 pub fn row(self) -> u32 {
376 self.0.row
377 }
378
379 pub fn column(self) -> u32 {
380 self.0.column
381 }
382}
383
384impl From<Point> for TabPoint {
385 fn from(point: Point) -> Self {
386 Self(point)
387 }
388}
389
390pub type TabEdit = text::Edit<TabPoint>;
391
392#[derive(Clone, Debug, Default, Eq, PartialEq)]
393pub struct TextSummary {
394 pub lines: Point,
395 pub first_line_chars: u32,
396 pub last_line_chars: u32,
397 pub longest_row: u32,
398 pub longest_row_chars: u32,
399}
400
401impl<'a> From<&'a str> for TextSummary {
402 fn from(text: &'a str) -> Self {
403 let sum = text::TextSummary::from(text);
404
405 TextSummary {
406 lines: sum.lines,
407 first_line_chars: sum.first_line_chars,
408 last_line_chars: sum.last_line_chars,
409 longest_row: sum.longest_row,
410 longest_row_chars: sum.longest_row_chars,
411 }
412 }
413}
414
415impl<'a> std::ops::AddAssign<&'a Self> for TextSummary {
416 fn add_assign(&mut self, other: &'a Self) {
417 let joined_chars = self.last_line_chars + other.first_line_chars;
418 if joined_chars > self.longest_row_chars {
419 self.longest_row = self.lines.row;
420 self.longest_row_chars = joined_chars;
421 }
422 if other.longest_row_chars > self.longest_row_chars {
423 self.longest_row = self.lines.row + other.longest_row;
424 self.longest_row_chars = other.longest_row_chars;
425 }
426
427 if self.lines.row == 0 {
428 self.first_line_chars += other.first_line_chars;
429 }
430
431 if other.lines.row == 0 {
432 self.last_line_chars += other.first_line_chars;
433 } else {
434 self.last_line_chars = other.last_line_chars;
435 }
436
437 self.lines += &other.lines;
438 }
439}
440
441// Handles a tab width <= 16
442const SPACES: &str = " ";
443
444pub struct TabChunks<'a> {
445 suggestion_chunks: SuggestionChunks<'a>,
446 chunk: Chunk<'a>,
447 column: usize,
448 output_position: Point,
449 max_output_position: Point,
450 tab_size: NonZeroU32,
451 skip_leading_tab: bool,
452}
453
454impl<'a> Iterator for TabChunks<'a> {
455 type Item = Chunk<'a>;
456
457 fn next(&mut self) -> Option<Self::Item> {
458 if self.chunk.text.is_empty() {
459 if let Some(chunk) = self.suggestion_chunks.next() {
460 self.chunk = chunk;
461 if self.skip_leading_tab {
462 self.chunk.text = &self.chunk.text[1..];
463 self.skip_leading_tab = false;
464 }
465 } else {
466 return None;
467 }
468 }
469
470 for (ix, c) in self.chunk.text.char_indices() {
471 match c {
472 '\t' => {
473 if ix > 0 {
474 let (prefix, suffix) = self.chunk.text.split_at(ix);
475 self.chunk.text = suffix;
476 return Some(Chunk {
477 text: prefix,
478 ..self.chunk
479 });
480 } else {
481 self.chunk.text = &self.chunk.text[1..];
482 let tab_size = self.tab_size.get() as u32;
483 let mut len = tab_size - self.column as u32 % tab_size;
484 let next_output_position = cmp::min(
485 self.output_position + Point::new(0, len),
486 self.max_output_position,
487 );
488 len = next_output_position.column - self.output_position.column;
489 self.column += len as usize;
490 self.output_position = next_output_position;
491 return Some(Chunk {
492 text: &SPACES[0..len as usize],
493 ..self.chunk
494 });
495 }
496 }
497 '\n' => {
498 self.column = 0;
499 self.output_position += Point::new(1, 0);
500 }
501 _ => {
502 self.column += 1;
503 self.output_position.column += c.len_utf8() as u32;
504 }
505 }
506 }
507
508 Some(mem::take(&mut self.chunk))
509 }
510}
511
512#[cfg(test)]
513mod tests {
514 use super::*;
515 use crate::{
516 display_map::{fold_map::FoldMap, suggestion_map::SuggestionMap},
517 MultiBuffer,
518 };
519 use rand::{prelude::StdRng, Rng};
520
521 #[test]
522 fn test_expand_tabs() {
523 assert_eq!(
524 TabSnapshot::expand_tabs("\t".chars(), 0, 4.try_into().unwrap()),
525 0
526 );
527 assert_eq!(
528 TabSnapshot::expand_tabs("\t".chars(), 1, 4.try_into().unwrap()),
529 4
530 );
531 assert_eq!(
532 TabSnapshot::expand_tabs("\ta".chars(), 2, 4.try_into().unwrap()),
533 5
534 );
535 }
536
537 #[gpui::test(iterations = 100)]
538 fn test_random_tabs(cx: &mut gpui::MutableAppContext, mut rng: StdRng) {
539 let tab_size = NonZeroU32::new(rng.gen_range(1..=4)).unwrap();
540 let len = rng.gen_range(0..30);
541 let buffer = if rng.gen() {
542 let text = util::RandomCharIter::new(&mut rng)
543 .take(len)
544 .collect::<String>();
545 MultiBuffer::build_simple(&text, cx)
546 } else {
547 MultiBuffer::build_random(&mut rng, cx)
548 };
549 let buffer_snapshot = buffer.read(cx).snapshot(cx);
550 log::info!("Buffer text: {:?}", buffer_snapshot.text());
551
552 let (mut fold_map, _) = FoldMap::new(buffer_snapshot.clone());
553 fold_map.randomly_mutate(&mut rng);
554 let (fold_snapshot, _) = fold_map.read(buffer_snapshot, vec![]);
555 log::info!("FoldMap text: {:?}", fold_snapshot.text());
556 let (suggestion_map, _) = SuggestionMap::new(fold_snapshot);
557 let (suggestion_snapshot, _) = suggestion_map.randomly_mutate(&mut rng);
558 log::info!("SuggestionMap text: {:?}", suggestion_snapshot.text());
559
560 let (_, tabs_snapshot) = TabMap::new(suggestion_snapshot.clone(), tab_size);
561 let text = text::Rope::from(tabs_snapshot.text().as_str());
562 log::info!(
563 "TabMap text (tab size: {}): {:?}",
564 tab_size,
565 tabs_snapshot.text(),
566 );
567
568 for _ in 0..5 {
569 let end_row = rng.gen_range(0..=text.max_point().row);
570 let end_column = rng.gen_range(0..=text.line_len(end_row));
571 let mut end = TabPoint(text.clip_point(Point::new(end_row, end_column), Bias::Right));
572 let start_row = rng.gen_range(0..=text.max_point().row);
573 let start_column = rng.gen_range(0..=text.line_len(start_row));
574 let mut start =
575 TabPoint(text.clip_point(Point::new(start_row, start_column), Bias::Left));
576 if start > end {
577 mem::swap(&mut start, &mut end);
578 }
579
580 let expected_text = text
581 .chunks_in_range(text.point_to_offset(start.0)..text.point_to_offset(end.0))
582 .collect::<String>();
583 let expected_summary = TextSummary::from(expected_text.as_str());
584 assert_eq!(
585 expected_text,
586 tabs_snapshot
587 .chunks(start..end, false, None, None)
588 .map(|c| c.text)
589 .collect::<String>(),
590 "chunks({:?}..{:?})",
591 start,
592 end
593 );
594
595 let mut actual_summary = tabs_snapshot.text_summary_for_range(start..end);
596 if tab_size.get() > 1 && suggestion_snapshot.text().contains('\t') {
597 actual_summary.longest_row = expected_summary.longest_row;
598 actual_summary.longest_row_chars = expected_summary.longest_row_chars;
599 }
600 assert_eq!(actual_summary, expected_summary);
601 }
602
603 for row in 0..=text.max_point().row {
604 assert_eq!(tabs_snapshot.line_len(row), text.line_len(row));
605 }
606 }
607}