Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 67 additions & 5 deletions crates/cli/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32365,7 +32365,7 @@ mod tests {
),
(
"backtick-fenced-code",
"before\n```rust\n# literal heading\n- literal bullet with @{session:abc123}\n[Run](agentd:action/example)\n```\nafter fence\nZEND",
"before\n```rust\n# literal 界🙂 heading\n- literal bullet with @{session:abc123}\n[Run](agentd:action/example)\n```\nafter fence\nZEND",
),
(
"timeline",
Expand Down Expand Up @@ -32869,9 +32869,9 @@ mod tests {
}

#[tokio::test]
async fn playbook_backtick_fence_formats_inline_and_keeps_multiline_literal() {
async fn playbook_backtick_fence_formats_multiline_and_preserves_source_rows() {
let (mut app, _dir, server) = empty_app().await;
let md = "```rust\n# not a heading\n- not a bullet\n@{session:literal}\n![shot](/tmp/shot.png)\n[Run](agentd:action/example)\n```";
let md = "```rust\n# 界🙂 not a heading\n- not a bullet\n@{session:literal}\n![shot](/tmp/shot.png)\n[Run](agentd:action/example)\n```";
app.playbook_popup = Some(playbook_popup_for_test("s1", md, 0));

let lines = crate::ui::render_playbook_markdown_lines_for_test(&app, md);
Expand All @@ -32884,11 +32884,22 @@ mod tests {
.collect::<String>()
})
.collect::<Vec<_>>();
assert_eq!(painted, md.lines().collect::<Vec<_>>());
assert_eq!(
painted,
[
"rust",
"# 界🙂 not a heading",
"- not a bullet",
"@{session:literal}",
"![shot](/tmp/shot.png)",
"[Run](agentd:action/example)",
"",
]
);
assert!(lines
.iter()
.flat_map(|line| &line.spans)
.all(|span| span.style.bg.is_none()));
.all(|span| span.style.bg == Some(app.theme.inactive_highlight_bg)));

let area = Rect::new(0, 0, 80, 20);
assert!(crate::ui::playbook_session_clip_hits(Some(&app), md, 0, area).is_empty());
Expand All @@ -32914,6 +32925,57 @@ mod tests {
"Tab must not apply Markdown list indentation inside raw code"
);

let closing_boundary = md.chars().count();
app.playbook_popup = Some(playbook_popup_for_test("s1", md, closing_boundary));
let cjk_start = "```rust\n# ".chars().count();
assert_eq!(
crate::ui::playbook_cursor_visual_pos(Some(&app), md, cjk_start, 80),
(1, 2)
);
assert_eq!(
crate::ui::playbook_cursor_visual_pos(Some(&app), md, cjk_start + 1, 80),
(1, 4),
"a CJK source character inside the fence advances by two cells"
);
assert_eq!(
crate::ui::playbook_cursor_visual_pos(Some(&app), md, closing_boundary, 80),
(6, 0),
"the hidden closer stays on its own source row"
);
assert_eq!(
crate::ui::playbook_visual_to_cursor(Some(&app), md, 6, 0, 80),
closing_boundary,
"clicking the hidden closing boundary resolves to its source end"
);
app.delete_playbook_back();
assert_eq!(
app.playbook_popup.as_ref().unwrap().buffer,
md.strip_suffix("```").unwrap(),
"Backspace at the hidden closing boundary removes the complete run"
);
let revealed = crate::ui::render_playbook_markdown_lines_for_test(
&app,
&app.playbook_popup.as_ref().unwrap().buffer,
);
assert_eq!(
revealed[0]
.spans
.iter()
.map(|span| span.content.as_ref())
.collect::<String>(),
"```rust"
);
app.insert_playbook_text("```");
assert_eq!(app.playbook_popup.as_ref().unwrap().buffer, md);
assert_eq!(
crate::ui::render_playbook_markdown_lines_for_test(&app, md)[0]
.spans
.iter()
.map(|span| span.content.as_ref())
.collect::<String>(),
"rust"
);

let boundary_source = "before ```界🙂 @{session:literal}```";
let inline = format!("{boundary_source} after");
app.playbook_popup = Some(playbook_popup_for_test(
Expand Down
7 changes: 7 additions & 0 deletions crates/cli/src/app/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3023,6 +3023,13 @@ impl App {
return;
}
let (char_start, char_end) = if let Some(range) =
crate::playbook_markdown::playbook_closing_multiline_fence_before_cursor(
&popup.buffer,
popup.cursor,
)
{
(range.start, range.end)
} else if let Some(range) =
crate::playbook_markdown::playbook_closing_inline_fence_before_cursor(
&popup.buffer,
popup.cursor,
Expand Down
113 changes: 110 additions & 3 deletions crates/cli/src/playbook_markdown.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! Source-preserving Markdown context needed by the Playbook editor.
//!
//! Completed one-line triple-backtick spans are presented like completed
//! inline code. Multiline and incomplete fences remain literal and inert. A
//! shared classifier keeps painting, hit-testing, and editing in agreement.
//! inline code. Completed multiline fences keep one rendered row per source
//! line while hiding only their delimiter glyphs; incomplete fences remain
//! literal and inert. A shared classifier keeps painting, hit-testing, and
//! editing in agreement.

use std::ops::Range;

Expand All @@ -11,12 +13,22 @@ pub(crate) enum PlaybookLineKind {
Markdown,
CodeFence,
Code,
FormattedCodeFence,
FormattedCode,
}

impl PlaybookLineKind {
pub(crate) fn is_markdown(self) -> bool {
matches!(self, Self::Markdown)
}

pub(crate) fn is_formatted_code(self) -> bool {
matches!(self, Self::FormattedCodeFence | Self::FormattedCode)
}

pub(crate) fn is_formatted_fence(self) -> bool {
matches!(self, Self::FormattedCodeFence)
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -161,6 +173,67 @@ fn closing_backtick_fence(raw: &str, opening_len: usize) -> Option<(usize, usize
(ticks >= opening_len && tail.trim().is_empty()).then_some((spaces, ticks))
}

/// Presentation kind for every source line. A multiline fence is promoted to
/// the formatted variants only after a valid closer is present; this lets an
/// incomplete fence remain literal while still suppressing Markdown features.
pub(crate) fn playbook_line_kinds(markdown: &str) -> Vec<PlaybookLineKind> {
let lines = markdown.split('\n').collect::<Vec<_>>();
let mut kinds = vec![PlaybookLineKind::Markdown; lines.len()];
let mut open: Option<(usize, usize)> = None;

for (index, raw) in lines.iter().enumerate() {
if let Some((open_index, open_len)) = open {
if closing_backtick_fence(raw, open_len).is_some() {
kinds[open_index] = PlaybookLineKind::FormattedCodeFence;
for kind in &mut kinds[open_index + 1..index] {
*kind = PlaybookLineKind::FormattedCode;
}
kinds[index] = PlaybookLineKind::FormattedCodeFence;
open = None;
} else {
kinds[index] = PlaybookLineKind::Code;
}
} else if let Some((_, ticks)) = opening_backtick_fence(raw) {
kinds[index] = PlaybookLineKind::CodeFence;
open = Some((index, ticks));
}
}

kinds
}

/// Byte range of the backtick run on a multiline fence delimiter line.
pub(crate) fn playbook_fence_delimiter(raw: &str) -> Option<Range<usize>> {
let (spaces, ticks, _) = backtick_fence(raw)?;
Some(spaces..spaces + ticks)
}

/// Closing multiline-fence delimiter immediately before the source cursor.
/// Returned offsets are Unicode character offsets in the full document.
pub(crate) fn playbook_closing_multiline_fence_before_cursor(
markdown: &str,
cursor: usize,
) -> Option<Range<usize>> {
let cursor = cursor.min(markdown.chars().count());
let mut line_start = 0usize;
let mut open_len = None;
for raw in markdown.split('\n') {
if let Some(required) = open_len {
if let Some((spaces, ticks)) = closing_backtick_fence(raw, required) {
let delimiter_end = line_start + spaces + ticks;
if cursor == delimiter_end {
return Some(line_start + spaces..delimiter_end);
}
open_len = None;
}
} else if let Some((_, ticks)) = opening_backtick_fence(raw) {
open_len = Some(ticks);
}
line_start += raw.chars().count() + 1;
}
None
}

/// Whether the source character at `offset` belongs to a fence delimiter or
/// fenced body. This includes incomplete fences so extensions remain inert
/// while their literal source is visible.
Expand Down Expand Up @@ -193,7 +266,7 @@ mod tests {
use super::*;

#[test]
fn classifies_multiline_fences_as_literal_inert_source() {
fn classifies_multiline_fences_as_inert_source() {
let mut classifier = PlaybookLineClassifier::default();
let kinds = ["before", "```rust", "# literal", "```", "after"]
.into_iter()
Expand All @@ -211,6 +284,40 @@ mod tests {
);
}

#[test]
fn completed_multiline_fence_gets_row_stable_formatted_kinds() {
assert_eq!(
playbook_line_kinds("before\n```rust\n界🙂\n```\nafter"),
[
PlaybookLineKind::Markdown,
PlaybookLineKind::FormattedCodeFence,
PlaybookLineKind::FormattedCode,
PlaybookLineKind::FormattedCodeFence,
PlaybookLineKind::Markdown,
]
);
assert_eq!(
playbook_line_kinds("```rust\nstill open"),
[PlaybookLineKind::CodeFence, PlaybookLineKind::Code]
);
}

#[test]
fn multiline_closing_boundary_is_source_offset_and_cjk_safe() {
let markdown = "前\n```rust\n界🙂\n ``` \nafter";
let cursor = "前\n```rust\n界🙂\n ```".chars().count();
let closing = playbook_closing_multiline_fence_before_cursor(markdown, cursor).unwrap();
assert_eq!(
markdown
.chars()
.skip(closing.start)
.take(closing.len())
.collect::<String>(),
"```"
);
assert!(playbook_closing_multiline_fence_before_cursor(markdown, cursor - 1).is_none());
}

#[test]
fn closing_fence_must_match_opening_run() {
let mut classifier = PlaybookLineClassifier::default();
Expand Down
Loading
Loading