Previously we were iterating over all hunks across all diffs on every frame. Now we can read off the required information as a `SumTree` summary in constant time. Self-Review Checklist: - [x] I've reviewed my own diff for quality, security, and reliability - [x] Unsafe blocks (if any) have justifying comments - [x] The content is consistent with the [UI/UX checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist) - [ ] Tests cover the new/changed behavior - [x] Performance impact has been considered and is acceptable Release Notes: - git: Fixed bad performance in large branch diffs.
269 lines
9.5 KiB
Rust
269 lines
9.5 KiB
Rust
use crate::{MultiBufferDimension, MultiBufferOffset, MultiBufferOffsetUtf16};
|
|
|
|
use super::{ExcerptId, MultiBufferSnapshot, ToOffset, ToPoint};
|
|
use language::Point;
|
|
use std::{
|
|
cmp::Ordering,
|
|
ops::{Add, AddAssign, Range, Sub},
|
|
};
|
|
use sum_tree::Bias;
|
|
|
|
/// A stable reference to a position within a [`MultiBuffer`](super::MultiBuffer).
|
|
///
|
|
/// Unlike simple offsets, anchors remain valid as the text is edited, automatically
|
|
/// adjusting to reflect insertions and deletions around them.
|
|
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
|
|
pub struct Anchor {
|
|
/// Identifies which excerpt within the multi-buffer this anchor belongs to.
|
|
/// A multi-buffer can contain multiple excerpts from different buffers.
|
|
pub excerpt_id: ExcerptId,
|
|
/// The position within the excerpt's underlying buffer. This is a stable
|
|
/// reference that remains valid as the buffer text is edited.
|
|
pub text_anchor: text::Anchor,
|
|
/// When present, indicates this anchor points into deleted text within an
|
|
/// expanded diff hunk. The anchor references a position in the diff base
|
|
/// (original) text rather than the current buffer text. This is used when
|
|
/// displaying inline diffs where deleted lines are shown.
|
|
pub diff_base_anchor: Option<text::Anchor>,
|
|
}
|
|
|
|
impl std::fmt::Debug for Anchor {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
if self.is_min() {
|
|
return write!(f, "Anchor::min({:?})", self.text_anchor.buffer_id);
|
|
}
|
|
if self.is_max() {
|
|
return write!(f, "Anchor::max({:?})", self.text_anchor.buffer_id);
|
|
}
|
|
|
|
f.debug_struct("Anchor")
|
|
.field("excerpt_id", &self.excerpt_id)
|
|
.field("text_anchor", &self.text_anchor)
|
|
.field("diff_base_anchor", &self.diff_base_anchor)
|
|
.finish()
|
|
}
|
|
}
|
|
|
|
impl Anchor {
|
|
pub fn with_diff_base_anchor(self, diff_base_anchor: text::Anchor) -> Self {
|
|
Self {
|
|
diff_base_anchor: Some(diff_base_anchor),
|
|
..self
|
|
}
|
|
}
|
|
|
|
pub fn in_buffer(excerpt_id: ExcerptId, text_anchor: text::Anchor) -> Self {
|
|
Self {
|
|
excerpt_id,
|
|
text_anchor,
|
|
diff_base_anchor: None,
|
|
}
|
|
}
|
|
|
|
pub fn range_in_buffer(excerpt_id: ExcerptId, range: Range<text::Anchor>) -> Range<Self> {
|
|
Self::in_buffer(excerpt_id, range.start)..Self::in_buffer(excerpt_id, range.end)
|
|
}
|
|
|
|
pub fn min() -> Self {
|
|
Self {
|
|
excerpt_id: ExcerptId::min(),
|
|
text_anchor: text::Anchor::MIN,
|
|
diff_base_anchor: None,
|
|
}
|
|
}
|
|
|
|
pub fn max() -> Self {
|
|
Self {
|
|
excerpt_id: ExcerptId::max(),
|
|
text_anchor: text::Anchor::MAX,
|
|
diff_base_anchor: None,
|
|
}
|
|
}
|
|
|
|
pub fn is_min(&self) -> bool {
|
|
self.excerpt_id == ExcerptId::min()
|
|
&& self.text_anchor.is_min()
|
|
&& self.diff_base_anchor.is_none()
|
|
}
|
|
|
|
pub fn is_max(&self) -> bool {
|
|
self.excerpt_id == ExcerptId::max()
|
|
&& self.text_anchor.is_max()
|
|
&& self.diff_base_anchor.is_none()
|
|
}
|
|
|
|
pub fn cmp(&self, other: &Anchor, snapshot: &MultiBufferSnapshot) -> Ordering {
|
|
if self == other {
|
|
return Ordering::Equal;
|
|
}
|
|
|
|
let self_excerpt_id = snapshot.latest_excerpt_id(self.excerpt_id);
|
|
let other_excerpt_id = snapshot.latest_excerpt_id(other.excerpt_id);
|
|
|
|
let excerpt_id_cmp = self_excerpt_id.cmp(&other_excerpt_id, snapshot);
|
|
if excerpt_id_cmp.is_ne() {
|
|
return excerpt_id_cmp;
|
|
}
|
|
if self_excerpt_id == ExcerptId::max()
|
|
&& self.text_anchor.is_max()
|
|
&& self.text_anchor.is_max()
|
|
&& self.diff_base_anchor.is_none()
|
|
&& other.diff_base_anchor.is_none()
|
|
{
|
|
return Ordering::Equal;
|
|
}
|
|
if let Some(excerpt) = snapshot.excerpt(self_excerpt_id) {
|
|
let text_cmp = self.text_anchor.cmp(&other.text_anchor, &excerpt.buffer);
|
|
if text_cmp.is_ne() {
|
|
return text_cmp;
|
|
}
|
|
if (self.diff_base_anchor.is_some() || other.diff_base_anchor.is_some())
|
|
&& let Some(base_text) = snapshot
|
|
.diff_state(excerpt.buffer_id)
|
|
.map(|diff| diff.base_text())
|
|
{
|
|
let self_anchor = self.diff_base_anchor.filter(|a| a.is_valid(base_text));
|
|
let other_anchor = other.diff_base_anchor.filter(|a| a.is_valid(base_text));
|
|
return match (self_anchor, other_anchor) {
|
|
(Some(a), Some(b)) => a.cmp(&b, base_text),
|
|
(Some(_), None) => match other.text_anchor.bias {
|
|
Bias::Left => Ordering::Greater,
|
|
Bias::Right => Ordering::Less,
|
|
},
|
|
(None, Some(_)) => match self.text_anchor.bias {
|
|
Bias::Left => Ordering::Less,
|
|
Bias::Right => Ordering::Greater,
|
|
},
|
|
(None, None) => Ordering::Equal,
|
|
};
|
|
}
|
|
}
|
|
Ordering::Equal
|
|
}
|
|
|
|
pub fn bias(&self) -> Bias {
|
|
self.text_anchor.bias
|
|
}
|
|
|
|
pub fn bias_left(&self, snapshot: &MultiBufferSnapshot) -> Anchor {
|
|
if self.text_anchor.bias != Bias::Left
|
|
&& let Some(excerpt) = snapshot.excerpt(self.excerpt_id)
|
|
{
|
|
return Self {
|
|
excerpt_id: excerpt.id,
|
|
text_anchor: self.text_anchor.bias_left(&excerpt.buffer),
|
|
diff_base_anchor: self.diff_base_anchor.map(|a| {
|
|
if let Some(base_text) = snapshot
|
|
.diff_state(excerpt.buffer_id)
|
|
.map(|diff| diff.base_text())
|
|
&& a.is_valid(&base_text)
|
|
{
|
|
return a.bias_left(base_text);
|
|
}
|
|
a
|
|
}),
|
|
};
|
|
}
|
|
*self
|
|
}
|
|
|
|
pub fn bias_right(&self, snapshot: &MultiBufferSnapshot) -> Anchor {
|
|
if self.text_anchor.bias != Bias::Right
|
|
&& let Some(excerpt) = snapshot.excerpt(self.excerpt_id)
|
|
{
|
|
return Self {
|
|
excerpt_id: excerpt.id,
|
|
text_anchor: self.text_anchor.bias_right(&excerpt.buffer),
|
|
diff_base_anchor: self.diff_base_anchor.map(|a| {
|
|
if let Some(base_text) = snapshot
|
|
.diff_state(excerpt.buffer_id)
|
|
.map(|diff| diff.base_text())
|
|
&& a.is_valid(&base_text)
|
|
{
|
|
return a.bias_right(base_text);
|
|
}
|
|
a
|
|
}),
|
|
};
|
|
}
|
|
*self
|
|
}
|
|
|
|
pub fn summary<D>(&self, snapshot: &MultiBufferSnapshot) -> D
|
|
where
|
|
D: MultiBufferDimension
|
|
+ Ord
|
|
+ Sub<Output = D::TextDimension>
|
|
+ Sub<D::TextDimension, Output = D>
|
|
+ AddAssign<D::TextDimension>
|
|
+ Add<D::TextDimension, Output = D>,
|
|
D::TextDimension: Sub<Output = D::TextDimension> + Ord,
|
|
{
|
|
snapshot.summary_for_anchor(self)
|
|
}
|
|
|
|
pub fn is_valid(&self, snapshot: &MultiBufferSnapshot) -> bool {
|
|
if self.is_min() || self.is_max() {
|
|
true
|
|
} else if let Some(excerpt) = snapshot.excerpt(self.excerpt_id) {
|
|
(self.text_anchor == excerpt.range.context.start
|
|
|| self.text_anchor == excerpt.range.context.end
|
|
|| self.text_anchor.is_valid(&excerpt.buffer))
|
|
&& excerpt.contains(self)
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
}
|
|
|
|
impl ToOffset for Anchor {
|
|
fn to_offset(&self, snapshot: &MultiBufferSnapshot) -> MultiBufferOffset {
|
|
self.summary(snapshot)
|
|
}
|
|
fn to_offset_utf16(&self, snapshot: &MultiBufferSnapshot) -> MultiBufferOffsetUtf16 {
|
|
self.summary(snapshot)
|
|
}
|
|
}
|
|
|
|
impl ToPoint for Anchor {
|
|
fn to_point<'a>(&self, snapshot: &MultiBufferSnapshot) -> Point {
|
|
self.summary(snapshot)
|
|
}
|
|
fn to_point_utf16(&self, snapshot: &MultiBufferSnapshot) -> rope::PointUtf16 {
|
|
self.summary(snapshot)
|
|
}
|
|
}
|
|
|
|
pub trait AnchorRangeExt {
|
|
fn cmp(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> Ordering;
|
|
fn includes(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> bool;
|
|
fn overlaps(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> bool;
|
|
fn to_offset(&self, content: &MultiBufferSnapshot) -> Range<MultiBufferOffset>;
|
|
fn to_point(&self, content: &MultiBufferSnapshot) -> Range<Point>;
|
|
}
|
|
|
|
impl AnchorRangeExt for Range<Anchor> {
|
|
fn cmp(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> Ordering {
|
|
match self.start.cmp(&other.start, buffer) {
|
|
Ordering::Equal => other.end.cmp(&self.end, buffer),
|
|
ord => ord,
|
|
}
|
|
}
|
|
|
|
fn includes(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> bool {
|
|
self.start.cmp(&other.start, buffer).is_le() && other.end.cmp(&self.end, buffer).is_le()
|
|
}
|
|
|
|
fn overlaps(&self, other: &Range<Anchor>, buffer: &MultiBufferSnapshot) -> bool {
|
|
self.end.cmp(&other.start, buffer).is_ge() && self.start.cmp(&other.end, buffer).is_le()
|
|
}
|
|
|
|
fn to_offset(&self, content: &MultiBufferSnapshot) -> Range<MultiBufferOffset> {
|
|
self.start.to_offset(content)..self.end.to_offset(content)
|
|
}
|
|
|
|
fn to_point(&self, content: &MultiBufferSnapshot) -> Range<Point> {
|
|
self.start.to_point(content)..self.end.to_point(content)
|
|
}
|
|
}
|