From a76cd778c4eaf6af69f68f31190563836e25fb89 Mon Sep 17 00:00:00 2001
From: Danilo Leal <67129314+danilo-leal@users.noreply.github.com>
Date: Tue, 3 Dec 2024 10:07:59 -0300
Subject: [PATCH] Disable hunk diff arrow buttons when there's only one hunk
(#21437)
Closes https://github.com/zed-industries/zed/issues/20817
| One hunk | Multiple hunks |
|--------|--------|
|
|
|
Release Notes:
- Fixed showing prev/next hunk navigation buttons when there is only one
hunk
---
crates/editor/src/hunk_diff.rs | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/crates/editor/src/hunk_diff.rs b/crates/editor/src/hunk_diff.rs
index 27bb8ac5574514289d33ec15c8fc11f980b4ff80..3da005cd2c82f437140ac7b2aecc7c5a97fa11bf 100644
--- a/crates/editor/src/hunk_diff.rs
+++ b/crates/editor/src/hunk_diff.rs
@@ -399,6 +399,12 @@ impl Editor {
}
}
+ fn has_multiple_hunks(&self, cx: &AppContext) -> bool {
+ let snapshot = self.buffer.read(cx).snapshot(cx);
+ let mut hunks = snapshot.git_diff_hunks_in_range(MultiBufferRow::MIN..MultiBufferRow::MAX);
+ hunks.nth(1).is_some()
+ }
+
fn hunk_header_block(
&self,
hunk: &HoveredHunk,
@@ -428,6 +434,7 @@ impl Editor {
render: Arc::new({
let editor = cx.view().clone();
let hunk = hunk.clone();
+ let has_multiple_hunks = self.has_multiple_hunks(cx);
move |cx| {
let hunk_controls_menu_handle =
@@ -471,6 +478,7 @@ impl Editor {
IconButton::new("next-hunk", IconName::ArrowDown)
.shape(IconButtonShape::Square)
.icon_size(IconSize::Small)
+ .disabled(!has_multiple_hunks)
.tooltip({
let focus_handle = editor.focus_handle(cx);
move |cx| {
@@ -499,6 +507,7 @@ impl Editor {
IconButton::new("prev-hunk", IconName::ArrowUp)
.shape(IconButtonShape::Square)
.icon_size(IconSize::Small)
+ .disabled(!has_multiple_hunks)
.tooltip({
let focus_handle = editor.focus_handle(cx);
move |cx| {