Allow to toggle git hunk diffs (#11080)

Part of https://github.com/zed-industries/zed/issues/4523

Added two new actions with the default keybindings

```
"cmd-'": "editor::ToggleHunkDiff",
"cmd-\"": "editor::ExpandAllHunkDiffs",
```

that allow to browse git hunk diffs in Zed:


https://github.com/zed-industries/zed/assets/2690773/9a8a7d10-ed06-4960-b4ee-fe28fc5c4768


The hunks are dynamic and alter on user folds and modifications, or
toggle hidden, if the modifications were not adjacent to the expanded
hunk.


Release Notes:

- Added `editor::ToggleHunkDiff` (`cmd-'`) and
`editor::ExpandAllHunkDiffs` (`cmd-"`) actions to browse git hunk diffs
in Zed
This commit is contained in:
Kirill Bulatov 2024-05-01 22:47:36 +03:00 committed by GitHub
parent 5831d80f51
commit caa0d35b8b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
24 changed files with 3115 additions and 249 deletions

View file

@ -75,3 +75,93 @@ pub(crate) fn build_editor_with_project(
) -> Editor {
Editor::new(EditorMode::Full, buffer, Some(project), cx)
}
#[cfg(any(test, feature = "test-support"))]
pub fn editor_hunks(
editor: &Editor,
snapshot: &DisplaySnapshot,
cx: &mut ViewContext<'_, Editor>,
) -> Vec<(String, git::diff::DiffHunkStatus, core::ops::Range<u32>)> {
use text::Point;
snapshot
.buffer_snapshot
.git_diff_hunks_in_range(0..u32::MAX)
.map(|hunk| {
let display_range = Point::new(hunk.associated_range.start, 0)
.to_display_point(snapshot)
.row()
..Point::new(hunk.associated_range.end, 0)
.to_display_point(snapshot)
.row();
let (_, buffer, _) = editor
.buffer()
.read(cx)
.excerpt_containing(Point::new(hunk.associated_range.start, 0), cx)
.expect("no excerpt for expanded buffer's hunk start");
let diff_base = &buffer
.read(cx)
.diff_base()
.expect("should have a diff base for expanded hunk")
[hunk.diff_base_byte_range.clone()];
(diff_base.to_owned(), hunk.status(), display_range)
})
.collect()
}
#[cfg(any(test, feature = "test-support"))]
pub fn expanded_hunks(
editor: &Editor,
snapshot: &DisplaySnapshot,
cx: &mut ViewContext<'_, Editor>,
) -> Vec<(String, git::diff::DiffHunkStatus, core::ops::Range<u32>)> {
editor
.expanded_hunks
.hunks(false)
.map(|expanded_hunk| {
let hunk_display_range = expanded_hunk
.hunk_range
.start
.to_display_point(snapshot)
.row()
..expanded_hunk
.hunk_range
.end
.to_display_point(snapshot)
.row();
let (_, buffer, _) = editor
.buffer()
.read(cx)
.excerpt_containing(expanded_hunk.hunk_range.start, cx)
.expect("no excerpt for expanded buffer's hunk start");
let diff_base = &buffer
.read(cx)
.diff_base()
.expect("should have a diff base for expanded hunk")
[expanded_hunk.diff_base_byte_range.clone()];
(
diff_base.to_owned(),
expanded_hunk.status,
hunk_display_range,
)
})
.collect()
}
#[cfg(any(test, feature = "test-support"))]
pub fn expanded_hunks_background_highlights(
editor: &Editor,
snapshot: &DisplaySnapshot,
) -> Vec<core::ops::Range<u32>> {
use itertools::Itertools;
editor
.highlighted_rows::<crate::DiffRowHighlight>()
.into_iter()
.flatten()
.map(|(range, _)| {
range.start.to_display_point(snapshot).row()..range.end.to_display_point(snapshot).row()
})
.unique()
.collect()
}