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
This commit is contained in:
Felipe Coury
2026-09-24 17:55:29 +00:00
committed by copyberry
parent f6fd7f17ed
commit a33fb9751c
38 changed files with 1790 additions and 41 deletions
@@ -21,7 +21,7 @@ fn pending_copy_feedback_survives_expiry_and_selection_replacement() {
&cells,
&selected,
/*clear_selection*/ true,
|_| Ok(CopyStatus::Pending(1)),
|_, _format| Ok(CopyStatus::Pending(1)),
);
view.show_copy_feedback(&pending, selected.chars().count());
view.show_copy_feedback(&Ok(CopyStatus::Busy), /*characters*/ 0);
@@ -80,7 +80,7 @@ fn copy_on_select_waits_for_release_and_retains_selection() {
&cells,
&copied,
/*clear_selection*/ false,
|_| Ok(CopyStatus::Pending(1)),
|_, _format| Ok(CopyStatus::Pending(1)),
)
.unwrap();
assert_eq!(
@@ -130,10 +130,15 @@ fn copying_selection_at_bottom_does_not_show_back_to_bottom() {
view.begin_selection(&cells, /*column*/ 0, /*row*/ 2, /*clicks*/ 2);
view.end_drag();
let selected = view.selected_text(&cells).expect("selected word");
view.copy_selected_text_with(&cells, &selected, /*clear_selection*/ true, |text| {
assert_eq!(text, "needle");
Ok(crate::clipboard_copy::CopyStatus::Confirmed)
})
view.copy_selected_text_with(
&cells,
&selected,
/*clear_selection*/ true,
|text, _format| {
assert_eq!(text, "needle");
Ok(crate::clipboard_copy::CopyStatus::Confirmed)
},
)
.expect("successful copy");
let after = paint(&mut view, &cells, /*width*/ 40);
@@ -221,9 +226,12 @@ fn copying_live_selection_retains_the_displayed_revision() {
view.sync_live_tail(/*width*/ 40, /*key*/ None, |_| {
Some(vec![HyperlinkLine::from("changed live text")])
});
view.copy_selected_text_with(&cells, &selected, /*clear_selection*/ true, |_| {
Ok(crate::clipboard_copy::CopyStatus::Confirmed)
})
view.copy_selected_text_with(
&cells,
&selected,
/*clear_selection*/ true,
|_, _format| Ok(crate::clipboard_copy::CopyStatus::Confirmed),
)
.unwrap();
let buffer = paint(&mut view, &cells, /*width*/ 40);
assert!(!view.is_following());
@@ -0,0 +1,688 @@
use super::*;
use crate::clipboard_copy::CopyFormat;
use crate::history_cell::AgentMarkdownCell;
use crate::history_cell::HistoryCell;
use pretty_assertions::assert_eq;
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use std::path::Path;
#[test]
fn selected_markdown_preserves_lists_and_inline_code() {
let cells: Vec<Arc<dyn HistoryCell>> = vec![Arc::new(AgentMarkdownCell::new(
"- setting `closeRequested_`,\n- removing the tailer from `tailers_`,\n- clearing its live/discovery state.".into(),
Path::new("/"),
))];
let mut view = TranscriptView::default();
let area = Rect::new(
/*x*/ 0, /*y*/ 0, /*width*/ 32, /*height*/ 24,
);
view.render(area, &mut Buffer::empty(area), &cells);
// Exercise the public gesture path rather than manufacturing metadata.
use crossterm::event::KeyModifiers;
use crossterm::event::MouseButton;
use crossterm::event::MouseEvent;
use crossterm::event::MouseEventKind;
view.handle_mouse(
MouseEvent {
kind: MouseEventKind::Down(MouseButton::Left),
column: 2,
row: 0,
modifiers: KeyModifiers::NONE,
},
&cells,
);
view.handle_mouse(
MouseEvent {
kind: MouseEventKind::Drag(MouseButton::Left),
column: 31,
row: 23,
modifiers: KeyModifiers::NONE,
},
&cells,
);
let plain = view.selected_text(&cells).unwrap();
view.copy_selected_text_with(
&cells,
&plain,
/*clear_selection*/ true,
|text, format| {
assert_eq!(format, CopyFormat::Markdown);
insta::assert_snapshot!(text);
insta::assert_snapshot!(
"selected_list_html",
crate::clipboard_html::render_markdown(text)
);
Ok(crate::clipboard_copy::CopyStatus::Confirmed)
},
)
.unwrap();
}
fn markdown_layout(markdown: &str, width: u16) -> TextLayout {
let cell = AgentMarkdownCell::new(markdown.into(), Path::new("/"));
TextLayout::new(
cell.retained_hyperlink_lines(width, /*detailed*/ false),
width,
)
}
fn payload(layout: &TextLayout, range: std::ops::Range<usize>) -> (String, CopyFormat) {
let mut lines = Vec::new();
layout.copy_lines(range.clone(), "", &mut lines);
crate::markdown_copy::selection(&lines, &layout.text()[range])
}
#[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", "&#32;**world**"),
(
"**strong *nested* text**",
"strong nested text",
"**strong _nested_ text**",
),
(
r"escaped \![label](https://example.com)",
"escaped !label",
r"escaped \![label](<https://example.com>)",
),
(
"[documentation](https://example.com)",
"ment", // codespell:ignore ment
"[ment](<https://example.com>)", // codespell:ignore ment
),
("[**label**](/repo/file.rs)", "label", "**label**"),
(
"escaped \\*literal\\* &amp; `code`",
"*literal* & code",
"\\*literal\\* \\& `code`",
),
] {
let layout = markdown_layout(source, /*width*/ 80);
let start = layout.text().find(selected).expect(source);
assert_eq!(
payload(&layout, start..start + selected.len()),
(expected.into(), CopyFormat::Markdown),
"{source}"
);
}
}
#[test]
fn inline_boundaries_preserve_selected_text_and_formatting() {
let mut copies = Vec::new();
for (source, selected, expected_html) in [
(
"_a **b**_**c**",
"a bc",
"<p><em>a</em> <strong><em>b</em>c</strong></p>\n",
),
(
"_a **b**_**c**",
"bc",
"<p><strong><em>b</em>c</strong></p>\n",
),
(
"*a **b***[**c**](/repo)",
"a bc",
"<p><em>a</em> <strong><em>b</em>c</strong></p>\n",
),
("_*a*_[_b_](/repo)", "ab", "<p><em>ab</em></p>\n"),
(
"*[a **b**](https://example.com) c*",
"a b",
"<p><a href=\"https://example.com\"><em>a</em> <strong><em>b</em></strong></a></p>\n",
),
(
"a*b*c a**b**c",
"abc abc",
"<p>a<em>b</em>c a<strong>b</strong>c</p>\n",
),
("&#97;_b_&#99;", "abc", "<p>a<em>b</em>c</p>\n"),
("&#97;__b__&#99;", "abc", "<p>a<strong>b</strong>c</p>\n"),
("&#769;_b_", "\u{301}b", "<p>\u{301}<em>b</em></p>\n"),
("&#97;*!*&#98;", "a!b", "<p>a<em>!</em>b</p>\n"),
("&#97;*&#12290;*&#98;", "a。b", "<p>a<em>。</em>b</p>\n"),
(
"&#97;**_b_**&#99;",
"abc",
"<p>a<strong><em>b</em></strong>c</p>\n",
),
(
"&#97;**`b`**&#99;",
"abc",
"<p>a<strong><code>b</code></strong>c</p>\n",
),
("_a_[b](/repo)", "ab", "<p><em>a</em>b</p>\n"),
(
"&#32;&#32;&#32;&#32;**hello**",
" hello",
"<p> <strong>hello</strong></p>\n",
),
] {
let layout = markdown_layout(source, /*width*/ 80);
let start = layout.text().find(selected).expect(source);
let (copied, format) = payload(&layout, start..start + selected.len());
assert_eq!(format, CopyFormat::Markdown);
let html = crate::clipboard_html::render_markdown(&copied);
assert_eq!(html, expected_html, "{source:?} => {copied:?}");
copies.push(format!(
"{source}\nSelected: {selected:?}\nMarkdown: {copied}\n{html}"
));
}
insta::assert_snapshot!(copies.join("\n"));
}
#[test]
fn lists_keep_nesting_and_paragraphs_without_sibling_spacers() {
let source = "3. first item long enough to wrap\n4. second item\n - nested `code`\n\n another paragraph\n - next nested\n5. final item\n\nOutside.";
let mut copies = Vec::new();
for width in [24, 80] {
let layout = markdown_layout(source, width);
let copied = payload(&layout, 0..layout.text().len()).0;
assert_eq!(
payload(&layout.rewrap(/*width*/ 14), 0..layout.text().len()).0,
copied
);
copies.push(copied);
}
assert_eq!(copies[0], copies[1]);
insta::assert_snapshot!(copies[0]);
}
#[test]
fn code_only_is_literal_and_mixed_code_has_safe_fences() {
let layout = markdown_layout(
"Prose\n\n````text\n *literal* <tag>\n```\n````\n\nAfter",
/*width*/ 80,
);
let selected = " *literal* <tag>\n```";
let start = layout.text().find(selected).unwrap();
assert_eq!(
payload(&layout, start..start + selected.len()),
(selected.into(), CopyFormat::PlainText)
);
let (copy, format) = payload(&layout, 0..layout.text().len());
assert_eq!(format, CopyFormat::Markdown);
assert!(copy.contains("````\n *literal* <tag>\n```\n````"));
insta::assert_snapshot!(crate::clipboard_html::render_markdown(&copy));
}
#[test]
fn mixed_plain_output_remains_literal_in_html() {
let layout = markdown_layout("**Answer**", /*width*/ 40);
let literal = "*log* <tag>\n- not a list";
let output = TextLayout::new(
literal.lines().map(HyperlinkLine::from).collect(),
/*width*/ 40,
);
let mut lines = Vec::new();
layout.copy_lines(0..layout.text().len(), "", &mut lines);
output.copy_lines(0..output.text().len(), "\n", &mut lines);
let (copy, format) = crate::markdown_copy::selection(&lines, "unused");
assert_eq!(format, CopyFormat::Markdown);
assert_eq!(
crate::clipboard_html::render_markdown(&copy),
"<p><strong>Answer</strong></p>\n<pre><code>*log* &lt;tag&gt;\n- not a list\n</code></pre>\n"
);
assert_eq!(
payload(&output, 0..output.text().len()),
(literal.into(), CopyFormat::PlainText)
);
}
#[test]
fn mixed_user_prose_preserves_literal_text_without_code_fences() {
let cell = crate::history_cell::new_user_prompt(
" Explain *literal*\nnext <tag>".into(),
Vec::new(),
Vec::new(),
vec!["https://example.com/image.png".into()],
);
let user = TextLayout::new(
cell.retained_hyperlink_lines(/*width*/ 24, /*detailed*/ false),
/*width*/ 24,
);
assert_eq!(
payload(&user, 0..user.text().len()),
(user.text().into(), CopyFormat::PlainText)
);
let user = user.rewrap(/*width*/ 12);
let answer = markdown_layout("**Answer**", /*width*/ 24);
let tool = TextLayout::new(vec![HyperlinkLine::from("*log* <tag>")], /*width*/ 24);
let mut copies = Vec::new();
for layouts in [[&user, &answer, &tool], [&answer, &tool, &user]] {
let mut lines = Vec::new();
for layout in layouts {
layout.copy_lines(0..layout.text().len(), "\n\n", &mut lines);
}
let (text, format) = crate::markdown_copy::selection(&lines, "unused");
assert_eq!(format, CopyFormat::Markdown);
copies.push(crate::clipboard_html::render_markdown(&text));
}
insta::assert_snapshot!(copies.join("\n---\n"));
}
#[test]
fn copied_selection_keeps_its_revision_and_format_across_resize() {
use crate::clipboard_copy::CopyStatus;
let cell: Arc<dyn HistoryCell> = Arc::new(AgentMarkdownCell::new(
"before `selected` after".into(),
Path::new("/"),
));
let mut cells = vec![cell];
let mut view = TranscriptView::default();
let area = Rect::new(
/*x*/ 0, /*y*/ 0, /*width*/ 40, /*height*/ 4,
);
view.render(area, &mut Buffer::empty(area), &cells);
let layout = view.layout(&cells, /*index*/ 0).unwrap();
let start = layout.text().find("selected").unwrap();
view.begin_selection(
&cells,
layout.column_for_offset(start + 8),
/*row*/ 0,
/*clicks*/ 1,
);
view.extend_selection(layout.column_for_offset(start), /*row*/ 0);
view.end_drag();
assert_eq!(view.selected_text(&cells).as_deref(), Some("selected"));
cells[0] = Arc::new(AgentMarkdownCell::new("replacement".into(), Path::new("/")));
let narrow = Rect::new(
/*x*/ 0, /*y*/ 0, /*width*/ 12, /*height*/ 4,
);
view.render(narrow, &mut Buffer::empty(narrow), &cells);
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));
Ok(CopyStatus::Pending(1))
})
.unwrap();
assert_eq!(view.selected_text(&cells).as_deref(), Some("selected"));
assert_eq!(
view.finish_copy(
&cells,
&(1, Ok(CopyStatus::Confirmed)),
/*current*/ true
),
Some(false)
);
assert_eq!(view.has_selection_range(), !clear_selection);
}
}
#[test]
fn literal_punctuation_is_not_reinterpreted_and_selected_breaks_survive() {
for (source, expected_html) in [
("&amp;amp;", "<p>&amp;amp;</p>\n"),
(r"1\) literal", "<p>1) literal</p>\n"),
("foo\n\\=\\=\\=", "<p>foo\n===</p>\n"),
] {
let layout = markdown_layout(source, /*width*/ 80);
let copy = payload(&layout, 0..layout.text().len()).0;
assert_eq!(crate::clipboard_html::render_markdown(&copy), expected_html);
}
let layout = markdown_layout("first\n\nsecond", /*width*/ 80);
assert_eq!(payload(&layout, 5..layout.text().len()).0, "\n\nsecond");
}
#[test]
fn explicit_hard_breaks_survive_only_when_selected() {
let layout = markdown_layout("first \nsecond", /*width*/ 80);
assert_eq!(payload(&layout, 0..5).0, "first");
assert_eq!(payload(&layout, 0..6).0, "first \n");
assert_eq!(
crate::clipboard_html::render_markdown(&payload(&layout, 0..layout.text().len()).0),
"<p>first<br />\nsecond</p>\n"
);
}
#[test]
fn task_lists_and_transformed_tables_keep_their_meaning() {
for (source, expected) in [
("- [x] done\n- [ ] next", "- [x] done\n- [ ] next"),
("1. [x] done\n2. [ ] next", "1. [x] done\n2. [ ] next"),
] {
let layout = markdown_layout(source, /*width*/ 80);
assert_eq!(payload(&layout, 0..layout.text().len()).0, expected);
}
let layout = markdown_layout(
"Prose\n\n| A | B |\n| - | - |\n| x | y |",
/*width*/ 80,
);
let copied = payload(&layout, 0..layout.text().len()).0;
let html = crate::clipboard_html::render_markdown(&copied);
assert!(html.contains("<pre><code>"), "{copied}\n{html}");
}
#[test]
fn link_entities_are_not_decoded_twice() {
let layout = markdown_layout(
"[label](https://example.com/?x=&amp;amp;)",
/*width*/ 80,
);
let copy = payload(&layout, 0..5).0;
assert_eq!(
crate::clipboard_html::render_markdown(&copy),
"<p><a href=\"https://example.com/?x=&amp;amp;\">label</a></p>\n"
);
}
#[test]
fn selected_local_link_keeps_one_visible_destination() {
let layout = markdown_layout("Open [**label**](/repo/file.rs).", /*width*/ 80);
let copied = payload(&layout, 0..layout.text().len()).0;
assert_eq!(copied, "Open **label** (repo/file.rs).");
assert_eq!(
crate::clipboard_html::render_markdown(&copied),
"<p>Open <strong>label</strong> (repo/file.rs).</p>\n"
);
}
#[test]
fn complete_rules_keep_structure_and_partial_rules_keep_selected_text() {
let mut copies = Vec::new();
for (source, partial) in [
("Before\n\n---\n\nAfter", "—"),
("Before\n\n> ---\n\nAfter", "> —"),
] {
let layout = markdown_layout(source, /*width*/ 80);
let copied = payload(&layout, 0..layout.text().len()).0;
copies.push(format!(
"{copied}\n{}",
crate::clipboard_html::render_markdown(&copied)
));
let start = layout.text().find('—').unwrap();
assert_eq!(payload(&layout, start..start + "—".len()).0, partial);
}
insta::assert_snapshot!(copies.join("\n---\n"));
}
#[test]
fn table_spillover_keeps_prose_outside_the_literal_table() {
let layout = markdown_layout(
"**Before**\n\n| A | B |\n| --- | --- |\n| one | two |\nHTML block:\n<div>Prose after the table.</div>\n\nAfter",
/*width*/ 80,
);
let copied = payload(&layout, 0..layout.text().len()).0;
insta::assert_snapshot!(format!(
"{copied}\n{}",
crate::clipboard_html::render_markdown(&copied)
));
}
#[test]
fn partial_prose_keeps_leading_whitespace_outside_code_blocks() {
let mut copies = Vec::new();
for source in [
"prefix suffix",
"prefix\tsuffix",
"**prefix suffix**",
"> prefix suffix",
] {
let layout = markdown_layout(source, /*width*/ 80);
let start = layout.text().find("prefix").unwrap() + "prefix".len();
let copied = payload(&layout, start..layout.text().len()).0;
copies.push(format!(
"{copied}\n{}",
crate::clipboard_html::render_markdown(&copied)
));
}
insta::assert_snapshot!(copies.join("\n---\n"));
}
#[test]
fn nested_tables_keep_their_copy_containers() {
let mut copies = Vec::new();
for source in [
"Before\n\n> | A | B |\n> | --- | --- |\n> | one | two |",
"Before\n\n- intro\n\n | A | B |\n | --- | --- |\n | one | two |",
] {
let layout = markdown_layout(source, /*width*/ 80);
let copied = payload(&layout, 0..layout.text().len()).0;
copies.push(format!(
"{copied}\n{}",
crate::clipboard_html::render_markdown(&copied)
));
}
insta::assert_snapshot!(copies.join("\n---\n"));
}
#[test]
fn selected_hard_breaks_survive_surrounding_blocks() {
let source = "First paragraph.\n\nSecond paragraph with a hard break: \nnext line.\n\n> quoted **words** \n> next quote line\n\nFinal paragraph.";
let mut stream = crate::streaming::controller::StreamController::new(
Some(78),
Path::new("/"),
crate::history_cell::HistoryRenderMode::Rich,
);
let mut cells: Vec<Arc<dyn HistoryCell>> = Vec::new();
for chunk in source.split_inclusive('\n') {
stream.push(chunk);
while let Some(cell) = stream.on_commit_tick_batch(/*max_lines*/ 1).0 {
cells.push(cell.into());
}
}
if let Some(cell) = stream.finalize().0 {
cells.push(cell.into());
}
let area = Rect::new(
/*x*/ 0, /*y*/ 0, /*width*/ 80, /*height*/ 40,
);
let mut view = TranscriptView::default();
view.render(area, &mut Buffer::empty(area), &cells);
view.begin_selection(&cells, /*column*/ 2, /*row*/ 0, /*clicks*/ 1);
view.extend_selection(/*column*/ 79, /*row*/ 39);
let plain = view.selected_text(&cells).unwrap();
view.copy_selected_text_with(&cells, &plain, /*clear_selection*/ true, |text, format| {
assert_eq!(format, CopyFormat::Markdown);
assert_eq!(
crate::clipboard_html::render_markdown(text),
"<p>First paragraph.</p>\n<p>Second paragraph with a hard break:<br />\nnext line.</p>\n<blockquote>\n<p>quoted <strong>words</strong><br />\nnext quote line</p>\n</blockquote>\n<p>Final paragraph.</p>\n"
);
Ok(crate::clipboard_copy::CopyStatus::Confirmed)
}).unwrap();
}
#[test]
fn selected_open_code_fence_keeps_streamed_rows_in_one_block() {
let mut stream = crate::streaming::controller::StreamController::new(
/*width*/ Some(78),
Path::new("/"),
crate::history_cell::HistoryRenderMode::Rich,
);
let mut cells: Vec<Arc<dyn HistoryCell>> = Vec::new();
for chunk in ["Prose\n\n```rust\nlet a = 1;\n", "let b = 2;\n"] {
stream.push(chunk);
while let Some(cell) = stream.on_commit_tick_batch(/*max_lines*/ 1).0 {
cells.push(cell.into());
}
}
let area = Rect::new(
/*x*/ 0, /*y*/ 0, /*width*/ 80, /*height*/ 10,
);
let mut view = TranscriptView::default();
view.render(area, &mut Buffer::empty(area), &cells);
view.begin_selection(&cells, /*column*/ 2, /*row*/ 0, /*clicks*/ 1);
view.extend_selection(/*column*/ 79, /*row*/ 9);
let plain = view.selected_text(&cells).unwrap();
view.copy_selected_text_with(
&cells,
&plain,
/*clear_selection*/ true,
|text, format| {
assert_eq!(format, CopyFormat::Markdown);
insta::assert_snapshot!(crate::clipboard_html::render_markdown(text), @r#"
<p>Prose</p>
<pre><code>let a = 1;
let b = 2;
</code></pre>
"#);
Ok(crate::clipboard_copy::CopyStatus::Confirmed)
},
)
.unwrap();
}
#[test]
fn cross_cell_selection_keeps_boundary_breaks() {
let first = markdown_layout("first", /*width*/ 40);
let second = markdown_layout("**second**", /*width*/ 40);
for (left, right, expected) in [(5..5, 0..6, "\n**second**"), (0..5, 0..0, "first\n")] {
let mut lines = Vec::new();
first.copy_lines(left, "", &mut lines);
second.copy_lines(right, "\n", &mut lines);
assert_eq!(
crate::markdown_copy::selection(&lines, "unused").0,
expected
);
}
let rule = markdown_layout("---", /*width*/ 40);
let mut lines = Vec::new();
first.copy_lines(0..first.text().len(), "", &mut lines);
rule.copy_lines(0..rule.text().len(), "\n", &mut lines);
let copied = crate::markdown_copy::selection(&lines, "unused").0;
assert_eq!(
crate::clipboard_html::render_markdown(&copied),
"<p>first</p>\n<hr />\n"
);
}
#[test]
fn adjacent_top_level_code_blocks_keep_blank_lines_and_boundaries() {
let layout = markdown_layout(
"Prose\n\n```\none\n\ninside\n```\n\n```\ntwo\n```",
/*width*/ 80,
);
let copied = payload(&layout, 0..layout.text().len()).0;
insta::assert_snapshot!(format!(
"Markdown:\n{copied}\n\nHTML:\n{}",
crate::clipboard_html::render_markdown(&copied)
));
}
#[test]
fn adjacent_code_containers_remain_separate() {
for (source, expected_html) in [
(
"Prose\n\n> ```\n> quoted\n> ```\n\n```\nunquoted\n```",
"<p>Prose</p>\n<blockquote>\n<pre><code>quoted\n</code></pre>\n</blockquote>\n<pre><code>unquoted\n</code></pre>\n",
),
(
"Prose\n\n- ```\n one\n ```\n- ```\n two\n ```",
"<p>Prose</p>\n<ul>\n<li>\n<pre><code>one\n</code></pre>\n</li>\n<li>\n<pre><code>two\n</code></pre>\n</li>\n</ul>\n",
),
(
"- before\n\n ```text\n let value = 1;\n ```\n\n after\n\n> quoted \n> next",
"<ul>\n<li>\n<p>before</p>\n<pre><code>let value = 1;\n</code></pre>\n<p>after</p>\n</li>\n</ul>\n<blockquote>\n<p>quoted<br />\nnext</p>\n</blockquote>\n",
),
] {
let layout = markdown_layout(source, /*width*/ 80);
let copied = payload(&layout, 0..layout.text().len()).0;
assert_eq!(
crate::clipboard_html::render_markdown(&copied),
expected_html,
"{copied}"
);
}
}
#[test]
fn selected_list_and_code_containers_keep_their_structure() {
for (source, start, end, expected) in [
(
"- parent\n - nested `code`",
"nested",
"code",
"- nested `code`",
),
(
"1. first\n2. **second**",
"irst",
"second",
"1. irst\n2. **second**",
),
(
"10. first\n\n second paragraph\n11. next",
"first",
"next",
"10. first\n\n second paragraph\n11. next",
),
(
"1. intro\n\n more words\n2. next",
"words",
"next",
"1. words\n2. next",
),
(
"- outer A\n - inner A\n- outer B\n - inner B",
"inner A",
"inner B",
"- inner A\n- outer B\n - inner B",
),
(
"> - outer\n> > - nested `code`",
"nested",
"code",
"> > - nested `code`",
),
(
"10. [x] first\n\n paragraph\n11. [ ] next",
"first",
"next",
"10. [x] first\n\n paragraph\n11. [ ] next",
),
(
"10. before\n\n ```\n code\n ```\n\n after",
"before",
"after",
"10. before\n\n ```\n code\n ```\n\n after",
),
(
"- outer\n - before\n\n ```\n code\n ```\n\n after",
"before",
"after",
"- before\n\n ```\n code\n ```\n\n after",
),
(
"Before\n\n indented_code()\n\nAfter",
"Before",
"After",
"Before\n\n```\nindented_code()\n```\n\nAfter",
),
(
"10. intro\n\n ```\n code\n ```\n\n after",
"code",
"after",
"10. ```\n code\n ```\n\n after",
),
(
"- [x] intro\n\n code\n\n after",
"code",
"after",
"- [x]\n ```\n code\n ```\n\n after",
),
(
"> - intro\n>\n> ```\n> code\n> ```\n>\n> after",
"code",
"after",
"> - ```\n> code\n> ```\n>\n> after",
),
] {
let layout = markdown_layout(source, /*width*/ 80);
let start = layout.text().find(start).expect(source);
let end = layout.text().rfind(end).expect(source) + end.len();
let copied = payload(&layout, start..end).0;
assert_eq!(
crate::clipboard_html::render_markdown(&copied),
crate::clipboard_html::render_markdown(expected),
"{copied:?} from {source:?}"
);
}
}
@@ -186,9 +186,12 @@ fn paused_header_reservation_survives_copy_until_scroll_or_resize() {
);
let selected = view.selected_text(&cells).expect("selected word");
assert_eq!(selected, "done");
view.copy_selected_text_with(&cells, &selected, /*clear_selection*/ true, |_| {
Ok(crate::clipboard_copy::CopyStatus::Confirmed)
})
view.copy_selected_text_with(
&cells,
&selected,
/*clear_selection*/ true,
|_, _format| Ok(crate::clipboard_copy::CopyStatus::Confirmed),
)
.expect("confirmed copy");
let bookmark = view.bookmark(&cells);
view.set_presentation(/*detailed*/ true, HistoryRenderMode::Rich);
@@ -38,7 +38,7 @@ fn right_click_retains_selection_until_confirmed_and_preserves_reading_position(
&cells,
&selected,
/*clear_selection*/ true,
|copied| {
|copied, _format| {
assert_eq!(copied, selected);
result.clone()
},
+33 -9
View File
@@ -159,7 +159,10 @@ impl TranscriptView {
})
}
pub(crate) fn selected_text(&mut self, cells: &[Arc<dyn HistoryCell>]) -> Option<String> {
fn selected_ranges(
&mut self,
cells: &[Arc<dyn HistoryCell>],
) -> Option<Vec<(Arc<TextLayout>, std::ops::Range<usize>)>> {
if !self.has_selection_range() {
return None;
}
@@ -176,16 +179,12 @@ impl TranscriptView {
if start == end {
return None;
}
let mut text = String::new();
let mut previous: Option<Arc<TextLayout>> = None;
let mut ranges = Vec::new();
for index in start.index..=end.index {
let layout = self.layout(cells, index)?;
if layout.row_count() == 0 {
continue;
}
if let Some(previous) = &previous {
text.push_str(previous.separator_after(&layout));
}
let begin = if index == start.index {
start.offset
} else {
@@ -196,7 +195,19 @@ impl TranscriptView {
} else {
layout.text().len()
};
text.push_str(layout.text().get(begin..finish)?);
ranges.push((layout, begin..finish));
}
Some(ranges)
}
pub(crate) fn selected_text(&mut self, cells: &[Arc<dyn HistoryCell>]) -> Option<String> {
let mut text = String::new();
let mut previous: Option<Arc<TextLayout>> = None;
for (layout, range) in self.selected_ranges(cells)? {
if let Some(previous) = &previous {
text.push_str(previous.separator_after(&layout));
}
text.push_str(layout.text().get(range)?);
previous = Some(layout);
}
text.retain(|ch| !ch.is_control() || matches!(ch, '\n' | '\t'));
@@ -210,9 +221,22 @@ impl TranscriptView {
cells: &[Arc<dyn HistoryCell>],
text: &str,
clear_selection: bool,
copy: impl FnOnce(&str) -> Result<crate::clipboard_copy::CopyStatus, String>,
copy: impl FnOnce(
&str,
crate::clipboard_copy::CopyFormat,
) -> Result<crate::clipboard_copy::CopyStatus, String>,
) -> Result<crate::clipboard_copy::CopyStatus, String> {
let result = copy(text);
let mut lines = Vec::new();
let mut previous: Option<Arc<TextLayout>> = None;
for (layout, range) in self.selected_ranges(cells).unwrap_or_default() {
let separator = previous
.as_ref()
.map_or("", |previous| previous.separator_after(&layout));
layout.copy_lines(range, separator, &mut lines);
previous = Some(layout);
}
let (text, format) = crate::markdown_copy::selection(&lines, text);
let result = copy(&text, format);
match result {
Ok(crate::clipboard_copy::CopyStatus::Confirmed) => {
if clear_selection {
@@ -98,7 +98,7 @@ fn copy_shortcuts_clear_selection_only_after_confirmed_delivery() {
&cells,
&text,
/*clear_selection*/ true,
|text| {
|text, _format| {
assert_eq!(text, "selected\tcafé");
Err("clipboard unavailable".to_string())
},
@@ -118,7 +118,7 @@ fn copy_shortcuts_clear_selection_only_after_confirmed_delivery() {
&cells,
&text,
/*clear_selection*/ true,
|text| {
|text, _format| {
assert_eq!(text, "selected\tcafé");
Ok(status)
},
@@ -169,7 +169,7 @@ fn command_c_ignores_release_and_copies_only_an_active_selection() {
panic!("Cmd+C must copy the transcript selection");
};
assert_eq!(text, "selected café");
view.copy_selected_text_with(&cells, &text, /*clear_selection*/ true, |_| {
view.copy_selected_text_with(&cells, &text, /*clear_selection*/ true, |_, _format| {
Ok(crate::clipboard_copy::CopyStatus::Confirmed)
})
.unwrap();
@@ -648,7 +648,7 @@ fn pending_copy_only_finishes_the_original_selection() {
&cells,
&selected,
/*clear_selection*/ true,
|_| Ok(CopyStatus::Pending(1)),
|_, _format| Ok(CopyStatus::Pending(1)),
)
.unwrap();
view.follow_pending_copy();
@@ -0,0 +1,25 @@
---
source: tui/src/transcript_view/markdown_copy_tests.rs
expression: "format!(\"Markdown:\\n{copied}\\n\\nHTML:\\n{}\",\ncrate::clipboard_html::render_markdown(&copied))"
---
Markdown:
Prose
```
one
inside
```
```
two
```
HTML:
<p>Prose</p>
<pre><code>one
inside
</code></pre>
<pre><code>two
</code></pre>
@@ -0,0 +1,10 @@
---
source: tui/src/transcript_view/markdown_copy_tests.rs
assertion_line: 88
expression: "crate::clipboard_html::render_markdown(&copy)"
---
<p>Prose</p>
<pre><code> *literal* &lt;tag&gt;
```
</code></pre>
<p>After</p>
@@ -0,0 +1,26 @@
---
source: tui/src/transcript_view/markdown_copy_tests.rs
assertion_line: 348
expression: "copies.join(\"\\n---\\n\")"
---
Before
***
After
<p>Before</p>
<hr />
<p>After</p>
---
Before
>
> ***
After
<p>Before</p>
<blockquote>
<hr />
</blockquote>
<p>After</p>
@@ -0,0 +1,79 @@
---
source: tui/src/transcript_view/markdown_copy_tests.rs
assertion_line: 139
expression: "copies.join(\"\\n\")"
---
_a **b**_**c**
Selected: "a bc"
Markdown: _a_ **_b_&#99;**
<p><em>a</em> <strong><em>b</em>c</strong></p>
_a **b**_**c**
Selected: "bc"
Markdown: **_b_&#99;**
<p><strong><em>b</em>c</strong></p>
*a **b***[**c**](/repo)
Selected: "a bc"
Markdown: _a_ **_b_&#99;**
<p><em>a</em> <strong><em>b</em>c</strong></p>
_*a*_[_b_](/repo)
Selected: "ab"
Markdown: _ab_
<p><em>ab</em></p>
*[a **b**](https://example.com) c*
Selected: "a b"
Markdown: [_a_ **_b_**](<https://example.com>)
<p><a href="https://example.com"><em>a</em> <strong><em>b</em></strong></a></p>
a*b*c a**b**c
Selected: "abc abc"
Markdown: &#97;_b_&#99; a**b**c
<p>a<em>b</em>c a<strong>b</strong>c</p>
&#97;_b_&#99;
Selected: "abc"
Markdown: &#97;_b_&#99;
<p>a<em>b</em>c</p>
&#97;__b__&#99;
Selected: "abc"
Markdown: a**b**c
<p>a<strong>b</strong>c</p>
&#769;_b_
Selected: "\u{301}b"
Markdown: &#769;_b_
<p>́<em>b</em></p>
&#97;*!*&#98;
Selected: "a!b"
Markdown: &#97;_\!_&#98;
<p>a<em>!</em>b</p>
&#97;*&#12290;*&#98;
Selected: "a。b"
Markdown: &#97;_。_&#98;
<p>a<em>。</em>b</p>
&#97;**_b_**&#99;
Selected: "abc"
Markdown: &#97;**_b_**&#99;
<p>a<strong><em>b</em></strong>c</p>
&#97;**`b`**&#99;
Selected: "abc"
Markdown: &#97;**`b`**&#99;
<p>a<strong><code>b</code></strong>c</p>
_a_[b](/repo)
Selected: "ab"
Markdown: _a_&#98;
<p><em>a</em>b</p>
&#32;&#32;&#32;&#32;**hello**
Selected: " hello"
Markdown: &#32;&#32;&#32;&#32;**hello**
<p> <strong>hello</strong></p>
@@ -0,0 +1,14 @@
---
source: tui/src/transcript_view/markdown_copy_tests.rs
assertion_line: 76
expression: "copies[0]"
---
3. first item long enough to wrap
4. second item
- nested `code`
another paragraph
- next nested
5. final item
Outside.
@@ -0,0 +1,19 @@
---
source: tui/src/transcript_view/markdown_copy_tests.rs
expression: "copies.join(\"\\n---\\n\")"
---
<p>[Image #1]</p>
<p> Explain *literal*<br />
next &lt;tag&gt;</p>
<p><strong>Answer</strong></p>
<pre><code>*log* &lt;tag&gt;
</code></pre>
---
<p><strong>Answer</strong></p>
<pre><code>*log* &lt;tag&gt;
</code></pre>
<p>[Image #1]</p>
<p> Explain *literal*<br />
next &lt;tag&gt;</p>
@@ -0,0 +1,40 @@
---
source: tui/src/transcript_view/markdown_copy_tests.rs
assertion_line: 393
expression: "copies.join(\"\\n---\\n\")"
---
Before
> ```
> A B
> ━━━━━ ━━━━━
> one two
> ```
<p>Before</p>
<blockquote>
<pre><code> A B
━━━━━ ━━━━━
one two
</code></pre>
</blockquote>
---
Before
- intro
```
A B
━━━━━ ━━━━━
one two
```
<p>Before</p>
<ul>
<li>
<p>intro</p>
<pre><code> A B
━━━━━ ━━━━━
one two
</code></pre>
</li>
</ul>
@@ -0,0 +1,19 @@
---
source: tui/src/transcript_view/markdown_copy_tests.rs
assertion_line: 379
expression: "copies.join(\"\\n---\\n\")"
---
&#32;&#32;&#32;&#32;suffix
<p> suffix</p>
---
&#9;suffix
<p> suffix</p>
---
&#32;&#32;&#32;&#32;**suffix**
<p> <strong>suffix</strong></p>
---
&#32;&#32;&#32;&#32;suffix
<p> suffix</p>
@@ -0,0 +1,10 @@
---
source: tui/src/transcript_view/markdown_copy_tests.rs
assertion_line: 27
expression: "crate::clipboard_html::render_markdown(text)"
---
<ul>
<li>setting <code>closeRequested_</code>,</li>
<li>removing the tailer from <code>tailers_</code>,</li>
<li>clearing its live/discovery state.</li>
</ul>
@@ -0,0 +1,8 @@
---
source: tui/src/transcript_view/markdown_copy_tests.rs
assertion_line: 26
expression: text
---
- setting `closeRequested_`,
- removing the tailer from `tailers_`,
- clearing its live/discovery state.
@@ -0,0 +1,24 @@
---
source: tui/src/transcript_view/markdown_copy_tests.rs
assertion_line: 358
expression: "format!(\"{copied}\\n{}\", crate::clipboard_html::render_markdown(&copied))"
---
**Before**
```
A B
━━━━━ ━━━━━
one two
```
HTML block:
\<div\>Prose after the table.\</div\>
After
<p><strong>Before</strong></p>
<pre><code> A B
━━━━━ ━━━━━
one two
</code></pre>
<p>HTML block:
&lt;div&gt;Prose after the table.&lt;/div&gt;</p>
<p>After</p>
+23
View File
@@ -179,6 +179,29 @@ impl TextLayout {
}
}
pub(super) fn copy_lines(
&self,
range: Range<usize>,
separator: &str,
output: &mut Vec<crate::markdown_copy::SelectedLine>,
) {
let mut offset = 0;
for (index, line) in self.logical.iter().enumerate() {
let end = offset + line.origin.range.len();
if range.start <= end && range.end >= offset {
let selected = line.origin.range.start + range.start.saturating_sub(offset)
..line.origin.range.start + range.end.min(end) - offset;
crate::markdown_copy::SelectedLine::append(
output,
line.origin.clone(),
selected,
if index == 0 { separator } else { "\n" },
);
}
offset = end + 1;
}
}
pub(super) fn text(&self) -> &str {
&self.text
}