History

sum_tree: Store context on cursor (#34904)

This gets rid of the need to pass context to all cursor functions. In practice context is always immutable when interacting with cursors. A nicety of this is in the follow-up PR we will be able to implement Iterator for all Cursors/filter cursors (hell, we may be able to get rid of filter cursor altogether, as it is just a custom `filter` impl on iterator trait). Release Notes: - N/A

Piotr Osiewicz committed 1 year ago

Commit 64d0fec1 parent fa3e1cc23 files · +749 −876Browse files

Showing the changes to crates/buffer_diff/src/buffer_diff.rs (1 of 23 files). Show all

Unified · Split

crates/buffer_diff/src/buffer_diff.rs+20−22
@@ -343,8 +343,7 @@ impl BufferDiffInner { · 342 unmodified lines
343 ..343 ..
344 } in hunks.iter().cloned()344 } in hunks.iter().cloned()
345 {345 {
346 let preceding_pending_hunks =346 let preceding_pending_hunks = old_pending_hunks.slice(&buffer_range.start, Bias::Left);
347 old_pending_hunks.slice(&buffer_range.start, Bias::Left, buffer);
348 pending_hunks.append(preceding_pending_hunks, buffer);347 pending_hunks.append(preceding_pending_hunks, buffer);
349 348
350 // Skip all overlapping or adjacent old pending hunks349 // Skip all overlapping or adjacent old pending hunks
@@ -355,7 +354,7 @@ impl BufferDiffInner { · 4 unmodified lines
355 .cmp(&buffer_range.end, buffer)354 .cmp(&buffer_range.end, buffer)
356 .is_le()355 .is_le()
357 }) {356 }) {
358 old_pending_hunks.next(buffer);357 old_pending_hunks.next();
359 }358 }
360 359
361 if (stage && secondary_status == DiffHunkSecondaryStatus::NoSecondaryHunk)360 if (stage && secondary_status == DiffHunkSecondaryStatus::NoSecondaryHunk)
@@ -379,10 +378,10 @@ impl BufferDiffInner { · 17 unmodified lines
379 );378 );
380 }379 }
381 // append the remainder380 // append the remainder
382 pending_hunks.append(old_pending_hunks.suffix(buffer), buffer);381 pending_hunks.append(old_pending_hunks.suffix(), buffer);
383 382
384 let mut unstaged_hunk_cursor = unstaged_diff.hunks.cursor::<DiffHunkSummary>(buffer);383 let mut unstaged_hunk_cursor = unstaged_diff.hunks.cursor::<DiffHunkSummary>(buffer);
385 unstaged_hunk_cursor.next(buffer);384 unstaged_hunk_cursor.next();
386 385
387 // then, iterate over all pending hunks (both new ones and the existing ones) and compute the edits386 // then, iterate over all pending hunks (both new ones and the existing ones) and compute the edits
388 let mut prev_unstaged_hunk_buffer_end = 0;387 let mut prev_unstaged_hunk_buffer_end = 0;
@@ -397,8 +396,7 @@ impl BufferDiffInner { · 8 unmodified lines
397 }) = pending_hunks_iter.next()396 }) = pending_hunks_iter.next()
398 {397 {
399 // Advance unstaged_hunk_cursor to skip unstaged hunks before current hunk398 // Advance unstaged_hunk_cursor to skip unstaged hunks before current hunk
400 let skipped_unstaged =399 let skipped_unstaged = unstaged_hunk_cursor.slice(&buffer_range.start, Bias::Left);
401 unstaged_hunk_cursor.slice(&buffer_range.start, Bias::Left, buffer);
402 400
403 if let Some(unstaged_hunk) = skipped_unstaged.last() {401 if let Some(unstaged_hunk) = skipped_unstaged.last() {
404 prev_unstaged_hunk_base_text_end = unstaged_hunk.diff_base_byte_range.end;402 prev_unstaged_hunk_base_text_end = unstaged_hunk.diff_base_byte_range.end;
@@ -425,7 +423,7 @@ impl BufferDiffInner { · 20 unmodified lines
425 buffer_offset_range.end =423 buffer_offset_range.end =
426 buffer_offset_range.end.max(unstaged_hunk_offset_range.end);424 buffer_offset_range.end.max(unstaged_hunk_offset_range.end);
427 425
428 unstaged_hunk_cursor.next(buffer);426 unstaged_hunk_cursor.next();
429 continue;427 continue;
430 }428 }
431 }429 }
@@ -514,7 +512,7 @@ impl BufferDiffInner { · 82 unmodified lines
514 });512 });
515 513
516 let anchor_iter = iter::from_fn(move || {514 let anchor_iter = iter::from_fn(move || {
517 cursor.next(buffer);515 cursor.next();
518 cursor.item()516 cursor.item()
519 })517 })
520 .flat_map(move |hunk| {518 .flat_map(move |hunk| {
@@ -531,12 +529,12 @@ impl BufferDiffInner { · 10 unmodified lines
531 });529 });
532 530
533 let mut pending_hunks_cursor = self.pending_hunks.cursor::<DiffHunkSummary>(buffer);531 let mut pending_hunks_cursor = self.pending_hunks.cursor::<DiffHunkSummary>(buffer);
534 pending_hunks_cursor.next(buffer);532 pending_hunks_cursor.next();
535 533
536 let mut secondary_cursor = None;534 let mut secondary_cursor = None;
537 if let Some(secondary) = secondary.as_ref() {535 if let Some(secondary) = secondary.as_ref() {
538 let mut cursor = secondary.hunks.cursor::<DiffHunkSummary>(buffer);536 let mut cursor = secondary.hunks.cursor::<DiffHunkSummary>(buffer);
539 cursor.next(buffer);537 cursor.next();
540 secondary_cursor = Some(cursor);538 secondary_cursor = Some(cursor);
541 }539 }
542 540
@@ -564,7 +562,7 @@ impl BufferDiffInner { · 21 unmodified lines
564 .cmp(&pending_hunks_cursor.start().buffer_range.start, buffer)562 .cmp(&pending_hunks_cursor.start().buffer_range.start, buffer)
565 .is_gt()563 .is_gt()
566 {564 {
567 pending_hunks_cursor.seek_forward(&start_anchor, Bias::Left, buffer);565 pending_hunks_cursor.seek_forward(&start_anchor, Bias::Left);
568 }566 }
569 567
570 if let Some(pending_hunk) = pending_hunks_cursor.item() {568 if let Some(pending_hunk) = pending_hunks_cursor.item() {
@@ -590,7 +588,7 @@ impl BufferDiffInner { · 19 unmodified lines
590 .cmp(&secondary_cursor.start().buffer_range.start, buffer)588 .cmp(&secondary_cursor.start().buffer_range.start, buffer)
591 .is_gt()589 .is_gt()
592 {590 {
593 secondary_cursor.seek_forward(&start_anchor, Bias::Left, buffer);591 secondary_cursor.seek_forward(&start_anchor, Bias::Left);
594 }592 }
595 593
596 if let Some(secondary_hunk) = secondary_cursor.item() {594 if let Some(secondary_hunk) = secondary_cursor.item() {
@@ -635,7 +633,7 @@ impl BufferDiffInner { · 38 unmodified lines
635 });633 });
636 634
637 iter::from_fn(move || {635 iter::from_fn(move || {
638 cursor.prev(buffer);636 cursor.prev();
639 637
640 let hunk = cursor.item()?;638 let hunk = cursor.item()?;
641 let range = hunk.buffer_range.to_point(buffer);639 let range = hunk.buffer_range.to_point(buffer);
@@ -653,8 +651,8 @@ impl BufferDiffInner { · 11 unmodified lines
653 fn compare(&self, old: &Self, new_snapshot: &text::BufferSnapshot) -> Option<Range<Anchor>> {651 fn compare(&self, old: &Self, new_snapshot: &text::BufferSnapshot) -> Option<Range<Anchor>> {
654 let mut new_cursor = self.hunks.cursor::<()>(new_snapshot);652 let mut new_cursor = self.hunks.cursor::<()>(new_snapshot);
655 let mut old_cursor = old.hunks.cursor::<()>(new_snapshot);653 let mut old_cursor = old.hunks.cursor::<()>(new_snapshot);
656 old_cursor.next(new_snapshot);654 old_cursor.next();
657 new_cursor.next(new_snapshot);655 new_cursor.next();
658 let mut start = None;656 let mut start = None;
659 let mut end = None;657 let mut end = None;
660 658
@@ -669,7 +667,7 @@ impl BufferDiffInner { · 8 unmodified lines
669 Ordering::Less => {667 Ordering::Less => {
670 start.get_or_insert(new_hunk.buffer_range.start);668 start.get_or_insert(new_hunk.buffer_range.start);
671 end.replace(new_hunk.buffer_range.end);669 end.replace(new_hunk.buffer_range.end);
672 new_cursor.next(new_snapshot);670 new_cursor.next();
673 }671 }
674 Ordering::Equal => {672 Ordering::Equal => {
675 if new_hunk != old_hunk {673 if new_hunk != old_hunk {
@@ -686,25 +684,25 @@ impl BufferDiffInner { · 10 unmodified lines
686 }684 }
687 }685 }
688 686
689 new_cursor.next(new_snapshot);687 new_cursor.next();
690 old_cursor.next(new_snapshot);688 old_cursor.next();
691 }689 }
692 Ordering::Greater => {690 Ordering::Greater => {
693 start.get_or_insert(old_hunk.buffer_range.start);691 start.get_or_insert(old_hunk.buffer_range.start);
694 end.replace(old_hunk.buffer_range.end);692 end.replace(old_hunk.buffer_range.end);
695 old_cursor.next(new_snapshot);693 old_cursor.next();
696 }694 }
697 }695 }
698 }696 }
699 (Some(new_hunk), None) => {697 (Some(new_hunk), None) => {
700 start.get_or_insert(new_hunk.buffer_range.start);698 start.get_or_insert(new_hunk.buffer_range.start);
701 end.replace(new_hunk.buffer_range.end);699 end.replace(new_hunk.buffer_range.end);
702 new_cursor.next(new_snapshot);700 new_cursor.next();
703 }701 }
704 (None, Some(old_hunk)) => {702 (None, Some(old_hunk)) => {
705 start.get_or_insert(old_hunk.buffer_range.start);703 start.get_or_insert(old_hunk.buffer_range.start);
706 end.replace(old_hunk.buffer_range.end);704 end.replace(old_hunk.buffer_range.end);
707 old_cursor.next(new_snapshot);705 old_cursor.next();
708 }706 }
709 (None, None) => break,707 (None, None) => break,
710 }708 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t