panache 2.34.0

An LSP, formatter, and linter for Pandoc markdown, Quarto, and RMarkdown
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
//! Hashpipe-style YAML option formatting for Quarto executable chunks.
//!
//! Converts inline chunk options to Quarto's new hashpipe format with proper
//! line wrapping and language-specific comment prefixes.

use crate::config::WrapMode;
use crate::parser::utils::chunk_options::ChunkOptionValue;
use crate::parser::utils::chunk_options::hashpipe_comment_prefix;
use crate::parser::utils::hashpipe_normalizer::normalize_hashpipe_header;
use crate::syntax::{AstNode, ChunkInfoItem, CodeInfo, SyntaxNode};
use crate::yaml_engine;

/// A chunk option with a classified value (simple or expression).
type ClassifiedOption = (String, ChunkOptionValue);

/// An option extracted from CST: (key, value, is_quoted).
/// For labels, key is None.
type CstOption = (Option<String>, Option<String>, bool);

/// Value types that can appear in chunk options.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ValueType {
    Boolean,
    Numeric,
    String,           // Accepts quoted strings and simple barewords
    QuotedStringOnly, // Only accepts quoted strings, not barewords
}

/// Allowlist of chunk options safe for hashpipe conversion.
///
/// Each entry maps an option name to the value types it accepts in hashpipe format.
/// Options not in this list will stay inline to avoid unknown type restrictions.
///
/// Based on knitr documentation: https://yihui.org/knitr/options/
const HASHPIPE_SAFE_OPTIONS: &[(&str, &[ValueType])] = &[
    // Chunk identification
    ("label", &[ValueType::QuotedStringOnly]), // Chunk label/name - only quoted to be safe
    // Code evaluation
    ("eval", &[ValueType::Boolean]), // Can also be numeric vector, but keep those inline
    // Text output
    ("echo", &[ValueType::Boolean]), // Can also be numeric vector, but keep those inline
    ("results", &[ValueType::String]), // "markup", "asis", "hold", "hide"
    ("collapse", &[ValueType::Boolean]),
    ("warning", &[ValueType::Boolean]), // Can also be NA or numeric, but keep those inline
    ("message", &[ValueType::Boolean]),
    ("error", &[ValueType::Boolean]), // Can also be numeric 0/1/2, but keep those inline
    ("include", &[ValueType::Boolean]),
    ("strip-white", &[ValueType::Boolean]),
    // Code decoration
    ("comment", &[ValueType::String]),
    ("highlight", &[ValueType::Boolean]),
    ("prompt", &[ValueType::Boolean]),
    ("size", &[ValueType::String]),       // LaTeX font sizes
    ("background", &[ValueType::String]), // Color values
    // Cache
    ("cache", &[ValueType::Boolean]), // Can also be path, but keep those inline
    ("cache-path", &[ValueType::String]),
    ("cache-lazy", &[ValueType::Boolean]),
    ("cache-comments", &[ValueType::Boolean]),
    ("cache-rebuild", &[ValueType::Boolean]),
    ("autodep", &[ValueType::Boolean]),
    // Plots
    ("fig-path", &[ValueType::String]),
    ("fig-keep", &[ValueType::String]), // "high", "none", "all", "first", "last"
    ("fig-show", &[ValueType::String]), // "asis", "hold", "animate", "hide"
    ("dev", &[ValueType::String]),      // "png", "pdf", "svg", etc.
    // Figure dimensions and layout
    ("fig-width", &[ValueType::Numeric]),
    ("fig-height", &[ValueType::Numeric]),
    ("fig-asp", &[ValueType::Numeric]),  // Aspect ratio
    ("fig-dim", &[ValueType::Numeric]),  // Can be vector, but single numeric values work
    ("out-width", &[ValueType::String]), // "50%", "300px", etc.
    ("out-height", &[ValueType::String]),
    ("fig-align", &[ValueType::String]), // "left", "center", "right", "default"
    ("fig-env", &[ValueType::String]),
    ("fig-pos", &[ValueType::String]),
    ("fig-scap", &[ValueType::String]),
    // Figure captions and alt text
    ("fig-cap", &[ValueType::String]),
    ("fig-alt", &[ValueType::String]),
    ("fig-subcap", &[ValueType::String]),
    // Plot parameters
    ("dpi", &[ValueType::Numeric]),
    // Animation
    ("aniopts", &[ValueType::String]),
    ("ffmpeg-format", &[ValueType::String]),
    // Quarto-specific code display
    ("code-fold", &[ValueType::Boolean, ValueType::String]), // true/false or "show"/"hide"
    ("code-summary", &[ValueType::String]),
    ("code-overflow", &[ValueType::String]), // "wrap" or "scroll"
    ("code-line-numbers", &[ValueType::Boolean]),
    // Output classes/attributes
    ("classes", &[ValueType::String]),
];

/// Mapping of common chunk option names from R dot-notation to YAML dash-notation.
///
/// Explicit overrides for common options. If not in this table, the default rule
/// is to replace dots with dashes (e.g., `fig.width` → `fig-width`).
const OPTION_NAME_OVERRIDES: &[(&str, &str)] = &[
    ("fig.cap", "fig-cap"),
    ("fig.alt", "fig-alt"),
    ("fig.width", "fig-width"),
    ("fig.height", "fig-height"),
    ("fig.align", "fig-align"),
    ("fig.pos", "fig-pos"),
    ("fig.env", "fig-env"),
    ("fig.scap", "fig-scap"),
    ("fig.lp", "fig-lp"),
    ("fig.subcap", "fig-subcap"),
    ("fig.ncol", "fig-ncol"),
    ("fig.sep", "fig-sep"),
    ("fig.process", "fig-process"),
    ("fig.show", "fig-show"),
    ("fig.keep", "fig-keep"),
    ("out.width", "out-width"),
    ("out.height", "out-height"),
    ("out.extra", "out-extra"),
];

/// Get the comment prefix for hashpipe options based on the chunk language.
///
/// Returns None for unknown languages to avoid using incorrect comment syntax.
///
/// Different languages use different comment syntax:
/// - R, Python, Julia, Bash, Ruby, Perl: `#|`
/// - C, C++, Java, JavaScript, Rust, Go, etc.: `//|`
/// - SQL dialects: `--|`
pub fn get_comment_prefix(language: &str) -> Option<&'static str> {
    hashpipe_comment_prefix(language)
}

/// Normalize a chunk option name from R dot-notation to YAML dash-notation.
///
/// First checks the override table, then falls back to replacing dots with dashes.
pub fn normalize_option_name(name: &str) -> String {
    // Check override table first
    for (old, new) in OPTION_NAME_OVERRIDES {
        if name == *old {
            return (*new).to_string();
        }
    }

    // Default: replace dots with dashes
    name.replace('.', "-")
}

/// Normalize a chunk option value for YAML syntax.
///
/// Converts R boolean literals to lowercase YAML booleans.
/// For quoted strings, preserves the quotes.
pub fn normalize_value(value: &str) -> String {
    match value {
        "TRUE" | "T" => "true".to_string(),
        "FALSE" | "F" => "false".to_string(),
        _ => value.to_string(),
    }
}

/// Extract chunk options from inline info-string and optional parsed leading hashpipe lines.
///
/// Returns ((simple_options, complex_options), had_leading_hashpipe_options).
/// When both sources provide the same normalized key, inline info-string options win.
pub fn split_options_from_cst_with_content(
    info_node: &SyntaxNode,
    content: &str,
    prefix: &str,
) -> ((Vec<ClassifiedOption>, Vec<CstOption>), bool) {
    #[derive(Clone)]
    enum Entry {
        Simple(ClassifiedOption),
        Complex(CstOption),
    }

    fn upsert(entries: &mut Vec<(String, Entry)>, normalized_key: String, entry: Entry) {
        if let Some(pos) = entries.iter().position(|(k, _)| *k == normalized_key) {
            entries[pos] = (normalized_key, entry);
        } else {
            entries.push((normalized_key, entry));
        }
    }

    fn insert_if_absent(entries: &mut Vec<(String, Entry)>, normalized_key: String, entry: Entry) {
        if entries.iter().any(|(k, _)| *k == normalized_key) {
            return;
        }
        entries.push((normalized_key, entry));
    }

    fn push_inline_option(
        entries: &mut Vec<(String, Entry)>,
        key: String,
        value: String,
        is_quoted: bool,
    ) {
        let normalized_key = normalize_option_name(&key);
        if let Some(classified_value) =
            classify_option_for_hashpipe(&normalized_key, &value, is_quoted)
        {
            upsert(
                entries,
                normalized_key.clone(),
                Entry::Simple((normalized_key, classified_value)),
            );
        } else {
            upsert(
                entries,
                normalized_key,
                Entry::Complex((Some(key), Some(value), is_quoted)),
            );
        }
    }

    fn push_content_option(
        entries: &mut Vec<(String, Entry)>,
        key: String,
        value: String,
        is_quoted: bool,
    ) {
        let normalized_key = normalize_option_name(&key);
        let rendered = if is_quoted {
            format!("\"{}\"", value)
        } else {
            value
        };
        insert_if_absent(
            entries,
            normalized_key.clone(),
            Entry::Simple((normalized_key, ChunkOptionValue::Simple(rendered))),
        );
    }

    let mut entries: Vec<(String, Entry)> = Vec::new();
    let mut had_content_hashpipe = false;
    let mut pending_label_parts: Vec<String> = Vec::new();

    // 1) Inline options from CODE_INFO CHUNK_OPTIONS (highest precedence)
    let Some(info) = CodeInfo::cast(info_node.clone()) else {
        return ((Vec::new(), Vec::new()), false);
    };

    for item in info.chunk_items() {
        match item {
            ChunkInfoItem::Label(label) => {
                let label_value = label.text();
                if !label_value.is_empty() {
                    pending_label_parts.push(label_value);
                }
            }
            ChunkInfoItem::Option(opt) => {
                if !pending_label_parts.is_empty() {
                    upsert(
                        &mut entries,
                        "label".to_string(),
                        Entry::Simple((
                            "label".to_string(),
                            ChunkOptionValue::Simple(pending_label_parts.join(" ")),
                        )),
                    );
                    pending_label_parts.clear();
                }
                if let (Some(key), Some(value)) = (opt.key(), opt.value()) {
                    push_inline_option(&mut entries, key, value, opt.is_quoted());
                }
            }
        }
    }

    if !pending_label_parts.is_empty() {
        upsert(
            &mut entries,
            "label".to_string(),
            Entry::Simple((
                "label".to_string(),
                ChunkOptionValue::Simple(pending_label_parts.join(" ")),
            )),
        );
    }

    // 2) Existing leading hashpipe options from CODE_CONTENT text (lower precedence).
    // Parse multiline quoted values so rewrapping can normalize them.
    if let Some(normalized) = normalize_hashpipe_header(content, prefix)
        && let Some(options) = extract_options_from_normalized_yaml(&normalized.normalized_yaml)
    {
        had_content_hashpipe = true;
        for (key, value) in options {
            push_content_option(&mut entries, key, value, false);
        }
    }

    let mut simple = Vec::new();
    let mut complex = Vec::new();
    for (_, entry) in entries {
        match entry {
            Entry::Simple(s) => simple.push(s),
            Entry::Complex(c) => complex.push(c),
        }
    }

    ((simple, complex), had_content_hashpipe)
}

fn extract_options_from_normalized_yaml(normalized_yaml: &str) -> Option<Vec<(String, String)>> {
    let yaml_syntax = yaml_parser::parse(normalized_yaml).ok()?;
    let root = yaml_parser::ast::Root::cast(yaml_syntax)?;
    let map = root
        .documents()
        .next()
        .and_then(|doc| doc.block())
        .and_then(|block| block.block_map())?;

    let mut options = Vec::new();
    for entry in map.entries() {
        let key = hashpipe_map_entry_key(&entry)?;
        let value = hashpipe_map_entry_value_text(normalized_yaml, &entry);
        options.push((key, value));
    }
    Some(options)
}

fn hashpipe_map_entry_key(entry: &yaml_parser::ast::BlockMapEntry) -> Option<String> {
    let key = entry.key()?;
    if let Some(flow) = key.flow() {
        return hashpipe_flow_scalar_text(&flow);
    }
    let block = key.block()?;
    let flow = hashpipe_block_to_flow_scalar(&block)?;
    hashpipe_flow_scalar_text(&flow)
}

fn hashpipe_map_entry_value_text(
    normalized_yaml: &str,
    entry: &yaml_parser::ast::BlockMapEntry,
) -> String {
    let Some(value) = entry.value() else {
        return String::new();
    };

    if let Some(flow) = value.flow() {
        return hashpipe_flow_value_text(&flow).unwrap_or_else(|| {
            let range = flow.syntax().text_range();
            let start: usize = range.start().into();
            let end: usize = range.end().into();
            normalized_yaml[start..end].trim().to_string()
        });
    }

    if let Some(block) = value.block() {
        let range = block.syntax().text_range();
        let start: usize = range.start().into();
        let end: usize = range.end().into();
        return normalized_yaml[start..end].to_string();
    }

    let range = value.syntax().text_range();
    let start: usize = range.start().into();
    let end: usize = range.end().into();
    normalized_yaml[start..end].to_string()
}

fn hashpipe_block_to_flow_scalar(
    block: &yaml_parser::ast::Block,
) -> Option<yaml_parser::ast::Flow> {
    block
        .syntax()
        .children()
        .find_map(yaml_parser::ast::Flow::cast)
}

fn hashpipe_flow_scalar_text(flow: &yaml_parser::ast::Flow) -> Option<String> {
    let token = if let Some(token) = flow.plain_scalar() {
        token
    } else if let Some(token) = flow.single_quoted_scalar() {
        token
    } else if let Some(token) = flow.double_qouted_scalar() {
        token
    } else {
        return None;
    };
    let mut value = token.text().to_string();
    if token.kind() == yaml_parser::SyntaxKind::SINGLE_QUOTED_SCALAR {
        value = value.trim_matches('\'').to_string();
    } else if token.kind() == yaml_parser::SyntaxKind::DOUBLE_QUOTED_SCALAR {
        value = value.trim_matches('"').to_string();
    }
    Some(value)
}

fn hashpipe_flow_value_text(flow: &yaml_parser::ast::Flow) -> Option<String> {
    if let Some(token) = flow.plain_scalar() {
        return Some(token.text().to_string());
    }
    if let Some(token) = flow.single_quoted_scalar() {
        return Some(token.text().to_string());
    }
    if let Some(token) = flow.double_qouted_scalar() {
        return Some(token.text().to_string());
    }
    None
}

fn is_yaml_block_scalar_indicator(value: &str) -> bool {
    let s = value.trim();
    if s.is_empty() {
        return false;
    }
    let mut chars = s.chars();
    let Some(style) = chars.next() else {
        return false;
    };
    if style != '|' && style != '>' {
        return false;
    }
    chars.all(|ch| ch == '+' || ch == '-' || ch.is_ascii_digit())
}

/// Classify an option value for hashpipe conversion.
///
/// Returns Some(ClassifiedValue) if the option is safe for hashpipe, None otherwise.
fn classify_option_for_hashpipe(
    key: &str,
    value: &str,
    is_quoted: bool,
) -> Option<ChunkOptionValue> {
    use crate::parser::utils::chunk_options::{is_boolean_literal, is_numeric_literal};

    // Find allowed value types for this option
    let allowed_types = HASHPIPE_SAFE_OPTIONS
        .iter()
        .find(|(name, _)| *name == key)
        .map(|(_, types)| *types)?;

    // Check if value type matches allowed types
    if is_quoted {
        // Quoted string - safe if String or QuotedStringOnly is allowed
        if allowed_types.contains(&ValueType::String)
            || allowed_types.contains(&ValueType::QuotedStringOnly)
        {
            return Some(ChunkOptionValue::Simple(format!("\"{}\"", value)));
        }
    } else {
        // Unquoted - check boolean, numeric, or simple bareword
        if allowed_types.contains(&ValueType::Boolean)
            && (is_boolean_literal(value) || matches!(value, "true" | "false"))
        {
            return Some(ChunkOptionValue::Simple(value.to_ascii_lowercase()));
        }
        if is_numeric_literal(value) && allowed_types.contains(&ValueType::Numeric) {
            return Some(ChunkOptionValue::Simple(value.to_string()));
        }
        // Unquoted string (bareword) - only if String is allowed (not QuotedStringOnly)
        if allowed_types.contains(&ValueType::String) && is_simple_bareword(value) {
            return Some(ChunkOptionValue::Simple(value.to_string()));
        }
    }

    // Doesn't match allowed types or is complex expression - keep inline
    None
}

/// Check if a value is a simple bareword (identifier), not an expression.
///
/// Always returns false - we don't convert barewords to be safe.
/// Inline format requires quotes for string values (e.g., results="asis" not results=asis).
fn is_simple_bareword(_s: &str) -> bool {
    false
}

/// Format a single hashpipe option line with wrapping support.
///
/// If the option line exceeds `line_width`, wraps at word boundaries with
/// proper continuation indentation (2 spaces after the comment prefix).
pub fn format_hashpipe_option_with_wrap(
    prefix: &str,
    key: &str,
    value: &str,
    line_width: usize,
) -> Vec<String> {
    fn floor_char_boundary(s: &str, max: usize) -> usize {
        let mut idx = max.min(s.len());
        while idx > 0 && !s.is_char_boundary(idx) {
            idx -= 1;
        }
        idx
    }

    if let Some((first, rest)) = value.split_once('\n')
        && is_yaml_block_scalar_indicator(first)
    {
        let mut lines = vec![format!("{} {}: {}", prefix, key, first)];
        lines.extend(rest.split('\n').map(|line| format!("{}{}", prefix, line)));
        return lines;
    }
    if let Some((first, rest)) = value.split_once('\n') {
        let mut lines = vec![if first.is_empty() {
            format!("{} {}:", prefix, key)
        } else {
            format!("{} {}: {}", prefix, key, first)
        }];
        lines.extend(rest.split('\n').map(|line| format!("{} {}", prefix, line)));
        return lines;
    }

    let first_line = format!("{} {}: {}", prefix, key, value);

    // Check if wrapping is needed
    if first_line.len() <= line_width {
        return vec![first_line];
    }

    // Calculate available space
    let first_prefix = format!("{} {}: ", prefix, key);
    let available_first = line_width.saturating_sub(first_prefix.len());

    // If even the prefix is too long, return as-is (don't break mid-word)
    if available_first < 10 {
        return vec![first_line];
    }

    let continuation_prefix = format!("{}   ", prefix); // 3 spaces after prefix
    let available_continuation = line_width.saturating_sub(continuation_prefix.len());

    let mut lines = Vec::new();
    let mut remaining = value;
    let mut is_first = true;

    while !remaining.is_empty() {
        let available = if is_first {
            available_first
        } else {
            available_continuation
        };

        // Find word boundary at or before available length
        let break_point = if remaining.len() <= available {
            remaining.len()
        } else {
            // Find last space before or at available
            let upper = floor_char_boundary(remaining, available);
            if upper == 0 {
                remaining
                    .char_indices()
                    .nth(1)
                    .map(|(i, _)| i)
                    .unwrap_or(remaining.len())
            } else {
                remaining[..upper]
                    .rfind(' ')
                    .map(|i| i + 1) // Include the space
                    .unwrap_or(upper) // No space found, break at safe boundary
            }
        };

        let chunk = &remaining[..break_point].trim_end();
        if is_first {
            lines.push(format!("{}{}", first_prefix, chunk));
            is_first = false;
        } else {
            lines.push(format!("{}{}", continuation_prefix, chunk));
        }

        remaining = remaining[break_point..].trim_start();
    }

    lines
}

/// Format chunk options as hashpipe lines.
///
/// Only formats simple values; complex expressions are filtered out.
/// Returns None if the language's comment syntax is unknown.
/// Returns Some(Vec<String>) with formatted hashpipe lines for known languages.
pub fn format_as_hashpipe(
    language: &str,
    options: &[ClassifiedOption],
    line_width: usize,
    wrap: Option<&WrapMode>,
) -> Option<Vec<String>> {
    let prefix = get_comment_prefix(language)?; // Return None if unknown language
    let mut output = Vec::new();
    let mut yaml_entries: Vec<(String, String)> = Vec::new();

    for (key, value) in options {
        // Only format simple values
        if let ChunkOptionValue::Simple(v) = value {
            let norm_key = normalize_option_name(key);
            let norm_val = normalize_value(v);

            // Handle bare options (no value)
            let value_str = if norm_val.is_empty() {
                "true".to_string() // Bare option means true
            } else {
                norm_val
            };

            yaml_entries.push((norm_key.clone(), value_str.clone()));
            let lines = format_hashpipe_option_with_wrap(prefix, &norm_key, &value_str, line_width);
            output.extend(lines);
        }
    }

    if !yaml_entries.is_empty() {
        let yaml_text = yaml_entries
            .iter()
            .map(|(key, value)| {
                if value.starts_with('\n') {
                    if value.ends_with('\n') {
                        format!("{key}:{value}")
                    } else {
                        format!("{key}:{value}\n")
                    }
                } else {
                    format!("{key}: {value}\n")
                }
            })
            .collect::<String>();
        // pretty_yaml wraps to the width of raw YAML text. Hashpipe output adds
        // a comment prefix (`#| `, `//| `, `--| `) before every rendered line,
        // so subtract that width to keep final emitted lines within line_width.
        let yaml_print_width = line_width.saturating_sub(prefix.len() + 1);
        let yaml_config = crate::config::Config {
            line_width: yaml_print_width,
            wrap: wrap.cloned(),
            ..Default::default()
        };
        if let Ok(formatted_yaml) = yaml_engine::format_yaml_with_config(&yaml_text, &yaml_config) {
            let lines = formatted_yaml
                .lines()
                .map(|line| {
                    if line.is_empty() {
                        prefix.to_string()
                    } else {
                        format!("{} {}", prefix, line)
                    }
                })
                .collect::<Vec<_>>();
            return Some(lines);
        }
    }

    Some(output)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parser::utils::chunk_options::ChunkOptionValue;

    #[test]
    fn test_get_comment_prefix_r() {
        assert_eq!(get_comment_prefix("r"), Some("#|"));
        assert_eq!(get_comment_prefix("R"), Some("#|"));
    }

    #[test]
    fn test_get_comment_prefix_python() {
        assert_eq!(get_comment_prefix("python"), Some("#|"));
        assert_eq!(get_comment_prefix("Python"), Some("#|"));
    }

    #[test]
    fn test_get_comment_prefix_cpp() {
        assert_eq!(get_comment_prefix("cpp"), Some("//|"));
        assert_eq!(get_comment_prefix("c++"), Some("//|"));
        assert_eq!(get_comment_prefix("C++"), Some("//|"));
    }

    #[test]
    fn test_get_comment_prefix_sql() {
        assert_eq!(get_comment_prefix("sql"), Some("--|"));
        assert_eq!(get_comment_prefix("SQL"), Some("--|"));
    }

    #[test]
    fn test_get_comment_prefix_unknown() {
        assert_eq!(get_comment_prefix("unknown"), None);
        assert_eq!(get_comment_prefix("fortran"), None);
        assert_eq!(get_comment_prefix("matlab"), None);
    }

    #[test]
    fn test_normalize_option_name_override() {
        assert_eq!(normalize_option_name("fig.cap"), "fig-cap");
        assert_eq!(normalize_option_name("fig.width"), "fig-width");
    }

    #[test]
    fn test_normalize_option_name_default() {
        assert_eq!(normalize_option_name("my.option"), "my-option");
        assert_eq!(normalize_option_name("some.long.name"), "some-long-name");
    }

    #[test]
    fn test_normalize_option_name_no_dots() {
        assert_eq!(normalize_option_name("echo"), "echo");
        assert_eq!(normalize_option_name("warning"), "warning");
    }

    #[test]
    fn test_normalize_value_booleans() {
        assert_eq!(normalize_value("TRUE"), "true");
        assert_eq!(normalize_value("FALSE"), "false");
        assert_eq!(normalize_value("T"), "true");
        assert_eq!(normalize_value("F"), "false");
    }

    #[test]
    fn test_normalize_value_other() {
        assert_eq!(normalize_value("7"), "7");
        assert_eq!(normalize_value("\"hello\""), "\"hello\"");
        assert_eq!(normalize_value("3.14"), "3.14");
    }

    #[test]
    fn test_format_hashpipe_option_short() {
        let lines = format_hashpipe_option_with_wrap("#|", "echo", "true", 80);
        assert_eq!(lines.len(), 1);
        assert_eq!(lines[0], "#| echo: true");
    }

    #[test]
    fn test_format_hashpipe_option_wrap() {
        let long_caption =
            "This is a very long caption that definitely exceeds the line width and needs to wrap";
        let lines = format_hashpipe_option_with_wrap("#|", "fig-cap", long_caption, 80);

        assert!(lines.len() > 1, "Should wrap into multiple lines");
        assert!(lines[0].starts_with("#| fig-cap:"));
        assert!(lines[1].starts_with("#|   ")); // 3-space indent
        assert!(lines[0].len() <= 80);
        // Continuation lines might be slightly over due to word boundaries
    }

    #[test]
    fn test_format_hashpipe_option_wrap_handles_utf8_boundaries() {
        let value = "comparison data for three methods:- Student’s t, Bayes factor, and Welch’s t.";
        let lines = format_hashpipe_option_with_wrap("#|", "fig-cap", value, 60);

        assert!(lines.len() > 1, "Should wrap into multiple lines");
        assert!(lines[0].starts_with("#| fig-cap:"));
        assert!(lines[1].starts_with("#|   "));
    }

    #[test]
    fn test_format_hashpipe_option_block_scalar() {
        let value = "|\n   A caption\n   spanning lines";
        let lines = format_hashpipe_option_with_wrap("#|", "fig-cap", value, 80);
        assert_eq!(
            lines,
            vec!["#| fig-cap: |", "#|   A caption", "#|   spanning lines"]
        );
    }

    #[test]
    fn test_format_hashpipe_option_indented_yaml_multiline() {
        let value = "\n  - a\n  - b";
        let lines = format_hashpipe_option_with_wrap("#|", "list", value, 80);
        assert_eq!(lines, vec!["#| list:", "#|   - a", "#|   - b"]);
    }

    #[test]
    fn test_format_as_hashpipe_simple() {
        let options = vec![
            (
                "echo".to_string(),
                ChunkOptionValue::Simple("TRUE".to_string()),
            ),
            (
                "fig.width".to_string(),
                ChunkOptionValue::Simple("7".to_string()),
            ),
        ];

        let lines = format_as_hashpipe("r", &options, 80, None).unwrap();
        assert_eq!(lines.len(), 2);
        assert_eq!(lines[0], "#| echo: true");
        assert_eq!(lines[1], "#| fig-width: 7");
    }

    #[test]
    fn test_format_as_hashpipe_skips_expressions() {
        let options = vec![
            (
                "echo".to_string(),
                ChunkOptionValue::Simple("TRUE".to_string()),
            ),
            (
                "label".to_string(),
                ChunkOptionValue::Expression("my_var".to_string()),
            ),
        ];

        let lines = format_as_hashpipe("r", &options, 80, None).unwrap();
        assert_eq!(lines.len(), 1); // Only echo, label skipped
        assert_eq!(lines[0], "#| echo: true");
    }

    #[test]
    fn test_format_as_hashpipe_unknown_language() {
        let options = vec![(
            "echo".to_string(),
            ChunkOptionValue::Simple("TRUE".to_string()),
        )];

        // Unknown language should return None
        let result = format_as_hashpipe("fortran", &options, 80, None);
        assert!(result.is_none());
    }
}