Keep all hunks expanded in proposed change editor (#18598)

Also, fix visual bug when pressing escape with a non-empty selection in
a deleted text block.

Release Notes:

- N/A

Co-authored-by: Antonio <antonio@zed.dev>
This commit is contained in:
Max Brunsfeld 2024-10-01 12:58:12 -06:00 committed by GitHub
parent 9b148f3dcc
commit 7dcb0de28c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 259 additions and 190 deletions

View file

@ -3059,7 +3059,7 @@ impl Editor {
} }
pub fn cancel(&mut self, _: &Cancel, cx: &mut ViewContext<Self>) { pub fn cancel(&mut self, _: &Cancel, cx: &mut ViewContext<Self>) {
if self.clear_clicked_diff_hunks(cx) { if self.clear_expanded_diff_hunks(cx) {
cx.notify(); cx.notify();
return; return;
} }

View file

@ -32,6 +32,7 @@ pub(super) struct ExpandedHunks {
pub(crate) hunks: Vec<ExpandedHunk>, pub(crate) hunks: Vec<ExpandedHunk>,
diff_base: HashMap<BufferId, DiffBaseBuffer>, diff_base: HashMap<BufferId, DiffBaseBuffer>,
hunk_update_tasks: HashMap<Option<BufferId>, Task<()>>, hunk_update_tasks: HashMap<Option<BufferId>, Task<()>>,
expand_all: bool,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -72,6 +73,10 @@ impl ExpandedHunks {
} }
impl Editor { impl Editor {
pub fn set_expand_all_diff_hunks(&mut self) {
self.expanded_hunks.expand_all = true;
}
pub(super) fn toggle_hovered_hunk( pub(super) fn toggle_hovered_hunk(
&mut self, &mut self,
hovered_hunk: &HoveredHunk, hovered_hunk: &HoveredHunk,
@ -133,6 +138,10 @@ impl Editor {
hunks_to_toggle: Vec<MultiBufferDiffHunk>, hunks_to_toggle: Vec<MultiBufferDiffHunk>,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) { ) {
if self.expanded_hunks.expand_all {
return;
}
let previous_toggle_task = self.expanded_hunks.hunk_update_tasks.remove(&None); let previous_toggle_task = self.expanded_hunks.hunk_update_tasks.remove(&None);
let new_toggle_task = cx.spawn(move |editor, mut cx| async move { let new_toggle_task = cx.spawn(move |editor, mut cx| async move {
if let Some(task) = previous_toggle_task { if let Some(task) = previous_toggle_task {
@ -426,7 +435,8 @@ impl Editor {
.child( .child(
h_flex() h_flex()
.gap_1() .gap_1()
.child( .when(!is_branch_buffer, |row| {
row.child(
IconButton::new("next-hunk", IconName::ArrowDown) IconButton::new("next-hunk", IconName::ArrowDown)
.shape(IconButtonShape::Square) .shape(IconButtonShape::Square)
.icon_size(IconSize::Small) .icon_size(IconSize::Small)
@ -482,6 +492,7 @@ impl Editor {
} }
}), }),
) )
})
.child( .child(
IconButton::new("discard", IconName::Undo) IconButton::new("discard", IconName::Undo)
.shape(IconButtonShape::Square) .shape(IconButtonShape::Square)
@ -527,13 +538,15 @@ impl Editor {
} }
}), }),
) )
.when(is_branch_buffer, |this| { .map(|this| {
if is_branch_buffer {
this.child( this.child(
IconButton::new("apply", IconName::Check) IconButton::new("apply", IconName::Check)
.shape(IconButtonShape::Square) .shape(IconButtonShape::Square)
.icon_size(IconSize::Small) .icon_size(IconSize::Small)
.tooltip({ .tooltip({
let focus_handle = editor.focus_handle(cx); let focus_handle =
editor.focus_handle(cx);
move |cx| { move |cx| {
Tooltip::for_action_in( Tooltip::for_action_in(
"Apply Hunk", "Apply Hunk",
@ -549,15 +562,16 @@ impl Editor {
move |_event, cx| { move |_event, cx| {
editor.update(cx, |editor, cx| { editor.update(cx, |editor, cx| {
editor.apply_changes_in_range( editor.apply_changes_in_range(
hunk.multi_buffer_range.clone(), hunk.multi_buffer_range
.clone(),
cx, cx,
); );
}); });
} }
}), }),
) )
}) } else {
.child({ this.child({
let focus = editor.focus_handle(cx); let focus = editor.focus_handle(cx);
PopoverMenu::new("hunk-controls-dropdown") PopoverMenu::new("hunk-controls-dropdown")
.trigger( .trigger(
@ -569,13 +583,18 @@ impl Editor {
.icon_size(IconSize::Small) .icon_size(IconSize::Small)
.style(ButtonStyle::Subtle) .style(ButtonStyle::Subtle)
.selected( .selected(
hunk_controls_menu_handle.is_deployed(), hunk_controls_menu_handle
.is_deployed(),
) )
.when( .when(
!hunk_controls_menu_handle.is_deployed(), !hunk_controls_menu_handle
.is_deployed(),
|this| { |this| {
this.tooltip(|cx| { this.tooltip(|cx| {
Tooltip::text("Hunk Controls", cx) Tooltip::text(
"Hunk Controls",
cx,
)
}) })
}, },
), ),
@ -584,18 +603,25 @@ impl Editor {
.with_handle(hunk_controls_menu_handle) .with_handle(hunk_controls_menu_handle)
.menu(move |cx| { .menu(move |cx| {
let focus = focus.clone(); let focus = focus.clone();
let menu = let menu = ContextMenu::build(
ContextMenu::build(cx, move |menu, _| { cx,
menu.context(focus.clone()).action( move |menu, _| {
menu.context(focus.clone())
.action(
"Discard All", "Discard All",
RevertFile.boxed_clone(), RevertFile
.boxed_clone(),
) )
}); },
);
Some(menu) Some(menu)
}) })
})
}
}), }),
) )
.child( .when(!is_branch_buffer, |div| {
div.child(
IconButton::new("collapse", IconName::Close) IconButton::new("collapse", IconName::Close)
.shape(IconButtonShape::Square) .shape(IconButtonShape::Square)
.icon_size(IconSize::Small) .icon_size(IconSize::Small)
@ -619,7 +645,8 @@ impl Editor {
}); });
} }
}), }),
), )
}),
) )
.into_any_element() .into_any_element()
} }
@ -694,7 +721,10 @@ impl Editor {
} }
} }
pub(super) fn clear_clicked_diff_hunks(&mut self, cx: &mut ViewContext<'_, Editor>) -> bool { pub(super) fn clear_expanded_diff_hunks(&mut self, cx: &mut ViewContext<'_, Editor>) -> bool {
if self.expanded_hunks.expand_all {
return false;
}
self.expanded_hunks.hunk_update_tasks.clear(); self.expanded_hunks.hunk_update_tasks.clear();
self.clear_row_highlights::<DiffRowHighlight>(); self.clear_row_highlights::<DiffRowHighlight>();
let to_remove = self let to_remove = self
@ -798,16 +828,27 @@ impl Editor {
status, status,
} => { } => {
let hunk_display_range = display_row_range; let hunk_display_range = display_row_range;
if expanded_hunk_display_range.start if expanded_hunk_display_range.start
> hunk_display_range.end > hunk_display_range.end
{ {
recalculated_hunks.next(); recalculated_hunks.next();
if editor.expanded_hunks.expand_all {
hunks_to_reexpand.push(HoveredHunk {
status,
multi_buffer_range,
diff_base_byte_range,
});
}
continue; continue;
} else if expanded_hunk_display_range.end }
if expanded_hunk_display_range.end
< hunk_display_range.start < hunk_display_range.start
{ {
break; break;
} else { }
if !expanded_hunk.folded if !expanded_hunk.folded
&& expanded_hunk_display_range == hunk_display_range && expanded_hunk_display_range == hunk_display_range
&& expanded_hunk.status == hunk_status(buffer_hunk) && expanded_hunk.status == hunk_status(buffer_hunk)
@ -828,7 +869,6 @@ impl Editor {
} }
} }
} }
}
if !retain { if !retain {
blocks_to_remove.extend(expanded_hunk.blocks.drain(..)); blocks_to_remove.extend(expanded_hunk.blocks.drain(..));
highlights_to_remove.push(expanded_hunk.hunk_range.clone()); highlights_to_remove.push(expanded_hunk.hunk_range.clone());
@ -836,6 +876,26 @@ impl Editor {
retain retain
}); });
if editor.expanded_hunks.expand_all {
for hunk in recalculated_hunks {
match diff_hunk_to_display(&hunk, &snapshot) {
DisplayDiffHunk::Folded { .. } => {}
DisplayDiffHunk::Unfolded {
diff_base_byte_range,
multi_buffer_range,
status,
..
} => {
hunks_to_reexpand.push(HoveredHunk {
status,
multi_buffer_range,
diff_base_byte_range,
});
}
}
}
}
editor.remove_highlighted_rows::<DiffRowHighlight>(highlights_to_remove, cx); editor.remove_highlighted_rows::<DiffRowHighlight>(highlights_to_remove, cx);
editor.remove_blocks(blocks_to_remove, None, cx); editor.remove_blocks(blocks_to_remove, None, cx);
@ -1000,13 +1060,15 @@ fn editor_with_deleted_text(
editor.scroll_manager.set_forbid_vertical_scroll(true); editor.scroll_manager.set_forbid_vertical_scroll(true);
editor.set_read_only(true); editor.set_read_only(true);
editor.set_show_inline_completions(Some(false), cx); editor.set_show_inline_completions(Some(false), cx);
editor.highlight_rows::<DiffRowHighlight>(
enum DeletedBlockRowHighlight {}
editor.highlight_rows::<DeletedBlockRowHighlight>(
Anchor::min()..Anchor::max(), Anchor::min()..Anchor::max(),
deleted_color, deleted_color,
false, false,
cx, cx,
); );
editor.set_current_line_highlight(Some(CurrentLineHighlight::None)); editor.set_current_line_highlight(Some(CurrentLineHighlight::None)); //
editor editor
._subscriptions ._subscriptions
.extend([cx.on_blur(&editor.focus_handle, |editor, cx| { .extend([cx.on_blur(&editor.focus_handle, |editor, cx| {
@ -1015,17 +1077,20 @@ fn editor_with_deleted_text(
}); });
})]); })]);
let parent_editor_for_reverts = parent_editor.clone();
let original_multi_buffer_range = hunk.multi_buffer_range.clone(); let original_multi_buffer_range = hunk.multi_buffer_range.clone();
let diff_base_range = hunk.diff_base_byte_range.clone(); let diff_base_range = hunk.diff_base_byte_range.clone();
editor editor
.register_action::<RevertSelectedHunks>(move |_, cx| { .register_action::<RevertSelectedHunks>({
parent_editor_for_reverts let parent_editor = parent_editor.clone();
move |_, cx| {
parent_editor
.update(cx, |editor, cx| { .update(cx, |editor, cx| {
let Some((buffer, original_text)) = let Some((buffer, original_text)) =
editor.buffer().update(cx, |buffer, cx| { editor.buffer().update(cx, |buffer, cx| {
let (_, buffer, _) = buffer let (_, buffer, _) = buffer.excerpt_containing(
.excerpt_containing(original_multi_buffer_range.start, cx)?; original_multi_buffer_range.start,
cx,
)?;
let original_text = let original_text =
buffer.read(cx).diff_base()?.slice(diff_base_range.clone()); buffer.read(cx).diff_base()?.slice(diff_base_range.clone());
Some((buffer, Arc::from(original_text.to_string()))) Some((buffer, Arc::from(original_text.to_string())))
@ -1046,6 +1111,7 @@ fn editor_with_deleted_text(
}); });
}) })
.ok(); .ok();
}
}) })
.detach(); .detach();
let hunk = hunk.clone(); let hunk = hunk.clone();

View file

@ -63,8 +63,11 @@ impl ProposedChangesEditor {
let (recalculate_diffs_tx, mut recalculate_diffs_rx) = mpsc::unbounded(); let (recalculate_diffs_tx, mut recalculate_diffs_rx) = mpsc::unbounded();
Self { Self {
editor: cx editor: cx.new_view(|cx| {
.new_view(|cx| Editor::for_multibuffer(multibuffer.clone(), project, true, cx)), let mut editor = Editor::for_multibuffer(multibuffer.clone(), project, true, cx);
editor.set_expand_all_diff_hunks();
editor
}),
recalculate_diffs_tx, recalculate_diffs_tx,
_recalculate_diffs_task: cx.spawn(|_, mut cx| async move { _recalculate_diffs_task: cx.spawn(|_, mut cx| async move {
let mut buffers_to_diff = HashSet::default(); let mut buffers_to_diff = HashSet::default();