Rework diff rendering to allow putting the cursor into deleted text, soft-wrapping and scrolling deleted text correctly (#22994)

Closes #12553

* [x] Fix `diff_hunk_before`
* [x] Fix failure to show deleted text when expanding hunk w/ cursor on
second line of the hunk
* [x] Failure to expand diff hunk below the cursor.
* [x] Delete the whole file, and expand the diff. Backspace over the
deleted hunk, panic!
* [x] Go-to-line now counts the diff hunks, but it should not
* [x] backspace at the beginning of a deleted hunk deletes too much text
* [x] Indent guides are rendered incorrectly 
* [ ] Fix randomized multi buffer tests

Maybe:
* [ ] Buffer search should include deleted text (in vim mode it turns
out I use `/x` all the time to jump to the next x I can see).
* [ ] vim: should refuse to switch into insert mode if selection is
fully within a diff.
* [ ] vim `o` command when cursor is on last line of deleted hunk.
* [ ] vim `shift-o` on first line of deleted hunk moves cursor but
doesn't insert line
* [x] `enter` at end of diff hunk inserts a new line but doesn't move
cursor
* [x] (`shift-enter` at start of diff hunk does nothing)
* [ ] Inserting a line just before an expanded hunk collapses it

Release Notes:


- Improved diff rendering, allowing you to navigate with your cursor
inside of deleted text in diff hunks.

---------

Co-authored-by: Conrad <conrad@zed.dev>
Co-authored-by: Cole <cole@zed.dev>
Co-authored-by: Mikayla <mikayla@zed.dev>
Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
Co-authored-by: Michael <michael@zed.dev>
Co-authored-by: Agus <agus@zed.dev>
Co-authored-by: João <joao@zed.dev>
This commit is contained in:
Max Brunsfeld 2025-01-24 13:18:22 -08:00 committed by GitHub
parent 1fdae4bae0
commit d2c55cbe3d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
64 changed files with 7653 additions and 5495 deletions

View file

@ -27,12 +27,16 @@ collections.workspace = true
ctor.workspace = true
env_logger.workspace = true
futures.workspace = true
git.workspace = true
gpui.workspace = true
itertools.workspace = true
language.workspace = true
log.workspace = true
parking_lot.workspace = true
project.workspace = true
rand.workspace = true
rope.workspace = true
smol.workspace = true
settings.workspace = true
serde.workspace = true
smallvec.workspace = true
@ -45,7 +49,10 @@ util.workspace = true
[dev-dependencies]
gpui = { workspace = true, features = ["test-support"] }
language = { workspace = true, features = ["test-support"] }
project = { workspace = true, features = ["test-support"] }
rand.workspace = true
settings = { workspace = true, features = ["test-support"] }
text = { workspace = true, features = ["test-support"] }
util = { workspace = true, features = ["test-support"] }
pretty_assertions.workspace = true
indoc.workspace = true

View file

@ -12,14 +12,38 @@ pub struct Anchor {
pub buffer_id: Option<BufferId>,
pub excerpt_id: ExcerptId,
pub text_anchor: text::Anchor,
pub diff_base_anchor: Option<text::Anchor>,
}
impl Anchor {
pub fn in_buffer(
excerpt_id: ExcerptId,
buffer_id: BufferId,
text_anchor: text::Anchor,
) -> Self {
Self {
buffer_id: Some(buffer_id),
excerpt_id,
text_anchor,
diff_base_anchor: None,
}
}
pub fn range_in_buffer(
excerpt_id: ExcerptId,
buffer_id: BufferId,
range: Range<text::Anchor>,
) -> Range<Self> {
Self::in_buffer(excerpt_id, buffer_id, range.start)
..Self::in_buffer(excerpt_id, buffer_id, range.end)
}
pub fn min() -> Self {
Self {
buffer_id: None,
excerpt_id: ExcerptId::min(),
text_anchor: text::Anchor::MIN,
diff_base_anchor: None,
}
}
@ -28,22 +52,47 @@ impl Anchor {
buffer_id: None,
excerpt_id: ExcerptId::max(),
text_anchor: text::Anchor::MAX,
diff_base_anchor: None,
}
}
pub fn cmp(&self, other: &Anchor, snapshot: &MultiBufferSnapshot) -> Ordering {
let excerpt_id_cmp = self.excerpt_id.cmp(&other.excerpt_id, snapshot);
if excerpt_id_cmp.is_eq() {
if self.excerpt_id == ExcerptId::min() || self.excerpt_id == ExcerptId::max() {
Ordering::Equal
} else if let Some(excerpt) = snapshot.excerpt(self.excerpt_id) {
self.text_anchor.cmp(&other.text_anchor, &excerpt.buffer)
} else {
Ordering::Equal
}
} else {
excerpt_id_cmp
if excerpt_id_cmp.is_ne() {
return excerpt_id_cmp;
}
if self.excerpt_id == ExcerptId::min() || self.excerpt_id == ExcerptId::max() {
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() {
if let Some(diff_base) = snapshot.diffs.get(&excerpt.buffer_id) {
let self_anchor = self
.diff_base_anchor
.filter(|a| diff_base.base_text.can_resolve(a));
let other_anchor = other
.diff_base_anchor
.filter(|a| diff_base.base_text.can_resolve(a));
return match (self_anchor, other_anchor) {
(Some(a), Some(b)) => a.cmp(&b, &diff_base.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 {
@ -57,6 +106,14 @@ impl Anchor {
buffer_id: self.buffer_id,
excerpt_id: self.excerpt_id,
text_anchor: self.text_anchor.bias_left(&excerpt.buffer),
diff_base_anchor: self.diff_base_anchor.map(|a| {
if let Some(base) = snapshot.diffs.get(&excerpt.buffer_id) {
if a.buffer_id == Some(base.base_text.remote_id()) {
return a.bias_left(&base.base_text);
}
}
a
}),
};
}
}
@ -70,6 +127,14 @@ impl Anchor {
buffer_id: self.buffer_id,
excerpt_id: self.excerpt_id,
text_anchor: self.text_anchor.bias_right(&excerpt.buffer),
diff_base_anchor: self.diff_base_anchor.map(|a| {
if let Some(base) = snapshot.diffs.get(&excerpt.buffer_id) {
if a.buffer_id == Some(base.base_text.remote_id()) {
return a.bias_right(&base.base_text);
}
}
a
}),
};
}
}

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,264 @@
use std::{
fmt::{Debug, Display},
marker::PhantomData,
ops::{Add, AddAssign, Sub, SubAssign},
};
use text::Point;
#[repr(transparent)]
pub struct TypedOffset<T> {
pub value: usize,
_marker: PhantomData<T>,
}
#[repr(transparent)]
pub struct TypedPoint<T> {
pub value: Point,
_marker: PhantomData<T>,
}
#[repr(transparent)]
pub struct TypedRow<T> {
pub value: u32,
_marker: PhantomData<T>,
}
impl<T> TypedOffset<T> {
pub fn new(offset: usize) -> Self {
Self {
value: offset,
_marker: PhantomData,
}
}
pub fn saturating_sub(self, n: TypedOffset<T>) -> Self {
Self {
value: self.value.saturating_sub(n.value),
_marker: PhantomData,
}
}
pub fn zero() -> Self {
Self::new(0)
}
pub fn is_zero(&self) -> bool {
self.value == 0
}
}
impl<T> TypedPoint<T> {
pub fn new(row: u32, column: u32) -> Self {
Self {
value: Point::new(row, column),
_marker: PhantomData,
}
}
pub fn wrap(point: Point) -> Self {
Self {
value: point,
_marker: PhantomData,
}
}
pub fn row(&self) -> u32 {
self.value.row
}
pub fn column(&self) -> u32 {
self.value.column
}
pub fn zero() -> Self {
Self::wrap(Point::zero())
}
pub fn is_zero(&self) -> bool {
self.value.is_zero()
}
}
impl<T> TypedRow<T> {
pub fn new(row: u32) -> Self {
Self {
value: row,
_marker: PhantomData,
}
}
}
impl<T> Copy for TypedOffset<T> {}
impl<T> Copy for TypedPoint<T> {}
impl<T> Copy for TypedRow<T> {}
impl<T> Clone for TypedOffset<T> {
fn clone(&self) -> Self {
*self
}
}
impl<T> Clone for TypedPoint<T> {
fn clone(&self) -> Self {
*self
}
}
impl<T> Clone for TypedRow<T> {
fn clone(&self) -> Self {
*self
}
}
impl<T> Default for TypedOffset<T> {
fn default() -> Self {
Self::new(0)
}
}
impl<T> Default for TypedPoint<T> {
fn default() -> Self {
Self::wrap(Point::default())
}
}
impl<T> Default for TypedRow<T> {
fn default() -> Self {
Self::new(0)
}
}
impl<T> PartialOrd for TypedOffset<T> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.value.cmp(&other.value))
}
}
impl<T> PartialOrd for TypedPoint<T> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.value.cmp(&other.value))
}
}
impl<T> PartialOrd for TypedRow<T> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.value.cmp(&other.value))
}
}
impl<T> Ord for TypedOffset<T> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.value.cmp(&other.value)
}
}
impl<T> Ord for TypedPoint<T> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.value.cmp(&other.value)
}
}
impl<T> Ord for TypedRow<T> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.value.cmp(&other.value)
}
}
impl<T> PartialEq for TypedOffset<T> {
fn eq(&self, other: &Self) -> bool {
self.value == other.value
}
}
impl<T> PartialEq for TypedPoint<T> {
fn eq(&self, other: &Self) -> bool {
self.value == other.value
}
}
impl<T> PartialEq for TypedRow<T> {
fn eq(&self, other: &Self) -> bool {
self.value == other.value
}
}
impl<T> Eq for TypedOffset<T> {}
impl<T> Eq for TypedPoint<T> {}
impl<T> Eq for TypedRow<T> {}
impl<T> Debug for TypedOffset<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}Offset({})", type_name::<T>(), self.value)
}
}
impl<T> Debug for TypedPoint<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}Point({}, {})",
type_name::<T>(),
self.value.row,
self.value.column
)
}
}
impl<T> Debug for TypedRow<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}Row({})", type_name::<T>(), self.value)
}
}
impl<T> Display for TypedOffset<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.value, f)
}
}
impl<T> Display for TypedRow<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.value, f)
}
}
fn type_name<T>() -> &'static str {
std::any::type_name::<T>().split("::").last().unwrap()
}
impl<T> Add<TypedOffset<T>> for TypedOffset<T> {
type Output = Self;
fn add(self, other: Self) -> Self {
TypedOffset::new(self.value + other.value)
}
}
impl<T> Add<TypedPoint<T>> for TypedPoint<T> {
type Output = Self;
fn add(self, other: Self) -> Self {
TypedPoint::wrap(self.value + other.value)
}
}
impl<T> Sub<TypedOffset<T>> for TypedOffset<T> {
type Output = Self;
fn sub(self, other: Self) -> Self {
TypedOffset::new(self.value - other.value)
}
}
impl<T> Sub<TypedPoint<T>> for TypedPoint<T> {
type Output = Self;
fn sub(self, other: Self) -> Self {
TypedPoint::wrap(self.value - other.value)
}
}
impl<T> AddAssign<TypedOffset<T>> for TypedOffset<T> {
fn add_assign(&mut self, other: Self) {
self.value += other.value;
}
}
impl<T> AddAssign<TypedPoint<T>> for TypedPoint<T> {
fn add_assign(&mut self, other: Self) {
self.value += other.value;
}
}
impl<T> SubAssign<Self> for TypedOffset<T> {
fn sub_assign(&mut self, other: Self) {
self.value -= other.value;
}
}
impl<T> SubAssign<Self> for TypedRow<T> {
fn sub_assign(&mut self, other: Self) {
self.value -= other.value;
}
}