Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Add keybindings for accepting hunks (#19749)
Richard Feldmanc9db1b9a7bc2
diff --git a/assets/keymaps/default-linux.json b/assets/keymaps/default-linux.jsonindex fca38a45a43..9df8debb70e 100644--- a/assets/keymaps/default-linux.json+++ b/assets/keymaps/default-linux.json@@ -505,6 +505,13 @@"ctrl-enter": "assistant::InlineAssist"}},+ {+ "context": "ProposedChangesEditor",+ "bindings": {+ "ctrl-shift-y": "editor::ApplyDiffHunk",+ "ctrl-alt-a": "editor::ApplyAllDiffHunks"+ }+ },{"context": "Editor && jupyter && !ContextEditor","bindings": {diff --git a/assets/keymaps/default-macos.json b/assets/keymaps/default-macos.jsonindex c39b7c06dae..c0bad344ab6 100644--- a/assets/keymaps/default-macos.json+++ b/assets/keymaps/default-macos.json@@ -538,6 +538,13 @@"ctrl-enter": "assistant::InlineAssist"}},+ {+ "context": "ProposedChangesEditor",+ "bindings": {+ "cmd-shift-y": "editor::ApplyDiffHunk",+ "cmd-shift-a": "editor::ApplyAllDiffHunks"+ }+ },{"context": "PromptEditor","bindings": {diff --git a/crates/editor/src/actions.rs b/crates/editor/src/actions.rsindex 5f866f99972..b190283776e 100644--- a/crates/editor/src/actions.rs+++ b/crates/editor/src/actions.rs@@ -193,6 +193,7 @@ gpui::actions!(AcceptPartialInlineCompletion,AddSelectionAbove,AddSelectionBelow,+ ApplyAllDiffHunks,ApplyDiffHunk,Backspace,Cancel,diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rsindex 7402badc1e9..12e9d74914a 100644--- a/crates/editor/src/element.rs+++ b/crates/editor/src/element.rs@@ -444,6 +444,7 @@ impl EditorElement {register_action(view, cx, Editor::accept_inline_completion);register_action(view, cx, Editor::revert_file);register_action(view, cx, Editor::revert_selected_hunks);+ register_action(view, cx, Editor::apply_all_diff_hunks);register_action(view, cx, Editor::apply_selected_diff_hunks);register_action(view, cx, Editor::open_active_item_in_terminal);register_action(view, cx, Editor::reload_file)diff --git a/crates/editor/src/hunk_diff.rs b/crates/editor/src/hunk_diff.rsindex 9f66d27a644..c55f36a9be4 100644--- a/crates/editor/src/hunk_diff.rs+++ b/crates/editor/src/hunk_diff.rs@@ -16,10 +16,10 @@ use util::RangeExt;use workspace::Item;use crate::{- editor_settings::CurrentLineHighlight, hunk_status, hunks_for_selections, ApplyDiffHunk,- BlockPlacement, BlockProperties, BlockStyle, CustomBlockId, DiffRowHighlight, DisplayRow,- DisplaySnapshot, Editor, EditorElement, ExpandAllHunkDiffs, GoToHunk, GoToPrevHunk, RevertFile,- RevertSelectedHunks, ToDisplayPoint, ToggleHunkDiff,+ editor_settings::CurrentLineHighlight, hunk_status, hunks_for_selections, ApplyAllDiffHunks,+ ApplyDiffHunk, BlockPlacement, BlockProperties, BlockStyle, CustomBlockId, DiffRowHighlight,+ DisplayRow, DisplaySnapshot, Editor, EditorElement, ExpandAllHunkDiffs, GoToHunk, GoToPrevHunk,+ RevertFile, RevertSelectedHunks, ToDisplayPoint, ToggleHunkDiff,};#[derive(Debug, Clone)]@@ -352,7 +352,11 @@ impl Editor {None}- pub(crate) fn apply_all_diff_hunks(&mut self, cx: &mut ViewContext<Self>) {+ pub(crate) fn apply_all_diff_hunks(+ &mut self,+ _: &ApplyAllDiffHunks,+ cx: &mut ViewContext<Self>,+ ) {let buffers = self.buffer.read(cx).all_buffers();for branch_buffer in buffers {branch_buffer.update(cx, |branch_buffer, cx| {diff --git a/crates/editor/src/proposed_changes_editor.rs b/crates/editor/src/proposed_changes_editor.rsindex ae9f251a1f1..ac97fe18da7 100644--- a/crates/editor/src/proposed_changes_editor.rs+++ b/crates/editor/src/proposed_changes_editor.rs@@ -1,4 +1,4 @@-use crate::{Editor, EditorEvent, SemanticsProvider};+use crate::{ApplyAllDiffHunks, Editor, EditorEvent, SemanticsProvider};use collections::HashSet;use futures::{channel::mpsc, future::join_all};use gpui::{AppContext, EventEmitter, FocusableView, Model, Render, Subscription, Task, View};@@ -8,7 +8,7 @@ use project::Project;use smol::stream::StreamExt;use std::{any::TypeId, ops::Range, rc::Rc, time::Duration};use text::ToOffset;-use ui::prelude::*;+use ui::{prelude::*, ButtonLike, KeyBinding};use workspace::{searchable::SearchableItemHandle, Item, ItemHandle as _, ToolbarItemEvent, ToolbarItemLocation,ToolbarItemView, Workspace,@@ -232,7 +232,10 @@ impl ProposedChangesEditor {impl Render for ProposedChangesEditor {fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {- self.editor.clone()+ div()+ .size_full()+ .key_context("ProposedChangesEditor")+ .child(self.editor.clone())}}@@ -331,17 +334,21 @@ impl ProposedChangesEditorToolbar {}impl Render for ProposedChangesEditorToolbar {- fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {- let editor = self.current_editor.clone();- Button::new("apply-changes", "Apply All").on_click(move |_, cx| {- if let Some(editor) = &editor {- editor.update(cx, |editor, cx| {- editor.editor.update(cx, |editor, cx| {- editor.apply_all_diff_hunks(cx);- })- });+ fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {+ let button_like = ButtonLike::new("apply-changes").child(Label::new("Apply All"));++ match &self.current_editor {+ Some(editor) => {+ let focus_handle = editor.focus_handle(cx);+ let keybinding = KeyBinding::for_action_in(&ApplyAllDiffHunks, &focus_handle, cx)+ .map(|binding| binding.into_any_element());++ button_like.children(keybinding).on_click({+ move |_event, cx| focus_handle.dispatch_action(&ApplyAllDiffHunks, cx)+ })}- })+ None => button_like.disabled(true),+ }}}