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;
let is_expanded = if can_expand {
active_thread
.read(cx) .read(cx)
.expanded_code_blocks .expanded_code_blocks
.get(&(message_id, ix)) .get(&(message_id, ix))
.copied() .copied()
.unwrap_or(true); .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,9 +560,7 @@ fn render_markdown_code_block(
} }
}), }),
) )
.when( .when(can_expand, |header| {
metadata.line_count > MAX_UNCOLLAPSED_LINES_IN_CODE_BLOCK,
|header| {
header.child( header.child(
IconButton::new( IconButton::new(
("expand-collapse-code", ix), ("expand-collapse-code", ix),
@ -586,12 +591,11 @@ fn render_markdown_code_block(
} }
}), }),
) )
}, }),
),
); );
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()