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

Release Notes:

- Added an `editor::RevertFile` action
This commit is contained in:
CharlesChen0823 2024-08-13 19:44:41 +08:00 committed by GitHub
parent ab98f16280
commit fe190359d5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 21 additions and 29 deletions

View file

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

View file

@ -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>) { pub fn revert_selected_hunks(&mut self, _: &RevertSelectedHunks, cx: &mut ViewContext<Self>) {
let revert_changes = self.gather_revert_changes(&self.selections.disjoint_anchors(), cx); let revert_changes = self.gather_revert_changes(&self.selections.disjoint_anchors(), cx);
if !revert_changes.is_empty() { if !revert_changes.is_empty() {

View file

@ -413,6 +413,7 @@ impl EditorElement {
register_action(view, cx, Editor::unique_lines_case_sensitive); register_action(view, cx, Editor::unique_lines_case_sensitive);
register_action(view, cx, Editor::accept_partial_inline_completion); register_action(view, cx, Editor::accept_partial_inline_completion);
register_action(view, cx, Editor::accept_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::revert_selected_hunks);
register_action(view, cx, Editor::open_active_item_in_terminal) register_action(view, cx, Editor::open_active_item_in_terminal)
} }

View file

@ -24,8 +24,8 @@ use crate::{
hunk_status, hunks_for_selections, hunk_status, hunks_for_selections,
mouse_context_menu::MouseContextMenu, mouse_context_menu::MouseContextMenu,
BlockDisposition, BlockProperties, BlockStyle, CustomBlockId, DiffRowHighlight, Editor, BlockDisposition, BlockProperties, BlockStyle, CustomBlockId, DiffRowHighlight, Editor,
EditorElement, EditorSnapshot, ExpandAllHunkDiffs, RangeToAnchorExt, RevertSelectedHunks, EditorElement, EditorSnapshot, ExpandAllHunkDiffs, RangeToAnchorExt, RevertFile,
ToDisplayPoint, ToggleHunkDiff, RevertSelectedHunks, ToDisplayPoint, ToggleHunkDiff,
}; };
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -139,33 +139,7 @@ impl Editor {
} }
} }
}) })
.entry("Revert File", None, { .action("Revert File", RevertFile.boxed_clone())
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);
});
});
}
}
})
}), }),
cx, cx,
) )