tab_map.rs

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