render local file links from target paths (#13857)

Co-authored-by: Josh McKinney <joshka@openai.com>
This commit is contained in:
pash-openaiandJosh McKinney authored and Michael Bolin committed 2026-03-11 12:33:09 -07:00
1 parent c8446d7cf3
commit da74da6684
9 files changed
+619 -160

No files matched your search

+35 -13
View File
@@ -1,4 +1,6 @@
use ratatui::text::Line;
use std::path::Path;
use std::path::PathBuf;
use crate::markdown;
@@ -8,14 +10,22 @@ pub(crate) struct MarkdownStreamCollector {
buffer: String,
committed_line_count: usize,
width: Option<usize>,
cwd: PathBuf,
}
impl MarkdownStreamCollector {
pub fn new(width: Option<usize>) -> Self {
/// Create a collector that renders markdown using `cwd` for local file-link display.
///
/// The collector snapshots `cwd` into owned state because stream commits can happen long after
/// construction. The same `cwd` should be reused for the entire stream lifecycle; mixing
/// different working directories within one stream would make the same link render with
/// different path prefixes across incremental commits.
pub fn new(width: Option<usize>, cwd: &Path) -> Self {
Self {
buffer: String::new(),
committed_line_count: 0,
width,
cwd: cwd.to_path_buf(),
}
}
@@ -41,7 +51,7 @@ impl MarkdownStreamCollector {
return Vec::new();
};
let mut rendered: Vec<Line<'static>> = Vec::new();
markdown::append_markdown(&source, self.width, &mut rendered);
markdown::append_markdown(&source, self.width, Some(self.cwd.as_path()), &mut rendered);
let mut complete_line_count = rendered.len();
if complete_line_count > 0
&& crate::render::line_utils::is_blank_line_spaces_only(
@@ -82,7 +92,7 @@ impl MarkdownStreamCollector {
tracing::trace!("markdown finalize (raw source):\n---\n{source}\n---");
let mut rendered: Vec<Line<'static>> = Vec::new();
markdown::append_markdown(&source, self.width, &mut rendered);
markdown::append_markdown(&source, self.width, Some(self.cwd.as_path()), &mut rendered);
let out = if self.committed_line_count >= rendered.len() {
Vec::new()
@@ -96,12 +106,19 @@ impl MarkdownStreamCollector {
}
}
#[cfg(test)]
fn test_cwd() -> PathBuf {
// These tests only need a stable absolute cwd; using temp_dir() avoids baking Unix- or
// Windows-specific root semantics into the fixtures.
std::env::temp_dir()
}
#[cfg(test)]
pub(crate) fn simulate_stream_markdown_for_tests(
deltas: &[&str],
finalize: bool,
) -> Vec<Line<'static>> {
let mut collector = MarkdownStreamCollector::new(None);
let mut collector = MarkdownStreamCollector::new(None, &test_cwd());
let mut out = Vec::new();
for d in deltas {
collector.push_delta(d);
@@ -122,7 +139,7 @@ mod tests {
#[tokio::test]
async fn no_commit_until_newline() {
let mut c = super::MarkdownStreamCollector::new(None);
let mut c = super::MarkdownStreamCollector::new(None, &super::test_cwd());
c.push_delta("Hello, world");
let out = c.commit_complete_lines();
assert!(out.is_empty(), "should not commit without newline");
@@ -133,7 +150,7 @@ mod tests {
#[tokio::test]
async fn finalize_commits_partial_line() {
let mut c = super::MarkdownStreamCollector::new(None);
let mut c = super::MarkdownStreamCollector::new(None, &super::test_cwd());
c.push_delta("Line without newline");
let out = c.finalize_and_drain();
assert_eq!(out.len(), 1);
@@ -253,7 +270,7 @@ mod tests {
async fn heading_starts_on_new_line_when_following_paragraph() {
// Stream a paragraph line, then a heading on the next line.
// Expect two distinct rendered lines: "Hello." and "Heading".
let mut c = super::MarkdownStreamCollector::new(None);
let mut c = super::MarkdownStreamCollector::new(None, &super::test_cwd());
c.push_delta("Hello.\n");
let out1 = c.commit_complete_lines();
let s1: Vec<String> = out1
@@ -309,7 +326,7 @@ mod tests {
// Paragraph without trailing newline, then a chunk that starts with the newline
// and the heading text, then a final newline. The collector should first commit
// only the paragraph line, and later commit the heading as its own line.
let mut c = super::MarkdownStreamCollector::new(None);
let mut c = super::MarkdownStreamCollector::new(None, &super::test_cwd());
c.push_delta("Sounds good!");
// No commit yet
assert!(c.commit_complete_lines().is_empty());
@@ -354,7 +371,8 @@ mod tests {
// Sanity check raw markdown rendering for a simple line does not produce spurious extras.
let mut rendered: Vec<ratatui::text::Line<'static>> = Vec::new();
crate::markdown::append_markdown("Hello.\n", None, &mut rendered);
let test_cwd = super::test_cwd();
crate::markdown::append_markdown("Hello.\n", None, Some(test_cwd.as_path()), &mut rendered);
let rendered_strings: Vec<String> = rendered
.iter()
.map(|l| {
@@ -414,7 +432,8 @@ mod tests {
let streamed_str = lines_to_plain_strings(&streamed);
let mut rendered_all: Vec<ratatui::text::Line<'static>> = Vec::new();
crate::markdown::append_markdown(input, None, &mut rendered_all);
let test_cwd = super::test_cwd();
crate::markdown::append_markdown(input, None, Some(test_cwd.as_path()), &mut rendered_all);
let rendered_all_str = lines_to_plain_strings(&rendered_all);
assert_eq!(
@@ -520,7 +539,8 @@ mod tests {
let full: String = deltas.iter().copied().collect();
let mut rendered_all: Vec<ratatui::text::Line<'static>> = Vec::new();
crate::markdown::append_markdown(&full, None, &mut rendered_all);
let test_cwd = super::test_cwd();
crate::markdown::append_markdown(&full, None, Some(test_cwd.as_path()), &mut rendered_all);
let rendered_all_strs = lines_to_plain_strings(&rendered_all);
assert_eq!(
@@ -608,7 +628,8 @@ mod tests {
// Compute a full render for diagnostics only.
let full: String = deltas.iter().copied().collect();
let mut rendered_all: Vec<ratatui::text::Line<'static>> = Vec::new();
crate::markdown::append_markdown(&full, None, &mut rendered_all);
let test_cwd = super::test_cwd();
crate::markdown::append_markdown(&full, None, Some(test_cwd.as_path()), &mut rendered_all);
// Also assert exact expected plain strings for clarity.
let expected = vec![
@@ -635,7 +656,8 @@ mod tests {
let streamed_strs = lines_to_plain_strings(&streamed);
let full: String = deltas.iter().copied().collect();
let mut rendered: Vec<ratatui::text::Line<'static>> = Vec::new();
crate::markdown::append_markdown(&full, None, &mut rendered);
let test_cwd = super::test_cwd();
crate::markdown::append_markdown(&full, None, Some(test_cwd.as_path()), &mut rendered);
let rendered_strs = lines_to_plain_strings(&rendered);
assert_eq!(streamed_strs, rendered_strs, "full:\n---\n{full}\n---");
}