Misc improvement of code for agent markdown codeblock (#30388)

Release Notes:

- N/A
This commit is contained in:
Michael Sloan 2025-05-09 16:48:24 +02:00 committed by GitHub
parent 9cff5cfe3a
commit 08f516ce9a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -327,6 +327,7 @@ fn tool_use_markdown_style(window: &Window, cx: &mut App) -> MarkdownStyle {
} }
} }
const CODEBLOCK_CONTAINER_GROUP: &str = "codeblock_container";
const MAX_UNCOLLAPSED_LINES_IN_CODE_BLOCK: usize = 10; const MAX_UNCOLLAPSED_LINES_IN_CODE_BLOCK: usize = 10;
fn render_markdown_code_block( fn render_markdown_code_block(
@ -485,12 +486,18 @@ fn render_markdown_code_block(
.copied_code_block_ids .copied_code_block_ids
.contains(&(message_id, ix)); .contains(&(message_id, ix));
let is_expanded = active_thread let can_expand = metadata.line_count > MAX_UNCOLLAPSED_LINES_IN_CODE_BLOCK;
.read(cx)
.expanded_code_blocks let is_expanded = if can_expand {
.get(&(message_id, ix)) active_thread
.copied() .read(cx)
.unwrap_or(true); .expanded_code_blocks
.get(&(message_id, ix))
.copied()
.unwrap_or(false)
} else {
false
};
let codeblock_header_bg = cx let codeblock_header_bg = cx
.theme() .theme()
@ -511,7 +518,7 @@ fn render_markdown_code_block(
.children(label) .children(label)
.child( .child(
h_flex() h_flex()
.visible_on_hover("codeblock_container") .visible_on_hover(CODEBLOCK_CONTAINER_GROUP)
.gap_1() .gap_1()
.child( .child(
IconButton::new( IconButton::new(
@ -553,45 +560,42 @@ fn render_markdown_code_block(
} }
}), }),
) )
.when( .when(can_expand, |header| {
metadata.line_count > MAX_UNCOLLAPSED_LINES_IN_CODE_BLOCK, header.child(
|header| { IconButton::new(
header.child( ("expand-collapse-code", ix),
IconButton::new( if is_expanded {
("expand-collapse-code", ix), IconName::ChevronUp
if is_expanded {
IconName::ChevronUp
} else {
IconName::ChevronDown
},
)
.icon_color(Color::Muted)
.shape(ui::IconButtonShape::Square)
.tooltip(Tooltip::text(if is_expanded {
"Collapse Code"
} else { } else {
"Expand Code" IconName::ChevronDown
})) },
.on_click({
let active_thread = active_thread.clone();
move |_event, _window, cx| {
active_thread.update(cx, |this, cx| {
let is_expanded = this
.expanded_code_blocks
.entry((message_id, ix))
.or_insert(true);
*is_expanded = !*is_expanded;
cx.notify();
});
}
}),
) )
}, .icon_color(Color::Muted)
), .shape(ui::IconButtonShape::Square)
.tooltip(Tooltip::text(if is_expanded {
"Collapse Code"
} else {
"Expand Code"
}))
.on_click({
let active_thread = active_thread.clone();
move |_event, _window, cx| {
active_thread.update(cx, |this, cx| {
let is_expanded = this
.expanded_code_blocks
.entry((message_id, ix))
.or_insert(true);
*is_expanded = !*is_expanded;
cx.notify();
});
}
}),
)
}),
); );
v_flex() v_flex()
.group("codeblock_container") .group(CODEBLOCK_CONTAINER_GROUP)
.my_2() .my_2()
.overflow_hidden() .overflow_hidden()
.rounded_lg() .rounded_lg()
@ -599,10 +603,7 @@ fn render_markdown_code_block(
.border_color(cx.theme().colors().border.opacity(0.6)) .border_color(cx.theme().colors().border.opacity(0.6))
.bg(cx.theme().colors().editor_background) .bg(cx.theme().colors().editor_background)
.child(codeblock_header) .child(codeblock_header)
.when( .when(can_expand && !is_expanded, |this| this.max_h_80())
metadata.line_count > MAX_UNCOLLAPSED_LINES_IN_CODE_BLOCK && !is_expanded,
|this| this.max_h_80(),
)
} }
fn render_code_language( fn render_code_language(
@ -2353,19 +2354,22 @@ impl ActiveThread {
let editor_bg = cx.theme().colors().editor_background; let editor_bg = cx.theme().colors().editor_background;
move |el, range, metadata, _, cx| { move |el, range, metadata, _, cx| {
let can_expand = metadata.line_count
> MAX_UNCOLLAPSED_LINES_IN_CODE_BLOCK;
if !can_expand {
return el;
}
let is_expanded = active_thread let is_expanded = active_thread
.read(cx) .read(cx)
.expanded_code_blocks .expanded_code_blocks
.get(&(message_id, range.start)) .get(&(message_id, range.start))
.copied() .copied()
.unwrap_or(true); .unwrap_or(false);
if is_expanded {
if is_expanded
|| metadata.line_count
<= MAX_UNCOLLAPSED_LINES_IN_CODE_BLOCK
{
return el; return el;
} }
el.child( el.child(
div() div()
.absolute() .absolute()