From 92c99c6bfd9791381c0a11a9dba69d6bc2fe3d75 Mon Sep 17 00:00:00 2001 From: Smit Barmase Date: Sun, 1 Mar 2026 02:42:35 +0530 Subject: [PATCH] markdown: Fix horizontal autoscroll for code blocks in Agent Panel (#50416) Release Notes: - Fixed Agent Panel code blocks not scrolling horizontally during text selection. --- crates/markdown/src/markdown.rs | 34 ++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/crates/markdown/src/markdown.rs b/crates/markdown/src/markdown.rs index 1cd19ffb8f7cfa16ab1aa95af9425690aba78707..087b7153704c215ec27eae653879ffe9f11ebf09 100644 --- a/crates/markdown/src/markdown.rs +++ b/crates/markdown/src/markdown.rs @@ -15,6 +15,7 @@ use ui::Checkbox; use ui::CopyButton; use std::borrow::Cow; +use std::collections::BTreeMap; use std::iter; use std::mem; use std::ops::Range; @@ -246,7 +247,7 @@ pub struct Markdown { fallback_code_block_language: Option, options: Options, copied_code_blocks: HashSet, - code_block_scroll_handles: HashMap, + code_block_scroll_handles: BTreeMap, context_menu_selected_text: Option, } @@ -316,7 +317,7 @@ impl Markdown { parse_links_only: false, }, copied_code_blocks: HashSet::default(), - code_block_scroll_handles: HashMap::default(), + code_block_scroll_handles: BTreeMap::default(), context_menu_selected_text: None, }; this.parse(cx); @@ -341,7 +342,7 @@ impl Markdown { parse_links_only: true, }, copied_code_blocks: HashSet::default(), - code_block_scroll_handles: HashMap::default(), + code_block_scroll_handles: BTreeMap::default(), context_menu_selected_text: None, }; this.parse(cx); @@ -364,6 +365,32 @@ impl Markdown { self.code_block_scroll_handles.clear(); } + fn autoscroll_code_block(&self, source_index: usize, cursor_position: Point) { + let Some((_, scroll_handle)) = self + .code_block_scroll_handles + .range(..=source_index) + .next_back() + else { + return; + }; + + let bounds = scroll_handle.bounds(); + if cursor_position.y < bounds.top() || cursor_position.y > bounds.bottom() { + return; + } + + let horizontal_delta = if cursor_position.x < bounds.left() { + bounds.left() - cursor_position.x + } else if cursor_position.x > bounds.right() { + bounds.right() - cursor_position.x + } else { + return; + }; + + let offset = scroll_handle.offset(); + scroll_handle.set_offset(point(offset.x + horizontal_delta, offset.y)); + } + pub fn is_parsing(&self) -> bool { self.pending_parse.is_some() } @@ -902,6 +929,7 @@ impl MarkdownElement { Ok(ix) | Err(ix) => ix, }; markdown.selection.set_head(source_index, &rendered_text); + markdown.autoscroll_code_block(source_index, event.position); markdown.autoscroll_request = Some(source_index); cx.notify(); } else {