editor: Add revert file action to command palette (#16012)

CharlesChen0823 created

Release Notes:

- Added an `editor::RevertFile` action

Change summary

crates/editor/src/actions.rs   |  1 +
crates/editor/src/editor.rs    | 16 ++++++++++++++++
crates/editor/src/element.rs   |  1 +
crates/editor/src/hunk_diff.rs | 32 +++-----------------------------
4 files changed, 21 insertions(+), 29 deletions(-)

Detailed changes

crates/editor/src/actions.rs 🔗

@@ -266,6 +266,7 @@ gpui::actions!(
         RestartLanguageServer,
         RevealInFileManager,
         ReverseLines,
+        RevertFile,
         RevertSelectedHunks,
         ScrollCursorBottom,
         ScrollCursorCenter,

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() {

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)
     }

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,
         )