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>) {
if self.clear_clicked_diff_hunks(cx) {
if self.clear_expanded_diff_hunks(cx) {
cx.notify();
return;
}

View file

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

View file

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