Link to cited code blocks (#28217)

<img width="612" alt="Screenshot 2025-04-06 at 9 59 41 PM"
src="https://github.com/user-attachments/assets/3a996b4a-ef5c-4ca6-bd16-3b180b364a3a"
/>

Release Notes:

- Agent panel now shows links to relevant source code files above code
blocks.
This commit is contained in:
Richard Feldman 2025-04-07 11:01:34 -04:00 committed by GitHub
parent 8049fc1038
commit fa90b3a986
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 522 additions and 32 deletions

View file

@ -1,9 +1,12 @@
pub mod parser;
mod path_range;
use file_icons::FileIcons;
use std::collections::{HashMap, HashSet};
use std::iter;
use std::mem;
use std::ops::Range;
use std::path::PathBuf;
use std::rc::Rc;
use std::sync::Arc;
use std::time::Duration;
@ -19,8 +22,9 @@ use language::{Language, LanguageRegistry, Rope};
use parser::{MarkdownEvent, MarkdownTag, MarkdownTagEnd, parse_links_only, parse_markdown};
use pulldown_cmark::Alignment;
use theme::SyntaxTheme;
use ui::{Tooltip, prelude::*};
use ui::{ButtonLike, Tooltip, prelude::*};
use util::{ResultExt, TryFutureExt};
use workspace::Workspace;
use crate::parser::CodeBlockKind;
@ -210,13 +214,15 @@ impl Markdown {
return anyhow::Ok(ParsedMarkdown {
events: Arc::from(parse_links_only(source.as_ref())),
source,
languages: HashMap::default(),
languages_by_name: HashMap::default(),
languages_by_path: HashMap::default(),
});
}
let (events, language_names) = parse_markdown(&source);
let mut languages = HashMap::with_capacity(language_names.len());
for name in language_names {
if let Some(registry) = language_registry.as_ref() {
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());
if let Some(registry) = language_registry.as_ref() {
for name in language_names {
let language = if !name.is_empty() {
registry.language_for_name(&name)
} else if let Some(fallback) = &fallback {
@ -225,14 +231,21 @@ impl Markdown {
continue;
};
if let Ok(language) = language.await {
languages.insert(name, language);
languages_by_name.insert(name, language);
}
}
for path in paths {
if let Ok(language) = registry.language_for_file_path(&path).await {
languages_by_path.insert(path, language);
}
}
}
anyhow::Ok(ParsedMarkdown {
source,
events: Arc::from(events),
languages,
languages_by_name,
languages_by_path,
})
});
@ -308,7 +321,8 @@ impl Selection {
pub struct ParsedMarkdown {
source: SharedString,
events: Arc<[(Range<usize>, MarkdownEvent)]>,
languages: HashMap<SharedString, Arc<Language>>,
languages_by_name: HashMap<SharedString, Arc<Language>>,
languages_by_path: HashMap<PathBuf, Arc<Language>>,
}
impl ParsedMarkdown {
@ -574,7 +588,9 @@ impl Element for MarkdownElement {
} else {
0
};
for (range, event) in parsed_markdown.events.iter() {
let code_citation_id = SharedString::from("code-citation-link");
for (index, (range, event)) in parsed_markdown.events.iter().enumerate() {
match event {
MarkdownEvent::Start(tag) => {
match tag {
@ -613,10 +629,102 @@ impl Element for MarkdownElement {
);
}
MarkdownTag::CodeBlock(kind) => {
let language = if let CodeBlockKind::Fenced(language) = kind {
parsed_markdown.languages.get(language).cloned()
} else {
None
let language = match kind {
CodeBlockKind::Fenced => None,
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| {
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()
}
_ => None,
};
// This is a parent container that we can position the copy button inside.