From 4ee2daeded8da156f2d2319b15e0c3d2a2e16dc7 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 17 Sep 2025 08:48:47 +0200 Subject: [PATCH] markdown: Fix indented codeblocks having incorrect content ranges (#38225) Closes https://github.com/zed-industries/zed/issues/37743 Release Notes: - Fixed agent panel panicking when streaming indented codeblocks from agent output --- crates/markdown/src/markdown.rs | 4 ++-- crates/markdown/src/parser.rs | 25 +++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/crates/markdown/src/markdown.rs b/crates/markdown/src/markdown.rs index 4e1d3ac51e148439e57a4a1c305dabc31cbc2046..c2f8025e32d70cdd9500afdf0a4fc02a334a8521 100644 --- a/crates/markdown/src/markdown.rs +++ b/crates/markdown/src/markdown.rs @@ -1079,7 +1079,7 @@ impl Element for MarkdownElement { { builder.modify_current_div(|el| { let content_range = parser::extract_code_block_content_range( - parsed_markdown.source()[range.clone()].trim(), + &parsed_markdown.source()[range.clone()], ); let content_range = content_range.start + range.start ..content_range.end + range.start; @@ -1110,7 +1110,7 @@ impl Element for MarkdownElement { { builder.modify_current_div(|el| { let content_range = parser::extract_code_block_content_range( - parsed_markdown.source()[range.clone()].trim(), + &parsed_markdown.source()[range.clone()], ); let content_range = content_range.start + range.start ..content_range.end + range.start; diff --git a/crates/markdown/src/parser.rs b/crates/markdown/src/parser.rs index 3720e5b1ef5f61f0a209ac5617119de61ed05517..d60d34b41e7efc99970f72b15a8ea9c4c79eb6f9 100644 --- a/crates/markdown/src/parser.rs +++ b/crates/markdown/src/parser.rs @@ -67,7 +67,7 @@ pub fn parse_markdown( MarkdownTag::CodeBlock { kind: CodeBlockKind::Indented, metadata: CodeBlockMetadata { - content_range: range.start + 1..range.end + 1, + content_range: range.clone(), line_count: 1, }, } @@ -698,7 +698,28 @@ mod tests { HashSet::from(["rust".into()]), HashSet::new() ) - ) + ); + assert_eq!( + parse_markdown(" fn main() {}"), + ( + vec![ + ( + 4..16, + Start(CodeBlock { + kind: CodeBlockKind::Indented, + metadata: CodeBlockMetadata { + content_range: 4..16, + line_count: 1 + } + }) + ), + (4..16, Text), + (4..16, End(MarkdownTagEnd::CodeBlock)) + ], + HashSet::new(), + HashSet::new() + ) + ); } #[test]