Skip to main content

rumdl_lib/rules/
md005_list_indent.rs

1//!
2//! Rule MD005: Inconsistent indentation for list items at the same level
3//!
4//! See [docs/md005.md](../../docs/md005.md) for full documentation, configuration, and examples.
5
6use crate::utils::blockquote::effective_indent_in_blockquote;
7use crate::utils::range_utils::calculate_match_range;
8
9use crate::rule::{Fix, LintError, LintResult, LintWarning, Rule, RuleCategory, Severity};
10// No regex patterns needed for this rule
11use std::collections::HashMap;
12use toml;
13
14/// Type alias for parent content column groups, keyed by (parent_col, is_ordered).
15/// Used by `group_by_parent_content_column` to separate ordered and unordered items.
16type ParentContentGroups<'a> = HashMap<(usize, bool), Vec<(usize, usize, &'a crate::lint_context::LineInfo)>>;
17
18/// Rule MD005: Inconsistent indentation for list items at the same level
19#[derive(Clone, Default)]
20pub struct MD005ListIndent {
21    /// Expected indentation for top-level lists (from MD007 config)
22    top_level_indent: usize,
23}
24
25/// Cache for fast line information lookups to avoid O(n²) scanning
26struct LineCacheInfo {
27    /// Indentation level for each line (0 for empty lines)
28    indentation: Vec<usize>,
29    /// Blockquote nesting level for each line (0 for non-blockquote lines)
30    blockquote_levels: Vec<usize>,
31    /// Line content references for blockquote-aware indent calculation
32    line_contents: Vec<String>,
33    /// Bit flags: bit 0 = has_content, bit 1 = is_list_item, bit 2 = is_continuation_content
34    flags: Vec<u8>,
35    /// Parent list item line number for each list item (1-indexed, 0 = no parent)
36    /// Pre-computed in O(n) to avoid O(n²) backward scanning
37    parent_map: HashMap<usize, usize>,
38}
39
40const FLAG_HAS_CONTENT: u8 = 1;
41const FLAG_IS_LIST_ITEM: u8 = 2;
42
43impl LineCacheInfo {
44    /// Build cache from context in one O(n) pass
45    fn new(ctx: &crate::lint_context::LintContext) -> Self {
46        let total_lines = ctx.lines.len();
47        let mut indentation = Vec::with_capacity(total_lines);
48        let mut blockquote_levels = Vec::with_capacity(total_lines);
49        let mut line_contents = Vec::with_capacity(total_lines);
50        let mut flags = Vec::with_capacity(total_lines);
51        let mut parent_map = HashMap::new();
52
53        // Track most recent list item at each indentation level for O(1) parent lookups
54        // Key: marker_column, Value: line_num (1-indexed)
55        //
56        // Algorithm correctness invariant:
57        // For each list item L at line N with marker_column M:
58        //   parent_map[N] = the line number of the most recent list item P where:
59        //     1. P.line < N (appears before L)
60        //     2. P.marker_column < M (less indented than L)
61        //     3. P.marker_column is maximal among all candidates (closest parent)
62        //
63        // This matches the original O(n) backward scan logic but pre-computes in O(n).
64        let mut indent_stack: Vec<(usize, usize)> = Vec::new();
65
66        for (idx, line_info) in ctx.lines.iter().enumerate() {
67            let line_content = line_info.content(ctx.content);
68            let content = line_content.trim_start();
69            let line_indent = line_info.byte_len - content.len();
70
71            indentation.push(line_indent);
72
73            // Store blockquote level for blockquote-aware indent calculation
74            let bq_level = line_info.blockquote.as_ref().map(|bq| bq.nesting_level).unwrap_or(0);
75            blockquote_levels.push(bq_level);
76
77            // Store line content for blockquote-aware indent calculation
78            line_contents.push(line_content.to_string());
79
80            let mut flag = 0u8;
81            if !content.is_empty() {
82                flag |= FLAG_HAS_CONTENT;
83            }
84            if let Some(list_item) = &line_info.list_item {
85                flag |= FLAG_IS_LIST_ITEM;
86
87                let line_num = idx + 1; // Convert to 1-indexed
88                let marker_column = list_item.marker_column;
89
90                // Maintain a monotonic stack of indentation levels (O(1) amortized)
91                while let Some(&(indent, _)) = indent_stack.last() {
92                    if indent < marker_column {
93                        break;
94                    }
95                    indent_stack.pop();
96                }
97
98                if let Some((_, parent_line)) = indent_stack.last() {
99                    parent_map.insert(line_num, *parent_line);
100                }
101
102                indent_stack.push((marker_column, line_num));
103            }
104            flags.push(flag);
105        }
106
107        Self {
108            indentation,
109            blockquote_levels,
110            line_contents,
111            flags,
112            parent_map,
113        }
114    }
115
116    /// Check if line has content
117    fn has_content(&self, idx: usize) -> bool {
118        self.flags.get(idx).is_some_and(|&f| f & FLAG_HAS_CONTENT != 0)
119    }
120
121    /// Check if line is a list item
122    fn is_list_item(&self, idx: usize) -> bool {
123        self.flags.get(idx).is_some_and(|&f| f & FLAG_IS_LIST_ITEM != 0)
124    }
125
126    /// Get blockquote info for a line (level and prefix length)
127    fn blockquote_info(&self, line: usize) -> (usize, usize) {
128        if line == 0 || line > self.line_contents.len() {
129            return (0, 0);
130        }
131        let idx = line - 1;
132        let bq_level = self.blockquote_levels.get(idx).copied().unwrap_or(0);
133        if bq_level == 0 {
134            return (0, 0);
135        }
136        // Calculate prefix length from line content
137        let content = &self.line_contents[idx];
138        let mut prefix_len = 0;
139        let mut found = 0;
140        for c in content.chars() {
141            prefix_len += c.len_utf8();
142            if c == '>' {
143                found += 1;
144                if found == bq_level {
145                    // Include optional space after last >
146                    if content.get(prefix_len..prefix_len + 1) == Some(" ") {
147                        prefix_len += 1;
148                    }
149                    break;
150                }
151            }
152        }
153        (bq_level, prefix_len)
154    }
155
156    /// Fast O(n) check for continuation content between lines using cached data
157    ///
158    /// For blockquote-aware detection, also pass the parent's blockquote level and
159    /// blockquote prefix length. These are used to calculate effective indentation
160    /// for lines inside blockquotes.
161    fn find_continuation_indent(
162        &self,
163        start_line: usize,
164        end_line: usize,
165        parent_content_column: usize,
166        parent_bq_level: usize,
167        parent_bq_prefix_len: usize,
168    ) -> Option<usize> {
169        if start_line == 0 || start_line > end_line || end_line > self.indentation.len() {
170            return None;
171        }
172
173        // For blockquote lists, min continuation indent is the content column
174        // WITHOUT the blockquote prefix portion
175        let min_continuation_indent = if parent_bq_level > 0 {
176            parent_content_column.saturating_sub(parent_bq_prefix_len)
177        } else {
178            parent_content_column
179        };
180
181        // Convert to 0-indexed
182        let start_idx = start_line - 1;
183        let end_idx = end_line - 1;
184
185        for idx in start_idx..=end_idx {
186            // Skip empty lines and list items
187            if !self.has_content(idx) || self.is_list_item(idx) {
188                continue;
189            }
190
191            // Calculate effective indent (blockquote-aware)
192            let line_bq_level = self.blockquote_levels.get(idx).copied().unwrap_or(0);
193            let raw_indent = self.indentation[idx];
194            let effective_indent = if line_bq_level == parent_bq_level && parent_bq_level > 0 {
195                effective_indent_in_blockquote(&self.line_contents[idx], parent_bq_level, raw_indent)
196            } else {
197                raw_indent
198            };
199
200            // If this line is indented at or past the min continuation indent,
201            // it's continuation content
202            if effective_indent >= min_continuation_indent {
203                return Some(effective_indent);
204            }
205        }
206        None
207    }
208
209    /// Fast O(n) check if any continuation content exists after parent
210    ///
211    /// For blockquote-aware detection, also pass the parent's blockquote level and
212    /// blockquote prefix length.
213    fn has_continuation_content(
214        &self,
215        parent_line: usize,
216        current_line: usize,
217        parent_content_column: usize,
218        parent_bq_level: usize,
219        parent_bq_prefix_len: usize,
220    ) -> bool {
221        if parent_line == 0 || current_line <= parent_line || current_line > self.indentation.len() {
222            return false;
223        }
224
225        // For blockquote lists, min continuation indent is the content column
226        // WITHOUT the blockquote prefix portion
227        let min_continuation_indent = if parent_bq_level > 0 {
228            parent_content_column.saturating_sub(parent_bq_prefix_len)
229        } else {
230            parent_content_column
231        };
232
233        // Convert to 0-indexed
234        let start_idx = parent_line; // parent_line + 1 - 1
235        let end_idx = current_line - 2; // current_line - 1 - 1
236
237        if start_idx > end_idx {
238            return false;
239        }
240
241        for idx in start_idx..=end_idx {
242            // Skip empty lines and list items
243            if !self.has_content(idx) || self.is_list_item(idx) {
244                continue;
245            }
246
247            // Calculate effective indent (blockquote-aware)
248            let line_bq_level = self.blockquote_levels.get(idx).copied().unwrap_or(0);
249            let raw_indent = self.indentation[idx];
250            let effective_indent = if line_bq_level == parent_bq_level && parent_bq_level > 0 {
251                effective_indent_in_blockquote(&self.line_contents[idx], parent_bq_level, raw_indent)
252            } else {
253                raw_indent
254            };
255
256            // If this line is indented at or past the min continuation indent,
257            // it's continuation content
258            if effective_indent >= min_continuation_indent {
259                return true;
260            }
261        }
262        false
263    }
264}
265
266impl MD005ListIndent {
267    /// Gap tolerance for grouping list blocks as one logical structure.
268    /// Markdown allows blank lines within lists, so we need some tolerance.
269    /// 2 lines handles: 1 blank line + potential interruption
270    const LIST_GROUP_GAP_TOLERANCE: usize = 2;
271
272    /// Minimum indentation increase to be considered a child (not same level).
273    /// Per Markdown convention, nested items need at least 2 more spaces.
274    const MIN_CHILD_INDENT_INCREASE: usize = 2;
275
276    /// Tolerance for considering items at "same level" despite minor indent differences.
277    /// Allows for 1 space difference to accommodate inconsistent formatting.
278    const SAME_LEVEL_TOLERANCE: i32 = 1;
279
280    /// Standard continuation list indentation offset from parent content column.
281    /// Lists that are continuation content typically indent 2 spaces from parent content.
282    const STANDARD_CONTINUATION_OFFSET: usize = 2;
283
284    /// Creates a warning for an indent mismatch.
285    fn create_indent_warning(
286        &self,
287        ctx: &crate::lint_context::LintContext,
288        line_num: usize,
289        line_info: &crate::lint_context::LineInfo,
290        actual_indent: usize,
291        expected_indent: usize,
292    ) -> LintWarning {
293        let message = format!(
294            "Expected indentation of {} {}, found {}",
295            expected_indent,
296            if expected_indent == 1 { "space" } else { "spaces" },
297            actual_indent
298        );
299
300        let (start_line, start_col, end_line, end_col) = if actual_indent > 0 {
301            calculate_match_range(line_num, line_info.content(ctx.content), 0, actual_indent)
302        } else {
303            calculate_match_range(line_num, line_info.content(ctx.content), 0, 1)
304        };
305
306        // For blockquote-nested lists, we need to preserve the blockquote prefix
307        // Similar to how MD007 handles this case
308        let (fix_range, replacement) = if line_info.blockquote.is_some() {
309            // Calculate the range from start of line to the list marker position
310            let start_byte = line_info.byte_offset;
311            let mut end_byte = line_info.byte_offset;
312
313            // Get the list marker position from list_item
314            let marker_column = line_info
315                .list_item
316                .as_ref()
317                .map(|li| li.marker_column)
318                .unwrap_or(actual_indent);
319
320            // Calculate where the marker starts
321            for (i, ch) in line_info.content(ctx.content).chars().enumerate() {
322                if i >= marker_column {
323                    break;
324                }
325                end_byte += ch.len_utf8();
326            }
327
328            // Build the blockquote prefix
329            let mut blockquote_count = 0;
330            for ch in line_info.content(ctx.content).chars() {
331                if ch == '>' {
332                    blockquote_count += 1;
333                } else if ch != ' ' && ch != '\t' {
334                    break;
335                }
336            }
337
338            // Build the blockquote prefix (one '>' per level, with spaces between for nested)
339            let blockquote_prefix = if blockquote_count > 1 {
340                (0..blockquote_count)
341                    .map(|_| "> ")
342                    .collect::<String>()
343                    .trim_end()
344                    .to_string()
345            } else {
346                ">".to_string()
347            };
348
349            // Build replacement with blockquote prefix + correct indentation
350            let correct_indent = " ".repeat(expected_indent);
351            let replacement = format!("{blockquote_prefix} {correct_indent}");
352
353            (start_byte..end_byte, replacement)
354        } else {
355            // Non-blockquote case: original logic
356            let fix_range = if actual_indent > 0 {
357                let start_byte = ctx.line_offsets.get(line_num - 1).copied().unwrap_or(0);
358                let end_byte = start_byte + actual_indent;
359                start_byte..end_byte
360            } else {
361                let byte_pos = ctx.line_offsets.get(line_num - 1).copied().unwrap_or(0);
362                byte_pos..byte_pos
363            };
364
365            let replacement = if expected_indent > 0 {
366                " ".repeat(expected_indent)
367            } else {
368                String::new()
369            };
370
371            (fix_range, replacement)
372        };
373
374        LintWarning {
375            rule_name: Some(self.name().to_string()),
376            line: start_line,
377            column: start_col,
378            end_line,
379            end_column: end_col,
380            message,
381            severity: Severity::Warning,
382            fix: Some(Fix {
383                range: fix_range,
384                replacement,
385            }),
386        }
387    }
388
389    /// Checks consistency within a group of items and emits warnings.
390    /// Uses first-established indent as the expected value when inconsistencies are found.
391    fn check_indent_consistency(
392        &self,
393        ctx: &crate::lint_context::LintContext,
394        items: &[(usize, usize, &crate::lint_context::LineInfo)],
395        warnings: &mut Vec<LintWarning>,
396    ) {
397        if items.len() < 2 {
398            return;
399        }
400
401        // Sort items by line number to find first-established pattern
402        let mut sorted_items: Vec<_> = items.iter().collect();
403        sorted_items.sort_by_key(|(line_num, _, _)| *line_num);
404
405        let indents: std::collections::HashSet<usize> = sorted_items.iter().map(|(_, indent, _)| *indent).collect();
406
407        if indents.len() > 1 {
408            // Items have inconsistent indentation
409            // Use the first established indent as the expected value
410            let expected_indent = sorted_items.first().map(|(_, i, _)| *i).unwrap_or(0);
411
412            for (line_num, indent, line_info) in items {
413                if *indent != expected_indent {
414                    warnings.push(self.create_indent_warning(ctx, *line_num, line_info, *indent, expected_indent));
415                }
416            }
417        }
418    }
419
420    /// Groups items by their semantic parent's content column AND list type.
421    ///
422    /// By grouping by (parent_content_column, is_ordered), we enforce consistency
423    /// within each list type separately. This prevents oscillation with MD007, which
424    /// only adjusts unordered list indentation and may expect different values than
425    /// what ordered lists use. (fixes #287)
426    fn group_by_parent_content_column<'a>(
427        &self,
428        level: usize,
429        group: &[(usize, usize, &'a crate::lint_context::LineInfo)],
430        all_list_items: &[(
431            usize,
432            usize,
433            &crate::lint_context::LineInfo,
434            &crate::lint_context::ListItemInfo,
435        )],
436        level_map: &HashMap<usize, usize>,
437    ) -> ParentContentGroups<'a> {
438        let parent_level = level - 1;
439
440        // Build line->is_ordered map for O(1) lookup
441        let is_ordered_map: HashMap<usize, bool> = all_list_items
442            .iter()
443            .map(|(ln, _, _, item)| (*ln, item.is_ordered))
444            .collect();
445
446        let mut parent_content_groups: ParentContentGroups<'a> = HashMap::new();
447
448        for (line_num, indent, line_info) in group {
449            let item_is_ordered = is_ordered_map.get(line_num).copied().unwrap_or(false);
450
451            // Find the most recent item at parent_level before this line
452            let mut parent_content_col: Option<usize> = None;
453
454            for (prev_line, _, _, list_item) in all_list_items.iter().rev() {
455                if *prev_line >= *line_num {
456                    continue;
457                }
458                if let Some(&prev_level) = level_map.get(prev_line)
459                    && prev_level == parent_level
460                {
461                    parent_content_col = Some(list_item.content_column);
462                    break;
463                }
464            }
465
466            if let Some(parent_col) = parent_content_col {
467                parent_content_groups
468                    .entry((parent_col, item_is_ordered))
469                    .or_default()
470                    .push((*line_num, *indent, *line_info));
471            }
472        }
473
474        parent_content_groups
475    }
476
477    /// Group related list blocks that should be treated as one logical list structure
478    fn group_related_list_blocks<'a>(
479        &self,
480        list_blocks: &'a [crate::lint_context::ListBlock],
481    ) -> Vec<Vec<&'a crate::lint_context::ListBlock>> {
482        if list_blocks.is_empty() {
483            return Vec::new();
484        }
485
486        let mut groups = Vec::new();
487        let mut current_group = vec![&list_blocks[0]];
488
489        for i in 1..list_blocks.len() {
490            let prev_block = &list_blocks[i - 1];
491            let current_block = &list_blocks[i];
492
493            // Check if blocks are consecutive (no significant gap between them)
494            let line_gap = current_block.start_line.saturating_sub(prev_block.end_line);
495
496            // Group blocks if they are close together
497            // This handles cases where mixed list types are split but should be treated together
498            if line_gap <= Self::LIST_GROUP_GAP_TOLERANCE {
499                current_group.push(current_block);
500            } else {
501                // Start a new group
502                groups.push(current_group);
503                current_group = vec![current_block];
504            }
505        }
506        groups.push(current_group);
507
508        groups
509    }
510
511    /// Check if a list item is continuation content of a parent list item
512    /// Uses pre-computed parent map for O(1) lookup instead of O(n) backward scanning
513    fn is_continuation_content(
514        &self,
515        ctx: &crate::lint_context::LintContext,
516        cache: &LineCacheInfo,
517        list_line: usize,
518        list_indent: usize,
519    ) -> bool {
520        // Use pre-computed parent map instead of O(n) backward scan
521        let parent_line = cache.parent_map.get(&list_line).copied();
522
523        if let Some(parent_line) = parent_line
524            && let Some(line_info) = ctx.line_info(parent_line)
525            && let Some(parent_list_item) = &line_info.list_item
526        {
527            let parent_marker_column = parent_list_item.marker_column;
528            let parent_content_column = parent_list_item.content_column;
529
530            // Get parent's blockquote info for blockquote-aware continuation detection
531            let parent_bq_level = line_info.blockquote.as_ref().map(|bq| bq.nesting_level).unwrap_or(0);
532            let parent_bq_prefix_len = line_info.blockquote.as_ref().map(|bq| bq.prefix.len()).unwrap_or(0);
533
534            // Check if there are continuation lines between parent and current list
535            let continuation_indent = cache.find_continuation_indent(
536                parent_line + 1,
537                list_line - 1,
538                parent_content_column,
539                parent_bq_level,
540                parent_bq_prefix_len,
541            );
542
543            if let Some(continuation_indent) = continuation_indent {
544                let is_standard_continuation =
545                    list_indent == parent_content_column + Self::STANDARD_CONTINUATION_OFFSET;
546                let matches_content_indent = list_indent == continuation_indent;
547
548                if matches_content_indent || is_standard_continuation {
549                    return true;
550                }
551            }
552
553            // Special case: if this list item is at the same indentation as previous
554            // continuation lists, it might be part of the same continuation block
555            if list_indent > parent_marker_column {
556                // Check if previous list items at this indentation are also continuation
557                if self.has_continuation_list_at_indent(
558                    ctx,
559                    cache,
560                    parent_line,
561                    list_line,
562                    list_indent,
563                    parent_content_column,
564                ) {
565                    return true;
566                }
567
568                // Get blockquote info for continuation check
569                let (parent_bq_level, parent_bq_prefix_len) = cache.blockquote_info(parent_line);
570                if cache.has_continuation_content(
571                    parent_line,
572                    list_line,
573                    parent_content_column,
574                    parent_bq_level,
575                    parent_bq_prefix_len,
576                ) {
577                    return true;
578                }
579            }
580        }
581
582        false
583    }
584
585    /// Check if there are continuation lists at the same indentation after a parent
586    fn has_continuation_list_at_indent(
587        &self,
588        ctx: &crate::lint_context::LintContext,
589        cache: &LineCacheInfo,
590        parent_line: usize,
591        current_line: usize,
592        list_indent: usize,
593        parent_content_column: usize,
594    ) -> bool {
595        // Get blockquote info from cache
596        let (parent_bq_level, parent_bq_prefix_len) = cache.blockquote_info(parent_line);
597
598        // Look for list items between parent and current that are at the same indentation
599        // and are part of continuation content
600        for line_num in (parent_line + 1)..current_line {
601            if let Some(line_info) = ctx.line_info(line_num)
602                && let Some(list_item) = &line_info.list_item
603                && list_item.marker_column == list_indent
604            {
605                // Found a list at same indentation - check if it has continuation content before it
606                if cache
607                    .find_continuation_indent(
608                        parent_line + 1,
609                        line_num - 1,
610                        parent_content_column,
611                        parent_bq_level,
612                        parent_bq_prefix_len,
613                    )
614                    .is_some()
615                {
616                    return true;
617                }
618            }
619        }
620        false
621    }
622
623    /// Check a group of related list blocks as one logical list structure
624    fn check_list_block_group(
625        &self,
626        ctx: &crate::lint_context::LintContext,
627        group: &[&crate::lint_context::ListBlock],
628        warnings: &mut Vec<LintWarning>,
629    ) -> Result<(), LintError> {
630        // Build cache once for O(n) preprocessing instead of O(n²) scanning
631        let cache = LineCacheInfo::new(ctx);
632
633        // First pass: collect all candidate items without filtering
634        // We need to process in line order so parents are seen before children
635        let mut candidate_items: Vec<(
636            usize,
637            usize,
638            &crate::lint_context::LineInfo,
639            &crate::lint_context::ListItemInfo,
640        )> = Vec::new();
641
642        for list_block in group {
643            for &item_line in &list_block.item_lines {
644                if let Some(line_info) = ctx.line_info(item_line)
645                    && let Some(list_item) = line_info.list_item.as_deref()
646                {
647                    // Calculate the effective indentation (considering blockquotes)
648                    let effective_indent = if let Some(blockquote) = &line_info.blockquote {
649                        // For blockquoted lists, use relative indentation within the blockquote
650                        list_item.marker_column.saturating_sub(blockquote.nesting_level * 2)
651                    } else {
652                        // For normal lists, use the marker column directly
653                        list_item.marker_column
654                    };
655
656                    candidate_items.push((item_line, effective_indent, line_info, list_item));
657                }
658            }
659        }
660
661        // Sort by line number so parents are processed before children
662        candidate_items.sort_by_key(|(line_num, _, _, _)| *line_num);
663
664        // Second pass: filter out continuation content AND their children
665        // When a parent is skipped, all its descendants must also be skipped
666        let mut skipped_lines: std::collections::HashSet<usize> = std::collections::HashSet::new();
667        let mut all_list_items: Vec<(
668            usize,
669            usize,
670            &crate::lint_context::LineInfo,
671            &crate::lint_context::ListItemInfo,
672        )> = Vec::new();
673
674        for (item_line, effective_indent, line_info, list_item) in candidate_items {
675            // Skip list items that are continuation content
676            if self.is_continuation_content(ctx, &cache, item_line, effective_indent) {
677                skipped_lines.insert(item_line);
678                continue;
679            }
680
681            // Also skip items whose parent was skipped (children of continuation content)
682            if let Some(&parent_line) = cache.parent_map.get(&item_line)
683                && skipped_lines.contains(&parent_line)
684            {
685                skipped_lines.insert(item_line);
686                continue;
687            }
688
689            all_list_items.push((item_line, effective_indent, line_info, list_item));
690        }
691
692        if all_list_items.is_empty() {
693            return Ok(());
694        }
695
696        // Sort by line number to process in order
697        all_list_items.sort_by_key(|(line_num, _, _, _)| *line_num);
698
699        // Build level mapping based on hierarchical structure
700        // Key insight: We need to identify which items are meant to be at the same level
701        // even if they have slightly different indentations (inconsistent formatting)
702        let mut level_map: HashMap<usize, usize> = HashMap::new();
703        let mut level_indents: HashMap<usize, Vec<usize>> = HashMap::new(); // Track all indents seen at each level
704
705        // Track the most recent item at each indent level for O(1) parent lookups
706        // Key: indent value, Value: (level, line_num)
707        let mut indent_to_level: HashMap<usize, (usize, usize)> = HashMap::new();
708
709        // Process items in order to build the level hierarchy - now O(n) instead of O(n²)
710        for (line_num, indent, _, _) in &all_list_items {
711            let level = if indent_to_level.is_empty() {
712                // First item establishes level 1
713                level_indents.entry(1).or_default().push(*indent);
714                1
715            } else {
716                // Find the appropriate level for this item
717                let mut determined_level = 0;
718
719                // First, check if this indent matches any existing level exactly
720                if let Some(&(existing_level, _)) = indent_to_level.get(indent) {
721                    determined_level = existing_level;
722                } else {
723                    // No exact match - determine level based on hierarchy
724                    // Find the most recent item with clearly less indentation (parent)
725                    // Instead of scanning backward O(n), look through tracked indents O(k) where k is number of unique indents
726                    let mut best_parent: Option<(usize, usize, usize)> = None; // (indent, level, line)
727
728                    for (&tracked_indent, &(tracked_level, tracked_line)) in &indent_to_level {
729                        if tracked_indent < *indent {
730                            // This is a potential parent (less indentation)
731                            // Keep the one with the largest indent (closest parent)
732                            if best_parent.is_none() || tracked_indent > best_parent.unwrap().0 {
733                                best_parent = Some((tracked_indent, tracked_level, tracked_line));
734                            }
735                        }
736                    }
737
738                    if let Some((parent_indent, parent_level, _parent_line)) = best_parent {
739                        // A clear parent has at least MIN_CHILD_INDENT_INCREASE spaces less indentation
740                        if parent_indent + Self::MIN_CHILD_INDENT_INCREASE <= *indent {
741                            // This is a child of the parent
742                            determined_level = parent_level + 1;
743                        } else if (*indent as i32 - parent_indent as i32).abs() <= Self::SAME_LEVEL_TOLERANCE {
744                            // Within SAME_LEVEL_TOLERANCE - likely meant to be same level but inconsistent
745                            determined_level = parent_level;
746                        } else {
747                            // Less than 2 space difference but more than 1
748                            // This is ambiguous - could be same level or child
749                            // Check if any existing level has a similar indent
750                            let mut found_similar = false;
751                            if let Some(indents_at_level) = level_indents.get(&parent_level) {
752                                for &level_indent in indents_at_level {
753                                    if (level_indent as i32 - *indent as i32).abs() <= Self::SAME_LEVEL_TOLERANCE {
754                                        determined_level = parent_level;
755                                        found_similar = true;
756                                        break;
757                                    }
758                                }
759                            }
760                            if !found_similar {
761                                // Treat as child since it has more indent
762                                determined_level = parent_level + 1;
763                            }
764                        }
765                    }
766
767                    // If still not determined, default to level 1
768                    if determined_level == 0 {
769                        determined_level = 1;
770                    }
771
772                    // Record this indent for the level
773                    level_indents.entry(determined_level).or_default().push(*indent);
774                }
775
776                determined_level
777            };
778
779            level_map.insert(*line_num, level);
780            // Track this indent and level for future O(1) lookups
781            indent_to_level.insert(*indent, (level, *line_num));
782        }
783
784        // Now group items by their level
785        let mut level_groups: HashMap<usize, Vec<(usize, usize, &crate::lint_context::LineInfo)>> = HashMap::new();
786        for (line_num, indent, line_info, _) in &all_list_items {
787            let level = level_map[line_num];
788            level_groups
789                .entry(level)
790                .or_default()
791                .push((*line_num, *indent, *line_info));
792        }
793
794        // For each level, check consistency
795        for (level, mut group) in level_groups {
796            group.sort_by_key(|(line_num, _, _)| *line_num);
797
798            if level == 1 {
799                // Top-level items should have the configured indentation
800                for (line_num, indent, line_info) in &group {
801                    if *indent != self.top_level_indent {
802                        warnings.push(self.create_indent_warning(
803                            ctx,
804                            *line_num,
805                            line_info,
806                            *indent,
807                            self.top_level_indent,
808                        ));
809                    }
810                }
811            } else {
812                // For sublists (level > 1), group items by their semantic parent's content column.
813                // This handles ordered lists where marker widths vary (e.g., "1. " vs "10. ").
814                let parent_content_groups =
815                    self.group_by_parent_content_column(level, &group, &all_list_items, &level_map);
816
817                // Check consistency within each parent content column group
818                for items in parent_content_groups.values() {
819                    self.check_indent_consistency(ctx, items, warnings);
820                }
821            }
822        }
823
824        Ok(())
825    }
826
827    /// Migrated to use centralized list blocks for better performance and accuracy
828    fn check_optimized(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
829        let content = ctx.content;
830
831        // Early returns for common cases
832        if content.is_empty() {
833            return Ok(Vec::new());
834        }
835
836        // Quick check for any list blocks before processing
837        if ctx.list_blocks.is_empty() {
838            return Ok(Vec::new());
839        }
840
841        let mut warnings = Vec::new();
842
843        // Group consecutive list blocks that should be treated as one logical structure
844        // This is needed because mixed list types (ordered/unordered) get split into separate blocks
845        let block_groups = self.group_related_list_blocks(&ctx.list_blocks);
846
847        for group in block_groups {
848            self.check_list_block_group(ctx, &group, &mut warnings)?;
849        }
850
851        Ok(warnings)
852    }
853}
854
855impl Rule for MD005ListIndent {
856    fn name(&self) -> &'static str {
857        "MD005"
858    }
859
860    fn description(&self) -> &'static str {
861        "List indentation should be consistent"
862    }
863
864    fn check(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
865        // Use optimized version
866        self.check_optimized(ctx)
867    }
868
869    fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
870        let warnings = self.check(ctx)?;
871        let warnings =
872            crate::utils::fix_utils::filter_warnings_by_inline_config(warnings, ctx.inline_config(), self.name());
873        if warnings.is_empty() {
874            return Ok(ctx.content.to_string());
875        }
876
877        // Sort warnings by position (descending) to apply from end to start
878        let mut warnings_with_fixes: Vec<_> = warnings
879            .into_iter()
880            .filter_map(|w| w.fix.clone().map(|fix| (w, fix)))
881            .collect();
882        warnings_with_fixes.sort_by_key(|(_, fix)| std::cmp::Reverse(fix.range.start));
883
884        // Apply fixes to content
885        let mut content = ctx.content.to_string();
886        for (_, fix) in warnings_with_fixes {
887            if fix.range.start <= content.len() && fix.range.end <= content.len() {
888                content.replace_range(fix.range, &fix.replacement);
889            }
890        }
891
892        Ok(content)
893    }
894
895    fn category(&self) -> RuleCategory {
896        RuleCategory::List
897    }
898
899    /// Check if this rule should be skipped
900    fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
901        // Skip if content is empty or has no list items
902        ctx.content.is_empty() || !ctx.lines.iter().any(|line| line.list_item.is_some())
903    }
904
905    fn as_any(&self) -> &dyn std::any::Any {
906        self
907    }
908
909    fn default_config_section(&self) -> Option<(String, toml::Value)> {
910        None
911    }
912
913    fn from_config(config: &crate::config::Config) -> Box<dyn Rule>
914    where
915        Self: Sized,
916    {
917        // Check MD007 configuration to understand expected list indentation
918        let mut top_level_indent = 0;
919
920        // Try to get MD007 configuration for top-level indentation
921        if let Some(md007_config) = config.rules.get("MD007") {
922            // Check for start_indented setting
923            if let Some(start_indented) = md007_config.values.get("start-indented")
924                && let Some(start_indented_bool) = start_indented.as_bool()
925                && start_indented_bool
926            {
927                // If start_indented is true, check for start_indent value
928                if let Some(start_indent) = md007_config.values.get("start-indent") {
929                    if let Some(indent_value) = start_indent.as_integer() {
930                        top_level_indent = indent_value as usize;
931                    }
932                } else {
933                    // Default start_indent when start_indented is true
934                    top_level_indent = 2;
935                }
936            }
937        }
938
939        Box::new(MD005ListIndent { top_level_indent })
940    }
941}
942
943#[cfg(test)]
944mod tests {
945    use super::*;
946    use crate::lint_context::LintContext;
947
948    #[test]
949    fn test_valid_unordered_list() {
950        let rule = MD005ListIndent::default();
951        let content = "\
952* Item 1
953* Item 2
954  * Nested 1
955  * Nested 2
956* Item 3";
957        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
958        let result = rule.check(&ctx).unwrap();
959        assert!(result.is_empty());
960    }
961
962    #[test]
963    fn test_valid_ordered_list() {
964        let rule = MD005ListIndent::default();
965        let content = "\
9661. Item 1
9672. Item 2
968   1. Nested 1
969   2. Nested 2
9703. Item 3";
971        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
972        let result = rule.check(&ctx).unwrap();
973        // With dynamic alignment, nested items should align with parent's text content
974        // Ordered items starting with "1. " have text at column 3, so nested items need 3 spaces
975        assert!(result.is_empty());
976    }
977
978    #[test]
979    fn test_invalid_unordered_indent() {
980        let rule = MD005ListIndent::default();
981        let content = "\
982* Item 1
983 * Item 2
984   * Nested 1";
985        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
986        let result = rule.check(&ctx).unwrap();
987        // With dynamic alignment, line 3 correctly aligns with line 2's text position
988        // Only line 2 is incorrectly indented
989        assert_eq!(result.len(), 1);
990        let fixed = rule.fix(&ctx).unwrap();
991        assert_eq!(fixed, "* Item 1\n* Item 2\n   * Nested 1");
992    }
993
994    #[test]
995    fn test_invalid_ordered_indent() {
996        let rule = MD005ListIndent::default();
997        let content = "\
9981. Item 1
999 2. Item 2
1000    1. Nested 1";
1001        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1002        let result = rule.check(&ctx).unwrap();
1003        assert_eq!(result.len(), 1);
1004        let fixed = rule.fix(&ctx).unwrap();
1005        // With dynamic alignment, ordered items align with parent's text content
1006        // Line 1 text starts at col 3, so line 2 should have 3 spaces
1007        // Line 3 already correctly aligns with line 2's text position
1008        assert_eq!(fixed, "1. Item 1\n2. Item 2\n    1. Nested 1");
1009    }
1010
1011    #[test]
1012    fn test_mixed_list_types() {
1013        let rule = MD005ListIndent::default();
1014        let content = "\
1015* Item 1
1016  1. Nested ordered
1017  * Nested unordered
1018* Item 2";
1019        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1020        let result = rule.check(&ctx).unwrap();
1021        assert!(result.is_empty());
1022    }
1023
1024    #[test]
1025    fn test_multiple_levels() {
1026        let rule = MD005ListIndent::default();
1027        let content = "\
1028* Level 1
1029   * Level 2
1030      * Level 3";
1031        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1032        let result = rule.check(&ctx).unwrap();
1033        // MD005 should now accept consistent 3-space increments
1034        assert!(result.is_empty(), "MD005 should accept consistent indentation pattern");
1035    }
1036
1037    #[test]
1038    fn test_empty_lines() {
1039        let rule = MD005ListIndent::default();
1040        let content = "\
1041* Item 1
1042
1043  * Nested 1
1044
1045* Item 2";
1046        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1047        let result = rule.check(&ctx).unwrap();
1048        assert!(result.is_empty());
1049    }
1050
1051    #[test]
1052    fn test_no_lists() {
1053        let rule = MD005ListIndent::default();
1054        let content = "\
1055Just some text
1056More text
1057Even more text";
1058        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1059        let result = rule.check(&ctx).unwrap();
1060        assert!(result.is_empty());
1061    }
1062
1063    #[test]
1064    fn test_complex_nesting() {
1065        let rule = MD005ListIndent::default();
1066        let content = "\
1067* Level 1
1068  * Level 2
1069    * Level 3
1070  * Back to 2
1071    1. Ordered 3
1072    2. Still 3
1073* Back to 1";
1074        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1075        let result = rule.check(&ctx).unwrap();
1076        assert!(result.is_empty());
1077    }
1078
1079    #[test]
1080    fn test_invalid_complex_nesting() {
1081        let rule = MD005ListIndent::default();
1082        let content = "\
1083* Level 1
1084   * Level 2
1085     * Level 3
1086   * Back to 2
1087      1. Ordered 3
1088     2. Still 3
1089* Back to 1";
1090        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1091        let result = rule.check(&ctx).unwrap();
1092        // Lines 5-6 have inconsistent indentation (6 vs 5 spaces) for the same level
1093        assert_eq!(result.len(), 1);
1094        assert!(
1095            result[0].message.contains("Expected indentation of 5 spaces, found 6")
1096                || result[0].message.contains("Expected indentation of 6 spaces, found 5")
1097        );
1098    }
1099
1100    #[test]
1101    fn test_with_lint_context() {
1102        let rule = MD005ListIndent::default();
1103
1104        // Test with consistent list indentation
1105        let content = "* Item 1\n* Item 2\n  * Nested item\n  * Another nested item";
1106        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1107        let result = rule.check(&ctx).unwrap();
1108        assert!(result.is_empty());
1109
1110        // Test with inconsistent list indentation
1111        let content = "* Item 1\n* Item 2\n * Nested item\n  * Another nested item";
1112        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1113        let result = rule.check(&ctx).unwrap();
1114        assert!(!result.is_empty()); // Should have at least one warning
1115
1116        // Test with different level indentation issues
1117        let content = "* Item 1\n  * Nested item\n * Another nested item with wrong indent";
1118        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1119        let result = rule.check(&ctx).unwrap();
1120        assert!(!result.is_empty()); // Should have at least one warning
1121    }
1122
1123    // Additional comprehensive tests
1124    #[test]
1125    fn test_list_with_continuations() {
1126        let rule = MD005ListIndent::default();
1127        let content = "\
1128* Item 1
1129  This is a continuation
1130  of the first item
1131  * Nested item
1132    with its own continuation
1133* Item 2";
1134        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1135        let result = rule.check(&ctx).unwrap();
1136        assert!(result.is_empty());
1137    }
1138
1139    #[test]
1140    fn test_list_in_blockquote() {
1141        let rule = MD005ListIndent::default();
1142        let content = "\
1143> * Item 1
1144>   * Nested 1
1145>   * Nested 2
1146> * Item 2";
1147        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1148        let result = rule.check(&ctx).unwrap();
1149
1150        // Blockquoted lists should have correct indentation within the blockquote context
1151        assert!(
1152            result.is_empty(),
1153            "Expected no warnings for correctly indented blockquote list, got: {result:?}"
1154        );
1155    }
1156
1157    #[test]
1158    fn test_list_with_code_blocks() {
1159        let rule = MD005ListIndent::default();
1160        let content = "\
1161* Item 1
1162  ```
1163  code block
1164  ```
1165  * Nested item
1166* Item 2";
1167        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1168        let result = rule.check(&ctx).unwrap();
1169        assert!(result.is_empty());
1170    }
1171
1172    #[test]
1173    fn test_list_with_tabs() {
1174        let rule = MD005ListIndent::default();
1175        // Tab at line start = 4 spaces = indented code per CommonMark, NOT a nested list
1176        // MD010 catches hard tabs, MD005 checks nested list indent consistency
1177        // This test now uses actual nested lists with mixed indentation
1178        let content = "* Item 1\n   * Wrong indent (3 spaces)\n  * Correct indent (2 spaces)";
1179        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1180        let result = rule.check(&ctx).unwrap();
1181        // Should detect inconsistent indentation (3 spaces vs 2 spaces)
1182        assert!(!result.is_empty());
1183    }
1184
1185    #[test]
1186    fn test_inconsistent_at_same_level() {
1187        let rule = MD005ListIndent::default();
1188        let content = "\
1189* Item 1
1190  * Nested 1
1191  * Nested 2
1192   * Wrong indent for same level
1193  * Nested 3";
1194        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1195        let result = rule.check(&ctx).unwrap();
1196        assert!(!result.is_empty());
1197        // Should flag the inconsistent item
1198        assert!(result.iter().any(|w| w.line == 4));
1199    }
1200
1201    #[test]
1202    fn test_zero_indent_top_level() {
1203        let rule = MD005ListIndent::default();
1204        // Use concat to preserve the leading space
1205        let content = concat!(" * Wrong indent\n", "* Correct\n", "  * Nested");
1206        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1207        let result = rule.check(&ctx).unwrap();
1208
1209        // Should flag the indented top-level item
1210        assert!(!result.is_empty());
1211        assert!(result.iter().any(|w| w.line == 1));
1212    }
1213
1214    #[test]
1215    fn test_fix_preserves_content() {
1216        let rule = MD005ListIndent::default();
1217        let content = "\
1218* Item with **bold** and *italic*
1219 * Wrong indent with `code`
1220   * Also wrong with [link](url)";
1221        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1222        let fixed = rule.fix(&ctx).unwrap();
1223        assert!(fixed.contains("**bold**"));
1224        assert!(fixed.contains("*italic*"));
1225        assert!(fixed.contains("`code`"));
1226        assert!(fixed.contains("[link](url)"));
1227    }
1228
1229    #[test]
1230    fn test_deeply_nested_lists() {
1231        let rule = MD005ListIndent::default();
1232        let content = "\
1233* L1
1234  * L2
1235    * L3
1236      * L4
1237        * L5
1238          * L6";
1239        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1240        let result = rule.check(&ctx).unwrap();
1241        assert!(result.is_empty());
1242    }
1243
1244    #[test]
1245    fn test_fix_multiple_issues() {
1246        let rule = MD005ListIndent::default();
1247        let content = "\
1248* Item 1
1249 * Wrong 1
1250   * Wrong 2
1251    * Wrong 3
1252  * Correct
1253   * Wrong 4";
1254        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1255        let fixed = rule.fix(&ctx).unwrap();
1256        // Should fix to consistent indentation
1257        let lines: Vec<&str> = fixed.lines().collect();
1258        assert_eq!(lines[0], "* Item 1");
1259        // All level 2 items should have same indent
1260        assert!(lines[1].starts_with("  * ") || lines[1].starts_with("* "));
1261    }
1262
1263    #[test]
1264    fn test_performance_large_document() {
1265        let rule = MD005ListIndent::default();
1266        let mut content = String::new();
1267        for i in 0..100 {
1268            content.push_str(&format!("* Item {i}\n"));
1269            content.push_str(&format!("  * Nested {i}\n"));
1270        }
1271        let ctx = LintContext::new(&content, crate::config::MarkdownFlavor::Standard, None);
1272        let result = rule.check(&ctx).unwrap();
1273        assert!(result.is_empty());
1274    }
1275
1276    #[test]
1277    fn test_column_positions() {
1278        let rule = MD005ListIndent::default();
1279        let content = " * Wrong indent";
1280        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1281        let result = rule.check(&ctx).unwrap();
1282        assert_eq!(result.len(), 1);
1283        assert_eq!(result[0].column, 1, "Expected column 1, got {}", result[0].column);
1284        assert_eq!(
1285            result[0].end_column, 2,
1286            "Expected end_column 2, got {}",
1287            result[0].end_column
1288        );
1289    }
1290
1291    #[test]
1292    fn test_should_skip() {
1293        let rule = MD005ListIndent::default();
1294
1295        // Empty content should skip
1296        let ctx = LintContext::new("", crate::config::MarkdownFlavor::Standard, None);
1297        assert!(rule.should_skip(&ctx));
1298
1299        // Content without lists should skip
1300        let ctx = LintContext::new("Just plain text", crate::config::MarkdownFlavor::Standard, None);
1301        assert!(rule.should_skip(&ctx));
1302
1303        // Content with lists should not skip
1304        let ctx = LintContext::new("* List item", crate::config::MarkdownFlavor::Standard, None);
1305        assert!(!rule.should_skip(&ctx));
1306
1307        let ctx = LintContext::new("1. Ordered list", crate::config::MarkdownFlavor::Standard, None);
1308        assert!(!rule.should_skip(&ctx));
1309    }
1310
1311    #[test]
1312    fn test_should_skip_validation() {
1313        let rule = MD005ListIndent::default();
1314        let content = "* List item";
1315        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1316        assert!(!rule.should_skip(&ctx));
1317
1318        let content = "No lists here";
1319        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1320        assert!(rule.should_skip(&ctx));
1321    }
1322
1323    #[test]
1324    fn test_edge_case_single_space_indent() {
1325        let rule = MD005ListIndent::default();
1326        let content = "\
1327* Item 1
1328 * Single space - wrong
1329  * Two spaces - correct";
1330        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1331        let result = rule.check(&ctx).unwrap();
1332        // Both the single space and two space items get warnings
1333        // because they establish inconsistent indentation at the same level
1334        assert_eq!(result.len(), 2);
1335        assert!(result.iter().any(|w| w.line == 2 && w.message.contains("found 1")));
1336    }
1337
1338    #[test]
1339    fn test_edge_case_three_space_indent() {
1340        let rule = MD005ListIndent::default();
1341        let content = "\
1342* Item 1
1343   * Three spaces - first establishes pattern
1344  * Two spaces - inconsistent with established pattern";
1345        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1346        let result = rule.check(&ctx).unwrap();
1347        // First-established indent (3) is the expected value
1348        // Line 3 with 2 spaces is inconsistent with the pattern
1349        // (Verified with markdownlint-cli: line 3 gets MD005, line 2 gets MD007)
1350        assert_eq!(result.len(), 1);
1351        assert!(result.iter().any(|w| w.line == 3 && w.message.contains("found 2")));
1352    }
1353
1354    #[test]
1355    fn test_nested_bullets_under_numbered_items() {
1356        let rule = MD005ListIndent::default();
1357        let content = "\
13581. **Active Directory/LDAP**
1359   - User authentication and directory services
1360   - LDAP for user information and validation
1361
13622. **Oracle Unified Directory (OUD)**
1363   - Extended user directory services
1364   - Verification of project account presence and changes";
1365        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1366        let result = rule.check(&ctx).unwrap();
1367        // Should have no warnings - 3 spaces is correct for bullets under numbered items
1368        assert!(
1369            result.is_empty(),
1370            "Expected no warnings for bullets with 3 spaces under numbered items, got: {result:?}"
1371        );
1372    }
1373
1374    #[test]
1375    fn test_nested_bullets_under_numbered_items_wrong_indent() {
1376        let rule = MD005ListIndent::default();
1377        let content = "\
13781. **Active Directory/LDAP**
1379  - Wrong: only 2 spaces
1380   - Correct: 3 spaces";
1381        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1382        let result = rule.check(&ctx).unwrap();
1383        // Should flag one of them as inconsistent
1384        assert_eq!(
1385            result.len(),
1386            1,
1387            "Expected 1 warning, got {}. Warnings: {:?}",
1388            result.len(),
1389            result
1390        );
1391        // Either line 2 or line 3 should be flagged for inconsistency
1392        assert!(
1393            result
1394                .iter()
1395                .any(|w| (w.line == 2 && w.message.contains("found 2"))
1396                    || (w.line == 3 && w.message.contains("found 3")))
1397        );
1398    }
1399
1400    #[test]
1401    fn test_regular_nested_bullets_still_work() {
1402        let rule = MD005ListIndent::default();
1403        let content = "\
1404* Top level
1405  * Second level (2 spaces is correct for bullets under bullets)
1406    * Third level (4 spaces)";
1407        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1408        let result = rule.check(&ctx).unwrap();
1409        // Should have no warnings - regular bullet nesting still uses 2-space increments
1410        assert!(
1411            result.is_empty(),
1412            "Expected no warnings for regular bullet nesting, got: {result:?}"
1413        );
1414    }
1415
1416    #[test]
1417    fn test_fix_range_accuracy() {
1418        let rule = MD005ListIndent::default();
1419        let content = " * Wrong indent";
1420        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1421        let result = rule.check(&ctx).unwrap();
1422        assert_eq!(result.len(), 1);
1423
1424        let fix = result[0].fix.as_ref().unwrap();
1425        // Fix should replace the single space with nothing (0 indent for level 1)
1426        assert_eq!(fix.replacement, "");
1427    }
1428
1429    #[test]
1430    fn test_four_space_indent_pattern() {
1431        let rule = MD005ListIndent::default();
1432        let content = "\
1433* Item 1
1434    * Item 2 with 4 spaces
1435        * Item 3 with 8 spaces
1436    * Item 4 with 4 spaces";
1437        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1438        let result = rule.check(&ctx).unwrap();
1439        // MD005 should accept consistent 4-space pattern
1440        assert!(
1441            result.is_empty(),
1442            "MD005 should accept consistent 4-space indentation pattern, got {} warnings",
1443            result.len()
1444        );
1445    }
1446
1447    #[test]
1448    fn test_issue_64_scenario() {
1449        // Test the exact scenario from issue #64
1450        let rule = MD005ListIndent::default();
1451        let content = "\
1452* Top level item
1453    * Sub item with 4 spaces (as configured in MD007)
1454        * Nested sub item with 8 spaces
1455    * Another sub item with 4 spaces
1456* Another top level";
1457
1458        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1459        let result = rule.check(&ctx).unwrap();
1460
1461        // MD005 should accept consistent 4-space pattern
1462        assert!(
1463            result.is_empty(),
1464            "MD005 should accept 4-space indentation when that's the pattern being used. Got {} warnings",
1465            result.len()
1466        );
1467    }
1468
1469    #[test]
1470    fn test_continuation_content_scenario() {
1471        let rule = MD005ListIndent::default();
1472        let content = "\
1473- **Changes to how the Python version is inferred** ([#16319](example))
1474
1475    In previous versions of Ruff, you could specify your Python version with:
1476
1477    - The `target-version` option in a `ruff.toml` file
1478    - The `project.requires-python` field in a `pyproject.toml` file";
1479
1480        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1481
1482        let result = rule.check(&ctx).unwrap();
1483
1484        // Should not flag continuation content lists as inconsistent
1485        assert!(
1486            result.is_empty(),
1487            "MD005 should not flag continuation content lists, got {} warnings: {:?}",
1488            result.len(),
1489            result
1490        );
1491    }
1492
1493    #[test]
1494    fn test_multiple_continuation_lists_scenario() {
1495        let rule = MD005ListIndent::default();
1496        let content = "\
1497- **Changes to how the Python version is inferred** ([#16319](example))
1498
1499    In previous versions of Ruff, you could specify your Python version with:
1500
1501    - The `target-version` option in a `ruff.toml` file
1502    - The `project.requires-python` field in a `pyproject.toml` file
1503
1504    In v0.10, config discovery has been updated to address this issue:
1505
1506    - If Ruff finds a `ruff.toml` file without a `target-version`, it will check
1507    - If Ruff finds a user-level configuration, the `requires-python` field will take precedence
1508    - If there is no config file, Ruff will search for the closest `pyproject.toml`";
1509
1510        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1511
1512        let result = rule.check(&ctx).unwrap();
1513
1514        // Should not flag continuation content lists as inconsistent
1515        assert!(
1516            result.is_empty(),
1517            "MD005 should not flag continuation content lists, got {} warnings: {:?}",
1518            result.len(),
1519            result
1520        );
1521    }
1522
1523    #[test]
1524    fn test_issue_115_sublist_after_code_block() {
1525        let rule = MD005ListIndent::default();
1526        let content = "\
15271. List item 1
1528
1529   ```rust
1530   fn foo() {}
1531   ```
1532
1533   Sublist:
1534
1535   - A
1536   - B
1537";
1538        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1539        let result = rule.check(&ctx).unwrap();
1540        // Sub-list items A and B are continuation content (3-space indent is correct)
1541        // because they appear after continuation content (code block and text) that is
1542        // indented at the parent's content_column (3 spaces)
1543        assert!(
1544            result.is_empty(),
1545            "Expected no warnings for sub-list after code block in list item, got {} warnings: {:?}",
1546            result.len(),
1547            result
1548        );
1549    }
1550
1551    #[test]
1552    fn test_edge_case_continuation_at_exact_boundary() {
1553        let rule = MD005ListIndent::default();
1554        // Text at EXACTLY parent_content_column (not greater than)
1555        let content = "\
1556* Item (content at column 2)
1557  Text at column 2 (exact boundary - continuation)
1558  * Sub at column 2";
1559        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1560        let result = rule.check(&ctx).unwrap();
1561        // The sub-list should be recognized as continuation content
1562        assert!(
1563            result.is_empty(),
1564            "Expected no warnings when text and sub-list are at exact parent content_column, got: {result:?}"
1565        );
1566    }
1567
1568    #[test]
1569    fn test_edge_case_unicode_in_continuation() {
1570        let rule = MD005ListIndent::default();
1571        let content = "\
1572* Parent
1573  Text with emoji 😀 and Unicode ñ characters
1574  * Sub-list should still work";
1575        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1576        let result = rule.check(&ctx).unwrap();
1577        // Unicode shouldn't break continuation detection
1578        assert!(
1579            result.is_empty(),
1580            "Expected no warnings with Unicode in continuation content, got: {result:?}"
1581        );
1582    }
1583
1584    #[test]
1585    fn test_edge_case_large_empty_line_gap() {
1586        let rule = MD005ListIndent::default();
1587        let content = "\
1588* Parent at line 1
1589  Continuation text
1590
1591
1592
1593  More continuation after many empty lines
1594
1595  * Child after gap
1596  * Another child";
1597        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1598        let result = rule.check(&ctx).unwrap();
1599        // Empty lines shouldn't break continuation detection
1600        assert!(
1601            result.is_empty(),
1602            "Expected no warnings with large gaps in continuation content, got: {result:?}"
1603        );
1604    }
1605
1606    #[test]
1607    fn test_edge_case_multiple_continuation_blocks_varying_indent() {
1608        let rule = MD005ListIndent::default();
1609        let content = "\
1610* Parent (content at column 2)
1611  First paragraph at column 2
1612    Indented quote at column 4
1613  Back to column 2
1614  * Sub-list at column 2";
1615        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1616        let result = rule.check(&ctx).unwrap();
1617        // Should handle varying indentation in continuation content
1618        assert!(
1619            result.is_empty(),
1620            "Expected no warnings with varying continuation indent, got: {result:?}"
1621        );
1622    }
1623
1624    #[test]
1625    fn test_edge_case_deep_nesting_no_continuation() {
1626        let rule = MD005ListIndent::default();
1627        let content = "\
1628* Parent
1629  * Immediate child (no continuation text before)
1630    * Grandchild
1631      * Great-grandchild
1632        * Great-great-grandchild
1633  * Another child at level 2";
1634        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1635        let result = rule.check(&ctx).unwrap();
1636        // Deep nesting without continuation content should work
1637        assert!(
1638            result.is_empty(),
1639            "Expected no warnings for deep nesting without continuation, got: {result:?}"
1640        );
1641    }
1642
1643    #[test]
1644    fn test_edge_case_blockquote_continuation_content() {
1645        let rule = MD005ListIndent::default();
1646        let content = "\
1647> * Parent in blockquote
1648>   Continuation in blockquote
1649>   * Sub-list in blockquote
1650>   * Another sub-list";
1651        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1652        let result = rule.check(&ctx).unwrap();
1653        // Blockquote continuation should work correctly
1654        assert!(
1655            result.is_empty(),
1656            "Expected no warnings for blockquote continuation, got: {result:?}"
1657        );
1658    }
1659
1660    #[test]
1661    fn test_edge_case_one_space_less_than_content_column() {
1662        let rule = MD005ListIndent::default();
1663        let content = "\
1664* Parent (content at column 2)
1665 Text at column 1 (one less than content_column - NOT continuation)
1666  * Child";
1667        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1668        let result = rule.check(&ctx).unwrap();
1669        // Text at column 1 should NOT be continuation (< parent_content_column)
1670        // This breaks the list context, so child should be treated as top-level
1671        // BUT since there's a parent at column 0, the child at column 2 is actually
1672        // a child of that parent, not continuation content
1673        // The test verifies the behavior is consistent
1674        assert!(
1675            result.is_empty() || !result.is_empty(),
1676            "Test should complete without panic"
1677        );
1678    }
1679
1680    #[test]
1681    fn test_edge_case_multiple_code_blocks_different_indentation() {
1682        let rule = MD005ListIndent::default();
1683        let content = "\
1684* Parent
1685  ```
1686  code at 2 spaces
1687  ```
1688    ```
1689    code at 4 spaces
1690    ```
1691  * Sub-list should not be confused";
1692        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1693        let result = rule.check(&ctx).unwrap();
1694        // Multiple code blocks shouldn't confuse continuation detection
1695        assert!(
1696            result.is_empty(),
1697            "Expected no warnings with multiple code blocks, got: {result:?}"
1698        );
1699    }
1700
1701    #[test]
1702    fn test_performance_very_large_document() {
1703        let rule = MD005ListIndent::default();
1704        let mut content = String::new();
1705
1706        // Create document with 1000 list items with continuation content
1707        for i in 0..1000 {
1708            content.push_str(&format!("* Item {i}\n"));
1709            content.push_str(&format!("  * Nested {i}\n"));
1710            if i % 10 == 0 {
1711                content.push_str("  Some continuation text\n");
1712            }
1713        }
1714
1715        let ctx = LintContext::new(&content, crate::config::MarkdownFlavor::Standard, None);
1716
1717        // Should complete quickly with O(n) optimization
1718        let start = std::time::Instant::now();
1719        let result = rule.check(&ctx).unwrap();
1720        let elapsed = start.elapsed();
1721
1722        assert!(result.is_empty());
1723        println!("Processed 1000 list items in {elapsed:?}");
1724        // Before optimization (O(n²)): ~seconds
1725        // After optimization (O(n)): ~milliseconds
1726        assert!(
1727            elapsed.as_secs() < 1,
1728            "Should complete in under 1 second, took {elapsed:?}"
1729        );
1730    }
1731
1732    #[test]
1733    fn test_ordered_list_variable_marker_width() {
1734        // Ordered lists with items 1-9 (marker "N. " = 3 chars) and 10+
1735        // (marker "NN. " = 4 chars) should have sublists aligned with parent content.
1736        // Sublists under items 1-9 are at column 3, sublists under 10+ are at column 4.
1737        // This should NOT trigger MD005 warnings.
1738        let rule = MD005ListIndent::default();
1739        let content = "\
17401. One
1741   - One
1742   - Two
17432. Two
1744   - One
17453. Three
1746   - One
17474. Four
1748   - One
17495. Five
1750   - One
17516. Six
1752   - One
17537. Seven
1754   - One
17558. Eight
1756   - One
17579. Nine
1758   - One
175910. Ten
1760    - One";
1761        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1762        let result = rule.check(&ctx).unwrap();
1763        assert!(
1764            result.is_empty(),
1765            "Expected no warnings for ordered list with variable marker widths, got: {result:?}"
1766        );
1767    }
1768
1769    #[test]
1770    fn test_ordered_list_inconsistent_siblings() {
1771        // MD005 checks that siblings (items under the same parent) have consistent indentation
1772        let rule = MD005ListIndent::default();
1773        let content = "\
17741. Item one
1775   - First sublist at 3 spaces
1776  - Second sublist at 2 spaces (inconsistent)
1777   - Third sublist at 3 spaces";
1778        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1779        let result = rule.check(&ctx).unwrap();
1780        // The item at column 2 should be flagged (inconsistent with siblings at column 3)
1781        assert_eq!(
1782            result.len(),
1783            1,
1784            "Expected 1 warning for inconsistent sibling indent, got: {result:?}"
1785        );
1786        assert!(result[0].message.contains("Expected indentation of 3"));
1787    }
1788
1789    #[test]
1790    fn test_ordered_list_single_sublist_no_warning() {
1791        // A single sublist item under a parent should not trigger MD005
1792        // (nothing to compare for consistency)
1793        let rule = MD005ListIndent::default();
1794        let content = "\
179510. Item ten
1796   - Only sublist at 3 spaces";
1797        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1798        let result = rule.check(&ctx).unwrap();
1799        // No warning because there's only one sibling
1800        assert!(
1801            result.is_empty(),
1802            "Expected no warnings for single sublist item, got: {result:?}"
1803        );
1804    }
1805
1806    #[test]
1807    fn test_sublists_grouped_by_parent_content_column() {
1808        // Sublists should be grouped by parent content column.
1809        // Items 9 and 10 have different marker widths (3 vs 4 chars), so their sublists
1810        // are at different column positions. Each group should be checked independently.
1811        let rule = MD005ListIndent::default();
1812        let content = "\
18139. Item nine
1814   - First sublist at 3 spaces
1815   - Second sublist at 3 spaces
1816   - Third sublist at 3 spaces
181710. Item ten
1818    - First sublist at 4 spaces
1819    - Second sublist at 4 spaces
1820    - Third sublist at 4 spaces";
1821        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1822        let result = rule.check(&ctx).unwrap();
1823        // No warnings: sublists under item 9 are at col 3 (consistent within group),
1824        // sublists under item 10 are at col 4 (consistent within their group)
1825        assert!(
1826            result.is_empty(),
1827            "Expected no warnings for sublists grouped by parent, got: {result:?}"
1828        );
1829    }
1830
1831    #[test]
1832    fn test_inconsistent_indent_within_parent_group() {
1833        // Test that inconsistency WITHIN a parent group is still detected
1834        let rule = MD005ListIndent::default();
1835        let content = "\
183610. Item ten
1837    - First sublist at 4 spaces
1838   - Second sublist at 3 spaces (inconsistent!)
1839    - Third sublist at 4 spaces";
1840        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1841        let result = rule.check(&ctx).unwrap();
1842        // The item at 3 spaces should be flagged (inconsistent with siblings at 4 spaces)
1843        assert_eq!(
1844            result.len(),
1845            1,
1846            "Expected 1 warning for inconsistent indent within parent group, got: {result:?}"
1847        );
1848        assert!(result[0].line == 3);
1849        assert!(result[0].message.contains("Expected indentation of 4"));
1850    }
1851
1852    #[test]
1853    fn test_blockquote_nested_list_fix_preserves_blockquote_prefix() {
1854        // Test that MD005 fix preserves blockquote prefix instead of removing it
1855        // This was a bug where ">  * item" would be fixed to "* item" (blockquote removed)
1856        // instead of "> * item" (blockquote preserved)
1857        use crate::rule::Rule;
1858
1859        let rule = MD005ListIndent::default();
1860        let content = ">  * Federation sender blacklists are now persisted.";
1861        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1862        let result = rule.check(&ctx).unwrap();
1863
1864        assert_eq!(result.len(), 1, "Expected 1 warning for extra indent");
1865
1866        // The fix should preserve the blockquote prefix
1867        assert!(result[0].fix.is_some(), "Should have a fix");
1868        let fixed = rule.fix(&ctx).expect("Fix should succeed");
1869
1870        // Verify blockquote prefix is preserved
1871        assert!(
1872            fixed.starts_with("> "),
1873            "Fixed content should start with blockquote prefix '> ', got: {fixed:?}"
1874        );
1875        assert!(
1876            !fixed.starts_with("* "),
1877            "Fixed content should NOT start with just '* ' (blockquote removed), got: {fixed:?}"
1878        );
1879        assert_eq!(
1880            fixed.trim(),
1881            "> * Federation sender blacklists are now persisted.",
1882            "Fixed content should be '> * Federation sender...' with single space after >"
1883        );
1884    }
1885
1886    #[test]
1887    fn test_nested_blockquote_list_fix_preserves_prefix() {
1888        // Test nested blockquotes (>> syntax)
1889        use crate::rule::Rule;
1890
1891        let rule = MD005ListIndent::default();
1892        let content = ">>   * Nested blockquote list item";
1893        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1894        let result = rule.check(&ctx).unwrap();
1895
1896        if !result.is_empty() {
1897            let fixed = rule.fix(&ctx).expect("Fix should succeed");
1898            // Should preserve the nested blockquote prefix
1899            assert!(
1900                fixed.contains(">>") || fixed.contains("> >"),
1901                "Fixed content should preserve nested blockquote prefix, got: {fixed:?}"
1902            );
1903        }
1904    }
1905}