Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

fix(picker): un-nest the code-block gutter in comment previews (#3306)

Maximilian Roos28b1bfba6e55
diff --git a/src/commands/picker/pr_pane.rs b/src/commands/picker/pr_pane.rs
index e5e08cf20..7f139acd3 100644
--- a/src/commands/picker/pr_pane.rs
+++ b/src/commands/picker/pr_pane.rs
@@ -114,9 +114,15 @@ pub(super) fn description(body: &str, width: usize) -> String {
/// off from its author header. The markdown wraps to the gutter's inner width
/// (the bar plus its pad take two columns) so the gutter's own wrap is a no-op
/// rather than re-breaking the already-styled lines.
+///
+/// The body renders *flush* ([`render_markdown_flush`](crate::md_help::render_markdown_flush)),
+/// not gutter-quoted: a comment's own fenced code block would otherwise get its
+/// own house gutter from the markdown renderer, and quoting that inside this
+/// gutter nests two bars — the outer re-wrap then strands continuation lines
+/// without a bar and drops their dim. Flush keeps a single gutter, and flush
+/// code blocks pre-wrap to the inner width so this gutter's wrap stays a no-op.
pub(super) fn markdown_in_gutter(body: &str, width: usize) -> String {
- let rendered =
- crate::md_help::render_markdown_in_help_with_width(body, Some(width.saturating_sub(2)));
+ let rendered = crate::md_help::render_markdown_flush(body, Some(width.saturating_sub(2)));
format_with_gutter(&rendered, Some(width))
}
@@ -124,6 +130,40 @@ pub(super) fn markdown_in_gutter(body: &str, width: usize) -> String {
mod tests {
use super::*;
+ #[test]
+ fn markdown_in_gutter_code_block_stays_single_guttered_and_dim() {
+ use worktrunk::styling::GUTTER;
+ // A comment whose body wraps a long line in a fenced code block. The
+ // renderer must not nest a second house bar (the code block's own gutter
+ // inside this one), which used to strand continuation lines bar-less and
+ // un-dimmed and break the alignment.
+ let body = "Some intro prose.\n\n```\nfirst second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth\n```";
+ let out = markdown_in_gutter(body, 40);
+ let bar = format!("{GUTTER}"); // the house bar's opening SGR
+
+ for line in out.lines() {
+ // Exactly one gutter bar per rendered line: the screenshot bug put two
+ // on some lines (nested) and zero on the stranded continuations.
+ assert_eq!(
+ line.matches(&bar).count(),
+ 1,
+ "every line carries exactly one gutter bar: {line:?}"
+ );
+ }
+
+ // Every wrapped piece of the code block stays dim — the bug dropped the
+ // dim on every line after the first.
+ let dim = anstyle::Style::new().dimmed();
+ let dim_open = format!("{dim}");
+ for word in ["first", "seventh", "twelfth"] {
+ let line = out
+ .lines()
+ .find(|l| l.contains(word))
+ .unwrap_or_else(|| panic!("code word {word:?} present in {out:?}"));
+ assert!(line.contains(&dim_open), "code line stays dim: {line:?}");
+ }
+ }
+
#[test]
fn header_with_and_without_title() {
let with = header(PrRef::pr(42), Some("Fix the flaky test"));
diff --git a/src/md_help.rs b/src/md_help.rs
index 26eedf856..7986db8ef 100644
--- a/src/md_help.rs
+++ b/src/md_help.rs
@@ -111,10 +111,19 @@ fn render_markdown(help: &str, width: Option<usize>, code_blocks: CodeBlocks) ->
// flush-left; the gutter mode quotes it in the house bar.
let content = code_block_lines.join("\n");
let formatted = if code_blocks == CodeBlocks::Flush {
+ // Flush code feeds the gutter-free `pr`/comments panes. Wrap each
+ // line to width (word-wrap, preserving indent) and dim every
+ // resulting piece, so a long line doesn't overflow the pane — and,
+ // when the comments pane re-quotes this in the house gutter, every
+ // wrapped piece already fits, keeping the gutter's own wrap a no-op
+ // and the dim consistent rather than landing only on the first line.
let dim = Style::new().dimmed();
code_block_lines
.iter()
- .map(|l| format!("{dim}{l}{dim:#}"))
+ .flat_map(|l| {
+ width.map_or_else(|| vec![l.to_string()], |w| wrap_styled_text(l, w))
+ })
+ .map(|piece| format!("{dim}{piece}{dim:#}"))
.collect::<Vec<_>>()
.join("\n")
} else {