1use super::fold_map::{self, FoldEdit, FoldPoint, FoldSnapshot};
2use crate::MultiBufferSnapshot;
3use language::{rope, Chunk};
4use parking_lot::Mutex;
5use std::{cmp, mem, ops::Range};
6use sum_tree::Bias;
7use text::Point;
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, false)
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, false)
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, false)
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>(&'a self, range: Range<TabPoint>, language_aware: bool) -> TabChunks<'a> {
147 let (input_start, expanded_char_column, to_next_stop) =
148 self.to_fold_point(range.start, Bias::Left);
149 let input_start = input_start.to_offset(&self.fold_snapshot);
150 let input_end = self
151 .to_fold_point(range.end, Bias::Right)
152 .0
153 .to_offset(&self.fold_snapshot);
154 let to_next_stop = if range.start.0 + Point::new(0, to_next_stop as u32) > range.end.0 {
155 (range.end.column() - range.start.column()) as usize
156 } else {
157 to_next_stop
158 };
159
160 TabChunks {
161 fold_chunks: self
162 .fold_snapshot
163 .chunks(input_start..input_end, language_aware),
164 column: expanded_char_column,
165 output_position: range.start.0,
166 max_output_position: range.end.0,
167 tab_size: self.tab_size,
168 chunk: Chunk {
169 text: &SPACES[0..to_next_stop],
170 ..Default::default()
171 },
172 skip_leading_tab: to_next_stop > 0,
173 }
174 }
175
176 pub fn buffer_rows(&self, row: u32) -> fold_map::FoldBufferRows {
177 self.fold_snapshot.buffer_rows(row)
178 }
179
180 #[cfg(test)]
181 pub fn text(&self) -> String {
182 self.chunks(TabPoint::zero()..self.max_point(), false)
183 .map(|chunk| chunk.text)
184 .collect()
185 }
186
187 pub fn max_point(&self) -> TabPoint {
188 self.to_tab_point(self.fold_snapshot.max_point())
189 }
190
191 pub fn clip_point(&self, point: TabPoint, bias: Bias) -> TabPoint {
192 self.to_tab_point(
193 self.fold_snapshot
194 .clip_point(self.to_fold_point(point, bias).0, bias),
195 )
196 }
197
198 pub fn to_tab_point(&self, input: FoldPoint) -> TabPoint {
199 let chars = self.fold_snapshot.chars_at(FoldPoint::new(input.row(), 0));
200 let expanded = Self::expand_tabs(chars, input.column() as usize, self.tab_size);
201 TabPoint::new(input.row(), expanded as u32)
202 }
203
204 pub fn to_fold_point(&self, output: TabPoint, bias: Bias) -> (FoldPoint, usize, usize) {
205 let chars = self.fold_snapshot.chars_at(FoldPoint::new(output.row(), 0));
206 let expanded = output.column() as usize;
207 let (collapsed, expanded_char_column, to_next_stop) =
208 Self::collapse_tabs(chars, expanded, bias, self.tab_size);
209 (
210 FoldPoint::new(output.row(), collapsed as u32),
211 expanded_char_column,
212 to_next_stop,
213 )
214 }
215
216 pub fn from_point(&self, point: Point, bias: Bias) -> TabPoint {
217 self.to_tab_point(self.fold_snapshot.to_fold_point(point, bias))
218 }
219
220 pub fn to_point(&self, point: TabPoint, bias: Bias) -> Point {
221 self.to_fold_point(point, bias)
222 .0
223 .to_buffer_point(&self.fold_snapshot)
224 }
225
226 fn expand_tabs(chars: impl Iterator<Item = char>, column: usize, tab_size: usize) -> usize {
227 let mut expanded_chars = 0;
228 let mut expanded_bytes = 0;
229 let mut collapsed_bytes = 0;
230 for c in chars {
231 if collapsed_bytes == column {
232 break;
233 }
234 if c == '\t' {
235 let tab_len = tab_size - expanded_chars % tab_size;
236 expanded_bytes += tab_len;
237 expanded_chars += tab_len;
238 } else {
239 expanded_bytes += c.len_utf8();
240 expanded_chars += 1;
241 }
242 collapsed_bytes += c.len_utf8();
243 }
244 expanded_bytes
245 }
246
247 fn collapse_tabs(
248 mut chars: impl Iterator<Item = char>,
249 column: usize,
250 bias: Bias,
251 tab_size: usize,
252 ) -> (usize, usize, usize) {
253 let mut expanded_bytes = 0;
254 let mut expanded_chars = 0;
255 let mut collapsed_bytes = 0;
256 while let Some(c) = chars.next() {
257 if expanded_bytes >= column {
258 break;
259 }
260
261 if c == '\t' {
262 let tab_len = tab_size - (expanded_chars % tab_size);
263 expanded_chars += tab_len;
264 expanded_bytes += tab_len;
265 if expanded_bytes > column {
266 expanded_chars -= expanded_bytes - column;
267 return match bias {
268 Bias::Left => (collapsed_bytes, expanded_chars, expanded_bytes - column),
269 Bias::Right => (collapsed_bytes + 1, expanded_chars, 0),
270 };
271 }
272 } else {
273 expanded_chars += 1;
274 expanded_bytes += c.len_utf8();
275 }
276
277 if expanded_bytes > column && matches!(bias, Bias::Left) {
278 expanded_chars -= 1;
279 break;
280 }
281
282 collapsed_bytes += c.len_utf8();
283 }
284 (collapsed_bytes, expanded_chars, 0)
285 }
286}
287
288#[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialOrd, PartialEq)]
289pub struct TabPoint(pub super::Point);
290
291impl TabPoint {
292 pub fn new(row: u32, column: u32) -> Self {
293 Self(super::Point::new(row, column))
294 }
295
296 pub fn zero() -> Self {
297 Self::new(0, 0)
298 }
299
300 pub fn row(self) -> u32 {
301 self.0.row
302 }
303
304 pub fn column(self) -> u32 {
305 self.0.column
306 }
307}
308
309impl From<super::Point> for TabPoint {
310 fn from(point: super::Point) -> Self {
311 Self(point)
312 }
313}
314
315pub type TabEdit = text::Edit<TabPoint>;
316
317#[derive(Clone, Debug, Default, Eq, PartialEq)]
318pub struct TextSummary {
319 pub lines: super::Point,
320 pub first_line_chars: u32,
321 pub last_line_chars: u32,
322 pub longest_row: u32,
323 pub longest_row_chars: u32,
324}
325
326impl<'a> From<&'a str> for TextSummary {
327 fn from(text: &'a str) -> Self {
328 let sum = rope::TextSummary::from(text);
329
330 TextSummary {
331 lines: sum.lines,
332 first_line_chars: sum.first_line_chars,
333 last_line_chars: sum.last_line_chars,
334 longest_row: sum.longest_row,
335 longest_row_chars: sum.longest_row_chars,
336 }
337 }
338}
339
340impl<'a> std::ops::AddAssign<&'a Self> for TextSummary {
341 fn add_assign(&mut self, other: &'a Self) {
342 let joined_chars = self.last_line_chars + other.first_line_chars;
343 if joined_chars > self.longest_row_chars {
344 self.longest_row = self.lines.row;
345 self.longest_row_chars = joined_chars;
346 }
347 if other.longest_row_chars > self.longest_row_chars {
348 self.longest_row = self.lines.row + other.longest_row;
349 self.longest_row_chars = other.longest_row_chars;
350 }
351
352 if self.lines.row == 0 {
353 self.first_line_chars += other.first_line_chars;
354 }
355
356 if other.lines.row == 0 {
357 self.last_line_chars += other.first_line_chars;
358 } else {
359 self.last_line_chars = other.last_line_chars;
360 }
361
362 self.lines += &other.lines;
363 }
364}
365
366// Handles a tab width <= 16
367const SPACES: &'static str = " ";
368
369pub struct TabChunks<'a> {
370 fold_chunks: fold_map::FoldChunks<'a>,
371 chunk: Chunk<'a>,
372 column: usize,
373 output_position: Point,
374 max_output_position: Point,
375 tab_size: usize,
376 skip_leading_tab: bool,
377}
378
379impl<'a> Iterator for TabChunks<'a> {
380 type Item = Chunk<'a>;
381
382 fn next(&mut self) -> Option<Self::Item> {
383 if self.chunk.text.is_empty() {
384 if let Some(chunk) = self.fold_chunks.next() {
385 self.chunk = chunk;
386 if self.skip_leading_tab {
387 self.chunk.text = &self.chunk.text[1..];
388 self.skip_leading_tab = false;
389 }
390 } else {
391 return None;
392 }
393 }
394
395 for (ix, c) in self.chunk.text.char_indices() {
396 match c {
397 '\t' => {
398 if ix > 0 {
399 let (prefix, suffix) = self.chunk.text.split_at(ix);
400 self.chunk.text = suffix;
401 return Some(Chunk {
402 text: prefix,
403 ..self.chunk
404 });
405 } else {
406 self.chunk.text = &self.chunk.text[1..];
407 let mut len = self.tab_size - self.column % self.tab_size;
408 let next_output_position = cmp::min(
409 self.output_position + Point::new(0, len as u32),
410 self.max_output_position,
411 );
412 len = (next_output_position.column - self.output_position.column) as usize;
413 self.column += len;
414 self.output_position = next_output_position;
415 return Some(Chunk {
416 text: &SPACES[0..len],
417 ..self.chunk
418 });
419 }
420 }
421 '\n' => {
422 self.column = 0;
423 self.output_position += Point::new(1, 0);
424 }
425 _ => {
426 self.column += 1;
427 self.output_position.column += c.len_utf8() as u32;
428 }
429 }
430 }
431
432 Some(mem::take(&mut self.chunk))
433 }
434}
435
436#[cfg(test)]
437mod tests {
438 use super::*;
439 use crate::{display_map::fold_map::FoldMap, MultiBuffer};
440 use rand::{prelude::StdRng, Rng};
441 use text::{RandomCharIter, Rope};
442
443 #[test]
444 fn test_expand_tabs() {
445 assert_eq!(TabSnapshot::expand_tabs("\t".chars(), 0, 4), 0);
446 assert_eq!(TabSnapshot::expand_tabs("\t".chars(), 1, 4), 4);
447 assert_eq!(TabSnapshot::expand_tabs("\ta".chars(), 2, 4), 5);
448 }
449
450 #[gpui::test(iterations = 100)]
451 fn test_random_tabs(cx: &mut gpui::MutableAppContext, mut rng: StdRng) {
452 let tab_size = rng.gen_range(1..=4);
453 let len = rng.gen_range(0..30);
454 let buffer = if rng.gen() {
455 let text = RandomCharIter::new(&mut rng).take(len).collect::<String>();
456 MultiBuffer::build_simple(&text, cx)
457 } else {
458 MultiBuffer::build_random(&mut rng, cx)
459 };
460 let buffer_snapshot = buffer.read(cx).snapshot(cx);
461 log::info!("Buffer text: {:?}", buffer_snapshot.text());
462
463 let (mut fold_map, _) = FoldMap::new(buffer_snapshot.clone());
464 fold_map.randomly_mutate(&mut rng);
465 let (folds_snapshot, _) = fold_map.read(buffer_snapshot.clone(), vec![]);
466 log::info!("FoldMap text: {:?}", folds_snapshot.text());
467
468 let (_, tabs_snapshot) = TabMap::new(folds_snapshot.clone(), tab_size);
469 let text = Rope::from(tabs_snapshot.text().as_str());
470 log::info!(
471 "TabMap text (tab size: {}): {:?}",
472 tab_size,
473 tabs_snapshot.text(),
474 );
475
476 for _ in 0..5 {
477 let end_row = rng.gen_range(0..=text.max_point().row);
478 let end_column = rng.gen_range(0..=text.line_len(end_row));
479 let mut end = TabPoint(text.clip_point(Point::new(end_row, end_column), Bias::Right));
480 let start_row = rng.gen_range(0..=text.max_point().row);
481 let start_column = rng.gen_range(0..=text.line_len(start_row));
482 let mut start =
483 TabPoint(text.clip_point(Point::new(start_row, start_column), Bias::Left));
484 if start > end {
485 mem::swap(&mut start, &mut end);
486 }
487
488 let expected_text = text
489 .chunks_in_range(text.point_to_offset(start.0)..text.point_to_offset(end.0))
490 .collect::<String>();
491 let expected_summary = TextSummary::from(expected_text.as_str());
492 assert_eq!(
493 expected_text,
494 tabs_snapshot
495 .chunks(start..end, false)
496 .map(|c| c.text)
497 .collect::<String>(),
498 "chunks({:?}..{:?})",
499 start,
500 end
501 );
502
503 let mut actual_summary = tabs_snapshot.text_summary_for_range(start..end);
504 if tab_size > 1 && folds_snapshot.text().contains('\t') {
505 actual_summary.longest_row = expected_summary.longest_row;
506 actual_summary.longest_row_chars = expected_summary.longest_row_chars;
507 }
508
509 assert_eq!(actual_summary, expected_summary,);
510 }
511 }
512}