From 3de3a369f502ec5f802ea64a1c3f190269870c2d Mon Sep 17 00:00:00 2001
From: Josh Piasecki <138541977+FloppyDisco@users.noreply.github.com>
Date: Thu, 13 Nov 2025 21:33:53 -0600
Subject: [PATCH] editor: Add `diffs_expanded` to key context when diff hunks
are expanded (#40617)
including a new identifier on the Editor key context will allow for some
more flexibility when creating keybindings.
for example i would like to be able to set the following:
```json
{
"context": "Editor",
"bindings": {
"pageup": ["editor::MovePageUp", { "center_cursor": true }],
"pagedown": ["editor::MovePageDown", { "center_cursor": true }],
}
},
{
"context": "Editor && diffs_expanded",
"bindings": {
"pageup": "editor::GoToPrevHunk",
"pagedown": "editor::GoToHunk",
}
},
```
very open to suggestions for the name. that's the best i could come up
with.
the action *IS* called `editor::ExpandAllDiffHunks` so this seems
fitting.
the identifier is included if *any* diff hunk is visible, even if some
of them have been closed using `editor::ToggleSelectedDiffHunk`
Release Notes:
- The Editor key context now includes 'diffs_expanded' when diff changes
are visible
---
crates/editor/src/editor.rs | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs
index 0d3325ff8212b6ae9dcc5a9c34dd13e4c5324178..8cb3d1abf7d026e7201c60834f355d0f5e56671d 100644
--- a/crates/editor/src/editor.rs
+++ b/crates/editor/src/editor.rs
@@ -2631,6 +2631,10 @@ impl Editor {
key_context.add("end_of_input");
}
+ if self.has_any_expanded_diff_hunks(cx) {
+ key_context.add("diffs_expanded");
+ }
+
key_context
}
@@ -19336,6 +19340,16 @@ impl Editor {
})
}
+ fn has_any_expanded_diff_hunks(&self, cx: &App) -> bool {
+ if self.buffer.read(cx).all_diff_hunks_expanded() {
+ return true;
+ }
+ let ranges = vec![Anchor::min()..Anchor::max()];
+ self.buffer
+ .read(cx)
+ .has_expanded_diff_hunks_in_ranges(&ranges, cx)
+ }
+
fn toggle_diff_hunks_in_ranges(
&mut self,
ranges: Vec>,