Introduce diff crate to unite BufferDiff and BufferChangeSet (#24392)

This is a refactoring PR that does three things:

- First, it introduces a new `diff` crate that holds the previous
contents of the `git::diff` module, plus the `BufferChangeSet` type
formerly of `project::buffer_store`. The new crate is necessary since
simply moving `BufferChangeSet` into `git::diff` results in a dependency
cycle due to the use of `language::Buffer` to represent the diff base in
`BufferChangeSet`.
- Second, it renames the two main types in the new diff crate:
`BufferDiff` becomes `BufferDiffSnapshot`, and `BufferChangeSet` becomes
`BufferDiff`. This reflects that the relationship between these two
types (immutable cheaply-cloneable "value" type + stateful "resource
type" with subscriptions) mirrors existing pairs like
`Buffer`/`BufferSnapshot`. References to "change sets" throughout the
codebase are updated to refer to "diffs" instead.
- Finally, it moves the base_text field of the new BufferDiff type to
BufferDiffSnapshot.

Release Notes:

- N/A

---------

Co-authored-by: maxbrunsfeld <max@zed.dev>
This commit is contained in:
Cole Miller 2025-02-06 18:52:32 -05:00 committed by GitHub
parent ffcad71bfa
commit 73c487c222
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
31 changed files with 922 additions and 875 deletions

View file

@ -2,6 +2,7 @@ use std::any::{Any, TypeId};
use anyhow::Result;
use collections::HashSet;
use diff::BufferDiff;
use editor::{scroll::Autoscroll, Editor, EditorEvent};
use feature_flags::FeatureFlagViewExt;
use futures::StreamExt;
@ -11,7 +12,7 @@ use gpui::{
};
use language::{Anchor, Buffer, Capability, OffsetRangeExt, Point};
use multi_buffer::{MultiBuffer, PathKey};
use project::{buffer_store::BufferChangeSet, git::GitState, Project, ProjectPath};
use project::{git::GitState, Project, ProjectPath};
use theme::ActiveTheme;
use ui::prelude::*;
use util::ResultExt as _;
@ -43,7 +44,7 @@ pub(crate) struct ProjectDiff {
struct DiffBuffer {
path_key: PathKey,
buffer: Entity<Buffer>,
change_set: Entity<BufferChangeSet>,
diff: Entity<BufferDiff>,
}
const CONFLICT_NAMESPACE: &'static str = "0";
@ -285,13 +286,13 @@ impl ProjectDiff {
let buffer = load_buffer.await?;
let changes = project
.update(&mut cx, |project, cx| {
project.open_uncommitted_changes(buffer.clone(), cx)
project.open_uncommitted_diff(buffer.clone(), cx)
})?
.await?;
Ok(DiffBuffer {
path_key,
buffer,
change_set: changes,
diff: changes,
})
}));
}
@ -312,15 +313,14 @@ impl ProjectDiff {
) {
let path_key = diff_buffer.path_key;
let buffer = diff_buffer.buffer;
let change_set = diff_buffer.change_set;
let diff = diff_buffer.diff;
let snapshot = buffer.read(cx).snapshot();
let change_set = change_set.read(cx);
let diff_hunk_ranges = if change_set.base_text.is_none() {
let diff = diff.read(cx);
let diff_hunk_ranges = if diff.snapshot.base_text.is_none() {
vec![Point::zero()..snapshot.max_point()]
} else {
change_set
.diff_hunks_intersecting_range(Anchor::MIN..Anchor::MAX, &snapshot)
diff.diff_hunks_intersecting_range(Anchor::MIN..Anchor::MAX, &snapshot)
.map(|diff_hunk| diff_hunk.buffer_range.to_point(&snapshot))
.collect::<Vec<_>>()
};