From a33fb9751c1e468b0062aae63e8f06e9d75e2376 Mon Sep 17 00:00:00 2001 From: Felipe Coury Date: Thu, 24 Sep 2026 17:32:24 +0000 Subject: [PATCH] Preserve Markdown formatting when copying transcript selections (#47896) ## Why Transcript selections were copied as plain text, losing the formatting of rendered Markdown when pasted. ## What changed - Retain semantic Markdown annotations through rendering, wrapping, and streaming, then serialize selected text with balanced markup. - Preserve inline formatting, web links, headings, lists, blockquotes, and hard breaks in Markdown clipboard payloads. - Keep code-only and other literal-only selections as plain text. In mixed selections, escape user prose and fence code, tables, and tool output to preserve their literal content. ## Testing Add unit tests and Markdown/HTML snapshots covering partial selections, nested containers, escaping, links, resize stability, and streaming boundaries. GitOrigin-RevId: f0476334a6bf8ad0fb57626627bd258443540852 --- codex-rs/tui/src/app.rs | 4 +- codex-rs/tui/src/app/event_dispatch.rs | 2 +- codex-rs/tui/src/app/owned_transcript.rs | 9 +- codex-rs/tui/src/app/transcript_export.rs | 5 +- .../tui/src/clipboard_copy/worker_tests.rs | 5 +- codex-rs/tui/src/history_cell/messages.rs | 1 + codex-rs/tui/src/inline_visualization.rs | 3 +- .../tui/src/inline_visualization_tests.rs | 22 + codex-rs/tui/src/lib.rs | 1 + codex-rs/tui/src/markdown_copy.rs | 418 +++++++++++ codex-rs/tui/src/markdown_copy_tests.rs | 120 +++ codex-rs/tui/src/markdown_render.rs | 128 +++- .../tui/src/markdown_render/task_lists.rs | 8 + codex-rs/tui/src/pager_overlay/transcript.rs | 2 +- codex-rs/tui/src/streaming/code_fence.rs | 12 +- .../tui/src/terminal_hyperlinks/source.rs | 7 + codex-rs/tui/src/transcript_view.rs | 4 + .../src/transcript_view/composer_gap_tests.rs | 2 +- .../transcript_view/copy_on_select_tests.rs | 2 +- .../transcript_view/follow_control_tests.rs | 22 +- .../transcript_view/markdown_copy_tests.rs | 688 ++++++++++++++++++ .../transcript_view/prompt_header_tests.rs | 9 +- .../transcript_view/right_click_copy_tests.rs | 2 +- codex-rs/tui/src/transcript_view/selection.rs | 42 +- .../src/transcript_view/selection_tests.rs | 8 +- ...locks_keep_blank_lines_and_boundaries.snap | 25 + ...iteral_and_mixed_code_has_safe_fences.snap | 10 + ..._and_partial_rules_keep_selected_text.snap | 26 + ...preserve_selected_text_and_formatting.snap | 79 ++ ...nd_paragraphs_without_sibling_spacers.snap | 14 + ...rves_literal_text_without_code_fences.snap | 19 + ...ted_tables_keep_their_copy_containers.snap | 40 + ...eading_whitespace_outside_code_blocks.snap | 19 + ...rkdown_copy_tests__selected_list_html.snap | 10 + ...kdown_preserves_lists_and_inline_code.snap | 8 + ...keeps_prose_outside_the_literal_table.snap | 24 + codex-rs/tui/src/transcript_view/text.rs | 23 + codex-rs/tui/src/tui/selection_clipboard.rs | 8 +- 38 files changed, 1790 insertions(+), 41 deletions(-) create mode 100644 codex-rs/tui/src/markdown_copy.rs create mode 100644 codex-rs/tui/src/markdown_copy_tests.rs create mode 100644 codex-rs/tui/src/transcript_view/markdown_copy_tests.rs create mode 100644 codex-rs/tui/src/transcript_view/snapshots/codex_tui__transcript_view__markdown_copy_tests__adjacent_top_level_code_blocks_keep_blank_lines_and_boundaries.snap create mode 100644 codex-rs/tui/src/transcript_view/snapshots/codex_tui__transcript_view__markdown_copy_tests__code_only_is_literal_and_mixed_code_has_safe_fences.snap create mode 100644 codex-rs/tui/src/transcript_view/snapshots/codex_tui__transcript_view__markdown_copy_tests__complete_rules_keep_structure_and_partial_rules_keep_selected_text.snap create mode 100644 codex-rs/tui/src/transcript_view/snapshots/codex_tui__transcript_view__markdown_copy_tests__inline_boundaries_preserve_selected_text_and_formatting.snap create mode 100644 codex-rs/tui/src/transcript_view/snapshots/codex_tui__transcript_view__markdown_copy_tests__lists_keep_nesting_and_paragraphs_without_sibling_spacers.snap create mode 100644 codex-rs/tui/src/transcript_view/snapshots/codex_tui__transcript_view__markdown_copy_tests__mixed_user_prose_preserves_literal_text_without_code_fences.snap create mode 100644 codex-rs/tui/src/transcript_view/snapshots/codex_tui__transcript_view__markdown_copy_tests__nested_tables_keep_their_copy_containers.snap create mode 100644 codex-rs/tui/src/transcript_view/snapshots/codex_tui__transcript_view__markdown_copy_tests__partial_prose_keeps_leading_whitespace_outside_code_blocks.snap create mode 100644 codex-rs/tui/src/transcript_view/snapshots/codex_tui__transcript_view__markdown_copy_tests__selected_list_html.snap create mode 100644 codex-rs/tui/src/transcript_view/snapshots/codex_tui__transcript_view__markdown_copy_tests__selected_markdown_preserves_lists_and_inline_code.snap create mode 100644 codex-rs/tui/src/transcript_view/snapshots/codex_tui__transcript_view__markdown_copy_tests__table_spillover_keeps_prose_outside_the_literal_table.snap diff --git a/codex-rs/tui/src/app.rs b/codex-rs/tui/src/app.rs index cad28387e6..d29f0cb59a 100644 --- a/codex-rs/tui/src/app.rs +++ b/codex-rs/tui/src/app.rs @@ -868,7 +868,9 @@ impl App { ) -> Result { self.finish_clipboard(tui); if matches!(&event, TuiEvent::Key(_)) - && self.handle_composer_copy_event(tui, &event, tui::Tui::copy_transcript_selection) + && self.handle_composer_copy_event(tui, &event, |tui, text| { + tui.copy_transcript_selection(text, crate::clipboard_copy::CopyFormat::PlainText) + }) { return Ok(AppRunControl::Continue); } diff --git a/codex-rs/tui/src/app/event_dispatch.rs b/codex-rs/tui/src/app/event_dispatch.rs index 1c4c986d62..3fa573ca10 100644 --- a/codex-rs/tui/src/app/event_dispatch.rs +++ b/codex-rs/tui/src/app/event_dispatch.rs @@ -345,7 +345,7 @@ impl App { } AppEvent::OpenWarnings => self.chat_widget.open_warnings(&self.transcript_cells), AppEvent::CopyWarning(text) => { - let result = tui.copy_transcript_selection(&text); + let result = tui.copy_transcript_selection(&text, crate::clipboard_copy::CopyFormat::PlainText); self.chat_widget.show_selection_copy_result(result); } AppEvent::OpenTranscriptExportFilePrompt => { diff --git a/codex-rs/tui/src/app/owned_transcript.rs b/codex-rs/tui/src/app/owned_transcript.rs index a902991272..fd6b707a72 100644 --- a/codex-rs/tui/src/app/owned_transcript.rs +++ b/codex-rs/tui/src/app/owned_transcript.rs @@ -292,7 +292,12 @@ impl App { self.render_owned_transcript(tui, size)?; } if composer_ready - && self.handle_composer_copy_event(tui, event, tui::Tui::copy_transcript_selection) + && self.handle_composer_copy_event(tui, event, |tui, text| { + tui.copy_transcript_selection( + text, + crate::clipboard_copy::CopyFormat::PlainText, + ) + }) { return Ok(true); } @@ -465,7 +470,7 @@ impl App { &self.transcript_cells, &text, !copy_on_select, - |text| tui.copy_transcript_selection(text), + |text, format| tui.copy_transcript_selection(text, format), ); self.transcript_view .show_copy_feedback(&result, text.chars().count()); diff --git a/codex-rs/tui/src/app/transcript_export.rs b/codex-rs/tui/src/app/transcript_export.rs index ea0762dcb6..cb1fe1bc99 100644 --- a/codex-rs/tui/src/app/transcript_export.rs +++ b/codex-rs/tui/src/app/transcript_export.rs @@ -55,7 +55,10 @@ impl App { let markdown = render_markdown_transcript(&cells)?; match destination { TranscriptExportDestination::Clipboard => { - let result = tui.copy_transcript_selection(&markdown); + let result = tui.copy_transcript_selection( + &markdown, + crate::clipboard_copy::CopyFormat::PlainText, + ); self.chat_widget.show_copy_result("conversation", result); } TranscriptExportDestination::File(path) => { diff --git a/codex-rs/tui/src/clipboard_copy/worker_tests.rs b/codex-rs/tui/src/clipboard_copy/worker_tests.rs index d564399e06..a75d4c08af 100644 --- a/codex-rs/tui/src/clipboard_copy/worker_tests.rs +++ b/codex-rs/tui/src/clipboard_copy/worker_tests.rs @@ -70,7 +70,10 @@ async fn blocked_copy_allows_overlay_exit_rejects_backlog_and_wakes_completion() ("café\nsecond line".into(), CopyFormat::Markdown) ); for _ in 0..100 { - assert_eq!(tui.copy_transcript_selection("newer"), Ok(CopyStatus::Busy)); + assert_eq!( + tui.copy_transcript_selection("newer", CopyFormat::PlainText), + Ok(CopyStatus::Busy) + ); } assert!(tui.clipboard.poll().is_none()); let mut overlay = Overlay::new_transcript( diff --git a/codex-rs/tui/src/history_cell/messages.rs b/codex-rs/tui/src/history_cell/messages.rs index 07676e3d21..f80f311c75 100644 --- a/codex-rs/tui/src/history_cell/messages.rs +++ b/codex-rs/tui/src/history_cell/messages.rs @@ -243,6 +243,7 @@ impl HistoryCell for UserHistoryCell { lines.push(HyperlinkLine::new(Line::from("").style(style))); for source in lines.iter_mut().filter_map(|line| line.source.as_mut()) { source.right_reserve = 1; + source.copy_as_prose = true; } lines } diff --git a/codex-rs/tui/src/inline_visualization.rs b/codex-rs/tui/src/inline_visualization.rs index 8bf4f77d5d..5f5c9cd5b8 100644 --- a/codex-rs/tui/src/inline_visualization.rs +++ b/codex-rs/tui/src/inline_visualization.rs @@ -30,6 +30,7 @@ const DIRECTIVE_PREFIX: &str = "::codex-inline-vis{"; const CONTENT_REFERENCE_PREFIX: &str = "\u{e200}visualize\u{e202}"; const CONTENT_REFERENCE_SUFFIX: char = '\u{e201}'; const MAX_FRAGMENT_BYTES: u64 = 2 * 1024 * 1024; +pub(crate) const LINK_PLACEHOLDER_PREFIX: &str = "https://codex.invalid/inline-visualization/"; #[derive(Clone, Debug)] pub(crate) struct InlineVisualizationContext { @@ -288,7 +289,7 @@ fn link_placeholder() -> String { let mut bytes = [0_u8; 24]; rand::rng().fill_bytes(&mut bytes); let token = URL_SAFE_NO_PAD.encode(bytes); - format!("https://codex.invalid/inline-visualization/{token}") + format!("{LINK_PLACEHOLDER_PREFIX}{token}") } fn parse_directive_file(directive: &str) -> Option> { diff --git a/codex-rs/tui/src/inline_visualization_tests.rs b/codex-rs/tui/src/inline_visualization_tests.rs index bb9e9b259b..7105d7c40e 100644 --- a/codex-rs/tui/src/inline_visualization_tests.rs +++ b/codex-rs/tui/src/inline_visualization_tests.rs @@ -359,6 +359,28 @@ fn finalized_agent_cell_replays_visualization_link() { .iter() .all(|destination| destination.starts_with("file://")) ); + let source = lines + .iter() + .find(|line| !line.hyperlinks.is_empty()) + .and_then(|line| line.source.clone()) + .expect("linked line copy source"); + let start = source.text.find("file://").expect("visible file URL"); + let mut selected = Vec::new(); + crate::markdown_copy::SelectedLine::append( + &mut selected, + source, + start..start + "file://".len(), + "", + ); + let (copied, format) = crate::markdown_copy::selection(&selected, "file://"); + assert_eq!( + (copied.as_str(), format), + ("file://", crate::clipboard_copy::CopyFormat::Markdown) + ); + assert_eq!( + crate::clipboard_html::render_markdown(&copied), + "

file://

\n" + ); } #[test] diff --git a/codex-rs/tui/src/lib.rs b/codex-rs/tui/src/lib.rs index a08ccd859c..892810792a 100644 --- a/codex-rs/tui/src/lib.rs +++ b/codex-rs/tui/src/lib.rs @@ -128,6 +128,7 @@ mod config_update; pub(crate) mod custom_terminal; mod daybreak; mod experimental_features; +mod markdown_copy; mod permission_discovery; mod pets; mod worktree_browser; diff --git a/codex-rs/tui/src/markdown_copy.rs b/codex-rs/tui/src/markdown_copy.rs new file mode 100644 index 0000000000..62b8b81acc --- /dev/null +++ b/codex-rs/tui/src/markdown_copy.rs @@ -0,0 +1,418 @@ +//! Semantic copy annotations in rendered-byte coordinates, independent of terminal styling. +//! +//! Wrapping and selection snapshots share each logical line's annotations. Only copying +//! serializes Markdown; literal-only selections keep their original text and clipboard format. +//! Rich selections preserve selected words and semantic formatting with balanced markup, +//! rather than reproducing the complete response's source spelling (available through `/copy`). + +use std::ops::Range; +use std::sync::Arc; + +use pulldown_cmark::Event; +use pulldown_cmark::Tag; + +use crate::clipboard_copy::CopyFormat; +use crate::terminal_hyperlinks::LogicalLineSource; + +// Bound retained inline stacks and container prefixes independently of parser nesting. +pub(crate) const MAX_COPY_DEPTH: usize = 64; + +// A fixed, deduplicated style order gives each nested wrapper a distinct delimiter family. +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub(crate) enum Inline { + // Keep one link around its formatted label instead of repeating its destination per style. + Link(Arc), + Delimiter(&'static str), + Code, + /// Balance parser events for links whose visible text has no copied wrapper. + Ignored, +} + +impl Inline { + pub(crate) fn from_event(event: &Event<'_>) -> Option { + match event { + Event::Start(Tag::Emphasis) => Some(Self::Delimiter("_")), + Event::Start(Tag::Strong) => Some(Self::Delimiter("**")), + Event::Start(Tag::Strikethrough) => Some(Self::Delimiter("~~")), + Event::Start(Tag::Link { dest_url, .. }) => Some( + if crate::terminal_hyperlinks::web_destination(dest_url).is_some() + && !dest_url.starts_with(crate::inline_visualization::LINK_PLACEHOLDER_PREFIX) + { + Self::Link(dest_url.as_ref().into()) + } else { + Self::Ignored + }, + ), + Event::Code(_) => Some(Self::Code), + _ => None, + } + } +} + +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub(crate) struct CopyLine { + pub(crate) prefix: String, + pub(crate) continuation: String, + /// Restore the containing item when a multiline selection starts in its later paragraph. + pub(crate) item_prefix: String, + pub(crate) code: bool, + pub(crate) rule: bool, + pub(crate) heading: usize, + pub(crate) hard_break: bool, + /// A visual separator inserted between sibling list items, never source content. + pub(crate) omit: bool, + runs: Vec<(Range, Vec)>, +} + +impl CopyLine { + pub(crate) fn push(&mut self, length: usize, inline: &[Inline]) { + if length == 0 { + return; + } + let mut inline = inline[..inline.len().min(MAX_COPY_DEPTH)] + .iter() + .filter(|mark| **mark != Inline::Ignored) + .cloned() + .collect::>(); + inline.sort(); + inline.dedup(); + let start = self + .runs + .last() + .map_or(/*default*/ 0, |(range, _)| range.end); + if let Some((range, previous)) = self.runs.last_mut() + && previous == &inline + { + range.end += length; + } else { + self.runs.push((start..start + length, inline)); + } + } + + fn render(&self, text: &str, range: Range, depth: usize) -> String { + if self.rule && range == (0..text.len()) { + return "***".to_owned(); + } + let mut out = String::new(); + let first = self.runs.partition_point(|(run, _)| run.end <= range.start); + let mut runs = self.runs[first..] + .iter() + .take_while(|(run, _)| run.start < range.end) + .peekable(); + while let Some((run, inline)) = runs.next() { + let start = run.start.max(range.start); + let mut end = run.end.min(range.end); + if start >= end { + continue; + } + let mark = inline.get(depth); + while let Some((next, inline)) = runs.peek() + && next.start == end + && inline.get(depth) == mark + && end < range.end + { + end = next.end.min(range.end); + runs.next(); + } + match mark { + Some(Inline::Code) => { + let content = &text[start..end]; + let fence = fence(content, /*minimum*/ 1); + let padding = + if content.starts_with(['`', ' ']) || content.ends_with(['`', ' ']) { + if content.chars().all(|ch| ch == ' ') { + "" + } else { + " " + } + } else { + "" + }; + append_inline( + &mut out, + &format!("{fence}{padding}{content}{padding}{fence}"), + ); + } + Some(mark) => { + let body = self.render(text, start..end, depth + 1); + let trimmed = body.trim(); + if trimmed.is_empty() { + out.push_str(&body); + continue; + } + out.push_str(&body[..body.len() - body.trim_start().len()]); + match mark { + Inline::Delimiter(delimiter) => { + append_inline(&mut out, &format!("{delimiter}{trimmed}{delimiter}")) + } + Inline::Link(destination) => { + let destination = destination + .replace('\\', "\\\\") + .replace('&', "&") + .replace('<', "%3C") + .replace('>', "%3E") + .replace('\n', "%0A") + .replace('\r', "%0D"); + append_inline(&mut out, &format!("[{trimmed}](<{destination}>)")); + } + Inline::Code | Inline::Ignored => unreachable!(), + } + out.push_str(&body[body.trim_end().len()..]); + } + None => { + let heading_end = self.heading.min(end).max(start); + out.push_str(&text[start..heading_end]); + append_inline(&mut out, &escape(&text[heading_end..end])); + } + } + } + out + } +} + +// Entity decoding and omitted link wrappers can turn a delimiter's punctuation neighbor into +// a word character. Protect that boundary without changing the selected text or adding spacing. +fn append_inline(out: &mut String, text: &str) { + let word = |ch: char| !ch.is_whitespace() && !ch.is_ascii_punctuation(); + let left = text.starts_with('_') + || text.starts_with(['*', '~']) + && text + .chars() + .next() + .and_then(|delimiter| text.trim_start_matches(delimiter).chars().next()) + .is_some_and(|ch| !ch.is_alphanumeric()); + if left && let Some(ch) = out.chars().last().filter(|ch| word(*ch)) { + out.pop(); + out.push_str(&format!("&#{};", u32::from(ch))); + } + let right = out.ends_with('_') + || out.ends_with(['*', '~']) + && out + .chars() + .next_back() + .and_then(|delimiter| out.trim_end_matches(delimiter).chars().next_back()) + .is_some_and(|ch| !ch.is_alphanumeric()); + if right && let Some(ch) = text.chars().next().filter(|ch| word(*ch)) { + out.push_str(&format!("&#{};", u32::from(ch))); + out.push_str(&text[ch.len_utf8()..]); + } else { + out.push_str(text); + } +} + +pub(crate) struct SelectedLine { + source: LogicalLineSource, + range: Range, + separator: String, +} + +impl SelectedLine { + pub(crate) fn append( + lines: &mut Vec, + source: LogicalLineSource, + range: Range, + separator: &str, + ) { + // Streaming can split the same logical line across history cells. + if let Some(previous) = lines.last_mut() + && Arc::ptr_eq(&previous.source.text, &source.text) + && previous.range.end <= range.start + { + previous.range.end = range.end; + } else { + let separator = if separator.starts_with('\n') + && lines.last().is_some_and(|previous| { + previous.range.end == previous.source.text.len() + && (previous.source.copy_as_prose + || previous + .source + .copy + .as_ref() + .is_some_and(|copy| copy.hard_break)) + }) { + format!(" {separator}") + } else { + separator.to_string() + }; + lines.push(Self { + source, + range, + separator, + }); + } + } +} + +pub(crate) fn selection(lines: &[SelectedLine], plain: &str) -> (String, CopyFormat) { + let rich = lines.iter().any(|line| { + !line.range.is_empty() + && line + .source + .copy + .as_ref() + .is_some_and(|copy| !copy.code && !copy.omit) + }); + if !rich { + return (plain.to_owned(), CopyFormat::PlainText); + } + let mut out = String::new(); + let mut lines = lines + .iter() + .filter(|line| !line.source.copy.as_ref().is_some_and(|copy| copy.omit)) + .peekable(); + let mut first = true; + let mut indentation: Option = None; + while let Some(line) = lines.next() { + if !first { + out.push_str(&line.separator); + } + let prefix = line.source.copy.as_ref().map_or("", |copy| { + if indentation.is_none() && !line.range.is_empty() && lines.peek().is_some() { + ©.item_prefix + } else { + ©.prefix + } + }); + if !line.range.is_empty() { + let spaces = prefix + .split("> ") + .map(|part| part.len() - part.trim_start_matches(' ').len()) + .sum(); + indentation = Some(indentation.map_or(spaces, |previous| previous.min(spaces))); + } + let prefix = dedent(prefix, indentation.unwrap_or(/*default*/ 0)); + if line.range.is_empty() { + if line.source.text.is_empty() { + out.push_str(prefix.trim_end()); + } + first = false; + continue; + } + if line.source.copy_as_prose { + let text = &line.source.text[line.range.clone()]; + push_prose_fragment(&mut out, &escape(text)); + } else if let Some(copy) = &line.source.copy + && !copy.code + { + if line.range.start == 0 || first && lines.peek().is_some() { + out.push_str(&prefix); + } + let body = copy.render(&line.source.text, line.range.clone(), /*depth*/ 0); + push_prose_fragment(&mut out, &body); + } else { + // Preserve whitespace and Markdown-looking tool output within mixed selections. + let mut code = line.source.text[line.range.clone()].to_owned(); + let continuation = line + .source + .copy + .as_ref() + .map_or("", |copy| copy.continuation.as_str()); + while let Some(next) = lines.peek() + && !next.source.copy_as_prose + && next.source.copy.is_some() == line.source.copy.is_some() + && next.source.copy.as_ref().is_none_or(|copy| copy.code) + && next + .source + .copy + .as_ref() + .map_or("", |copy| copy.prefix.as_str()) + == continuation + && next + .source + .copy + .as_ref() + .map_or("", |copy| copy.continuation.as_str()) + == continuation + { + code.push_str(&next.separator); + code.push_str(&next.source.text[next.range.clone()]); + lines.next(); + } + let fence = fence(&code, /*minimum*/ 3); + if !out.is_empty() && !out.ends_with('\n') { + out.push('\n'); + } + let continuation = dedent(continuation, indentation.unwrap_or(/*default*/ 0)); + // A checkbox label must precede the fence on its own line. + if prefix.ends_with("[ ] ") || prefix.ends_with("[x] ") { + out.push_str(prefix.trim_end()); + out.push('\n'); + out.push_str(&continuation); + } else { + out.push_str(&prefix); + } + out.push_str(&format!("{fence}\n")); + for row in code.split_inclusive('\n') { + out.push_str(&continuation); + out.push_str(row); + } + if !out.ends_with('\n') { + out.push('\n'); + } + out.push_str(&continuation); + out.push_str(&fence); + } + first = false; + } + out.retain(|ch| !ch.is_control() || matches!(ch, '\n' | '\t')); + (out, CopyFormat::Markdown) +} + +// Remove unselected ancestor list padding without discarding blockquote markers. +fn dedent(prefix: &str, mut spaces: usize) -> String { + prefix + .split_inclusive("> ") + .map(|part| { + let remove = spaces.min(part.len() - part.trim_start_matches(' ').len()); + spaces -= remove; + &part[remove..] + }) + .collect() +} + +fn fence(text: &str, minimum: usize) -> String { + "`".repeat( + text.split(|ch| ch != '`') + .map(str::len) + .max() + .unwrap_or(/*default*/ 0) + .saturating_add(/*rhs*/ 1) + .max(minimum), + ) +} + +// Append escaped prose without allowing its leading whitespace to become code indentation. +fn push_prose_fragment(out: &mut String, text: &str) { + let body = text.trim_start_matches([' ', '\t']); + for ch in text[..text.len() - body.len()].chars() { + out.push_str(if ch == '\t' { " " } else { " " }); + } + out.push_str(body); +} + +fn escape(text: &str) -> String { + let mut out = String::new(); + let mut whitespace_prefix = true; + let mut numeric_prefix = false; + let mut number_has_trailing_space = false; + for ch in text.chars() { + let block_marker = + "=-+".contains(ch) && whitespace_prefix || matches!(ch, '.' | ')') && numeric_prefix; + if "\\`*_[]<>~#|&!".contains(ch) || block_marker { + out.push('\\'); + } + out.push(ch); + if ch.is_whitespace() { + number_has_trailing_space |= numeric_prefix; + } else { + numeric_prefix = ch.is_ascii_digit() + && (whitespace_prefix || numeric_prefix && !number_has_trailing_space); + whitespace_prefix = false; + } + } + out +} + +#[cfg(test)] +#[path = "markdown_copy_tests.rs"] +mod tests; diff --git a/codex-rs/tui/src/markdown_copy_tests.rs b/codex-rs/tui/src/markdown_copy_tests.rs new file mode 100644 index 0000000000..7b57dc1c45 --- /dev/null +++ b/codex-rs/tui/src/markdown_copy_tests.rs @@ -0,0 +1,120 @@ +//! Verify escaping and bounded copy annotations through the real renderer. + +use super::*; +use crate::markdown_render::render_markdown_lines_with_width_and_cwd; +use pretty_assertions::assert_eq; +use std::path::Path; + +#[test] +fn escaping_tracks_block_prefixes_without_rescanning_them() { + for (text, expected) in [ + (" - item", " \\- item"), + (" 123.)", " 123\\.)"), + ("12\u{2003}.", "12\u{2003}\\."), + ("12 3.", "12 3."), + ("\u{2003}12\t)", "\u{2003}12\t\\)"), + ("12.34)", "12\\.34)"), + ("word - text", "word - text"), + ("\u{2003}+", "\u{2003}\\+"), + ] { + assert_eq!(escape(text), expected); + } + let digits = "1".repeat(/*n*/ 20_000); + let punctuation = ".".repeat(/*n*/ 20_000); + assert_eq!( + escape(&format!("{digits}{punctuation}")), + format!("{digits}\\{punctuation}") + ); +} + +#[test] +fn deeply_nested_lazy_continuations_keep_bounded_relative_copy_prefixes() { + let depth = 256; + let continuation_count = 256; + let markdown = format!( + "{}text\n{}", + "- ".repeat(depth), + "a\n".repeat(continuation_count) + ); + let lines = + render_markdown_lines_with_width_and_cwd(&markdown, /*width*/ None, /*cwd*/ None); + let copies = lines + .iter() + .filter_map(|line| line.source.as_ref()) + .filter_map(|source| source.copy.as_ref()) + .collect::>(); + let continuation = " ".repeat(MAX_COPY_DEPTH); + let item_prefix = format!("{}- ", " ".repeat(MAX_COPY_DEPTH - 1)); + assert_eq!(copies.len(), continuation_count + 1); + assert!( + copies + .iter() + .all(|copy| copy.continuation == continuation && copy.item_prefix == item_prefix) + ); + assert_eq!(copies[0].prefix, item_prefix); + assert!(copies[1..].iter().all(|copy| copy.prefix == continuation)); +} + +#[test] +fn code_display_padding_does_not_displace_a_retained_container() { + let depth = MAX_COPY_DEPTH + 1; + let indent = " ".repeat(depth); + let markdown = format!( + "{}before\n\n{indent}```\n{indent}code()\n{indent}```\n", + "- ".repeat(depth) + ); + let lines = + render_markdown_lines_with_width_and_cwd(&markdown, /*width*/ None, /*cwd*/ None); + let prefixes = lines + .iter() + .filter_map(|line| line.source.as_ref()) + .filter_map(|source| source.copy.as_ref()) + .filter(|copy| copy.code) + .map(|copy| copy.prefix.clone()) + .collect::>(); + assert_eq!(prefixes, vec![" ".repeat(MAX_COPY_DEPTH)]); +} + +#[test] +fn link_runs_share_destinations_and_bound_nested_formatting() { + for destination in ["https://example.com/", "/repo/"] { + let destination = format!("{destination}{}", "a".repeat(/*n*/ 4096)); + let label = format!( + "**prefix** {}middle{}", + "*a ".repeat(/*n*/ 128), + " b*".repeat(/*n*/ 128) + ); + let markdown = format!("[{label}](<{destination}>)"); + let lines = render_markdown_lines_with_width_and_cwd( + &markdown, + /*width*/ None, + Some(Path::new("/")), + ); + let runs = lines + .iter() + .filter_map(|line| line.source.as_ref()) + .filter_map(|source| source.copy.as_ref()) + .flat_map(|copy| ©.runs) + .map(|(_, inline)| inline) + .collect::>(); + assert!(runs.len() > 1); + assert!(runs.iter().all(|inline| inline.len() <= 5)); + let links = runs + .iter() + .flat_map(|inline| inline.iter()) + .filter_map(|inline| { + if let Inline::Link(destination) = inline { + Some(destination) + } else { + None + } + }) + .collect::>(); + if destination.starts_with('/') { + assert!(links.is_empty()); + continue; + } + assert!(links.len() > 1); + assert!(links.iter().all(|link| Arc::ptr_eq(links[0], link))); + } +} diff --git a/codex-rs/tui/src/markdown_render.rs b/codex-rs/tui/src/markdown_render.rs index 5eeab22f55..d15b95f31a 100644 --- a/codex-rs/tui/src/markdown_render.rs +++ b/codex-rs/tui/src/markdown_render.rs @@ -162,14 +162,24 @@ impl MarkdownStyles { struct IndentContext { prefix: Vec>, marker: Option>>, + copy_marker: String, is_list: bool, } impl IndentContext { fn new(prefix: Vec>, marker: Option>>, is_list: bool) -> Self { + // Keep the canonical marker separate from depth-dependent display padding. + let copy_marker = marker + .iter() + .flatten() + .map(|span| span.content.as_ref()) + .collect::() + .trim_start() + .replace('•', "-"); Self { prefix, marker, + copy_marker, is_list, } } @@ -396,6 +406,7 @@ struct LinkState { /// can collapse to this canonical target without losing descriptive labels. local_target_display: Option, local_label_spans: Vec>, + local_label_inline: Vec>, } fn should_render_link_destination(dest_url: &str) -> bool { @@ -415,6 +426,8 @@ struct Writer<'a, 'policy> { text: Vec, styles: MarkdownStyles, inline_styles: Vec