From fe190359d5d17dd8168c0c60d72c62eb8295d532 Mon Sep 17 00:00:00 2001 From: CharlesChen0823 Date: Tue, 13 Aug 2024 19:44:41 +0800 Subject: [PATCH] editor: Add revert file action to command palette (#16012) Release Notes: - Added an `editor::RevertFile` action --- 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(-) diff --git a/crates/editor/src/actions.rs b/crates/editor/src/actions.rs index 5e0b872695794d349a6e15cdf2c1f850fa3fb2ae..c060efd4e6241bfcaa132101858ba72e1d0ae288 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.rs index 9ba802426a964ad51ab89182f47972af908b1cdf..431310ca1ea24495b38a5c966088342de519381f 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) { + 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) { 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.rs index 4f8382a6e98b55cf9c274585a33f341d89c54b7c..44076193c1bd857af03a36bb76761e37e2aa267a 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.rs index 2c4438eb3bcf23538ee5966f0d6178f6ba911097..11f1da68e999b9eef41d1262081e7acfbabcf556 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, )