1use futures::channel::oneshot;
2use git2::{DiffLineType as GitDiffLineType, DiffOptions as GitOptions, Patch as GitPatch};
3use gpui::{App, AppContext as _, AsyncApp, Context, Entity, EventEmitter, Task};
4use language::{Language, LanguageRegistry};
5use rope::Rope;
6use std::{cmp::Ordering, future::Future, iter, mem, ops::Range, sync::Arc};
7use sum_tree::SumTree;
8use text::{Anchor, Bias, BufferId, OffsetRangeExt, Point, ToOffset as _};
9use util::ResultExt;
10
11pub struct BufferDiff {
12 pub buffer_id: BufferId,
13 inner: BufferDiffInner,
14 secondary_diff: Option<Entity<BufferDiff>>,
15}
16
17#[derive(Clone, Debug)]
18pub struct BufferDiffSnapshot {
19 inner: BufferDiffInner,
20 secondary_diff: Option<Box<BufferDiffSnapshot>>,
21}
22
23#[derive(Clone)]
24struct BufferDiffInner {
25 hunks: SumTree<InternalDiffHunk>,
26 pending_hunks: SumTree<PendingHunk>,
27 base_text: language::BufferSnapshot,
28 base_text_exists: bool,
29}
30
31#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
32pub struct DiffHunkStatus {
33 pub kind: DiffHunkStatusKind,
34 pub secondary: DiffHunkSecondaryStatus,
35}
36
37#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
38pub enum DiffHunkStatusKind {
39 Added,
40 Modified,
41 Deleted,
42}
43
44#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
45pub enum DiffHunkSecondaryStatus {
46 HasSecondaryHunk,
47 OverlapsWithSecondaryHunk,
48 NoSecondaryHunk,
49 SecondaryHunkAdditionPending,
50 SecondaryHunkRemovalPending,
51}
52
53/// A diff hunk resolved to rows in the buffer.
54#[derive(Debug, Clone, PartialEq, Eq)]
55pub struct DiffHunk {
56 /// The buffer range as points.
57 pub range: Range<Point>,
58 /// The range in the buffer to which this hunk corresponds.
59 pub buffer_range: Range<Anchor>,
60 /// The range in the buffer's diff base text to which this hunk corresponds.
61 pub diff_base_byte_range: Range<usize>,
62 pub secondary_status: DiffHunkSecondaryStatus,
63}
64
65/// We store [`InternalDiffHunk`]s internally so we don't need to store the additional row range.
66#[derive(Debug, Clone, PartialEq, Eq)]
67struct InternalDiffHunk {
68 buffer_range: Range<Anchor>,
69 diff_base_byte_range: Range<usize>,
70}
71
72#[derive(Debug, Clone, PartialEq, Eq)]
73struct PendingHunk {
74 buffer_range: Range<Anchor>,
75 diff_base_byte_range: Range<usize>,
76 buffer_version: clock::Global,
77 new_status: DiffHunkSecondaryStatus,
78}
79
80#[derive(Debug, Default, Clone)]
81pub struct DiffHunkSummary {
82 buffer_range: Range<Anchor>,
83}
84
85impl sum_tree::Item for InternalDiffHunk {
86 type Summary = DiffHunkSummary;
87
88 fn summary(&self, _cx: &text::BufferSnapshot) -> Self::Summary {
89 DiffHunkSummary {
90 buffer_range: self.buffer_range.clone(),
91 }
92 }
93}
94
95impl sum_tree::Item for PendingHunk {
96 type Summary = DiffHunkSummary;
97
98 fn summary(&self, _cx: &text::BufferSnapshot) -> Self::Summary {
99 DiffHunkSummary {
100 buffer_range: self.buffer_range.clone(),
101 }
102 }
103}
104
105impl sum_tree::Summary for DiffHunkSummary {
106 type Context = text::BufferSnapshot;
107
108 fn zero(_cx: &Self::Context) -> Self {
109 Default::default()
110 }
111
112 fn add_summary(&mut self, other: &Self, buffer: &Self::Context) {
113 self.buffer_range.start = self
114 .buffer_range
115 .start
116 .min(&other.buffer_range.start, buffer);
117 self.buffer_range.end = self.buffer_range.end.max(&other.buffer_range.end, buffer);
118 }
119}
120
121impl sum_tree::SeekTarget<'_, DiffHunkSummary, DiffHunkSummary> for Anchor {
122 fn cmp(&self, cursor_location: &DiffHunkSummary, buffer: &text::BufferSnapshot) -> Ordering {
123 if self
124 .cmp(&cursor_location.buffer_range.start, buffer)
125 .is_lt()
126 {
127 Ordering::Less
128 } else if self.cmp(&cursor_location.buffer_range.end, buffer).is_gt() {
129 Ordering::Greater
130 } else {
131 Ordering::Equal
132 }
133 }
134}
135
136impl std::fmt::Debug for BufferDiffInner {
137 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
138 f.debug_struct("BufferDiffSnapshot")
139 .field("hunks", &self.hunks)
140 .finish()
141 }
142}
143
144impl BufferDiffSnapshot {
145 pub fn is_empty(&self) -> bool {
146 self.inner.hunks.is_empty()
147 }
148
149 pub fn secondary_diff(&self) -> Option<&BufferDiffSnapshot> {
150 self.secondary_diff.as_deref()
151 }
152
153 pub fn hunks_intersecting_range<'a>(
154 &'a self,
155 range: Range<Anchor>,
156 buffer: &'a text::BufferSnapshot,
157 ) -> impl 'a + Iterator<Item = DiffHunk> {
158 let unstaged_counterpart = self.secondary_diff.as_ref().map(|diff| &diff.inner);
159 self.inner
160 .hunks_intersecting_range(range, buffer, unstaged_counterpart)
161 }
162
163 pub fn hunks_intersecting_range_rev<'a>(
164 &'a self,
165 range: Range<Anchor>,
166 buffer: &'a text::BufferSnapshot,
167 ) -> impl 'a + Iterator<Item = DiffHunk> {
168 self.inner.hunks_intersecting_range_rev(range, buffer)
169 }
170
171 pub fn base_text(&self) -> &language::BufferSnapshot {
172 &self.inner.base_text
173 }
174
175 pub fn base_texts_eq(&self, other: &Self) -> bool {
176 if self.inner.base_text_exists != other.inner.base_text_exists {
177 return false;
178 }
179 let left = &self.inner.base_text;
180 let right = &other.inner.base_text;
181 let (old_id, old_empty) = (left.remote_id(), left.is_empty());
182 let (new_id, new_empty) = (right.remote_id(), right.is_empty());
183 new_id == old_id || (new_empty && old_empty)
184 }
185}
186
187impl BufferDiffInner {
188 /// Returns the new index text and new pending hunks.
189 fn stage_or_unstage_hunks_impl(
190 &self,
191 unstaged_diff: &Self,
192 stage: bool,
193 hunks: &[DiffHunk],
194 buffer: &text::BufferSnapshot,
195 file_exists: bool,
196 ) -> (Option<Rope>, SumTree<PendingHunk>) {
197 let head_text = self
198 .base_text_exists
199 .then(|| self.base_text.as_rope().clone());
200 let index_text = unstaged_diff
201 .base_text_exists
202 .then(|| unstaged_diff.base_text.as_rope().clone());
203
204 // If the file doesn't exist in either HEAD or the index, then the
205 // entire file must be either created or deleted in the index.
206 let (index_text, head_text) = match (index_text, head_text) {
207 (Some(index_text), Some(head_text)) if file_exists || !stage => (index_text, head_text),
208 (index_text, head_text) => {
209 let (rope, new_status) = if stage {
210 log::debug!("stage all");
211 (
212 file_exists.then(|| buffer.as_rope().clone()),
213 DiffHunkSecondaryStatus::SecondaryHunkRemovalPending,
214 )
215 } else {
216 log::debug!("unstage all");
217 (
218 head_text,
219 DiffHunkSecondaryStatus::SecondaryHunkAdditionPending,
220 )
221 };
222
223 let hunk = PendingHunk {
224 buffer_range: Anchor::MIN..Anchor::MAX,
225 diff_base_byte_range: 0..index_text.map_or(0, |rope| rope.len()),
226 buffer_version: buffer.version().clone(),
227 new_status,
228 };
229 let tree = SumTree::from_item(hunk, buffer);
230 return (rope, tree);
231 }
232 };
233
234 let mut pending_hunks = SumTree::new(buffer);
235 let mut old_pending_hunks = unstaged_diff
236 .pending_hunks
237 .cursor::<DiffHunkSummary>(buffer);
238
239 // first, merge new hunks into pending_hunks
240 for DiffHunk {
241 buffer_range,
242 diff_base_byte_range,
243 secondary_status,
244 ..
245 } in hunks.iter().cloned()
246 {
247 let preceding_pending_hunks =
248 old_pending_hunks.slice(&buffer_range.start, Bias::Left, buffer);
249 pending_hunks.append(preceding_pending_hunks, buffer);
250
251 // Skip all overlapping or adjacent old pending hunks
252 while old_pending_hunks.item().is_some_and(|old_hunk| {
253 old_hunk
254 .buffer_range
255 .start
256 .cmp(&buffer_range.end, buffer)
257 .is_le()
258 }) {
259 old_pending_hunks.next(buffer);
260 }
261
262 if (stage && secondary_status == DiffHunkSecondaryStatus::NoSecondaryHunk)
263 || (!stage && secondary_status == DiffHunkSecondaryStatus::HasSecondaryHunk)
264 {
265 continue;
266 }
267
268 pending_hunks.push(
269 PendingHunk {
270 buffer_range,
271 diff_base_byte_range,
272 buffer_version: buffer.version().clone(),
273 new_status: if stage {
274 DiffHunkSecondaryStatus::SecondaryHunkRemovalPending
275 } else {
276 DiffHunkSecondaryStatus::SecondaryHunkAdditionPending
277 },
278 },
279 buffer,
280 );
281 }
282 // append the remainder
283 pending_hunks.append(old_pending_hunks.suffix(buffer), buffer);
284
285 let mut unstaged_hunk_cursor = unstaged_diff.hunks.cursor::<DiffHunkSummary>(buffer);
286 unstaged_hunk_cursor.next(buffer);
287
288 // then, iterate over all pending hunks (both new ones and the existing ones) and compute the edits
289 let mut prev_unstaged_hunk_buffer_end = 0;
290 let mut prev_unstaged_hunk_base_text_end = 0;
291 let mut edits = Vec::<(Range<usize>, String)>::new();
292 let mut pending_hunks_iter = pending_hunks.iter().cloned().peekable();
293 while let Some(PendingHunk {
294 buffer_range,
295 diff_base_byte_range,
296 ..
297 }) = pending_hunks_iter.next()
298 {
299 // Advance unstaged_hunk_cursor to skip unstaged hunks before current hunk
300 let skipped_unstaged =
301 unstaged_hunk_cursor.slice(&buffer_range.start, Bias::Left, buffer);
302
303 if let Some(unstaged_hunk) = skipped_unstaged.last() {
304 prev_unstaged_hunk_base_text_end = unstaged_hunk.diff_base_byte_range.end;
305 prev_unstaged_hunk_buffer_end = unstaged_hunk.buffer_range.end.to_offset(buffer);
306 }
307
308 // Find where this hunk is in the index if it doesn't overlap
309 let mut buffer_offset_range = buffer_range.to_offset(buffer);
310 let start_overshoot = buffer_offset_range.start - prev_unstaged_hunk_buffer_end;
311 let mut index_start = prev_unstaged_hunk_base_text_end + start_overshoot;
312
313 loop {
314 // Merge this hunk with any overlapping unstaged hunks.
315 if let Some(unstaged_hunk) = unstaged_hunk_cursor.item() {
316 let unstaged_hunk_offset_range = unstaged_hunk.buffer_range.to_offset(buffer);
317 if unstaged_hunk_offset_range.start <= buffer_offset_range.end {
318 prev_unstaged_hunk_base_text_end = unstaged_hunk.diff_base_byte_range.end;
319 prev_unstaged_hunk_buffer_end = unstaged_hunk_offset_range.end;
320
321 index_start = index_start.min(unstaged_hunk.diff_base_byte_range.start);
322 buffer_offset_range.start = buffer_offset_range
323 .start
324 .min(unstaged_hunk_offset_range.start);
325 buffer_offset_range.end =
326 buffer_offset_range.end.max(unstaged_hunk_offset_range.end);
327
328 unstaged_hunk_cursor.next(buffer);
329 continue;
330 }
331 }
332
333 // If any unstaged hunks were merged, then subsequent pending hunks may
334 // now overlap this hunk. Merge them.
335 if let Some(next_pending_hunk) = pending_hunks_iter.peek() {
336 let next_pending_hunk_offset_range =
337 next_pending_hunk.buffer_range.to_offset(buffer);
338 if next_pending_hunk_offset_range.start <= buffer_offset_range.end {
339 buffer_offset_range.end = next_pending_hunk_offset_range.end;
340 pending_hunks_iter.next();
341 continue;
342 }
343 }
344
345 break;
346 }
347
348 let end_overshoot = buffer_offset_range
349 .end
350 .saturating_sub(prev_unstaged_hunk_buffer_end);
351 let index_end = prev_unstaged_hunk_base_text_end + end_overshoot;
352 let index_byte_range = index_start..index_end;
353
354 let replacement_text = if stage {
355 log::debug!("stage hunk {:?}", buffer_offset_range);
356 buffer
357 .text_for_range(buffer_offset_range)
358 .collect::<String>()
359 } else {
360 log::debug!("unstage hunk {:?}", buffer_offset_range);
361 head_text
362 .chunks_in_range(diff_base_byte_range.clone())
363 .collect::<String>()
364 };
365
366 edits.push((index_byte_range, replacement_text));
367 }
368 drop(pending_hunks_iter);
369
370 #[cfg(debug_assertions)] // invariants: non-overlapping and sorted
371 {
372 for window in edits.windows(2) {
373 let (range_a, range_b) = (&window[0].0, &window[1].0);
374 debug_assert!(range_a.end < range_b.start);
375 }
376 }
377
378 let mut new_index_text = Rope::new();
379 let mut index_cursor = index_text.cursor(0);
380
381 for (old_range, replacement_text) in edits {
382 new_index_text.append(index_cursor.slice(old_range.start));
383 index_cursor.seek_forward(old_range.end);
384 new_index_text.push(&replacement_text);
385 }
386 new_index_text.append(index_cursor.suffix());
387 (Some(new_index_text), pending_hunks)
388 }
389
390 fn hunks_intersecting_range<'a>(
391 &'a self,
392 range: Range<Anchor>,
393 buffer: &'a text::BufferSnapshot,
394 secondary: Option<&'a Self>,
395 ) -> impl 'a + Iterator<Item = DiffHunk> {
396 let range = range.to_offset(buffer);
397
398 let mut cursor = self
399 .hunks
400 .filter::<_, DiffHunkSummary>(buffer, move |summary| {
401 let summary_range = summary.buffer_range.to_offset(buffer);
402 let before_start = summary_range.end < range.start;
403 let after_end = summary_range.start > range.end;
404 !before_start && !after_end
405 });
406
407 let anchor_iter = iter::from_fn(move || {
408 cursor.next(buffer);
409 cursor.item()
410 })
411 .flat_map(move |hunk| {
412 [
413 (
414 &hunk.buffer_range.start,
415 (hunk.buffer_range.start, hunk.diff_base_byte_range.start),
416 ),
417 (
418 &hunk.buffer_range.end,
419 (hunk.buffer_range.end, hunk.diff_base_byte_range.end),
420 ),
421 ]
422 });
423
424 let mut secondary_cursor = None;
425 let mut pending_hunks_cursor = None;
426 if let Some(secondary) = secondary.as_ref() {
427 let mut cursor = secondary.hunks.cursor::<DiffHunkSummary>(buffer);
428 cursor.next(buffer);
429 secondary_cursor = Some(cursor);
430 let mut cursor = secondary.pending_hunks.cursor::<DiffHunkSummary>(buffer);
431 cursor.next(buffer);
432 pending_hunks_cursor = Some(cursor);
433 }
434
435 let max_point = buffer.max_point();
436 let mut summaries = buffer.summaries_for_anchors_with_payload::<Point, _, _>(anchor_iter);
437 iter::from_fn(move || loop {
438 let (start_point, (start_anchor, start_base)) = summaries.next()?;
439 let (mut end_point, (mut end_anchor, end_base)) = summaries.next()?;
440
441 if !start_anchor.is_valid(buffer) {
442 continue;
443 }
444
445 if end_point.column > 0 && end_point < max_point {
446 end_point.row += 1;
447 end_point.column = 0;
448 end_anchor = buffer.anchor_before(end_point);
449 }
450
451 let mut secondary_status = DiffHunkSecondaryStatus::NoSecondaryHunk;
452
453 let mut has_pending = false;
454 if let Some(pending_cursor) = pending_hunks_cursor.as_mut() {
455 if start_anchor
456 .cmp(&pending_cursor.start().buffer_range.start, buffer)
457 .is_gt()
458 {
459 pending_cursor.seek_forward(&start_anchor, Bias::Left, buffer);
460 }
461
462 if let Some(pending_hunk) = pending_cursor.item() {
463 let mut pending_range = pending_hunk.buffer_range.to_point(buffer);
464 if pending_range.end.column > 0 {
465 pending_range.end.row += 1;
466 pending_range.end.column = 0;
467 }
468
469 if pending_range == (start_point..end_point) {
470 if !buffer.has_edits_since_in_range(
471 &pending_hunk.buffer_version,
472 start_anchor..end_anchor,
473 ) {
474 has_pending = true;
475 secondary_status = pending_hunk.new_status;
476 }
477 }
478 }
479 }
480
481 if let (Some(secondary_cursor), false) = (secondary_cursor.as_mut(), has_pending) {
482 if start_anchor
483 .cmp(&secondary_cursor.start().buffer_range.start, buffer)
484 .is_gt()
485 {
486 secondary_cursor.seek_forward(&start_anchor, Bias::Left, buffer);
487 }
488
489 if let Some(secondary_hunk) = secondary_cursor.item() {
490 let mut secondary_range = secondary_hunk.buffer_range.to_point(buffer);
491 if secondary_range.end.column > 0 {
492 secondary_range.end.row += 1;
493 secondary_range.end.column = 0;
494 }
495 if secondary_range.is_empty() && secondary_hunk.diff_base_byte_range.is_empty()
496 {
497 // ignore
498 } else if secondary_range == (start_point..end_point) {
499 secondary_status = DiffHunkSecondaryStatus::HasSecondaryHunk;
500 } else if secondary_range.start <= end_point {
501 secondary_status = DiffHunkSecondaryStatus::OverlapsWithSecondaryHunk;
502 }
503 }
504 }
505
506 return Some(DiffHunk {
507 range: start_point..end_point,
508 diff_base_byte_range: start_base..end_base,
509 buffer_range: start_anchor..end_anchor,
510 secondary_status,
511 });
512 })
513 }
514
515 fn hunks_intersecting_range_rev<'a>(
516 &'a self,
517 range: Range<Anchor>,
518 buffer: &'a text::BufferSnapshot,
519 ) -> impl 'a + Iterator<Item = DiffHunk> {
520 let mut cursor = self
521 .hunks
522 .filter::<_, DiffHunkSummary>(buffer, move |summary| {
523 let before_start = summary.buffer_range.end.cmp(&range.start, buffer).is_lt();
524 let after_end = summary.buffer_range.start.cmp(&range.end, buffer).is_gt();
525 !before_start && !after_end
526 });
527
528 iter::from_fn(move || {
529 cursor.prev(buffer);
530
531 let hunk = cursor.item()?;
532 let range = hunk.buffer_range.to_point(buffer);
533
534 Some(DiffHunk {
535 range,
536 diff_base_byte_range: hunk.diff_base_byte_range.clone(),
537 buffer_range: hunk.buffer_range.clone(),
538 // The secondary status is not used by callers of this method.
539 secondary_status: DiffHunkSecondaryStatus::NoSecondaryHunk,
540 })
541 })
542 }
543
544 fn compare(&self, old: &Self, new_snapshot: &text::BufferSnapshot) -> Option<Range<Anchor>> {
545 let mut new_cursor = self.hunks.cursor::<()>(new_snapshot);
546 let mut old_cursor = old.hunks.cursor::<()>(new_snapshot);
547 old_cursor.next(new_snapshot);
548 new_cursor.next(new_snapshot);
549 let mut start = None;
550 let mut end = None;
551
552 loop {
553 match (new_cursor.item(), old_cursor.item()) {
554 (Some(new_hunk), Some(old_hunk)) => {
555 match new_hunk
556 .buffer_range
557 .start
558 .cmp(&old_hunk.buffer_range.start, new_snapshot)
559 {
560 Ordering::Less => {
561 start.get_or_insert(new_hunk.buffer_range.start);
562 end.replace(new_hunk.buffer_range.end);
563 new_cursor.next(new_snapshot);
564 }
565 Ordering::Equal => {
566 if new_hunk != old_hunk {
567 start.get_or_insert(new_hunk.buffer_range.start);
568 if old_hunk
569 .buffer_range
570 .end
571 .cmp(&new_hunk.buffer_range.end, new_snapshot)
572 .is_ge()
573 {
574 end.replace(old_hunk.buffer_range.end);
575 } else {
576 end.replace(new_hunk.buffer_range.end);
577 }
578 }
579
580 new_cursor.next(new_snapshot);
581 old_cursor.next(new_snapshot);
582 }
583 Ordering::Greater => {
584 start.get_or_insert(old_hunk.buffer_range.start);
585 end.replace(old_hunk.buffer_range.end);
586 old_cursor.next(new_snapshot);
587 }
588 }
589 }
590 (Some(new_hunk), None) => {
591 start.get_or_insert(new_hunk.buffer_range.start);
592 end.replace(new_hunk.buffer_range.end);
593 new_cursor.next(new_snapshot);
594 }
595 (None, Some(old_hunk)) => {
596 start.get_or_insert(old_hunk.buffer_range.start);
597 end.replace(old_hunk.buffer_range.end);
598 old_cursor.next(new_snapshot);
599 }
600 (None, None) => break,
601 }
602 }
603
604 start.zip(end).map(|(start, end)| start..end)
605 }
606}
607
608fn compute_hunks(
609 diff_base: Option<(Arc<String>, Rope)>,
610 buffer: text::BufferSnapshot,
611) -> SumTree<InternalDiffHunk> {
612 let mut tree = SumTree::new(&buffer);
613
614 if let Some((diff_base, diff_base_rope)) = diff_base {
615 let buffer_text = buffer.as_rope().to_string();
616
617 let mut options = GitOptions::default();
618 options.context_lines(0);
619 let patch = GitPatch::from_buffers(
620 diff_base.as_bytes(),
621 None,
622 buffer_text.as_bytes(),
623 None,
624 Some(&mut options),
625 )
626 .log_err();
627
628 // A common case in Zed is that the empty buffer is represented as just a newline,
629 // but if we just compute a naive diff you get a "preserved" line in the middle,
630 // which is a bit odd.
631 if buffer_text == "\n" && diff_base.ends_with("\n") && diff_base.len() > 1 {
632 tree.push(
633 InternalDiffHunk {
634 buffer_range: buffer.anchor_before(0)..buffer.anchor_before(0),
635 diff_base_byte_range: 0..diff_base.len() - 1,
636 },
637 &buffer,
638 );
639 return tree;
640 }
641
642 if let Some(patch) = patch {
643 let mut divergence = 0;
644 for hunk_index in 0..patch.num_hunks() {
645 let hunk = process_patch_hunk(
646 &patch,
647 hunk_index,
648 &diff_base_rope,
649 &buffer,
650 &mut divergence,
651 );
652 tree.push(hunk, &buffer);
653 }
654 }
655 } else {
656 tree.push(
657 InternalDiffHunk {
658 buffer_range: Anchor::MIN..Anchor::MAX,
659 diff_base_byte_range: 0..0,
660 },
661 &buffer,
662 );
663 }
664
665 tree
666}
667
668fn process_patch_hunk(
669 patch: &GitPatch<'_>,
670 hunk_index: usize,
671 diff_base: &Rope,
672 buffer: &text::BufferSnapshot,
673 buffer_row_divergence: &mut i64,
674) -> InternalDiffHunk {
675 let line_item_count = patch.num_lines_in_hunk(hunk_index).unwrap();
676 assert!(line_item_count > 0);
677
678 let mut first_deletion_buffer_row: Option<u32> = None;
679 let mut buffer_row_range: Option<Range<u32>> = None;
680 let mut diff_base_byte_range: Option<Range<usize>> = None;
681 let mut first_addition_old_row: Option<u32> = None;
682
683 for line_index in 0..line_item_count {
684 let line = patch.line_in_hunk(hunk_index, line_index).unwrap();
685 let kind = line.origin_value();
686 let content_offset = line.content_offset() as isize;
687 let content_len = line.content().len() as isize;
688 match kind {
689 GitDiffLineType::Addition => {
690 if first_addition_old_row.is_none() {
691 first_addition_old_row = Some(
692 (line.new_lineno().unwrap() as i64 - *buffer_row_divergence - 1) as u32,
693 );
694 }
695 *buffer_row_divergence += 1;
696 let row = line.new_lineno().unwrap().saturating_sub(1);
697
698 match &mut buffer_row_range {
699 Some(Range { end, .. }) => *end = row + 1,
700 None => buffer_row_range = Some(row..row + 1),
701 }
702 }
703 GitDiffLineType::Deletion => {
704 let end = content_offset + content_len;
705
706 match &mut diff_base_byte_range {
707 Some(head_byte_range) => head_byte_range.end = end as usize,
708 None => diff_base_byte_range = Some(content_offset as usize..end as usize),
709 }
710
711 if first_deletion_buffer_row.is_none() {
712 let old_row = line.old_lineno().unwrap().saturating_sub(1);
713 let row = old_row as i64 + *buffer_row_divergence;
714 first_deletion_buffer_row = Some(row as u32);
715 }
716
717 *buffer_row_divergence -= 1;
718 }
719 _ => {}
720 }
721 }
722
723 let buffer_row_range = buffer_row_range.unwrap_or_else(|| {
724 // Pure deletion hunk without addition.
725 let row = first_deletion_buffer_row.unwrap();
726 row..row
727 });
728 let diff_base_byte_range = diff_base_byte_range.unwrap_or_else(|| {
729 // Pure addition hunk without deletion.
730 let row = first_addition_old_row.unwrap();
731 let offset = diff_base.point_to_offset(Point::new(row, 0));
732 offset..offset
733 });
734
735 let start = Point::new(buffer_row_range.start, 0);
736 let end = Point::new(buffer_row_range.end, 0);
737 let buffer_range = buffer.anchor_before(start)..buffer.anchor_before(end);
738 InternalDiffHunk {
739 buffer_range,
740 diff_base_byte_range,
741 }
742}
743
744impl std::fmt::Debug for BufferDiff {
745 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
746 f.debug_struct("BufferChangeSet")
747 .field("buffer_id", &self.buffer_id)
748 .field("snapshot", &self.inner)
749 .finish()
750 }
751}
752
753#[derive(Clone, Debug)]
754pub enum BufferDiffEvent {
755 DiffChanged {
756 changed_range: Option<Range<text::Anchor>>,
757 },
758 LanguageChanged,
759 HunksStagedOrUnstaged(Option<Rope>),
760}
761
762impl EventEmitter<BufferDiffEvent> for BufferDiff {}
763
764impl BufferDiff {
765 #[cfg(test)]
766 fn build_sync(
767 buffer: text::BufferSnapshot,
768 diff_base: String,
769 cx: &mut gpui::TestAppContext,
770 ) -> BufferDiffInner {
771 let snapshot =
772 cx.update(|cx| Self::build(buffer, Some(Arc::new(diff_base)), None, None, cx));
773 cx.executor().block(snapshot)
774 }
775
776 fn build(
777 buffer: text::BufferSnapshot,
778 base_text: Option<Arc<String>>,
779 language: Option<Arc<Language>>,
780 language_registry: Option<Arc<LanguageRegistry>>,
781 cx: &mut App,
782 ) -> impl Future<Output = BufferDiffInner> {
783 let base_text_pair;
784 let base_text_exists;
785 let base_text_snapshot;
786 if let Some(text) = &base_text {
787 let base_text_rope = Rope::from(text.as_str());
788 base_text_pair = Some((text.clone(), base_text_rope.clone()));
789 let snapshot = language::Buffer::build_snapshot(
790 base_text_rope,
791 language.clone(),
792 language_registry.clone(),
793 cx,
794 );
795 base_text_snapshot = cx.background_spawn(snapshot);
796 base_text_exists = true;
797 } else {
798 base_text_pair = None;
799 base_text_snapshot = Task::ready(language::Buffer::build_empty_snapshot(cx));
800 base_text_exists = false;
801 };
802
803 let hunks = cx.background_spawn({
804 let buffer = buffer.clone();
805 async move { compute_hunks(base_text_pair, buffer) }
806 });
807
808 async move {
809 let (base_text, hunks) = futures::join!(base_text_snapshot, hunks);
810 BufferDiffInner {
811 base_text,
812 hunks,
813 base_text_exists,
814 pending_hunks: SumTree::new(&buffer),
815 }
816 }
817 }
818
819 fn build_with_base_buffer(
820 buffer: text::BufferSnapshot,
821 base_text: Option<Arc<String>>,
822 base_text_snapshot: language::BufferSnapshot,
823 cx: &App,
824 ) -> impl Future<Output = BufferDiffInner> {
825 let base_text_exists = base_text.is_some();
826 let base_text_pair = base_text.map(|text| (text, base_text_snapshot.as_rope().clone()));
827 cx.background_spawn(async move {
828 BufferDiffInner {
829 base_text: base_text_snapshot,
830 pending_hunks: SumTree::new(&buffer),
831 hunks: compute_hunks(base_text_pair, buffer),
832 base_text_exists,
833 }
834 })
835 }
836
837 fn build_empty(buffer: &text::BufferSnapshot, cx: &mut App) -> BufferDiffInner {
838 BufferDiffInner {
839 base_text: language::Buffer::build_empty_snapshot(cx),
840 hunks: SumTree::new(buffer),
841 pending_hunks: SumTree::new(buffer),
842 base_text_exists: false,
843 }
844 }
845
846 pub fn set_secondary_diff(&mut self, diff: Entity<BufferDiff>) {
847 self.secondary_diff = Some(diff);
848 }
849
850 pub fn secondary_diff(&self) -> Option<Entity<BufferDiff>> {
851 self.secondary_diff.clone()
852 }
853
854 pub fn clear_pending_hunks(&mut self, cx: &mut Context<Self>) {
855 if let Some(secondary_diff) = &self.secondary_diff {
856 secondary_diff.update(cx, |diff, _| {
857 diff.inner.pending_hunks = SumTree::from_summary(DiffHunkSummary::default());
858 });
859 cx.emit(BufferDiffEvent::DiffChanged {
860 changed_range: Some(Anchor::MIN..Anchor::MAX),
861 });
862 }
863 }
864
865 pub fn stage_or_unstage_hunks(
866 &mut self,
867 stage: bool,
868 hunks: &[DiffHunk],
869 buffer: &text::BufferSnapshot,
870 file_exists: bool,
871 cx: &mut Context<Self>,
872 ) -> Option<Rope> {
873 let (new_index_text, new_pending_hunks) = self.inner.stage_or_unstage_hunks_impl(
874 &self.secondary_diff.as_ref()?.read(cx).inner,
875 stage,
876 &hunks,
877 buffer,
878 file_exists,
879 );
880
881 if let Some(unstaged_diff) = &self.secondary_diff {
882 unstaged_diff.update(cx, |diff, _| {
883 diff.inner.pending_hunks = new_pending_hunks;
884 });
885 }
886 cx.emit(BufferDiffEvent::HunksStagedOrUnstaged(
887 new_index_text.clone(),
888 ));
889 if let Some((first, last)) = hunks.first().zip(hunks.last()) {
890 let changed_range = first.buffer_range.start..last.buffer_range.end;
891 cx.emit(BufferDiffEvent::DiffChanged {
892 changed_range: Some(changed_range),
893 });
894 }
895 new_index_text
896 }
897
898 pub fn range_to_hunk_range(
899 &self,
900 range: Range<Anchor>,
901 buffer: &text::BufferSnapshot,
902 cx: &App,
903 ) -> Option<Range<Anchor>> {
904 let start = self
905 .hunks_intersecting_range(range.clone(), &buffer, cx)
906 .next()?
907 .buffer_range
908 .start;
909 let end = self
910 .hunks_intersecting_range_rev(range.clone(), &buffer)
911 .next()?
912 .buffer_range
913 .end;
914 Some(start..end)
915 }
916
917 pub async fn update_diff(
918 this: Entity<BufferDiff>,
919 buffer: text::BufferSnapshot,
920 base_text: Option<Arc<String>>,
921 base_text_changed: bool,
922 language_changed: bool,
923 language: Option<Arc<Language>>,
924 language_registry: Option<Arc<LanguageRegistry>>,
925 cx: &mut AsyncApp,
926 ) -> anyhow::Result<BufferDiffSnapshot> {
927 let inner = if base_text_changed || language_changed {
928 cx.update(|cx| {
929 Self::build(
930 buffer.clone(),
931 base_text,
932 language.clone(),
933 language_registry.clone(),
934 cx,
935 )
936 })?
937 .await
938 } else {
939 this.read_with(cx, |this, cx| {
940 Self::build_with_base_buffer(
941 buffer.clone(),
942 base_text,
943 this.base_text().clone(),
944 cx,
945 )
946 })?
947 .await
948 };
949 Ok(BufferDiffSnapshot {
950 inner,
951 secondary_diff: None,
952 })
953 }
954
955 pub fn set_snapshot(
956 &mut self,
957 buffer: &text::BufferSnapshot,
958 new_snapshot: BufferDiffSnapshot,
959 language_changed: bool,
960 secondary_changed_range: Option<Range<Anchor>>,
961 cx: &mut Context<Self>,
962 ) -> Option<Range<Anchor>> {
963 let changed_range = self.set_state(new_snapshot.inner, buffer);
964 if language_changed {
965 cx.emit(BufferDiffEvent::LanguageChanged);
966 }
967
968 let changed_range = match (secondary_changed_range, changed_range) {
969 (None, None) => None,
970 (Some(unstaged_range), None) => self.range_to_hunk_range(unstaged_range, &buffer, cx),
971 (None, Some(uncommitted_range)) => Some(uncommitted_range),
972 (Some(unstaged_range), Some(uncommitted_range)) => {
973 let mut start = uncommitted_range.start;
974 let mut end = uncommitted_range.end;
975 if let Some(unstaged_range) = self.range_to_hunk_range(unstaged_range, &buffer, cx)
976 {
977 start = unstaged_range.start.min(&uncommitted_range.start, &buffer);
978 end = unstaged_range.end.max(&uncommitted_range.end, &buffer);
979 }
980 Some(start..end)
981 }
982 };
983
984 cx.emit(BufferDiffEvent::DiffChanged {
985 changed_range: changed_range.clone(),
986 });
987 changed_range
988 }
989
990 fn set_state(
991 &mut self,
992 new_state: BufferDiffInner,
993 buffer: &text::BufferSnapshot,
994 ) -> Option<Range<Anchor>> {
995 let (base_text_changed, changed_range) =
996 match (self.inner.base_text_exists, new_state.base_text_exists) {
997 (false, false) => (true, None),
998 (true, true)
999 if self.inner.base_text.remote_id() == new_state.base_text.remote_id() =>
1000 {
1001 (false, new_state.compare(&self.inner, buffer))
1002 }
1003 _ => (true, Some(text::Anchor::MIN..text::Anchor::MAX)),
1004 };
1005
1006 let pending_hunks = mem::replace(&mut self.inner.pending_hunks, SumTree::new(buffer));
1007
1008 self.inner = new_state;
1009 if !base_text_changed {
1010 self.inner.pending_hunks = pending_hunks;
1011 }
1012 changed_range
1013 }
1014
1015 pub fn base_text(&self) -> &language::BufferSnapshot {
1016 &self.inner.base_text
1017 }
1018
1019 pub fn base_text_exists(&self) -> bool {
1020 self.inner.base_text_exists
1021 }
1022
1023 pub fn snapshot(&self, cx: &App) -> BufferDiffSnapshot {
1024 BufferDiffSnapshot {
1025 inner: self.inner.clone(),
1026 secondary_diff: self
1027 .secondary_diff
1028 .as_ref()
1029 .map(|diff| Box::new(diff.read(cx).snapshot(cx))),
1030 }
1031 }
1032
1033 pub fn hunks<'a>(
1034 &'a self,
1035 buffer_snapshot: &'a text::BufferSnapshot,
1036 cx: &'a App,
1037 ) -> impl 'a + Iterator<Item = DiffHunk> {
1038 self.hunks_intersecting_range(Anchor::MIN..Anchor::MAX, buffer_snapshot, cx)
1039 }
1040
1041 pub fn hunks_intersecting_range<'a>(
1042 &'a self,
1043 range: Range<text::Anchor>,
1044 buffer_snapshot: &'a text::BufferSnapshot,
1045 cx: &'a App,
1046 ) -> impl 'a + Iterator<Item = DiffHunk> {
1047 let unstaged_counterpart = self
1048 .secondary_diff
1049 .as_ref()
1050 .map(|diff| &diff.read(cx).inner);
1051 self.inner
1052 .hunks_intersecting_range(range, buffer_snapshot, unstaged_counterpart)
1053 }
1054
1055 pub fn hunks_intersecting_range_rev<'a>(
1056 &'a self,
1057 range: Range<text::Anchor>,
1058 buffer_snapshot: &'a text::BufferSnapshot,
1059 ) -> impl 'a + Iterator<Item = DiffHunk> {
1060 self.inner
1061 .hunks_intersecting_range_rev(range, buffer_snapshot)
1062 }
1063
1064 pub fn hunks_in_row_range<'a>(
1065 &'a self,
1066 range: Range<u32>,
1067 buffer: &'a text::BufferSnapshot,
1068 cx: &'a App,
1069 ) -> impl 'a + Iterator<Item = DiffHunk> {
1070 let start = buffer.anchor_before(Point::new(range.start, 0));
1071 let end = buffer.anchor_after(Point::new(range.end, 0));
1072 self.hunks_intersecting_range(start..end, buffer, cx)
1073 }
1074
1075 /// Used in cases where the change set isn't derived from git.
1076 pub fn set_base_text(
1077 &mut self,
1078 base_buffer: Entity<language::Buffer>,
1079 buffer: text::BufferSnapshot,
1080 cx: &mut Context<Self>,
1081 ) -> oneshot::Receiver<()> {
1082 let (tx, rx) = oneshot::channel();
1083 let this = cx.weak_entity();
1084 let base_buffer = base_buffer.read(cx);
1085 let language_registry = base_buffer.language_registry();
1086 let base_buffer = base_buffer.snapshot();
1087 let base_text = Arc::new(base_buffer.text());
1088
1089 let snapshot = BufferDiff::build(
1090 buffer.clone(),
1091 Some(base_text),
1092 base_buffer.language().cloned(),
1093 language_registry,
1094 cx,
1095 );
1096 let complete_on_drop = util::defer(|| {
1097 tx.send(()).ok();
1098 });
1099 cx.spawn(async move |_, cx| {
1100 let snapshot = snapshot.await;
1101 let Some(this) = this.upgrade() else {
1102 return;
1103 };
1104 this.update(cx, |this, _| {
1105 this.set_state(snapshot, &buffer);
1106 })
1107 .log_err();
1108 drop(complete_on_drop)
1109 })
1110 .detach();
1111 rx
1112 }
1113
1114 pub fn base_text_string(&self) -> Option<String> {
1115 self.inner
1116 .base_text_exists
1117 .then(|| self.inner.base_text.text())
1118 }
1119
1120 pub fn new(buffer: &text::BufferSnapshot, cx: &mut App) -> Self {
1121 BufferDiff {
1122 buffer_id: buffer.remote_id(),
1123 inner: BufferDiff::build_empty(buffer, cx),
1124 secondary_diff: None,
1125 }
1126 }
1127
1128 #[cfg(any(test, feature = "test-support"))]
1129 pub fn new_with_base_text(
1130 base_text: &str,
1131 buffer: &Entity<language::Buffer>,
1132 cx: &mut App,
1133 ) -> Self {
1134 let mut base_text = base_text.to_owned();
1135 text::LineEnding::normalize(&mut base_text);
1136 let snapshot = BufferDiff::build(
1137 buffer.read(cx).text_snapshot(),
1138 Some(base_text.into()),
1139 None,
1140 None,
1141 cx,
1142 );
1143 let snapshot = cx.background_executor().block(snapshot);
1144 BufferDiff {
1145 buffer_id: buffer.read(cx).remote_id(),
1146 inner: snapshot,
1147 secondary_diff: None,
1148 }
1149 }
1150
1151 #[cfg(any(test, feature = "test-support"))]
1152 pub fn recalculate_diff_sync(&mut self, buffer: text::BufferSnapshot, cx: &mut Context<Self>) {
1153 let base_text = self.base_text_string().map(Arc::new);
1154 let snapshot = BufferDiff::build_with_base_buffer(
1155 buffer.clone(),
1156 base_text,
1157 self.inner.base_text.clone(),
1158 cx,
1159 );
1160 let snapshot = cx.background_executor().block(snapshot);
1161 let changed_range = self.set_state(snapshot, &buffer);
1162 cx.emit(BufferDiffEvent::DiffChanged { changed_range });
1163 }
1164}
1165
1166impl DiffHunk {
1167 pub fn is_created_file(&self) -> bool {
1168 self.diff_base_byte_range == (0..0) && self.buffer_range == (Anchor::MIN..Anchor::MAX)
1169 }
1170
1171 pub fn status(&self) -> DiffHunkStatus {
1172 let kind = if self.buffer_range.start == self.buffer_range.end {
1173 DiffHunkStatusKind::Deleted
1174 } else if self.diff_base_byte_range.is_empty() {
1175 DiffHunkStatusKind::Added
1176 } else {
1177 DiffHunkStatusKind::Modified
1178 };
1179 DiffHunkStatus {
1180 kind,
1181 secondary: self.secondary_status,
1182 }
1183 }
1184}
1185
1186impl DiffHunkStatus {
1187 pub fn has_secondary_hunk(&self) -> bool {
1188 matches!(
1189 self.secondary,
1190 DiffHunkSecondaryStatus::HasSecondaryHunk
1191 | DiffHunkSecondaryStatus::SecondaryHunkAdditionPending
1192 | DiffHunkSecondaryStatus::OverlapsWithSecondaryHunk
1193 )
1194 }
1195
1196 pub fn is_pending(&self) -> bool {
1197 matches!(
1198 self.secondary,
1199 DiffHunkSecondaryStatus::SecondaryHunkAdditionPending
1200 | DiffHunkSecondaryStatus::SecondaryHunkRemovalPending
1201 )
1202 }
1203
1204 pub fn is_deleted(&self) -> bool {
1205 self.kind == DiffHunkStatusKind::Deleted
1206 }
1207
1208 pub fn is_added(&self) -> bool {
1209 self.kind == DiffHunkStatusKind::Added
1210 }
1211
1212 pub fn is_modified(&self) -> bool {
1213 self.kind == DiffHunkStatusKind::Modified
1214 }
1215
1216 pub fn added(secondary: DiffHunkSecondaryStatus) -> Self {
1217 Self {
1218 kind: DiffHunkStatusKind::Added,
1219 secondary,
1220 }
1221 }
1222
1223 pub fn modified(secondary: DiffHunkSecondaryStatus) -> Self {
1224 Self {
1225 kind: DiffHunkStatusKind::Modified,
1226 secondary,
1227 }
1228 }
1229
1230 pub fn deleted(secondary: DiffHunkSecondaryStatus) -> Self {
1231 Self {
1232 kind: DiffHunkStatusKind::Deleted,
1233 secondary,
1234 }
1235 }
1236
1237 pub fn deleted_none() -> Self {
1238 Self {
1239 kind: DiffHunkStatusKind::Deleted,
1240 secondary: DiffHunkSecondaryStatus::NoSecondaryHunk,
1241 }
1242 }
1243
1244 pub fn added_none() -> Self {
1245 Self {
1246 kind: DiffHunkStatusKind::Added,
1247 secondary: DiffHunkSecondaryStatus::NoSecondaryHunk,
1248 }
1249 }
1250
1251 pub fn modified_none() -> Self {
1252 Self {
1253 kind: DiffHunkStatusKind::Modified,
1254 secondary: DiffHunkSecondaryStatus::NoSecondaryHunk,
1255 }
1256 }
1257}
1258
1259#[cfg(any(test, feature = "test-support"))]
1260#[track_caller]
1261pub fn assert_hunks<ExpectedText, HunkIter>(
1262 diff_hunks: HunkIter,
1263 buffer: &text::BufferSnapshot,
1264 diff_base: &str,
1265 // Line range, deleted, added, status
1266 expected_hunks: &[(Range<u32>, ExpectedText, ExpectedText, DiffHunkStatus)],
1267) where
1268 HunkIter: Iterator<Item = DiffHunk>,
1269 ExpectedText: AsRef<str>,
1270{
1271 let actual_hunks = diff_hunks
1272 .map(|hunk| {
1273 (
1274 hunk.range.clone(),
1275 &diff_base[hunk.diff_base_byte_range.clone()],
1276 buffer
1277 .text_for_range(hunk.range.clone())
1278 .collect::<String>(),
1279 hunk.status(),
1280 )
1281 })
1282 .collect::<Vec<_>>();
1283
1284 let expected_hunks: Vec<_> = expected_hunks
1285 .iter()
1286 .map(|(line_range, deleted_text, added_text, status)| {
1287 (
1288 Point::new(line_range.start, 0)..Point::new(line_range.end, 0),
1289 deleted_text.as_ref(),
1290 added_text.as_ref().to_string(),
1291 *status,
1292 )
1293 })
1294 .collect();
1295
1296 pretty_assertions::assert_eq!(actual_hunks, expected_hunks);
1297}
1298
1299#[cfg(test)]
1300mod tests {
1301 use std::fmt::Write as _;
1302
1303 use super::*;
1304 use gpui::TestAppContext;
1305 use pretty_assertions::{assert_eq, assert_ne};
1306 use rand::{rngs::StdRng, Rng as _};
1307 use text::{Buffer, BufferId, Rope};
1308 use unindent::Unindent as _;
1309 use util::test::marked_text_ranges;
1310
1311 #[ctor::ctor]
1312 fn init_logger() {
1313 if std::env::var("RUST_LOG").is_ok() {
1314 env_logger::init();
1315 }
1316 }
1317
1318 #[gpui::test]
1319 async fn test_buffer_diff_simple(cx: &mut gpui::TestAppContext) {
1320 let diff_base = "
1321 one
1322 two
1323 three
1324 "
1325 .unindent();
1326
1327 let buffer_text = "
1328 one
1329 HELLO
1330 three
1331 "
1332 .unindent();
1333
1334 let mut buffer = Buffer::new(0, BufferId::new(1).unwrap(), buffer_text);
1335 let mut diff = BufferDiff::build_sync(buffer.clone(), diff_base.clone(), cx);
1336 assert_hunks(
1337 diff.hunks_intersecting_range(Anchor::MIN..Anchor::MAX, &buffer, None),
1338 &buffer,
1339 &diff_base,
1340 &[(1..2, "two\n", "HELLO\n", DiffHunkStatus::modified_none())],
1341 );
1342
1343 buffer.edit([(0..0, "point five\n")]);
1344 diff = BufferDiff::build_sync(buffer.clone(), diff_base.clone(), cx);
1345 assert_hunks(
1346 diff.hunks_intersecting_range(Anchor::MIN..Anchor::MAX, &buffer, None),
1347 &buffer,
1348 &diff_base,
1349 &[
1350 (0..1, "", "point five\n", DiffHunkStatus::added_none()),
1351 (2..3, "two\n", "HELLO\n", DiffHunkStatus::modified_none()),
1352 ],
1353 );
1354
1355 diff = cx.update(|cx| BufferDiff::build_empty(&buffer, cx));
1356 assert_hunks::<&str, _>(
1357 diff.hunks_intersecting_range(Anchor::MIN..Anchor::MAX, &buffer, None),
1358 &buffer,
1359 &diff_base,
1360 &[],
1361 );
1362 }
1363
1364 #[gpui::test]
1365 async fn test_buffer_diff_with_secondary(cx: &mut gpui::TestAppContext) {
1366 let head_text = "
1367 zero
1368 one
1369 two
1370 three
1371 four
1372 five
1373 six
1374 seven
1375 eight
1376 nine
1377 "
1378 .unindent();
1379
1380 let index_text = "
1381 zero
1382 one
1383 TWO
1384 three
1385 FOUR
1386 five
1387 six
1388 seven
1389 eight
1390 NINE
1391 "
1392 .unindent();
1393
1394 let buffer_text = "
1395 zero
1396 one
1397 TWO
1398 three
1399 FOUR
1400 FIVE
1401 six
1402 SEVEN
1403 eight
1404 nine
1405 "
1406 .unindent();
1407
1408 let buffer = Buffer::new(0, BufferId::new(1).unwrap(), buffer_text);
1409 let unstaged_diff = BufferDiff::build_sync(buffer.clone(), index_text.clone(), cx);
1410
1411 let uncommitted_diff = BufferDiff::build_sync(buffer.clone(), head_text.clone(), cx);
1412
1413 let expected_hunks = vec![
1414 (2..3, "two\n", "TWO\n", DiffHunkStatus::modified_none()),
1415 (
1416 4..6,
1417 "four\nfive\n",
1418 "FOUR\nFIVE\n",
1419 DiffHunkStatus::modified(DiffHunkSecondaryStatus::OverlapsWithSecondaryHunk),
1420 ),
1421 (
1422 7..8,
1423 "seven\n",
1424 "SEVEN\n",
1425 DiffHunkStatus::modified(DiffHunkSecondaryStatus::HasSecondaryHunk),
1426 ),
1427 ];
1428
1429 assert_hunks(
1430 uncommitted_diff.hunks_intersecting_range(
1431 Anchor::MIN..Anchor::MAX,
1432 &buffer,
1433 Some(&unstaged_diff),
1434 ),
1435 &buffer,
1436 &head_text,
1437 &expected_hunks,
1438 );
1439 }
1440
1441 #[gpui::test]
1442 async fn test_buffer_diff_range(cx: &mut TestAppContext) {
1443 let diff_base = Arc::new(
1444 "
1445 one
1446 two
1447 three
1448 four
1449 five
1450 six
1451 seven
1452 eight
1453 nine
1454 ten
1455 "
1456 .unindent(),
1457 );
1458
1459 let buffer_text = "
1460 A
1461 one
1462 B
1463 two
1464 C
1465 three
1466 HELLO
1467 four
1468 five
1469 SIXTEEN
1470 seven
1471 eight
1472 WORLD
1473 nine
1474
1475 ten
1476
1477 "
1478 .unindent();
1479
1480 let buffer = Buffer::new(0, BufferId::new(1).unwrap(), buffer_text);
1481 let diff = cx
1482 .update(|cx| {
1483 BufferDiff::build(buffer.snapshot(), Some(diff_base.clone()), None, None, cx)
1484 })
1485 .await;
1486 assert_eq!(
1487 diff.hunks_intersecting_range(Anchor::MIN..Anchor::MAX, &buffer, None)
1488 .count(),
1489 8
1490 );
1491
1492 assert_hunks(
1493 diff.hunks_intersecting_range(
1494 buffer.anchor_before(Point::new(7, 0))..buffer.anchor_before(Point::new(12, 0)),
1495 &buffer,
1496 None,
1497 ),
1498 &buffer,
1499 &diff_base,
1500 &[
1501 (6..7, "", "HELLO\n", DiffHunkStatus::added_none()),
1502 (9..10, "six\n", "SIXTEEN\n", DiffHunkStatus::modified_none()),
1503 (12..13, "", "WORLD\n", DiffHunkStatus::added_none()),
1504 ],
1505 );
1506 }
1507
1508 #[gpui::test]
1509 async fn test_stage_hunk(cx: &mut TestAppContext) {
1510 struct Example {
1511 name: &'static str,
1512 head_text: String,
1513 index_text: String,
1514 buffer_marked_text: String,
1515 final_index_text: String,
1516 }
1517
1518 let table = [
1519 Example {
1520 name: "uncommitted hunk straddles end of unstaged hunk",
1521 head_text: "
1522 one
1523 two
1524 three
1525 four
1526 five
1527 "
1528 .unindent(),
1529 index_text: "
1530 one
1531 TWO_HUNDRED
1532 three
1533 FOUR_HUNDRED
1534 five
1535 "
1536 .unindent(),
1537 buffer_marked_text: "
1538 ZERO
1539 one
1540 two
1541 «THREE_HUNDRED
1542 FOUR_HUNDRED»
1543 five
1544 SIX
1545 "
1546 .unindent(),
1547 final_index_text: "
1548 one
1549 two
1550 THREE_HUNDRED
1551 FOUR_HUNDRED
1552 five
1553 "
1554 .unindent(),
1555 },
1556 Example {
1557 name: "uncommitted hunk straddles start of unstaged hunk",
1558 head_text: "
1559 one
1560 two
1561 three
1562 four
1563 five
1564 "
1565 .unindent(),
1566 index_text: "
1567 one
1568 TWO_HUNDRED
1569 three
1570 FOUR_HUNDRED
1571 five
1572 "
1573 .unindent(),
1574 buffer_marked_text: "
1575 ZERO
1576 one
1577 «TWO_HUNDRED
1578 THREE_HUNDRED»
1579 four
1580 five
1581 SIX
1582 "
1583 .unindent(),
1584 final_index_text: "
1585 one
1586 TWO_HUNDRED
1587 THREE_HUNDRED
1588 four
1589 five
1590 "
1591 .unindent(),
1592 },
1593 Example {
1594 name: "uncommitted hunk strictly contains unstaged hunks",
1595 head_text: "
1596 one
1597 two
1598 three
1599 four
1600 five
1601 six
1602 seven
1603 "
1604 .unindent(),
1605 index_text: "
1606 one
1607 TWO
1608 THREE
1609 FOUR
1610 FIVE
1611 SIX
1612 seven
1613 "
1614 .unindent(),
1615 buffer_marked_text: "
1616 one
1617 TWO
1618 «THREE_HUNDRED
1619 FOUR
1620 FIVE_HUNDRED»
1621 SIX
1622 seven
1623 "
1624 .unindent(),
1625 final_index_text: "
1626 one
1627 TWO
1628 THREE_HUNDRED
1629 FOUR
1630 FIVE_HUNDRED
1631 SIX
1632 seven
1633 "
1634 .unindent(),
1635 },
1636 Example {
1637 name: "uncommitted deletion hunk",
1638 head_text: "
1639 one
1640 two
1641 three
1642 four
1643 five
1644 "
1645 .unindent(),
1646 index_text: "
1647 one
1648 two
1649 three
1650 four
1651 five
1652 "
1653 .unindent(),
1654 buffer_marked_text: "
1655 one
1656 ˇfive
1657 "
1658 .unindent(),
1659 final_index_text: "
1660 one
1661 five
1662 "
1663 .unindent(),
1664 },
1665 Example {
1666 name: "one unstaged hunk that contains two uncommitted hunks",
1667 head_text: "
1668 one
1669 two
1670
1671 three
1672 four
1673 "
1674 .unindent(),
1675 index_text: "
1676 one
1677 two
1678 three
1679 four
1680 "
1681 .unindent(),
1682 buffer_marked_text: "
1683 «one
1684
1685 three // modified
1686 four»
1687 "
1688 .unindent(),
1689 final_index_text: "
1690 one
1691
1692 three // modified
1693 four
1694 "
1695 .unindent(),
1696 },
1697 Example {
1698 name: "one uncommitted hunk that contains two unstaged hunks",
1699 head_text: "
1700 one
1701 two
1702 three
1703 four
1704 five
1705 "
1706 .unindent(),
1707 index_text: "
1708 ZERO
1709 one
1710 TWO
1711 THREE
1712 FOUR
1713 five
1714 "
1715 .unindent(),
1716 buffer_marked_text: "
1717 «one
1718 TWO_HUNDRED
1719 THREE
1720 FOUR_HUNDRED
1721 five»
1722 "
1723 .unindent(),
1724 final_index_text: "
1725 ZERO
1726 one
1727 TWO_HUNDRED
1728 THREE
1729 FOUR_HUNDRED
1730 five
1731 "
1732 .unindent(),
1733 },
1734 ];
1735
1736 for example in table {
1737 let (buffer_text, ranges) = marked_text_ranges(&example.buffer_marked_text, false);
1738 let buffer = Buffer::new(0, BufferId::new(1).unwrap(), buffer_text);
1739 let hunk_range =
1740 buffer.anchor_before(ranges[0].start)..buffer.anchor_before(ranges[0].end);
1741
1742 let unstaged = BufferDiff::build_sync(buffer.clone(), example.index_text.clone(), cx);
1743 let uncommitted = BufferDiff::build_sync(buffer.clone(), example.head_text.clone(), cx);
1744
1745 let unstaged_diff = cx.new(|cx| {
1746 let mut diff = BufferDiff::new(&buffer, cx);
1747 diff.set_state(unstaged, &buffer);
1748 diff
1749 });
1750
1751 let uncommitted_diff = cx.new(|cx| {
1752 let mut diff = BufferDiff::new(&buffer, cx);
1753 diff.set_state(uncommitted, &buffer);
1754 diff.set_secondary_diff(unstaged_diff);
1755 diff
1756 });
1757
1758 uncommitted_diff.update(cx, |diff, cx| {
1759 let hunks = diff
1760 .hunks_intersecting_range(hunk_range.clone(), &buffer, &cx)
1761 .collect::<Vec<_>>();
1762 for hunk in &hunks {
1763 assert_ne!(
1764 hunk.secondary_status,
1765 DiffHunkSecondaryStatus::NoSecondaryHunk
1766 )
1767 }
1768
1769 let new_index_text = diff
1770 .stage_or_unstage_hunks(true, &hunks, &buffer, true, cx)
1771 .unwrap()
1772 .to_string();
1773
1774 let hunks = diff
1775 .hunks_intersecting_range(hunk_range.clone(), &buffer, &cx)
1776 .collect::<Vec<_>>();
1777 for hunk in &hunks {
1778 assert_eq!(
1779 hunk.secondary_status,
1780 DiffHunkSecondaryStatus::SecondaryHunkRemovalPending
1781 )
1782 }
1783
1784 pretty_assertions::assert_eq!(
1785 new_index_text,
1786 example.final_index_text,
1787 "example: {}",
1788 example.name
1789 );
1790 });
1791 }
1792 }
1793
1794 #[gpui::test]
1795 async fn test_toggling_stage_and_unstage_same_hunk(cx: &mut TestAppContext) {
1796 let head_text = "
1797 one
1798 two
1799 three
1800 "
1801 .unindent();
1802 let index_text = head_text.clone();
1803 let buffer_text = "
1804 one
1805 three
1806 "
1807 .unindent();
1808
1809 let buffer = Buffer::new(0, BufferId::new(1).unwrap(), buffer_text.clone());
1810 let unstaged = BufferDiff::build_sync(buffer.clone(), index_text, cx);
1811 let uncommitted = BufferDiff::build_sync(buffer.clone(), head_text.clone(), cx);
1812 let unstaged_diff = cx.new(|cx| {
1813 let mut diff = BufferDiff::new(&buffer, cx);
1814 diff.set_state(unstaged, &buffer);
1815 diff
1816 });
1817 let uncommitted_diff = cx.new(|cx| {
1818 let mut diff = BufferDiff::new(&buffer, cx);
1819 diff.set_state(uncommitted, &buffer);
1820 diff.set_secondary_diff(unstaged_diff.clone());
1821 diff
1822 });
1823
1824 uncommitted_diff.update(cx, |diff, cx| {
1825 let hunk = diff.hunks(&buffer, cx).next().unwrap();
1826
1827 let new_index_text = diff
1828 .stage_or_unstage_hunks(true, &[hunk.clone()], &buffer, true, cx)
1829 .unwrap()
1830 .to_string();
1831 assert_eq!(new_index_text, buffer_text);
1832
1833 let hunk = diff.hunks(&buffer, &cx).next().unwrap();
1834 assert_eq!(
1835 hunk.secondary_status,
1836 DiffHunkSecondaryStatus::SecondaryHunkRemovalPending
1837 );
1838
1839 let index_text = diff
1840 .stage_or_unstage_hunks(false, &[hunk], &buffer, true, cx)
1841 .unwrap()
1842 .to_string();
1843 assert_eq!(index_text, head_text);
1844
1845 let hunk = diff.hunks(&buffer, &cx).next().unwrap();
1846 // optimistically unstaged (fine, could also be HasSecondaryHunk)
1847 assert_eq!(
1848 hunk.secondary_status,
1849 DiffHunkSecondaryStatus::SecondaryHunkAdditionPending
1850 );
1851 });
1852 }
1853
1854 #[gpui::test]
1855 async fn test_buffer_diff_compare(cx: &mut TestAppContext) {
1856 let base_text = "
1857 zero
1858 one
1859 two
1860 three
1861 four
1862 five
1863 six
1864 seven
1865 eight
1866 nine
1867 "
1868 .unindent();
1869
1870 let buffer_text_1 = "
1871 one
1872 three
1873 four
1874 five
1875 SIX
1876 seven
1877 eight
1878 NINE
1879 "
1880 .unindent();
1881
1882 let mut buffer = Buffer::new(0, BufferId::new(1).unwrap(), buffer_text_1);
1883
1884 let empty_diff = cx.update(|cx| BufferDiff::build_empty(&buffer, cx));
1885 let diff_1 = BufferDiff::build_sync(buffer.clone(), base_text.clone(), cx);
1886 let range = diff_1.compare(&empty_diff, &buffer).unwrap();
1887 assert_eq!(range.to_point(&buffer), Point::new(0, 0)..Point::new(8, 0));
1888
1889 // Edit does not affect the diff.
1890 buffer.edit_via_marked_text(
1891 &"
1892 one
1893 three
1894 four
1895 five
1896 «SIX.5»
1897 seven
1898 eight
1899 NINE
1900 "
1901 .unindent(),
1902 );
1903 let diff_2 = BufferDiff::build_sync(buffer.clone(), base_text.clone(), cx);
1904 assert_eq!(None, diff_2.compare(&diff_1, &buffer));
1905
1906 // Edit turns a deletion hunk into a modification.
1907 buffer.edit_via_marked_text(
1908 &"
1909 one
1910 «THREE»
1911 four
1912 five
1913 SIX.5
1914 seven
1915 eight
1916 NINE
1917 "
1918 .unindent(),
1919 );
1920 let diff_3 = BufferDiff::build_sync(buffer.clone(), base_text.clone(), cx);
1921 let range = diff_3.compare(&diff_2, &buffer).unwrap();
1922 assert_eq!(range.to_point(&buffer), Point::new(1, 0)..Point::new(2, 0));
1923
1924 // Edit turns a modification hunk into a deletion.
1925 buffer.edit_via_marked_text(
1926 &"
1927 one
1928 THREE
1929 four
1930 five«»
1931 seven
1932 eight
1933 NINE
1934 "
1935 .unindent(),
1936 );
1937 let diff_4 = BufferDiff::build_sync(buffer.clone(), base_text.clone(), cx);
1938 let range = diff_4.compare(&diff_3, &buffer).unwrap();
1939 assert_eq!(range.to_point(&buffer), Point::new(3, 4)..Point::new(4, 0));
1940
1941 // Edit introduces a new insertion hunk.
1942 buffer.edit_via_marked_text(
1943 &"
1944 one
1945 THREE
1946 four«
1947 FOUR.5
1948 »five
1949 seven
1950 eight
1951 NINE
1952 "
1953 .unindent(),
1954 );
1955 let diff_5 = BufferDiff::build_sync(buffer.snapshot(), base_text.clone(), cx);
1956 let range = diff_5.compare(&diff_4, &buffer).unwrap();
1957 assert_eq!(range.to_point(&buffer), Point::new(3, 0)..Point::new(4, 0));
1958
1959 // Edit removes a hunk.
1960 buffer.edit_via_marked_text(
1961 &"
1962 one
1963 THREE
1964 four
1965 FOUR.5
1966 five
1967 seven
1968 eight
1969 «nine»
1970 "
1971 .unindent(),
1972 );
1973 let diff_6 = BufferDiff::build_sync(buffer.snapshot(), base_text, cx);
1974 let range = diff_6.compare(&diff_5, &buffer).unwrap();
1975 assert_eq!(range.to_point(&buffer), Point::new(7, 0)..Point::new(8, 0));
1976 }
1977
1978 #[gpui::test(iterations = 100)]
1979 async fn test_staging_and_unstaging_hunks(cx: &mut TestAppContext, mut rng: StdRng) {
1980 fn gen_line(rng: &mut StdRng) -> String {
1981 if rng.gen_bool(0.2) {
1982 "\n".to_owned()
1983 } else {
1984 let c = rng.gen_range('A'..='Z');
1985 format!("{c}{c}{c}\n")
1986 }
1987 }
1988
1989 fn gen_working_copy(rng: &mut StdRng, head: &str) -> String {
1990 let mut old_lines = {
1991 let mut old_lines = Vec::new();
1992 let mut old_lines_iter = head.lines();
1993 while let Some(line) = old_lines_iter.next() {
1994 assert!(!line.ends_with("\n"));
1995 old_lines.push(line.to_owned());
1996 }
1997 if old_lines.last().is_some_and(|line| line.is_empty()) {
1998 old_lines.pop();
1999 }
2000 old_lines.into_iter()
2001 };
2002 let mut result = String::new();
2003 let unchanged_count = rng.gen_range(0..=old_lines.len());
2004 result +=
2005 &old_lines
2006 .by_ref()
2007 .take(unchanged_count)
2008 .fold(String::new(), |mut s, line| {
2009 writeln!(&mut s, "{line}").unwrap();
2010 s
2011 });
2012 while old_lines.len() > 0 {
2013 let deleted_count = rng.gen_range(0..=old_lines.len());
2014 let _advance = old_lines
2015 .by_ref()
2016 .take(deleted_count)
2017 .map(|line| line.len() + 1)
2018 .sum::<usize>();
2019 let minimum_added = if deleted_count == 0 { 1 } else { 0 };
2020 let added_count = rng.gen_range(minimum_added..=5);
2021 let addition = (0..added_count).map(|_| gen_line(rng)).collect::<String>();
2022 result += &addition;
2023
2024 if old_lines.len() > 0 {
2025 let blank_lines = old_lines.clone().take_while(|line| line.is_empty()).count();
2026 if blank_lines == old_lines.len() {
2027 break;
2028 };
2029 let unchanged_count = rng.gen_range((blank_lines + 1).max(1)..=old_lines.len());
2030 result += &old_lines.by_ref().take(unchanged_count).fold(
2031 String::new(),
2032 |mut s, line| {
2033 writeln!(&mut s, "{line}").unwrap();
2034 s
2035 },
2036 );
2037 }
2038 }
2039 result
2040 }
2041
2042 fn uncommitted_diff(
2043 working_copy: &language::BufferSnapshot,
2044 index_text: &Rope,
2045 head_text: String,
2046 cx: &mut TestAppContext,
2047 ) -> Entity<BufferDiff> {
2048 let inner = BufferDiff::build_sync(working_copy.text.clone(), head_text, cx);
2049 let secondary = BufferDiff {
2050 buffer_id: working_copy.remote_id(),
2051 inner: BufferDiff::build_sync(
2052 working_copy.text.clone(),
2053 index_text.to_string(),
2054 cx,
2055 ),
2056 secondary_diff: None,
2057 };
2058 let secondary = cx.new(|_| secondary);
2059 cx.new(|_| BufferDiff {
2060 buffer_id: working_copy.remote_id(),
2061 inner,
2062 secondary_diff: Some(secondary),
2063 })
2064 }
2065
2066 let operations = std::env::var("OPERATIONS")
2067 .map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
2068 .unwrap_or(10);
2069
2070 let rng = &mut rng;
2071 let head_text = ('a'..='z').fold(String::new(), |mut s, c| {
2072 writeln!(&mut s, "{c}{c}{c}").unwrap();
2073 s
2074 });
2075 let working_copy = gen_working_copy(rng, &head_text);
2076 let working_copy = cx.new(|cx| {
2077 language::Buffer::local_normalized(
2078 Rope::from(working_copy.as_str()),
2079 text::LineEnding::default(),
2080 cx,
2081 )
2082 });
2083 let working_copy = working_copy.read_with(cx, |working_copy, _| working_copy.snapshot());
2084 let mut index_text = if rng.gen() {
2085 Rope::from(head_text.as_str())
2086 } else {
2087 working_copy.as_rope().clone()
2088 };
2089
2090 let mut diff = uncommitted_diff(&working_copy, &index_text, head_text.clone(), cx);
2091 let mut hunks = diff.update(cx, |diff, cx| {
2092 diff.hunks_intersecting_range(Anchor::MIN..Anchor::MAX, &working_copy, cx)
2093 .collect::<Vec<_>>()
2094 });
2095 if hunks.len() == 0 {
2096 return;
2097 }
2098
2099 for _ in 0..operations {
2100 let i = rng.gen_range(0..hunks.len());
2101 let hunk = &mut hunks[i];
2102 let hunk_to_change = hunk.clone();
2103 let stage = match hunk.secondary_status {
2104 DiffHunkSecondaryStatus::HasSecondaryHunk => {
2105 hunk.secondary_status = DiffHunkSecondaryStatus::NoSecondaryHunk;
2106 true
2107 }
2108 DiffHunkSecondaryStatus::NoSecondaryHunk => {
2109 hunk.secondary_status = DiffHunkSecondaryStatus::HasSecondaryHunk;
2110 false
2111 }
2112 _ => unreachable!(),
2113 };
2114
2115 index_text = diff.update(cx, |diff, cx| {
2116 diff.stage_or_unstage_hunks(stage, &[hunk_to_change], &working_copy, true, cx)
2117 .unwrap()
2118 });
2119
2120 diff = uncommitted_diff(&working_copy, &index_text, head_text.clone(), cx);
2121 let found_hunks = diff.update(cx, |diff, cx| {
2122 diff.hunks_intersecting_range(Anchor::MIN..Anchor::MAX, &working_copy, cx)
2123 .collect::<Vec<_>>()
2124 });
2125 assert_eq!(hunks.len(), found_hunks.len());
2126
2127 for (expected_hunk, found_hunk) in hunks.iter().zip(&found_hunks) {
2128 assert_eq!(
2129 expected_hunk.buffer_range.to_point(&working_copy),
2130 found_hunk.buffer_range.to_point(&working_copy)
2131 );
2132 assert_eq!(
2133 expected_hunk.diff_base_byte_range,
2134 found_hunk.diff_base_byte_range
2135 );
2136 assert_eq!(expected_hunk.secondary_status, found_hunk.secondary_status);
2137 }
2138 hunks = found_hunks;
2139 }
2140 }
2141}