rumdl 0.1.51

A fast Markdown linter written in Rust (Ru(st) MarkDown Linter)
Documentation
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
use crate::LintContext;
/// Rule MD004: Use consistent style for unordered list markers
///
/// See [docs/md004.md](../../docs/md004.md) for full documentation, configuration, and examples.
///
/// Enforces that all unordered list items in a Markdown document use the same marker style ("*", "+", or "-") or are consistent with the first marker used, depending on configuration.
///
/// ## Purpose
///
/// Ensures visual and stylistic consistency for unordered lists, making documents easier to read and maintain.
///
/// ## Configuration Options
///
/// The rule supports configuring the required marker style:
/// ```yaml
/// MD004:
///   style: dash      # Options: "dash", "asterisk", "plus", or "consistent" (default)
/// ```
///
/// ## Examples
///
/// ### Correct (with style: dash)
/// ```markdown
/// - Item 1
/// - Item 2
///   - Nested item
/// - Item 3
/// ```
///
/// ### Incorrect (with style: dash)
/// ```markdown
/// * Item 1
/// - Item 2
/// + Item 3
/// ```
///
/// ## Behavior
///
/// - Checks each unordered list item for its marker character.
/// - In "consistent" mode, the most prevalent marker sets the style for the document (in case of tie, prefers dash).
/// - Skips code blocks and front matter.
/// - Reports a warning if a list item uses a different marker than the configured or detected style.
///
/// ## Fix Behavior
///
/// - Rewrites all unordered list markers to match the configured or detected style.
/// - Preserves indentation and content after the marker.
///
/// ## Rationale
///
/// Consistent list markers improve readability and reduce distraction, especially in large documents or when collaborating with others. This rule helps enforce a uniform style across all unordered lists.
use crate::rule::{Fix, LintError, LintResult, LintWarning, Rule, RuleCategory, Severity};
use toml;

mod md004_config;
use md004_config::MD004Config;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum UnorderedListStyle {
    Asterisk, // "*"
    Plus,     // "+"
    Dash,     // "-"
    #[default]
    Consistent, // Use the first marker in a file consistently
    Sublist,  // Each nesting level uses a different marker (*, +, -, cycling)
}

/// Rule MD004: Unordered list style
#[derive(Clone, Default)]
pub struct MD004UnorderedListStyle {
    config: MD004Config,
}

impl MD004UnorderedListStyle {
    pub fn new(style: UnorderedListStyle) -> Self {
        Self {
            config: MD004Config { style },
        }
    }

    pub fn from_config_struct(config: MD004Config) -> Self {
        Self { config }
    }

    /// Count marker prevalence across all unordered list items in the document
    /// Returns the most prevalent marker character, preferring dash in case of ties
    fn count_marker_prevalence(&self, ctx: &crate::lint_context::LintContext) -> Option<char> {
        let mut asterisk_count = 0;
        let mut dash_count = 0;
        let mut plus_count = 0;

        for list_block in &ctx.list_blocks {
            for &item_line in &list_block.item_lines {
                if let Some(line_info) = ctx.line_info(item_line)
                    && let Some(list_item) = &line_info.list_item
                    && !list_item.is_ordered
                {
                    match list_item.marker.chars().next()? {
                        '*' => asterisk_count += 1,
                        '-' => dash_count += 1,
                        '+' => plus_count += 1,
                        _ => {}
                    }
                }
            }
        }

        // Use the most prevalent marker as the target style
        // In case of a tie, prefer dash (most common, GitHub default)
        if dash_count >= asterisk_count && dash_count >= plus_count {
            Some('-')
        } else if asterisk_count >= plus_count {
            Some('*')
        } else {
            Some('+')
        }
    }
}

impl Rule for MD004UnorderedListStyle {
    fn name(&self) -> &'static str {
        "MD004"
    }

    fn description(&self) -> &'static str {
        "Use consistent style for unordered list markers"
    }

    fn check(&self, ctx: &LintContext) -> LintResult {
        // Early returns for performance
        if ctx.content.is_empty() {
            return Ok(Vec::new());
        }

        // Quick check for any list markers before processing
        if !ctx.likely_has_lists() {
            return Ok(Vec::new());
        }

        let mut warnings = Vec::new();

        // For consistent mode, count occurrences of each marker (prevalence-based approach)
        let target_marker_for_consistent = if self.config.style == UnorderedListStyle::Consistent {
            self.count_marker_prevalence(ctx)
        } else {
            None
        };

        // Use centralized list blocks for better performance and accuracy
        for list_block in &ctx.list_blocks {
            // Check each list item in this block
            // We need to check individual items even in mixed lists (ordered with nested unordered)
            for &item_line in &list_block.item_lines {
                if let Some(line_info) = ctx.line_info(item_line)
                    && let Some(list_item) = &line_info.list_item
                {
                    // Skip lines inside PyMdown blocks
                    if line_info.in_pymdown_block {
                        continue;
                    }

                    // Skip ordered list items - we only care about unordered ones
                    if list_item.is_ordered {
                        continue;
                    }

                    // Get the marker character
                    let marker = list_item.marker.chars().next().unwrap();

                    // Calculate offset for the marker position
                    let offset = line_info.byte_offset + list_item.marker_column;

                    match self.config.style {
                        UnorderedListStyle::Consistent => {
                            // For consistent mode, check against the most prevalent marker
                            if let Some(target) = target_marker_for_consistent
                                && marker != target
                            {
                                let (line, col) = ctx.offset_to_line_col(offset);
                                warnings.push(LintWarning {
                                    line,
                                    column: col,
                                    end_line: line,
                                    end_column: col + 1,
                                    message: format!("List marker '{marker}' does not match expected style '{target}'"),
                                    severity: Severity::Warning,
                                    rule_name: Some(self.name().to_string()),
                                    fix: Some(Fix {
                                        range: offset..offset + 1,
                                        replacement: target.to_string(),
                                    }),
                                });
                            }
                        }
                        UnorderedListStyle::Sublist => {
                            // Calculate expected marker based on indentation level
                            // Each 2 spaces of indentation represents a nesting level
                            let nesting_level = list_item.marker_column / 2;
                            let expected_marker = match nesting_level % 3 {
                                0 => '*',
                                1 => '+',
                                2 => '-',
                                _ => {
                                    // This should never happen as % 3 only returns 0, 1, or 2
                                    // but fallback to asterisk for safety
                                    '*'
                                }
                            };
                            if marker != expected_marker {
                                let (line, col) = ctx.offset_to_line_col(offset);
                                warnings.push(LintWarning {
                                        line,
                                        column: col,
                                        end_line: line,
                                        end_column: col + 1,
                                        message: format!(
                                            "List marker '{marker}' does not match expected style '{expected_marker}' for nesting level {nesting_level}"
                                        ),
                                        severity: Severity::Warning,
                                        rule_name: Some(self.name().to_string()),
                                        fix: Some(Fix {
                                            range: offset..offset + 1,
                                            replacement: expected_marker.to_string(),
                                        }),
                                    });
                            }
                        }
                        _ => {
                            // Handle specific style requirements (asterisk, dash, plus)
                            let target_marker = match self.config.style {
                                UnorderedListStyle::Asterisk => '*',
                                UnorderedListStyle::Dash => '-',
                                UnorderedListStyle::Plus => '+',
                                UnorderedListStyle::Consistent | UnorderedListStyle::Sublist => {
                                    // These cases are handled separately above
                                    // but fallback to asterisk for safety
                                    '*'
                                }
                            };
                            if marker != target_marker {
                                let (line, col) = ctx.offset_to_line_col(offset);
                                warnings.push(LintWarning {
                                    line,
                                    column: col,
                                    end_line: line,
                                    end_column: col + 1,
                                    message: format!(
                                        "List marker '{marker}' does not match expected style '{target_marker}'"
                                    ),
                                    severity: Severity::Warning,
                                    rule_name: Some(self.name().to_string()),
                                    fix: Some(Fix {
                                        range: offset..offset + 1,
                                        replacement: target_marker.to_string(),
                                    }),
                                });
                            }
                        }
                    }
                }
            }
        }

        Ok(warnings)
    }

    fn fix(&self, ctx: &LintContext) -> Result<String, LintError> {
        let mut lines: Vec<String> = ctx.content.lines().map(String::from).collect();

        // For consistent mode, count occurrences of each marker (prevalence-based approach)
        let target_marker_for_consistent = if self.config.style == UnorderedListStyle::Consistent {
            self.count_marker_prevalence(ctx)
        } else {
            None
        };

        // Use centralized list blocks
        for list_block in &ctx.list_blocks {
            // Process each list item in this block
            // We need to check individual items even in mixed lists
            for &item_line in &list_block.item_lines {
                if let Some(line_info) = ctx.line_info(item_line)
                    && let Some(list_item) = &line_info.list_item
                {
                    // Skip ordered list items - we only care about unordered ones
                    if list_item.is_ordered {
                        continue;
                    }

                    // If rule is disabled for this line, skip modification
                    if ctx.inline_config().is_rule_disabled(self.name(), item_line) {
                        continue;
                    }

                    let line_idx = item_line - 1;
                    if line_idx >= lines.len() {
                        continue;
                    }

                    let line = &lines[line_idx];
                    let marker = list_item.marker.chars().next().unwrap();

                    // Determine the target marker
                    let target_marker = match self.config.style {
                        UnorderedListStyle::Consistent => target_marker_for_consistent.unwrap_or(marker),
                        UnorderedListStyle::Sublist => {
                            // Calculate expected marker based on indentation level
                            // Each 2 spaces of indentation represents a nesting level
                            let nesting_level = list_item.marker_column / 2;
                            match nesting_level % 3 {
                                0 => '*',
                                1 => '+',
                                2 => '-',
                                _ => {
                                    // This should never happen as % 3 only returns 0, 1, or 2
                                    // but fallback to asterisk for safety
                                    '*'
                                }
                            }
                        }
                        UnorderedListStyle::Asterisk => '*',
                        UnorderedListStyle::Dash => '-',
                        UnorderedListStyle::Plus => '+',
                    };

                    // Replace the marker if needed
                    if marker != target_marker {
                        let marker_pos = list_item.marker_column;
                        if marker_pos < line.len() {
                            let mut new_line = String::new();
                            new_line.push_str(&line[..marker_pos]);
                            new_line.push(target_marker);
                            new_line.push_str(&line[marker_pos + 1..]);
                            lines[line_idx] = new_line;
                        }
                    }
                }
            }
        }

        let mut result = lines.join("\n");
        if ctx.content.ends_with('\n') {
            result.push('\n');
        }
        Ok(result)
    }

    /// Get the category of this rule for selective processing
    fn category(&self) -> RuleCategory {
        RuleCategory::List
    }

    /// Check if this rule should be skipped
    fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
        ctx.content.is_empty() || !ctx.likely_has_lists()
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn default_config_section(&self) -> Option<(String, toml::Value)> {
        let mut map = toml::map::Map::new();
        map.insert(
            "style".to_string(),
            toml::Value::String(match self.config.style {
                UnorderedListStyle::Asterisk => "asterisk".to_string(),
                UnorderedListStyle::Dash => "dash".to_string(),
                UnorderedListStyle::Plus => "plus".to_string(),
                UnorderedListStyle::Consistent => "consistent".to_string(),
                UnorderedListStyle::Sublist => "sublist".to_string(),
            }),
        );
        Some((self.name().to_string(), toml::Value::Table(map)))
    }

    fn from_config(config: &crate::config::Config) -> Box<dyn Rule>
    where
        Self: Sized,
    {
        let style = crate::config::get_rule_config_value::<String>(config, "MD004", "style")
            .unwrap_or_else(|| "consistent".to_string());
        let style = match style.as_str() {
            "asterisk" => UnorderedListStyle::Asterisk,
            "dash" => UnorderedListStyle::Dash,
            "plus" => UnorderedListStyle::Plus,
            "sublist" => UnorderedListStyle::Sublist,
            _ => UnorderedListStyle::Consistent,
        };
        Box::new(MD004UnorderedListStyle::new(style))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::lint_context::LintContext;
    use crate::rule::Rule;

    #[test]
    fn test_consistent_asterisk_style() {
        let rule = MD004UnorderedListStyle::new(UnorderedListStyle::Consistent);
        let content = "* Item 1\n* Item 2\n  * Nested\n* Item 3";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn test_consistent_dash_style() {
        let rule = MD004UnorderedListStyle::new(UnorderedListStyle::Consistent);
        let content = "- Item 1\n- Item 2\n  - Nested\n- Item 3";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn test_consistent_plus_style() {
        let rule = MD004UnorderedListStyle::new(UnorderedListStyle::Consistent);
        let content = "+ Item 1\n+ Item 2\n  + Nested\n+ Item 3";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn test_inconsistent_style_tie_prefers_dash() {
        let rule = MD004UnorderedListStyle::new(UnorderedListStyle::Consistent);
        // All markers appear once - tie should prefer dash
        let content = "* Item 1\n- Item 2\n+ Item 3";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 2);
        // Both asterisk and plus are flagged as wrong (dash is preferred on tie)
        assert_eq!(result[0].line, 1);
        assert_eq!(result[1].line, 3);
    }

    #[test]
    fn test_asterisk_style_enforced() {
        let rule = MD004UnorderedListStyle::new(UnorderedListStyle::Asterisk);
        let content = "* Item 1\n- Item 2\n+ Item 3";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 2);
        assert_eq!(result[0].message, "List marker '-' does not match expected style '*'");
        assert_eq!(result[1].message, "List marker '+' does not match expected style '*'");
    }

    #[test]
    fn test_dash_style_enforced() {
        let rule = MD004UnorderedListStyle::new(UnorderedListStyle::Dash);
        let content = "* Item 1\n- Item 2\n+ Item 3";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 2);
        assert_eq!(result[0].message, "List marker '*' does not match expected style '-'");
        assert_eq!(result[1].message, "List marker '+' does not match expected style '-'");
    }

    #[test]
    fn test_plus_style_enforced() {
        let rule = MD004UnorderedListStyle::new(UnorderedListStyle::Plus);
        let content = "* Item 1\n- Item 2\n+ Item 3";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 2);
        assert_eq!(result[0].message, "List marker '*' does not match expected style '+'");
        assert_eq!(result[1].message, "List marker '-' does not match expected style '+'");
    }

    #[test]
    fn test_fix_consistent_style_tie_prefers_dash() {
        let rule = MD004UnorderedListStyle::new(UnorderedListStyle::Consistent);
        // All markers appear once - tie should prefer dash
        let content = "* Item 1\n- Item 2\n+ Item 3";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, "- Item 1\n- Item 2\n- Item 3");
    }

    #[test]
    fn test_fix_asterisk_style() {
        let rule = MD004UnorderedListStyle::new(UnorderedListStyle::Asterisk);
        let content = "- Item 1\n+ Item 2\n- Item 3";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, "* Item 1\n* Item 2\n* Item 3");
    }

    #[test]
    fn test_fix_dash_style() {
        let rule = MD004UnorderedListStyle::new(UnorderedListStyle::Dash);
        let content = "* Item 1\n+ Item 2\n* Item 3";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, "- Item 1\n- Item 2\n- Item 3");
    }

    #[test]
    fn test_fix_plus_style() {
        let rule = MD004UnorderedListStyle::new(UnorderedListStyle::Plus);
        let content = "* Item 1\n- Item 2\n* Item 3";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, "+ Item 1\n+ Item 2\n+ Item 3");
    }

    #[test]
    fn test_nested_lists() {
        let rule = MD004UnorderedListStyle::new(UnorderedListStyle::Consistent);
        let content = "* Item 1\n  * Nested 1\n    * Double nested\n  - Wrong marker\n* Item 2";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].line, 4);
    }

    #[test]
    fn test_fix_nested_lists() {
        let rule = MD004UnorderedListStyle::new(UnorderedListStyle::Consistent);
        // * appears 2 times, - appears 2 times, + appears 1 time
        // Tie between * and - should prefer dash
        let content = "* Item 1\n  - Nested 1\n    + Double nested\n  - Nested 2\n* Item 2";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(
            fixed,
            "- Item 1\n  - Nested 1\n    - Double nested\n  - Nested 2\n- Item 2"
        );
    }

    #[test]
    fn test_with_code_blocks() {
        let rule = MD004UnorderedListStyle::new(UnorderedListStyle::Asterisk);
        let content = "* Item 1\n\n```\n- This is in code\n+ Not a list\n```\n\n- Item 2";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].line, 8);
    }

    #[test]
    fn test_with_blockquotes() {
        let rule = MD004UnorderedListStyle::new(UnorderedListStyle::Consistent);
        let content = "> * Item 1\n> - Item 2\n\n* Regular item\n+ Different marker";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        // Should detect inconsistencies both in blockquote and regular content
        assert!(result.len() >= 2);
    }

    #[test]
    fn test_empty_document() {
        let rule = MD004UnorderedListStyle::new(UnorderedListStyle::Asterisk);
        let content = "";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn test_no_lists() {
        let rule = MD004UnorderedListStyle::new(UnorderedListStyle::Asterisk);
        let content = "This is a paragraph.\n\nAnother paragraph.";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn test_ordered_lists_ignored() {
        let rule = MD004UnorderedListStyle::new(UnorderedListStyle::Asterisk);
        let content = "1. Item 1\n2. Item 2\n   1. Nested\n3. Item 3";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn test_mixed_ordered_unordered() {
        let rule = MD004UnorderedListStyle::new(UnorderedListStyle::Asterisk);
        let content = "1. Ordered\n   * Unordered nested\n   - Wrong marker\n2. Another ordered";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].line, 3);
    }

    #[test]
    fn test_fix_preserves_content() {
        let rule = MD004UnorderedListStyle::new(UnorderedListStyle::Dash);
        let content = "* Item with **bold** and *italic*\n+ Item with `code`\n* Item with [link](url)";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(
            fixed,
            "- Item with **bold** and *italic*\n- Item with `code`\n- Item with [link](url)"
        );
    }

    #[test]
    fn test_fix_preserves_indentation() {
        let rule = MD004UnorderedListStyle::new(UnorderedListStyle::Asterisk);
        let content = "  - Indented item\n    + Nested item\n  - Another indented";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, "  * Indented item\n    * Nested item\n  * Another indented");
    }

    #[test]
    fn test_multiple_spaces_after_marker() {
        let rule = MD004UnorderedListStyle::new(UnorderedListStyle::Consistent);
        // All markers appear once - tie should prefer dash
        let content = "*   Item 1\n-   Item 2\n+   Item 3";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 2);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, "-   Item 1\n-   Item 2\n-   Item 3");
    }

    #[test]
    fn test_tab_after_marker() {
        let rule = MD004UnorderedListStyle::new(UnorderedListStyle::Consistent);
        // Both markers appear once - tie should prefer dash
        let content = "*\tItem 1\n-\tItem 2";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 1);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, "-\tItem 1\n-\tItem 2");
    }

    #[test]
    fn test_edge_case_marker_at_end() {
        let rule = MD004UnorderedListStyle::new(UnorderedListStyle::Asterisk);
        // These are valid list items with minimal content (just a space)
        let content = "* \n- \n+ ";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 2); // Should flag - and + as wrong markers
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, "* \n* \n* ");
    }

    #[test]
    fn test_from_config() {
        let mut config = crate::config::Config::default();
        let mut rule_config = crate::config::RuleConfig::default();
        rule_config
            .values
            .insert("style".to_string(), toml::Value::String("plus".to_string()));
        config.rules.insert("MD004".to_string(), rule_config);

        let rule = MD004UnorderedListStyle::from_config(&config);
        let content = "* Item 1\n- Item 2";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 2);
    }

    #[test]
    fn test_default_config_section() {
        let rule = MD004UnorderedListStyle::new(UnorderedListStyle::Dash);
        let config = rule.default_config_section();
        assert!(config.is_some());
        let (name, value) = config.unwrap();
        assert_eq!(name, "MD004");
        if let toml::Value::Table(table) = value {
            assert_eq!(table.get("style").and_then(|v| v.as_str()), Some("dash"));
        } else {
            panic!("Expected table");
        }
    }

    #[test]
    fn test_sublist_style() {
        let rule = MD004UnorderedListStyle::new(UnorderedListStyle::Sublist);
        // Level 0 should use *, level 1 should use +, level 2 should use -
        let content = "* Item 1\n  + Item 2\n    - Item 3\n      * Item 4\n  + Item 5";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty(), "Sublist style should accept cycling markers");
    }

    #[test]
    fn test_sublist_style_incorrect() {
        let rule = MD004UnorderedListStyle::new(UnorderedListStyle::Sublist);
        // Wrong markers for each level
        let content = "- Item 1\n  * Item 2\n    + Item 3";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 3);
        assert_eq!(
            result[0].message,
            "List marker '-' does not match expected style '*' for nesting level 0"
        );
        assert_eq!(
            result[1].message,
            "List marker '*' does not match expected style '+' for nesting level 1"
        );
        assert_eq!(
            result[2].message,
            "List marker '+' does not match expected style '-' for nesting level 2"
        );
    }

    #[test]
    fn test_fix_sublist_style() {
        let rule = MD004UnorderedListStyle::new(UnorderedListStyle::Sublist);
        let content = "- Item 1\n  - Item 2\n    - Item 3\n      - Item 4";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, "* Item 1\n  + Item 2\n    - Item 3\n      * Item 4");
    }

    #[test]
    fn test_performance_large_document() {
        let rule = MD004UnorderedListStyle::new(UnorderedListStyle::Asterisk);
        let mut content = String::new();
        for i in 0..1000 {
            content.push_str(&format!(
                "{}Item {}\n",
                if i % 3 == 0 {
                    "* "
                } else if i % 3 == 1 {
                    "- "
                } else {
                    "+ "
                },
                i
            ));
        }
        let ctx = LintContext::new(&content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        // Should detect all non-asterisk markers
        assert!(result.len() > 600);
    }
}