Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
editor: Add revert file action to command palette (#16012)
CharlesChen0823fe190359d5d1
diff --git a/crates/editor/src/actions.rs b/crates/editor/src/actions.rsindex 5e0b8726957..c060efd4e62 100644--- a/crates/editor/src/actions.rs+++ b/crates/editor/src/actions.rs@@ -266,6 +266,7 @@ gpui::actions!(RestartLanguageServer,RevealInFileManager,ReverseLines,+ RevertFile,RevertSelectedHunks,ScrollCursorBottom,ScrollCursorCenter,diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rsindex 9ba802426a9..431310ca1ea 100644--- a/crates/editor/src/editor.rs+++ b/crates/editor/src/editor.rs@@ -5933,6 +5933,22 @@ impl Editor {})}+ pub fn revert_file(&mut self, _: &RevertFile, cx: &mut ViewContext<Self>) {+ let mut revert_changes = HashMap::default();+ let multi_buffer_snapshot = self.buffer.read(cx).snapshot(cx);+ for hunk in hunks_for_rows(+ Some(MultiBufferRow(0)..multi_buffer_snapshot.max_buffer_row()).into_iter(),+ &multi_buffer_snapshot,+ ) {+ Self::prepare_revert_change(&mut revert_changes, &self.buffer(), &hunk, cx);+ }+ if !revert_changes.is_empty() {+ self.transact(cx, |editor, cx| {+ editor.revert(revert_changes, cx);+ });+ }+ }+pub fn revert_selected_hunks(&mut self, _: &RevertSelectedHunks, cx: &mut ViewContext<Self>) {let revert_changes = self.gather_revert_changes(&self.selections.disjoint_anchors(), cx);if !revert_changes.is_empty() {diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rsindex 4f8382a6e98..44076193c1b 100644--- a/crates/editor/src/element.rs+++ b/crates/editor/src/element.rs@@ -413,6 +413,7 @@ impl EditorElement {register_action(view, cx, Editor::unique_lines_case_sensitive);register_action(view, cx, Editor::accept_partial_inline_completion);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::open_active_item_in_terminal)}diff --git a/crates/editor/src/hunk_diff.rs b/crates/editor/src/hunk_diff.rsindex 2c4438eb3bc..11f1da68e99 100644--- a/crates/editor/src/hunk_diff.rs+++ b/crates/editor/src/hunk_diff.rs@@ -24,8 +24,8 @@ use crate::{hunk_status, hunks_for_selections,mouse_context_menu::MouseContextMenu,BlockDisposition, BlockProperties, BlockStyle, CustomBlockId, DiffRowHighlight, Editor,- EditorElement, EditorSnapshot, ExpandAllHunkDiffs, RangeToAnchorExt, RevertSelectedHunks,- ToDisplayPoint, ToggleHunkDiff,+ EditorElement, EditorSnapshot, ExpandAllHunkDiffs, RangeToAnchorExt, RevertFile,+ RevertSelectedHunks, ToDisplayPoint, ToggleHunkDiff,};#[derive(Debug, Clone)]@@ -139,33 +139,7 @@ impl Editor {}}})- .entry("Revert File", None, {- let editor = editor_handle.clone();- move |cx| {- let mut revert_changes = HashMap::default();- let multi_buffer = editor.read(cx).buffer().clone();- let multi_buffer_snapshot = multi_buffer.read(cx).snapshot(cx);- for hunk in crate::hunks_for_rows(- Some(MultiBufferRow(0)..multi_buffer_snapshot.max_buffer_row())- .into_iter(),- &multi_buffer_snapshot,- ) {- Editor::prepare_revert_change(- &mut revert_changes,- &multi_buffer,- &hunk,- cx,- );- }- if !revert_changes.is_empty() {- editor.update(cx, |editor, cx| {- editor.transact(cx, |editor, cx| {- editor.revert(revert_changes, cx);- });- });- }- }- })+ .action("Revert File", RevertFile.boxed_clone())}),cx,)