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