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