rumdl_lib/rules/
md007_ul_indent.rs

1/// Rule MD007: Unordered list indentation
2///
3/// See [docs/md007.md](../../docs/md007.md) for full documentation, configuration, and examples.
4use crate::rule::{LintError, LintResult, LintWarning, Rule, RuleCategory, Severity};
5use crate::rule_config_serde::RuleConfig;
6use toml;
7
8mod md007_config;
9use md007_config::MD007Config;
10
11#[derive(Debug, Clone, Default)]
12pub struct MD007ULIndent {
13    config: MD007Config,
14}
15
16impl MD007ULIndent {
17    pub fn new(indent: usize) -> Self {
18        Self {
19            config: MD007Config {
20                indent,
21                start_indented: false,
22                start_indent: 2,
23                style: md007_config::IndentStyle::TextAligned,
24            },
25        }
26    }
27
28    pub fn from_config_struct(config: MD007Config) -> Self {
29        Self { config }
30    }
31
32    /// Convert character position to visual column (accounting for tabs)
33    fn char_pos_to_visual_column(content: &str, char_pos: usize) -> usize {
34        let mut visual_col = 0;
35
36        for (current_pos, ch) in content.chars().enumerate() {
37            if current_pos >= char_pos {
38                break;
39            }
40            if ch == '\t' {
41                // Tab moves to next multiple of 4
42                visual_col = (visual_col / 4 + 1) * 4;
43            } else {
44                visual_col += 1;
45            }
46        }
47        visual_col
48    }
49}
50
51impl Rule for MD007ULIndent {
52    fn name(&self) -> &'static str {
53        "MD007"
54    }
55
56    fn description(&self) -> &'static str {
57        "Unordered list indentation"
58    }
59
60    fn check(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
61        let mut warnings = Vec::new();
62        let mut list_stack: Vec<(usize, usize, bool, usize)> = Vec::new(); // Stack of (marker_visual_col, line_num, is_ordered, content_visual_col) for tracking nesting
63
64        for (line_idx, line_info) in ctx.lines.iter().enumerate() {
65            // Skip if this line is in a code block, front matter, or mkdocstrings
66            if line_info.in_code_block || line_info.in_front_matter || line_info.in_mkdocstrings {
67                continue;
68            }
69
70            // Check if this line has a list item
71            if let Some(list_item) = &line_info.list_item {
72                // For blockquoted lists, we need to calculate indentation relative to the blockquote content
73                // not the full line. This is because blockquoted lists follow the same indentation rules
74                // as regular lists, just within their blockquote context.
75                let (content_for_calculation, adjusted_marker_column) = if line_info.blockquote.is_some() {
76                    // Find the position after the blockquote prefix (> or >> etc)
77                    // We need to find where the actual content starts after all '>' characters and spaces
78                    let mut content_start = 0;
79                    let mut found_gt = false;
80
81                    for (i, ch) in line_info.content.chars().enumerate() {
82                        if ch == '>' {
83                            found_gt = true;
84                            content_start = i + 1;
85                        } else if found_gt && ch == ' ' {
86                            // Skip the space after '>'
87                            content_start = i + 1;
88                            break;
89                        } else if found_gt {
90                            // No space after '>', content starts here
91                            break;
92                        }
93                    }
94
95                    // Extract the content after the blockquote prefix
96                    let content_after_prefix = &line_info.content[content_start..];
97                    // Adjust the marker column to be relative to the content after the prefix
98                    let adjusted_col = if list_item.marker_column >= content_start {
99                        list_item.marker_column - content_start
100                    } else {
101                        // This shouldn't happen, but handle it gracefully
102                        list_item.marker_column
103                    };
104                    (content_after_prefix.to_string(), adjusted_col)
105                } else {
106                    (line_info.content.clone(), list_item.marker_column)
107                };
108
109                // Convert marker position to visual column
110                let visual_marker_column =
111                    Self::char_pos_to_visual_column(&content_for_calculation, adjusted_marker_column);
112
113                // Calculate content visual column for text-aligned style
114                let visual_content_column = if line_info.blockquote.is_some() {
115                    // For blockquoted content, we already have the adjusted content
116                    let adjusted_content_col =
117                        if list_item.content_column >= (line_info.content.len() - content_for_calculation.len()) {
118                            list_item.content_column - (line_info.content.len() - content_for_calculation.len())
119                        } else {
120                            list_item.content_column
121                        };
122                    Self::char_pos_to_visual_column(&content_for_calculation, adjusted_content_col)
123                } else {
124                    Self::char_pos_to_visual_column(&line_info.content, list_item.content_column)
125                };
126
127                // For nesting detection, treat 1-space indent as if it's at column 0
128                // because 1 space is insufficient to establish a nesting relationship
129                let visual_marker_for_nesting = if visual_marker_column == 1 {
130                    0
131                } else {
132                    visual_marker_column
133                };
134
135                // Clean up stack - remove items at same or deeper indentation
136                while let Some(&(indent, _, _, _)) = list_stack.last() {
137                    if indent >= visual_marker_for_nesting {
138                        list_stack.pop();
139                    } else {
140                        break;
141                    }
142                }
143
144                // For ordered list items, just track them in the stack
145                if list_item.is_ordered {
146                    // For ordered lists, we don't check indentation but we need to track for text-aligned children
147                    // Use the actual positions since we don't enforce indentation for ordered lists
148                    list_stack.push((visual_marker_column, line_idx, true, visual_content_column));
149                    continue;
150                }
151
152                // Only check unordered list items
153                if !list_item.is_ordered {
154                    // Now stack contains only parent items
155                    let nesting_level = list_stack.len();
156
157                    // Calculate expected indent first to determine expected content position
158                    let expected_indent = if self.config.start_indented {
159                        self.config.start_indent + (nesting_level * self.config.indent)
160                    } else {
161                        match self.config.style {
162                            md007_config::IndentStyle::Fixed => {
163                                // Fixed style: simple multiples of indent
164                                nesting_level * self.config.indent
165                            }
166                            md007_config::IndentStyle::TextAligned => {
167                                // Text-aligned style: child's marker aligns with parent's text content
168                                if nesting_level > 0 {
169                                    // Check if parent is an ordered list
170                                    if let Some(&(_, _parent_line_idx, _is_ordered, parent_content_visual_col)) =
171                                        list_stack.get(nesting_level - 1)
172                                    {
173                                        // Child marker is positioned where parent's text starts
174                                        parent_content_visual_col
175                                    } else {
176                                        // No parent at that level - for text-aligned, use standard alignment
177                                        // Each level aligns with previous level's text position
178                                        nesting_level * 2
179                                    }
180                                } else {
181                                    0 // First level, no indentation needed
182                                }
183                            }
184                        }
185                    };
186
187                    // Add current item to stack
188                    // Use actual marker position for cleanup logic
189                    // For text-aligned children, store the EXPECTED content position after fix
190                    // (not the actual position) to prevent error cascade
191                    let expected_content_visual_col = expected_indent + 2; // where content SHOULD be after fix
192                    list_stack.push((visual_marker_column, line_idx, false, expected_content_visual_col));
193
194                    // Skip first level check if start_indented is false
195                    // BUT always check items with 1 space indent (insufficient for nesting)
196                    if !self.config.start_indented && nesting_level == 0 && visual_marker_column != 1 {
197                        continue;
198                    }
199
200                    if visual_marker_column != expected_indent {
201                        // Generate fix for this list item
202                        let fix = {
203                            let correct_indent = " ".repeat(expected_indent);
204
205                            // Build the replacement string - need to preserve everything before the list marker
206                            // For blockquoted lines, this includes the blockquote prefix
207                            let replacement = if line_info.blockquote.is_some() {
208                                // Count the blockquote markers
209                                let mut blockquote_count = 0;
210                                for ch in line_info.content.chars() {
211                                    if ch == '>' {
212                                        blockquote_count += 1;
213                                    } else if ch != ' ' && ch != '\t' {
214                                        break;
215                                    }
216                                }
217                                // Build the blockquote prefix (one '>' per level, with spaces between for nested)
218                                let blockquote_prefix = if blockquote_count > 1 {
219                                    (0..blockquote_count)
220                                        .map(|_| "> ")
221                                        .collect::<String>()
222                                        .trim_end()
223                                        .to_string()
224                                } else {
225                                    ">".to_string()
226                                };
227                                // Add correct indentation after the blockquote prefix
228                                // Include one space after the blockquote marker(s) as part of the indent
229                                format!("{blockquote_prefix} {correct_indent}")
230                            } else {
231                                correct_indent
232                            };
233
234                            // Calculate the byte positions
235                            // The range should cover from start of line to the marker position
236                            let start_byte = line_info.byte_offset;
237                            let mut end_byte = line_info.byte_offset;
238
239                            // Calculate where the marker starts
240                            for (i, ch) in line_info.content.chars().enumerate() {
241                                if i >= list_item.marker_column {
242                                    break;
243                                }
244                                end_byte += ch.len_utf8();
245                            }
246
247                            Some(crate::rule::Fix {
248                                range: start_byte..end_byte,
249                                replacement,
250                            })
251                        };
252
253                        warnings.push(LintWarning {
254                            rule_name: Some(self.name()),
255                            message: format!(
256                                "Expected {expected_indent} spaces for indent depth {nesting_level}, found {visual_marker_column}"
257                            ),
258                            line: line_idx + 1, // Convert to 1-indexed
259                            column: 1,          // Start of line
260                            end_line: line_idx + 1,
261                            end_column: visual_marker_column + 1, // End of visual indentation
262                            severity: Severity::Warning,
263                            fix,
264                        });
265                    }
266                }
267            }
268        }
269        Ok(warnings)
270    }
271
272    /// Optimized check using document structure
273    fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
274        // Get all warnings with their fixes
275        let warnings = self.check(ctx)?;
276
277        // If no warnings, return original content
278        if warnings.is_empty() {
279            return Ok(ctx.content.to_string());
280        }
281
282        // Collect all fixes and sort by range start (descending) to apply from end to beginning
283        let mut fixes: Vec<_> = warnings
284            .iter()
285            .filter_map(|w| w.fix.as_ref().map(|f| (f.range.start, f.range.end, &f.replacement)))
286            .collect();
287        fixes.sort_by(|a, b| b.0.cmp(&a.0));
288
289        // Apply fixes from end to beginning to preserve byte offsets
290        let mut result = ctx.content.to_string();
291        for (start, end, replacement) in fixes {
292            if start < result.len() && end <= result.len() && start <= end {
293                result.replace_range(start..end, replacement);
294            }
295        }
296
297        Ok(result)
298    }
299
300    /// Get the category of this rule for selective processing
301    fn category(&self) -> RuleCategory {
302        RuleCategory::List
303    }
304
305    /// Check if this rule should be skipped
306    fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
307        // Skip if content is empty or has no unordered list items
308        ctx.content.is_empty()
309            || !ctx
310                .lines
311                .iter()
312                .any(|line| line.list_item.as_ref().is_some_and(|item| !item.is_ordered))
313    }
314
315    fn as_any(&self) -> &dyn std::any::Any {
316        self
317    }
318
319    fn default_config_section(&self) -> Option<(String, toml::Value)> {
320        let default_config = MD007Config::default();
321        let json_value = serde_json::to_value(&default_config).ok()?;
322        let toml_value = crate::rule_config_serde::json_to_toml_value(&json_value)?;
323
324        if let toml::Value::Table(table) = toml_value {
325            if !table.is_empty() {
326                Some((MD007Config::RULE_NAME.to_string(), toml::Value::Table(table)))
327            } else {
328                None
329            }
330        } else {
331            None
332        }
333    }
334
335    fn from_config(config: &crate::config::Config) -> Box<dyn Rule>
336    where
337        Self: Sized,
338    {
339        let mut rule_config = crate::rule_config_serde::load_rule_config::<MD007Config>(config);
340
341        // For markdownlint compatibility: if indent is explicitly configured and style is not,
342        // default to "fixed" style (markdownlint behavior) instead of "text-aligned"
343        if let Some(rule_cfg) = config.rules.get("MD007") {
344            let has_explicit_indent = rule_cfg.values.contains_key("indent");
345            let has_explicit_style = rule_cfg.values.contains_key("style");
346
347            if has_explicit_indent && !has_explicit_style && rule_config.indent != 2 {
348                // User set indent explicitly but not style, and it's not the default value
349                // Use fixed style for markdownlint compatibility
350                rule_config.style = md007_config::IndentStyle::Fixed;
351            }
352        }
353
354        Box::new(Self::from_config_struct(rule_config))
355    }
356}
357
358#[cfg(test)]
359mod tests {
360    use super::*;
361    use crate::lint_context::LintContext;
362    use crate::rule::Rule;
363
364    #[test]
365    fn test_valid_list_indent() {
366        let rule = MD007ULIndent::default();
367        let content = "* Item 1\n  * Item 2\n    * Item 3";
368        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
369        let result = rule.check(&ctx).unwrap();
370        assert!(
371            result.is_empty(),
372            "Expected no warnings for valid indentation, but got {} warnings",
373            result.len()
374        );
375    }
376
377    #[test]
378    fn test_invalid_list_indent() {
379        let rule = MD007ULIndent::default();
380        let content = "* Item 1\n   * Item 2\n      * Item 3";
381        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
382        let result = rule.check(&ctx).unwrap();
383        assert_eq!(result.len(), 2);
384        assert_eq!(result[0].line, 2);
385        assert_eq!(result[0].column, 1);
386        assert_eq!(result[1].line, 3);
387        assert_eq!(result[1].column, 1);
388    }
389
390    #[test]
391    fn test_mixed_indentation() {
392        let rule = MD007ULIndent::default();
393        let content = "* Item 1\n  * Item 2\n   * Item 3\n  * Item 4";
394        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
395        let result = rule.check(&ctx).unwrap();
396        assert_eq!(result.len(), 1);
397        assert_eq!(result[0].line, 3);
398        assert_eq!(result[0].column, 1);
399    }
400
401    #[test]
402    fn test_fix_indentation() {
403        let rule = MD007ULIndent::default();
404        let content = "* Item 1\n   * Item 2\n      * Item 3";
405        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
406        let result = rule.fix(&ctx).unwrap();
407        // With text-aligned style and non-cascade:
408        // Item 2 aligns with Item 1's text (2 spaces)
409        // Item 3 aligns with Item 2's expected text position (4 spaces)
410        let expected = "* Item 1\n  * Item 2\n    * Item 3";
411        assert_eq!(result, expected);
412    }
413
414    #[test]
415    fn test_md007_in_yaml_code_block() {
416        let rule = MD007ULIndent::default();
417        let content = r#"```yaml
418repos:
419-   repo: https://github.com/rvben/rumdl
420    rev: v0.5.0
421    hooks:
422    -   id: rumdl-check
423```"#;
424        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
425        let result = rule.check(&ctx).unwrap();
426        assert!(
427            result.is_empty(),
428            "MD007 should not trigger inside a code block, but got warnings: {result:?}"
429        );
430    }
431
432    #[test]
433    fn test_blockquoted_list_indent() {
434        let rule = MD007ULIndent::default();
435        let content = "> * Item 1\n>   * Item 2\n>     * Item 3";
436        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
437        let result = rule.check(&ctx).unwrap();
438        assert!(
439            result.is_empty(),
440            "Expected no warnings for valid blockquoted list indentation, but got {result:?}"
441        );
442    }
443
444    #[test]
445    fn test_blockquoted_list_invalid_indent() {
446        let rule = MD007ULIndent::default();
447        let content = "> * Item 1\n>    * Item 2\n>       * Item 3";
448        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
449        let result = rule.check(&ctx).unwrap();
450        assert_eq!(
451            result.len(),
452            2,
453            "Expected 2 warnings for invalid blockquoted list indentation, got {result:?}"
454        );
455        assert_eq!(result[0].line, 2);
456        assert_eq!(result[1].line, 3);
457    }
458
459    #[test]
460    fn test_nested_blockquote_list_indent() {
461        let rule = MD007ULIndent::default();
462        let content = "> > * Item 1\n> >   * Item 2\n> >     * Item 3";
463        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
464        let result = rule.check(&ctx).unwrap();
465        assert!(
466            result.is_empty(),
467            "Expected no warnings for valid nested blockquoted list indentation, but got {result:?}"
468        );
469    }
470
471    #[test]
472    fn test_blockquote_list_with_code_block() {
473        let rule = MD007ULIndent::default();
474        let content = "> * Item 1\n>   * Item 2\n>   ```\n>   code\n>   ```\n>   * Item 3";
475        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
476        let result = rule.check(&ctx).unwrap();
477        assert!(
478            result.is_empty(),
479            "MD007 should not trigger inside a code block within a blockquote, but got warnings: {result:?}"
480        );
481    }
482
483    #[test]
484    fn test_properly_indented_lists() {
485        let rule = MD007ULIndent::default();
486
487        // Test various properly indented lists
488        let test_cases = vec![
489            "* Item 1\n* Item 2",
490            "* Item 1\n  * Item 1.1\n    * Item 1.1.1",
491            "- Item 1\n  - Item 1.1",
492            "+ Item 1\n  + Item 1.1",
493            "* Item 1\n  * Item 1.1\n* Item 2\n  * Item 2.1",
494        ];
495
496        for content in test_cases {
497            let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
498            let result = rule.check(&ctx).unwrap();
499            assert!(
500                result.is_empty(),
501                "Expected no warnings for properly indented list:\n{}\nGot {} warnings",
502                content,
503                result.len()
504            );
505        }
506    }
507
508    #[test]
509    fn test_under_indented_lists() {
510        let rule = MD007ULIndent::default();
511
512        let test_cases = vec![
513            ("* Item 1\n * Item 1.1", 1, 2),                   // Expected 2 spaces, got 1
514            ("* Item 1\n  * Item 1.1\n   * Item 1.1.1", 1, 3), // Expected 4 spaces, got 3
515        ];
516
517        for (content, expected_warnings, line) in test_cases {
518            let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
519            let result = rule.check(&ctx).unwrap();
520            assert_eq!(
521                result.len(),
522                expected_warnings,
523                "Expected {expected_warnings} warnings for under-indented list:\n{content}"
524            );
525            if expected_warnings > 0 {
526                assert_eq!(result[0].line, line);
527            }
528        }
529    }
530
531    #[test]
532    fn test_over_indented_lists() {
533        let rule = MD007ULIndent::default();
534
535        let test_cases = vec![
536            ("* Item 1\n   * Item 1.1", 1, 2),                   // Expected 2 spaces, got 3
537            ("* Item 1\n    * Item 1.1", 1, 2),                  // Expected 2 spaces, got 4
538            ("* Item 1\n  * Item 1.1\n     * Item 1.1.1", 1, 3), // Expected 4 spaces, got 5
539        ];
540
541        for (content, expected_warnings, line) in test_cases {
542            let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
543            let result = rule.check(&ctx).unwrap();
544            assert_eq!(
545                result.len(),
546                expected_warnings,
547                "Expected {expected_warnings} warnings for over-indented list:\n{content}"
548            );
549            if expected_warnings > 0 {
550                assert_eq!(result[0].line, line);
551            }
552        }
553    }
554
555    #[test]
556    fn test_custom_indent_2_spaces() {
557        let rule = MD007ULIndent::new(2); // Default
558        let content = "* Item 1\n  * Item 2\n    * Item 3";
559        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
560        let result = rule.check(&ctx).unwrap();
561        assert!(result.is_empty());
562    }
563
564    #[test]
565    fn test_custom_indent_3_spaces() {
566        // Test dynamic alignment behavior (default start_indented=false)
567        let rule = MD007ULIndent::new(3);
568
569        let content = "* Item 1\n   * Item 2\n      * Item 3";
570        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
571        let result = rule.check(&ctx).unwrap();
572        // With dynamic alignment, Item 2 should align with Item 1's text (2 spaces)
573        // and Item 3 should align with Item 2's text (4 spaces), not fixed increments
574        assert!(!result.is_empty()); // Should have warnings due to alignment
575
576        // Test that dynamic alignment works correctly
577        // Item 3 should align with Item 2's text content (4 spaces)
578        let correct_content = "* Item 1\n  * Item 2\n    * Item 3";
579        let ctx = LintContext::new(correct_content, crate::config::MarkdownFlavor::Standard);
580        let result = rule.check(&ctx).unwrap();
581        assert!(result.is_empty());
582    }
583
584    #[test]
585    fn test_custom_indent_4_spaces() {
586        // Test dynamic alignment behavior (default start_indented=false)
587        let rule = MD007ULIndent::new(4);
588        let content = "* Item 1\n    * Item 2\n        * Item 3";
589        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
590        let result = rule.check(&ctx).unwrap();
591        // With dynamic alignment, should expect 2 spaces and 6 spaces, not 4 and 8
592        assert!(!result.is_empty()); // Should have warnings due to alignment
593
594        // Test correct dynamic alignment
595        // Item 3 should align with Item 2's text content (4 spaces)
596        let correct_content = "* Item 1\n  * Item 2\n    * Item 3";
597        let ctx = LintContext::new(correct_content, crate::config::MarkdownFlavor::Standard);
598        let result = rule.check(&ctx).unwrap();
599        assert!(result.is_empty());
600    }
601
602    #[test]
603    fn test_tab_indentation() {
604        let rule = MD007ULIndent::default();
605
606        // Single tab
607        let content = "* Item 1\n\t* Item 2";
608        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
609        let result = rule.check(&ctx).unwrap();
610        assert_eq!(result.len(), 1, "Tab indentation should trigger warning");
611
612        // Fix should convert tab to spaces
613        let fixed = rule.fix(&ctx).unwrap();
614        assert_eq!(fixed, "* Item 1\n  * Item 2");
615
616        // Multiple tabs
617        let content_multi = "* Item 1\n\t* Item 2\n\t\t* Item 3";
618        let ctx = LintContext::new(content_multi, crate::config::MarkdownFlavor::Standard);
619        let fixed = rule.fix(&ctx).unwrap();
620        // With non-cascade: Item 2 at 2 spaces, content at 4
621        // Item 3 aligns with Item 2's expected content at 4 spaces
622        assert_eq!(fixed, "* Item 1\n  * Item 2\n    * Item 3");
623
624        // Mixed tabs and spaces
625        let content_mixed = "* Item 1\n \t* Item 2\n\t * Item 3";
626        let ctx = LintContext::new(content_mixed, crate::config::MarkdownFlavor::Standard);
627        let fixed = rule.fix(&ctx).unwrap();
628        // With non-cascade: Item 2 at 2 spaces, content at 4
629        // Item 3 aligns with Item 2's expected content at 4 spaces
630        assert_eq!(fixed, "* Item 1\n  * Item 2\n    * Item 3");
631    }
632
633    #[test]
634    fn test_mixed_ordered_unordered_lists() {
635        let rule = MD007ULIndent::default();
636
637        // MD007 only checks unordered lists, so ordered lists should be ignored
638        // Note: 3 spaces is now correct for bullets under ordered items
639        let content = r#"1. Ordered item
640   * Unordered sub-item (correct - 3 spaces under ordered)
641   2. Ordered sub-item
642* Unordered item
643  1. Ordered sub-item
644  * Unordered sub-item"#;
645
646        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
647        let result = rule.check(&ctx).unwrap();
648        assert_eq!(result.len(), 0, "All unordered list indentation should be correct");
649
650        // No fix needed as all indentation is correct
651        let fixed = rule.fix(&ctx).unwrap();
652        assert_eq!(fixed, content);
653    }
654
655    #[test]
656    fn test_list_markers_variety() {
657        let rule = MD007ULIndent::default();
658
659        // Test all three unordered list markers
660        let content = r#"* Asterisk
661  * Nested asterisk
662- Hyphen
663  - Nested hyphen
664+ Plus
665  + Nested plus"#;
666
667        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
668        let result = rule.check(&ctx).unwrap();
669        assert!(
670            result.is_empty(),
671            "All unordered list markers should work with proper indentation"
672        );
673
674        // Test with wrong indentation for each marker type
675        let wrong_content = r#"* Asterisk
676   * Wrong asterisk
677- Hyphen
678 - Wrong hyphen
679+ Plus
680    + Wrong plus"#;
681
682        let ctx = LintContext::new(wrong_content, crate::config::MarkdownFlavor::Standard);
683        let result = rule.check(&ctx).unwrap();
684        assert_eq!(result.len(), 3, "All marker types should be checked for indentation");
685    }
686
687    #[test]
688    fn test_empty_list_items() {
689        let rule = MD007ULIndent::default();
690        let content = "* Item 1\n* \n  * Item 2";
691        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
692        let result = rule.check(&ctx).unwrap();
693        assert!(
694            result.is_empty(),
695            "Empty list items should not affect indentation checks"
696        );
697    }
698
699    #[test]
700    fn test_list_with_code_blocks() {
701        let rule = MD007ULIndent::default();
702        let content = r#"* Item 1
703  ```
704  code
705  ```
706  * Item 2
707    * Item 3"#;
708        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
709        let result = rule.check(&ctx).unwrap();
710        assert!(result.is_empty());
711    }
712
713    #[test]
714    fn test_list_in_front_matter() {
715        let rule = MD007ULIndent::default();
716        let content = r#"---
717tags:
718  - tag1
719  - tag2
720---
721* Item 1
722  * Item 2"#;
723        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
724        let result = rule.check(&ctx).unwrap();
725        assert!(result.is_empty(), "Lists in YAML front matter should be ignored");
726    }
727
728    #[test]
729    fn test_fix_preserves_content() {
730        let rule = MD007ULIndent::default();
731        let content = "* Item 1 with **bold** and *italic*\n   * Item 2 with `code`\n     * Item 3 with [link](url)";
732        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
733        let fixed = rule.fix(&ctx).unwrap();
734        // With non-cascade: Item 2 at 2 spaces, content at 4
735        // Item 3 aligns with Item 2's expected content at 4 spaces
736        let expected = "* Item 1 with **bold** and *italic*\n  * Item 2 with `code`\n    * Item 3 with [link](url)";
737        assert_eq!(fixed, expected, "Fix should only change indentation, not content");
738    }
739
740    #[test]
741    fn test_start_indented_config() {
742        let config = MD007Config {
743            start_indented: true,
744            start_indent: 4,
745            indent: 2,
746            style: md007_config::IndentStyle::TextAligned,
747        };
748        let rule = MD007ULIndent::from_config_struct(config);
749
750        // First level should be indented by start_indent (4 spaces)
751        // Level 0: 4 spaces (start_indent)
752        // Level 1: 6 spaces (start_indent + indent = 4 + 2)
753        // Level 2: 8 spaces (start_indent + 2*indent = 4 + 4)
754        let content = "    * Item 1\n      * Item 2\n        * Item 3";
755        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
756        let result = rule.check(&ctx).unwrap();
757        assert!(result.is_empty(), "Expected no warnings with start_indented config");
758
759        // Wrong first level indentation
760        let wrong_content = "  * Item 1\n    * Item 2";
761        let ctx = LintContext::new(wrong_content, crate::config::MarkdownFlavor::Standard);
762        let result = rule.check(&ctx).unwrap();
763        assert_eq!(result.len(), 2);
764        assert_eq!(result[0].line, 1);
765        assert_eq!(result[0].message, "Expected 4 spaces for indent depth 0, found 2");
766        assert_eq!(result[1].line, 2);
767        assert_eq!(result[1].message, "Expected 6 spaces for indent depth 1, found 4");
768
769        // Fix should correct to start_indent for first level
770        let fixed = rule.fix(&ctx).unwrap();
771        assert_eq!(fixed, "    * Item 1\n      * Item 2");
772    }
773
774    #[test]
775    fn test_start_indented_false_allows_any_first_level() {
776        let rule = MD007ULIndent::default(); // start_indented is false by default
777
778        // When start_indented is false, first level items at any indentation are allowed
779        let content = "   * Item 1"; // First level at 3 spaces
780        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
781        let result = rule.check(&ctx).unwrap();
782        assert!(
783            result.is_empty(),
784            "First level at any indentation should be allowed when start_indented is false"
785        );
786
787        // Multiple first level items at different indentations should all be allowed
788        let content = "* Item 1\n  * Item 2\n    * Item 3"; // All at level 0 (different indents)
789        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
790        let result = rule.check(&ctx).unwrap();
791        assert!(
792            result.is_empty(),
793            "All first-level items should be allowed at any indentation"
794        );
795    }
796
797    #[test]
798    fn test_deeply_nested_lists() {
799        let rule = MD007ULIndent::default();
800        let content = r#"* L1
801  * L2
802    * L3
803      * L4
804        * L5
805          * L6"#;
806        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
807        let result = rule.check(&ctx).unwrap();
808        assert!(result.is_empty());
809
810        // Test with wrong deep nesting
811        let wrong_content = r#"* L1
812  * L2
813    * L3
814      * L4
815         * L5
816            * L6"#;
817        let ctx = LintContext::new(wrong_content, crate::config::MarkdownFlavor::Standard);
818        let result = rule.check(&ctx).unwrap();
819        assert_eq!(result.len(), 2, "Deep nesting errors should be detected");
820    }
821
822    #[test]
823    fn test_excessive_indentation_detected() {
824        let rule = MD007ULIndent::default();
825
826        // Test excessive indentation (5 spaces instead of 2)
827        let content = "- Item 1\n     - Item 2 with 5 spaces";
828        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
829        let result = rule.check(&ctx).unwrap();
830        assert_eq!(result.len(), 1, "Should detect excessive indentation (5 instead of 2)");
831        assert_eq!(result[0].line, 2);
832        assert!(result[0].message.contains("Expected 2 spaces"));
833        assert!(result[0].message.contains("found 5"));
834
835        // Test slightly excessive indentation (3 spaces instead of 2)
836        let content = "- Item 1\n   - Item 2 with 3 spaces";
837        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
838        let result = rule.check(&ctx).unwrap();
839        assert_eq!(
840            result.len(),
841            1,
842            "Should detect slightly excessive indentation (3 instead of 2)"
843        );
844        assert_eq!(result[0].line, 2);
845        assert!(result[0].message.contains("Expected 2 spaces"));
846        assert!(result[0].message.contains("found 3"));
847
848        // Test insufficient indentation (1 space is treated as level 0, should be 0)
849        let content = "- Item 1\n - Item 2 with 1 space";
850        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
851        let result = rule.check(&ctx).unwrap();
852        assert_eq!(
853            result.len(),
854            1,
855            "Should detect 1-space indent (insufficient for nesting, expected 0)"
856        );
857        assert_eq!(result[0].line, 2);
858        assert!(result[0].message.contains("Expected 0 spaces"));
859        assert!(result[0].message.contains("found 1"));
860    }
861
862    #[test]
863    fn test_excessive_indentation_with_4_space_config() {
864        let rule = MD007ULIndent::new(4);
865
866        // Test excessive indentation (5 spaces instead of 4) - like Ruff's versioning.md
867        let content = "- Formatter:\n     - The stable style changed";
868        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
869        let result = rule.check(&ctx).unwrap();
870
871        // Due to text-aligned style, the expected indent should be 2 (aligning with "Formatter" text)
872        // But with 5 spaces, it's wrong
873        assert!(
874            !result.is_empty(),
875            "Should detect 5 spaces when expecting proper alignment"
876        );
877
878        // Test with correct alignment
879        let correct_content = "- Formatter:\n  - The stable style changed";
880        let ctx = LintContext::new(correct_content, crate::config::MarkdownFlavor::Standard);
881        let result = rule.check(&ctx).unwrap();
882        assert!(result.is_empty(), "Should accept correct text alignment");
883    }
884
885    #[test]
886    fn test_bullets_nested_under_numbered_items() {
887        let rule = MD007ULIndent::default();
888        let content = "\
8891. **Active Directory/LDAP**
890   - User authentication and directory services
891   - LDAP for user information and validation
892
8932. **Oracle Unified Directory (OUD)**
894   - Extended user directory services";
895        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
896        let result = rule.check(&ctx).unwrap();
897        // Should have no warnings - 3 spaces is correct for bullets under numbered items
898        assert!(
899            result.is_empty(),
900            "Expected no warnings for bullets with 3 spaces under numbered items, got: {result:?}"
901        );
902    }
903
904    #[test]
905    fn test_bullets_nested_under_numbered_items_wrong_indent() {
906        let rule = MD007ULIndent::default();
907        let content = "\
9081. **Active Directory/LDAP**
909  - Wrong: only 2 spaces";
910        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
911        let result = rule.check(&ctx).unwrap();
912        // Should flag incorrect indentation
913        assert_eq!(
914            result.len(),
915            1,
916            "Expected warning for incorrect indentation under numbered items"
917        );
918        assert!(
919            result
920                .iter()
921                .any(|w| w.line == 2 && w.message.contains("Expected 3 spaces"))
922        );
923    }
924
925    #[test]
926    fn test_regular_bullet_nesting_still_works() {
927        let rule = MD007ULIndent::default();
928        let content = "\
929* Top level
930  * Nested bullet (2 spaces is correct)
931    * Deeply nested (4 spaces)";
932        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard);
933        let result = rule.check(&ctx).unwrap();
934        // Should have no warnings - standard bullet nesting still uses 2-space increments
935        assert!(
936            result.is_empty(),
937            "Expected no warnings for standard bullet nesting, got: {result:?}"
938        );
939    }
940}