Lightweight coding agent that runs in your terminal
Rust
Copy selections within inline code as plain text (#49041)
Felipe Coury3074be908ac7
diff --git a/codex-rs/tui/src/markdown_copy.rs b/codex-rs/tui/src/markdown_copy.rsindex f70d7639d9..ae135e1b75 100644--- a/codex-rs/tui/src/markdown_copy.rs+++ b/codex-rs/tui/src/markdown_copy.rs@@ -93,6 +93,13 @@ impl CopyLine {}}+ fn is_inline_code(&self, range: &Range<usize>) -> bool {+ !range.is_empty()+ && self.runs.iter().any(|(run, inline)| {+ run.start <= range.start && range.end <= run.end && inline.contains(&Inline::Code)+ })+ }+fn render(&self, text: &str, range: Range<usize>, depth: usize) -> String {if self.rule && range == (0..text.len()) {return "***".to_owned();@@ -254,6 +261,16 @@ impl SelectedLine {}pub(crate) fn selection(lines: &[SelectedLine], plain: &str) -> (String, CopyFormat) {+ // Soft wraps and streamed fragments of the same logical line are already coalesced.+ if let [line] = lines+ && line+ .source+ .copy+ .as_ref()+ .is_some_and(|copy| copy.is_inline_code(&line.range))+ {+ return (plain.to_owned(), CopyFormat::PlainText);+ }let rich = lines.iter().any(|line| {!line.range.is_empty()&& line@@ -315,7 +332,12 @@ pub(crate) fn selection(lines: &[SelectedLine], plain: &str) -> (String, CopyFor{selected.extend(lines.next());}- let body = table::render(&selected, table);+ let entire_selection = first && lines.peek().is_none();+ let (mut body, format) = table::render(&selected, table, entire_selection);+ if format == CopyFormat::PlainText {+ body.retain(|ch| !ch.is_control() || matches!(ch, '\n' | '\t'));+ return (body, format);+ }let continuation = line.source.copydiff --git a/codex-rs/tui/src/markdown_copy/table.rs b/codex-rs/tui/src/markdown_copy/table.rsindex 63dfc8a329..affc6dcbfd 100644--- a/codex-rs/tui/src/markdown_copy/table.rs+++ b/codex-rs/tui/src/markdown_copy/table.rs@@ -6,6 +6,7 @@use super::CopyLine;use super::SelectedLine;+use crate::clipboard_copy::CopyFormat;use crate::terminal_hyperlinks::HyperlinkLine;use crate::terminal_hyperlinks::LogicalLineSource;use std::collections::BTreeMap;@@ -100,7 +101,11 @@ pub(crate) fn append(output: &mut Option<TableLine>, source: &LogicalLineSource,}}-pub(super) fn render(lines: &[&SelectedLine], table: &TableLine) -> String {+pub(super) fn render(+ lines: &[&SelectedLine],+ table: &TableLine,+ entire_selection: bool,+) -> (String, CopyFormat) {let mut cells: BTreeMap<(usize, usize), Vec<LogicalLineSource>> = BTreeMap::new();for line in lines {let Some(copy) = line@@ -155,6 +160,20 @@ pub(super) fn render(lines: &[&SelectedLine], table: &TableLine) -> String {.is_some_and(|table| table.fragments.is_empty())});let standalone = cells.len() == 1 && !includes_separator;+ if entire_selection+ && standalone+ && let Some(parts) = cells.values().next()+ && let [source] = parts.as_slice()+ && source+ .copy+ .as_ref()+ .is_some_and(|copy| copy.is_inline_code(&source.range))+ {+ return (+ source.text[source.range.clone()].to_owned(),+ CopyFormat::PlainText,+ );+ }let cells: BTreeMap<_, _> = cells.into_iter().map(|(key, parts)| {@@ -176,14 +195,20 @@ pub(super) fn render(lines: &[&SelectedLine], table: &TableLine) -> String {}).collect();if standalone {- return cells.into_values().next().unwrap_or_default();+ return (+ cells.into_values().next().unwrap_or_default(),+ CopyFormat::Markdown,+ );}if cells.is_empty() {- return lines- .iter()- .map(|line| super::escape(&line.source.text[line.range.clone()]))- .collect::<Vec<_>>()- .join("\n");+ return (+ lines+ .iter()+ .map(|line| super::escape(&line.source.text[line.range.clone()]))+ .collect::<Vec<_>>()+ .join("\n"),+ CopyFormat::Markdown,+ );}let mut rows: BTreeMap<usize, Vec<&str>> = BTreeMap::new();rows.insert(/*key*/ 0, vec![""; table.table.len()]);@@ -205,5 +230,5 @@ pub(super) fn render(lines: &[&SelectedLine], table: &TableLine) -> String {}}}- out+ (out, CopyFormat::Markdown)}diff --git a/codex-rs/tui/src/transcript_view/markdown_copy_tests.rs b/codex-rs/tui/src/transcript_view/markdown_copy_tests.rsindex 8642e6e366..443afe61d0 100644--- a/codex-rs/tui/src/transcript_view/markdown_copy_tests.rs+++ b/codex-rs/tui/src/transcript_view/markdown_copy_tests.rs@@ -82,8 +82,6 @@ fn payload(layout: &TextLayout, range: std::ops::Range<usize>) -> (String, CopyF#[test]fn partial_markup_is_balanced_and_preserves_selected_characters() {for (source, selected, expected) in [- ("before `closeRequested_` after", "Requested", "`Requested`"),- ("before ``a`b`` after", "a`", "`` a` ``"),("**hello café界**", "café界", "**café界**"),("before * first *", "first", "first"),("before **hello world** after", " world", " **world**"),@@ -119,6 +117,35 @@ fn partial_markup_is_balanced_and_preserves_selected_characters() {}}+#[test]+fn inline_code_selection_copies_only_selected_content() {+ let mut copies = Vec::new();+ for (source, selected) in [+ ("before `something` after", "something"),+ ("before `something` after", "some"),+ ("before `something` after", "thing"),+ ("before `closeRequested_` after", "Requested"),+ ("before ``a`b`` after", "a`"),+ ("- **`café界_*`**", "café界_*"),+ ("before ` leading and trailing ` after", " leading"),+ ] {+ for width in [18, 80] {+ let layout = markdown_layout(source, width);+ let start = layout.text().find(selected).expect(source);+ let copied = payload(&layout, start..start + selected.len());+ assert_eq!(copied, (selected.into(), CopyFormat::PlainText), "{source}");+ assert_eq!(+ payload(&layout.rewrap(/*width*/ 12), start..start + selected.len()),+ copied+ );+ if width == 80 {+ copies.push(format!("{source}\nSelected {selected:?} → {copied:?}"));+ }+ }+ }+ insta::assert_snapshot!(copies.join("\n"));+}+#[test]fn inline_boundaries_preserve_selected_text_and_formatting() {let mut copies = Vec::new();@@ -306,7 +333,7 @@ fn copied_selection_keeps_its_revision_and_format_across_resize() {assert_eq!(view.selected_text(&cells).as_deref(), Some("selected"));for clear_selection in [false, true] {view.copy_selected_text_with(&cells, "selected", clear_selection, |text, format| {- assert_eq!((text, format), ("`selected`", CopyFormat::Markdown));+ assert_eq!((text, format), ("selected", CopyFormat::PlainText));Ok(CopyStatus::Pending(1))}).unwrap();diff --git a/codex-rs/tui/src/transcript_view/snapshots/codex_tui__transcript_view__markdown_copy_tests__inline_code_selection_copies_only_selected_content.snap b/codex-rs/tui/src/transcript_view/snapshots/codex_tui__transcript_view__markdown_copy_tests__inline_code_selection_copies_only_selected_content.snapnew file mode 100644index 0000000000..9feeaa9dd6--- /dev/null+++ b/codex-rs/tui/src/transcript_view/snapshots/codex_tui__transcript_view__markdown_copy_tests__inline_code_selection_copies_only_selected_content.snap@@ -0,0 +1,18 @@+---+source: tui/src/transcript_view/markdown_copy_tests.rs+expression: "copies.join(\"\\n\")"+---+before `something` after+Selected "something" → ("something", PlainText)+before `something` after+Selected "some" → ("some", PlainText)+before `something` after+Selected "thing" → ("thing", PlainText)+before `closeRequested_` after+Selected "Requested" → ("Requested", PlainText)+before ``a`b`` after+Selected "a`" → ("a`", PlainText)+- **`café界_*`**+Selected "café界_*" → ("café界_*", PlainText)+before ` leading and trailing ` after+Selected " leading" → (" leading", PlainText)diff --git a/codex-rs/tui/src/transcript_view/table_copy_tests.rs b/codex-rs/tui/src/transcript_view/table_copy_tests.rsindex b792eaa31a..aaecaaae7f 100644--- a/codex-rs/tui/src/transcript_view/table_copy_tests.rs+++ b/codex-rs/tui/src/transcript_view/table_copy_tests.rs@@ -33,7 +33,33 @@ fn partial_table_selection_never_adds_unselected_cell_text() {/*width*/ 80,);let start = code.text().find("a|b").unwrap();- assert_eq!(payload(&code, start..start + 3).0, "`a|b`");+ assert_eq!(+ payload(&code, start..start + 3),+ ("a|b".into(), CopyFormat::PlainText)+ );+ assert_eq!(+ payload(&code, start + 1..start + 3),+ ("|b".into(), CopyFormat::PlainText)+ );+ let mixed = markdown_layout(+ "Before\n\n| `a\\|b` | B |\n|---|---|\n| left | right |",+ /*width*/ 80,+ );+ let end = mixed.text().find("a|b").unwrap() + "a|b".len();+ assert_eq!(+ payload(&mixed, 0..end),+ ("Before\n\n`a|b`".into(), CopyFormat::Markdown)+ );+ let controls = markdown_layout(+ "| A | B |\n|---|---|\n| `a\u{7}\\|b` | value |",+ /*width*/ 80,+ );+ let selected = "a\u{7}|b";+ let start = controls.text().find(selected).unwrap();+ assert_eq!(+ payload(&controls, start..start + selected.len()),+ ("a|b".into(), CopyFormat::PlainText)+ );let repeated = markdown_layout("| alpha SECRET omega |\n|---|\n| long_value_one |\n| long_value_two |",/*width*/ 12,