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