agent: Add headers for code blocks (#28253)

<img width="639" alt="image"
src="https://github.com/user-attachments/assets/1fd51387-cbdc-474d-b1a3-3d0201f3735a"
/>


Release Notes:

- N/A

---------

Co-authored-by: Danilo Leal <daniloleal09@gmail.com>
Co-authored-by: Antonio Scandurra <me@as-cii.com>
This commit is contained in:
Bennet Bo Fenner 2025-04-07 17:56:24 -06:00 committed by GitHub
parent d385a60ed1
commit b306a0221b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 439 additions and 262 deletions

View file

@ -1,12 +1,11 @@
pub mod parser;
mod path_range;
use file_icons::FileIcons;
use std::collections::{HashMap, HashSet};
use std::collections::HashSet;
use std::iter;
use std::mem;
use std::ops::Range;
use std::path::PathBuf;
use std::path::Path;
use std::rc::Rc;
use std::sync::Arc;
use std::time::Duration;
@ -21,10 +20,10 @@ use gpui::{
use language::{Language, LanguageRegistry, Rope};
use parser::{MarkdownEvent, MarkdownTag, MarkdownTagEnd, parse_links_only, parse_markdown};
use pulldown_cmark::Alignment;
use sum_tree::TreeMap;
use theme::SyntaxTheme;
use ui::{ButtonLike, Tooltip, prelude::*};
use ui::{Tooltip, prelude::*};
use util::{ResultExt, TryFutureExt};
use workspace::Workspace;
use crate::parser::CodeBlockKind;
@ -84,12 +83,18 @@ pub struct Markdown {
copied_code_blocks: HashSet<ElementId>,
}
#[derive(Debug)]
struct Options {
parse_links_only: bool,
copy_code_block_buttons: bool,
}
pub enum CodeBlockRenderer {
Default { copy_button: bool },
Custom { render: CodeBlockRenderFn },
}
pub type CodeBlockRenderFn =
Arc<dyn Fn(usize, &CodeBlockKind, &ParsedMarkdown, Range<usize>, &mut Window, &App) -> Div>;
actions!(markdown, [Copy, CopyAsMarkdown]);
impl Markdown {
@ -113,7 +118,6 @@ impl Markdown {
fallback_code_block_language,
options: Options {
parse_links_only: false,
copy_code_block_buttons: true,
},
copied_code_blocks: HashSet::new(),
};
@ -136,7 +140,6 @@ impl Markdown {
fallback_code_block_language: None,
options: Options {
parse_links_only: true,
copy_code_block_buttons: true,
},
copied_code_blocks: HashSet::new(),
};
@ -205,19 +208,19 @@ impl Markdown {
return anyhow::Ok(ParsedMarkdown {
events: Arc::from(parse_links_only(source.as_ref())),
source,
languages_by_name: HashMap::default(),
languages_by_path: HashMap::default(),
languages_by_name: TreeMap::default(),
languages_by_path: TreeMap::default(),
});
}
let (events, language_names, paths) = parse_markdown(&source);
let mut languages_by_name = HashMap::with_capacity(language_names.len());
let mut languages_by_path = HashMap::with_capacity(paths.len());
let mut languages_by_name = TreeMap::default();
let mut languages_by_path = TreeMap::default();
if let Some(registry) = language_registry.as_ref() {
for name in language_names {
let language = if !name.is_empty() {
registry.language_for_name(&name)
registry.language_for_name_or_extension(&name)
} else if let Some(fallback) = &fallback {
registry.language_for_name(fallback)
registry.language_for_name_or_extension(fallback)
} else {
continue;
};
@ -259,11 +262,6 @@ impl Markdown {
.await
}));
}
pub fn copy_code_block_buttons(mut self, should_copy: bool) -> Self {
self.options.copy_code_block_buttons = should_copy;
self
}
}
impl Focusable for Markdown {
@ -302,12 +300,12 @@ impl Selection {
}
}
#[derive(Default)]
#[derive(Clone, Default)]
pub struct ParsedMarkdown {
source: SharedString,
events: Arc<[(Range<usize>, MarkdownEvent)]>,
languages_by_name: HashMap<SharedString, Arc<Language>>,
languages_by_path: HashMap<PathBuf, Arc<Language>>,
pub source: SharedString,
pub events: Arc<[(Range<usize>, MarkdownEvent)]>,
pub languages_by_name: TreeMap<SharedString, Arc<Language>>,
pub languages_by_path: TreeMap<Arc<Path>, Arc<Language>>,
}
impl ParsedMarkdown {
@ -323,6 +321,7 @@ impl ParsedMarkdown {
pub struct MarkdownElement {
markdown: Entity<Markdown>,
style: MarkdownStyle,
code_block_renderer: CodeBlockRenderer,
on_url_click: Option<Box<dyn Fn(SharedString, &mut Window, &mut App)>>,
}
@ -331,10 +330,16 @@ impl MarkdownElement {
Self {
markdown,
style,
code_block_renderer: CodeBlockRenderer::Default { copy_button: true },
on_url_click: None,
}
}
pub fn code_block_renderer(mut self, variant: CodeBlockRenderer) -> Self {
self.code_block_renderer = variant;
self
}
pub fn on_url_click(
mut self,
handler: impl Fn(SharedString, &mut Window, &mut App) + 'static,
@ -589,7 +594,6 @@ impl Element for MarkdownElement {
0
};
let code_citation_id = SharedString::from("code-citation-link");
for (index, (range, event)) in parsed_markdown.events.iter().enumerate() {
match event {
MarkdownEvent::Start(tag) => {
@ -634,123 +638,80 @@ impl Element for MarkdownElement {
CodeBlockKind::FencedLang(language) => {
parsed_markdown.languages_by_name.get(language).cloned()
}
CodeBlockKind::FencedSrc(path_range) => {
// If the path actually exists in the project, render a link to it.
if let Some(project_path) =
window.root::<Workspace>().flatten().and_then(|workspace| {
if path_range.path.is_absolute() {
return None;
}
workspace
.read(cx)
.project()
.read(cx)
.find_project_path(&path_range.path, cx)
})
{
builder.flush_text();
builder.push_div(
div().relative().w_full(),
range,
markdown_end,
);
builder.modify_current_div(|el| {
let file_icon =
FileIcons::get_icon(&project_path.path, cx)
.map(|path| {
Icon::from_path(path)
.color(Color::Muted)
.into_any_element()
})
.unwrap_or_else(|| {
IconButton::new(
"file-path-icon",
IconName::File,
)
.shape(ui::IconButtonShape::Square)
.into_any_element()
});
el.child(
ButtonLike::new(ElementId::NamedInteger(
code_citation_id.clone(),
index,
))
.child(
div()
.mb_1()
.flex()
.items_center()
.gap_1()
.child(file_icon)
.child(
Label::new(
project_path
.path
.display()
.to_string(),
)
.color(Color::Muted)
.underline(),
),
)
.on_click({
let click_path = project_path.clone();
move |_, window, cx| {
if let Some(workspace) =
window.root::<Workspace>().flatten()
{
workspace.update(cx, |workspace, cx| {
workspace
.open_path(
click_path.clone(),
None,
true,
window,
cx,
)
.detach_and_log_err(cx);
})
}
}
}),
)
});
builder.pop_div();
}
parsed_markdown
.languages_by_path
.get(&path_range.path)
.cloned()
}
CodeBlockKind::FencedSrc(path_range) => parsed_markdown
.languages_by_path
.get(&path_range.path)
.cloned(),
_ => None,
};
// This is a parent container that we can position the copy button inside.
builder.push_div(div().relative().w_full(), range, markdown_end);
let is_indented = matches!(kind, CodeBlockKind::Indented);
let mut code_block = div()
.id(("code-block", range.start))
.rounded_lg()
.map(|mut code_block| {
if self.style.code_block_overflow_x_scroll {
code_block.style().restrict_scroll_to_axis = Some(true);
code_block.flex().overflow_x_scroll()
} else {
code_block.w_full()
match (&self.code_block_renderer, is_indented) {
(CodeBlockRenderer::Default { .. }, _) | (_, true) => {
// This is a parent container that we can position the copy button inside.
builder.push_div(
div().relative().w_full(),
range,
markdown_end,
);
let mut code_block = div()
.id(("code-block", range.start))
.rounded_lg()
.map(|mut code_block| {
if self.style.code_block_overflow_x_scroll {
code_block.style().restrict_scroll_to_axis =
Some(true);
code_block.flex().overflow_x_scroll()
} else {
code_block.w_full()
}
});
code_block.style().refine(&self.style.code_block);
if let Some(code_block_text_style) = &self.style.code_block.text
{
builder.push_text_style(code_block_text_style.to_owned());
}
});
code_block.style().refine(&self.style.code_block);
if let Some(code_block_text_style) = &self.style.code_block.text {
builder.push_text_style(code_block_text_style.to_owned());
builder.push_code_block(language);
builder.push_div(code_block, range, markdown_end);
}
(CodeBlockRenderer::Custom { render }, _) => {
let parent_container = render(
index,
kind,
&parsed_markdown,
range.clone(),
window,
cx,
);
builder.push_div(parent_container, range, markdown_end);
let mut code_block = div()
.id(("code-block", range.start))
.rounded_b_lg()
.map(|mut code_block| {
if self.style.code_block_overflow_x_scroll {
code_block.style().restrict_scroll_to_axis =
Some(true);
code_block.flex().overflow_x_scroll()
} else {
code_block.w_full()
}
});
code_block.style().refine(&self.style.code_block);
if let Some(code_block_text_style) = &self.style.code_block.text
{
builder.push_text_style(code_block_text_style.to_owned());
}
builder.push_code_block(language);
builder.push_div(code_block, range, markdown_end);
}
}
builder.push_code_block(language);
builder.push_div(code_block, range, markdown_end);
}
MarkdownTag::HtmlBlock => builder.push_div(div(), range, markdown_end),
MarkdownTag::List(bullet_index) => {
@ -885,61 +846,22 @@ impl Element for MarkdownElement {
builder.pop_text_style();
}
if self.markdown.read(cx).options.copy_code_block_buttons {
if matches!(
&self.code_block_renderer,
CodeBlockRenderer::Default { copy_button: true }
) {
builder.flush_text();
builder.modify_current_div(|el| {
let id =
ElementId::NamedInteger("copy-markdown-code".into(), range.end);
let was_copied =
self.markdown.read(cx).copied_code_blocks.contains(&id);
let copy_button = div().absolute().top_1().right_1().w_5().child(
IconButton::new(
id.clone(),
if was_copied {
IconName::Check
} else {
IconName::Copy
},
)
.icon_color(Color::Muted)
.shape(ui::IconButtonShape::Square)
.tooltip(Tooltip::text("Copy Code"))
.on_click({
let id = id.clone();
let markdown = self.markdown.clone();
let code = without_fences(
parsed_markdown.source()[range.clone()].trim(),
)
let code =
without_fences(parsed_markdown.source()[range.clone()].trim())
.to_string();
move |_event, _window, cx| {
let id = id.clone();
markdown.update(cx, |this, cx| {
this.copied_code_blocks.insert(id.clone());
cx.write_to_clipboard(ClipboardItem::new_string(
code.clone(),
));
cx.spawn(async move |this, cx| {
cx.background_executor()
.timer(Duration::from_secs(2))
.await;
cx.update(|cx| {
this.update(cx, |this, cx| {
this.copied_code_blocks.remove(&id);
cx.notify();
})
})
.ok();
})
.detach();
});
}
}),
let codeblock = render_copy_code_block_button(
range.end,
code,
self.markdown.clone(),
cx,
);
el.child(copy_button)
el.child(div().absolute().top_1().right_1().w_5().child(codeblock))
});
}
@ -1073,6 +995,52 @@ impl Element for MarkdownElement {
}
}
fn render_copy_code_block_button(
id: usize,
code: String,
markdown: Entity<Markdown>,
cx: &App,
) -> impl IntoElement {
let id = ElementId::NamedInteger("copy-markdown-code".into(), id);
let was_copied = markdown.read(cx).copied_code_blocks.contains(&id);
IconButton::new(
id.clone(),
if was_copied {
IconName::Check
} else {
IconName::Copy
},
)
.icon_color(Color::Muted)
.shape(ui::IconButtonShape::Square)
.tooltip(Tooltip::text("Copy Code"))
.on_click({
let id = id.clone();
let markdown = markdown.clone();
move |_event, _window, cx| {
let id = id.clone();
markdown.update(cx, |this, cx| {
this.copied_code_blocks.insert(id.clone());
cx.write_to_clipboard(ClipboardItem::new_string(code.clone()));
cx.spawn(async move |this, cx| {
cx.background_executor().timer(Duration::from_secs(2)).await;
cx.update(|cx| {
this.update(cx, |this, cx| {
this.copied_code_blocks.remove(&id);
cx.notify();
})
})
.ok();
})
.detach();
});
}
})
}
impl IntoElement for MarkdownElement {
type Element = Self;
@ -1529,7 +1497,7 @@ impl RenderedText {
/// Some markdown blocks are indented, and others have e.g. ```rust … ``` around them.
/// If this block is fenced with backticks, strip them off (and the language name).
/// We use this when copying code blocks to the clipboard.
fn without_fences(mut markdown: &str) -> &str {
pub fn without_fences(mut markdown: &str) -> &str {
if let Some(opening_backticks) = markdown.find("```") {
markdown = &markdown[opening_backticks..];