tab_map.rs

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