Skip to main content

rumdl_lib/rules/
md077_list_continuation_indent.rs

1//!
2//! Rule MD077: List continuation content indentation
3//!
4//! See [docs/md077.md](../../docs/md077.md) for full documentation, configuration, and examples.
5
6use std::ops::ControlFlow;
7
8use serde::{Deserialize, Serialize};
9
10use crate::lint_context::{LineInfo, LintContext};
11use crate::rule::{Fix, LintError, LintResult, LintWarning, Rule, RuleCategory, Severity};
12
13mod md077_config;
14use md077_config::MD077Config;
15
16/// How strictly MD077 enforces continuation-line indentation.
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
18#[serde(rename_all = "kebab-case")]
19pub enum ContinuationStyle {
20    /// Permit any continuation indent in `[0, content_column]` (CommonMark lazy
21    /// continuation). Only over-indentation and content that escapes the list
22    /// after a blank line are flagged.
23    #[default]
24    Any,
25    /// Require every continuation line to align to the item's content column.
26    /// Tight under-indented lazy continuation (which `any` permits) is also
27    /// flagged and snapped up to the content column.
28    Aligned,
29}
30
31/// Rule MD077: List continuation content indentation
32///
33/// In both tight continuation (no blank line) and loose continuation (after a
34/// blank line), content must not be **over-indented** beyond the item's content
35/// column. Additionally, in loose continuation content must not be
36/// **under-indented** below the content column (W+N rule), or it falls out of the
37/// list; tight under-indent is valid CommonMark lazy continuation and is left
38/// alone. Content indented to the content column + 4 or more is an indented code
39/// block, not continuation, and is not flagged.
40///
41/// Under the MkDocs flavor, a minimum of 4 spaces is enforced for ordered list
42/// items to satisfy Python-Markdown.
43///
44/// With `style = "aligned"`, tight under-indented continuation is additionally
45/// flagged and aligned to the content column (mdformat parity).
46#[derive(Clone, Default)]
47pub struct MD077ListContinuationIndent {
48    config: MD077Config,
49}
50
51impl MD077ListContinuationIndent {
52    /// Construct the rule with an explicit continuation style. `Default`
53    /// (`ContinuationStyle::Any`) preserves the historical behavior.
54    pub fn new(style: ContinuationStyle) -> Self {
55        Self {
56            config: MD077Config { style, indent: None },
57        }
58    }
59
60    pub fn from_config_struct(config: MD077Config) -> Self {
61        Self { config }
62    }
63}
64
65impl MD077ListContinuationIndent {
66    /// Width of a GFM task checkbox prefix including its trailing space:
67    /// `[ ] `, `[x] `, or `[X] ` — always exactly 4 bytes.
68    const TASK_CHECKBOX_PREFIX_LEN: usize = 4;
69
70    /// Returns true if the item line starts a GFM task list item, i.e. its
71    /// content column begins with `[ ] `, `[x] `, or `[X] `. The trailing
72    /// space is part of the match — `- [ ]` with no body is an empty list
73    /// item, not a task.
74    ///
75    /// Task items have a second, conventionally-accepted continuation column
76    /// at `content_col + 4` (aligned after the checkbox). MD013 reflow
77    /// produces this column for wrapped task lines, so MD077 has to accept
78    /// it to avoid a fix loop with MD013.
79    ///
80    /// `content_col` is a byte offset into `line`, not a visual column. The
81    /// CommonMark list parser produces byte-offset content columns, and the
82    /// checkbox prefix `[ ] ` is pure ASCII, so this byte-level comparison
83    /// is correct. Leading indent mixing tabs and spaces is irrelevant here
84    /// because `content_col` already points past any leading whitespace.
85    fn is_task_list_item(line: &str, content_col: usize) -> bool {
86        line.as_bytes()
87            .get(content_col..content_col + Self::TASK_CHECKBOX_PREFIX_LEN)
88            .is_some_and(|window| matches!(window, b"[ ] " | b"[x] " | b"[X] "))
89    }
90
91    /// Check if a trimmed line is a block-level construct (not list continuation).
92    fn is_block_level_construct(trimmed: &str) -> bool {
93        // Footnote definition: [^label]:
94        if trimmed.starts_with("[^") && trimmed.contains("]:") {
95            return true;
96        }
97        // Abbreviation definition: *[text]:
98        if trimmed.starts_with("*[") && trimmed.contains("]:") {
99            return true;
100        }
101        // Reference link definition: [label]: url
102        // Must start with [ but not be a regular link, footnote, or abbreviation
103        if trimmed.starts_with('[') && !trimmed.starts_with("[^") && trimmed.contains("]: ") {
104            return true;
105        }
106        false
107    }
108
109    /// Check if a trimmed line is a fenced code block delimiter (opener or closer).
110    fn is_code_fence(trimmed: &str) -> bool {
111        let bytes = trimmed.as_bytes();
112        if bytes.len() < 3 {
113            return false;
114        }
115        let ch = bytes[0];
116        (ch == b'`' || ch == b'~') && bytes[1] == ch && bytes[2] == ch
117    }
118
119    /// Check if a trimmed line starts with a list marker (*, -, +, or ordered).
120    /// Used to avoid flagging deeply indented list items that the parser doesn't
121    /// recognize as list items (e.g., with indent=8 configured in MD007).
122    fn starts_with_list_marker(trimmed: &str) -> bool {
123        let bytes = trimmed.as_bytes();
124        match bytes.first() {
125            Some(b'*' | b'-' | b'+') => bytes.get(1).is_some_and(|&b| b == b' ' || b == b'\t'),
126            Some(b'0'..=b'9') => {
127                let rest = trimmed.trim_start_matches(|c: char| c.is_ascii_digit());
128                rest.starts_with(". ") || rest.starts_with(") ")
129            }
130            _ => false,
131        }
132    }
133
134    /// Given the line number of a fenced code block opener, walk forward and
135    /// return the line number of the matching closer. Returns the opener itself
136    /// if no following line is in the code block (degenerate single-line block).
137    fn find_fence_closer(ctx: &LintContext, opener_line: usize) -> usize {
138        let mut closer_line = opener_line;
139        for peek in (opener_line + 1)..=ctx.lines.len() {
140            let Some(peek_info) = ctx.line_info(peek) else { break };
141            if peek_info.in_code_block {
142                closer_line = peek;
143            } else {
144                break;
145            }
146        }
147        closer_line
148    }
149
150    /// Build an atomic fix that reindents a fenced code block from its opener
151    /// through its matching closer.
152    ///
153    /// - **Opener and closer** are moved to `required` (the list item's
154    ///   content column, which is what MD077 actually flagged).
155    /// - **Interior lines** move by the same delta as the fence and are then
156    ///   clamped to `required`. Shifting by the delta preserves each line's
157    ///   offset from the fence, so the code block's internal structure (JSON
158    ///   nesting, indented sub-blocks) survives the move; the clamp
159    ///   guarantees fence pairing, since every non-blank line in the block
160    ///   ends at column ≥ `required` and the block stays inside the list
161    ///   item's scope after the fix.
162    ///
163    /// Only used for the under-indent direction (`required > opener_actual`);
164    /// over-indented fences are intentionally left untouched (see the
165    /// over-indent pass in `check`), so there is no down-shift case to handle.
166    ///
167    /// Why a compound fix rather than three independent fixes? MD077 and
168    /// MD031 run in the same iterative fix loop. If we only moved the
169    /// delimiters, an intermediate state would have mismatched
170    /// opener/closer indentation and MD031 would misread the block as
171    /// unpaired, injecting stray blank lines (issue #574).
172    ///
173    /// Why clamp the shift rather than shift alone? A bare delta shift is
174    /// not idempotent: interior starting below the list scope (e.g., col 0
175    /// under an opener at col 2 that needs to move to col 3) lands at col 1,
176    /// still below the scope, and the next MD077 pass re-flags it and snaps
177    /// it to `required`. The clamp reaches that end state in a single pass.
178    /// Clamping alone is not enough either: it pins every interior line at
179    /// or above `required` to its original column while the fence rises, so
180    /// the block's interior indentation shrinks by the delta and one-space
181    /// nesting disappears entirely.
182    ///
183    /// Leading tabs are normalized to spaces: CommonMark expands a tab to
184    /// the next column that's a multiple of 4, so simply prepending spaces
185    /// before a tab would let the tab snap back and cancel the shift. We
186    /// replace the whole leading-whitespace byte range with spaces.
187    fn build_compound_fence_fix(
188        ctx: &LintContext,
189        opener_line: usize,
190        closer_line: usize,
191        opener_actual: usize,
192        required: usize,
193    ) -> Option<Fix> {
194        if required <= opener_actual {
195            return None;
196        }
197        let opener_info = ctx.line_info(opener_line)?;
198        let closer_info = ctx.line_info(closer_line)?;
199
200        let fix_start = opener_info.byte_offset;
201        let fix_end = closer_info.byte_offset + closer_info.byte_len;
202
203        let mut replacement = String::new();
204        for i in opener_line..=closer_line {
205            let info = ctx.line_info(i)?;
206            if i > opener_line {
207                replacement.push('\n');
208            }
209            let line = info.content(ctx.content);
210            if info.is_blank {
211                // Blank lines have no content to shift; preserve verbatim.
212                replacement.push_str(line);
213            } else {
214                let new_visual = if i == opener_line || i == closer_line {
215                    required
216                } else {
217                    (info.visual_indent + (required - opener_actual)).max(required)
218                };
219                for _ in 0..new_visual {
220                    replacement.push(' ');
221                }
222                replacement.push_str(&line[info.indent..]);
223            }
224        }
225
226        Some(Fix::new(fix_start..fix_end, replacement))
227    }
228
229    /// Walk the continuation lines owned by a single list item, invoking
230    /// `per_line` for each *in-scope, non-blank, non-nested, non-skipped*
231    /// line with its pre-computed visual column and loose/tight state.
232    ///
233    /// This is the **single source of truth** for MD077's item-scope
234    /// traversal: both the sibling-column pre-pass and the main check loop
235    /// route through this method so their termination semantics cannot
236    /// drift. The callback sees only lines the rule actually needs to
237    /// reason about; it can return `ControlFlow::Break` for early exit.
238    ///
239    /// Termination conditions (applied before the callback fires):
240    /// - Headings and horizontal rules end the item unconditionally.
241    /// - After a blank line, content at or below the marker column has
242    ///   escaped the item; further lines are not delivered.
243    ///
244    /// Skipped silently (do not fire the callback):
245    /// - Blank lines (toggle `saw_blank`).
246    /// - Nested list items (reset `saw_blank`, track their content column).
247    /// - Lines inside any still-open nested item's scope: the walk keeps a
248    ///   stack of open nested (marker, content) columns, so a continuation
249    ///   of a middle nesting level defers to that level's own walk instead
250    ///   of being claimed by a shallower ancestor.
251    /// - Reference/footnote/abbreviation definitions and similar block
252    ///   constructs that aren't list continuation.
253    /// - Lines that `should_skip_line` rejects (code-block interior etc.).
254    fn walk_item_continuation<F>(
255        ctx: &LintContext,
256        item_line: usize,
257        range_end: usize,
258        marker_col: usize,
259        mut per_line: F,
260    ) where
261        F: FnMut(&ContinuationLine<'_>) -> ControlFlow<()>,
262    {
263        let mut saw_blank = false;
264        let mut saw_nested = false;
265        // Stack of open nested items as (marker_column, content_column),
266        // outermost first. A single scalar is not enough: with 3+ levels a
267        // continuation of a middle level sits below the innermost content
268        // column but at-or-past its own level's, and must still be skipped
269        // here so the owning level's walk evaluates it.
270        let mut nested_stack: Vec<(usize, usize)> = Vec::new();
271
272        for line_num in (item_line + 1)..=range_end {
273            let Some(info) = ctx.line_info(line_num) else {
274                continue;
275            };
276
277            let trimmed = info.content(ctx.content).trim_start();
278
279            if Self::should_skip_line(info, trimmed) {
280                continue;
281            }
282
283            if info.is_blank {
284                saw_blank = true;
285                continue;
286            }
287
288            if let Some(ref li) = info.list_item {
289                if li.marker_column > marker_col {
290                    // A sibling-or-shallower marker closes every nested item at
291                    // or past its column before this one opens.
292                    while nested_stack.last().is_some_and(|&(m, _)| m >= li.marker_column) {
293                        nested_stack.pop();
294                    }
295                    nested_stack.push((li.marker_column, li.content_column));
296                    // Sticky: once a nested child appears, every later line is a
297                    // (lazy) continuation of the deeper item, which owns it under
298                    // CommonMark. Ancestors must defer so the deepest item claims
299                    // and aligns it.
300                    saw_nested = true;
301                } else {
302                    nested_stack.clear();
303                }
304                saw_blank = false;
305                continue;
306            }
307
308            if info.heading.is_some() || info.is_horizontal_rule {
309                break;
310            }
311
312            if Self::is_block_level_construct(trimmed) {
313                continue;
314            }
315
316            let col = info.visual_indent;
317
318            // The line escapes every nested item whose content column it sits
319            // below; if any nested item stays open, the line is that item's
320            // continuation and its own walk owns the judgement.
321            while nested_stack.last().is_some_and(|&(_, c)| c > col) {
322                nested_stack.pop();
323            }
324            if !nested_stack.is_empty() {
325                continue;
326            }
327
328            if saw_blank && col <= marker_col {
329                break;
330            }
331
332            let line = ContinuationLine {
333                line_num,
334                info,
335                trimmed,
336                actual: col,
337                saw_blank,
338                saw_nested,
339            };
340            if per_line(&line).is_break() {
341                break;
342            }
343        }
344    }
345
346    /// Whether the item's owned continuation range holds a *latent* structural
347    /// construct: a list marker or table line the parser has not yet promoted to
348    /// its own block, but would once an earlier continuation line is reindented.
349    /// Such a promotion re-attributes the following lines to a different block
350    /// (different content column) and breaks single-pass idempotency, so the
351    /// caller skips the whole item when this returns true.
352    ///
353    /// A setext underline counts too: as a lazy continuation the parser reads a
354    /// `=`/`-` run as paragraph text, but reindenting the prose line above it
355    /// into the item turns the pair into a heading.
356    ///
357    /// Unlike [`walk_item_continuation`], this scans the full owned range and
358    /// does NOT stop at a heading or horizontal rule. A heading partway through
359    /// the continuation can itself be parse-unstable: an empty backtick run, for
360    /// instance, is read as a setext heading, and reindenting an earlier line
361    /// dissolves it and promotes a list marker that sits past it. Stopping at the
362    /// heading would hide that marker from this check and let the reindent break
363    /// idempotency. Lines already parsed as list items are real nested children,
364    /// not latent, and are handled by the walk's `saw_nested` logic instead.
365    fn item_range_has_latent_structure(ctx: &LintContext, item_line: usize, range_end: usize) -> bool {
366        (item_line + 1..=range_end).any(|line_num| {
367            ctx.line_info(line_num).is_some_and(|info| {
368                if info.is_blank || info.list_item.is_some() {
369                    return false;
370                }
371                let trimmed = info.content(ctx.content).trim_start();
372                !Self::should_skip_line(info, trimmed)
373                    && (Self::starts_with_list_marker(trimmed)
374                        || crate::utils::skip_context::is_table_line(trimmed)
375                        || Self::is_latent_setext_underline(ctx, line_num, trimmed))
376            })
377        })
378    }
379
380    /// Whether reindenting the line above this one would turn the pair into a
381    /// setext heading. An underline that is a lazy continuation is paragraph
382    /// text, so the parser reports no heading and the walk runs straight past it.
383    ///
384    /// Only paragraph text can become the heading's text line, so an underline
385    /// under an ATX heading, a thematic break or another underline is latent
386    /// nothing and must not hold the whole item back. The line above has to pass
387    /// both readings: `is_paragraph_context` speaks for the block it sits in,
388    /// which is what tells a closing code fence from the prose it looks like,
389    /// and the text predicate speaks for the line's own markers, which is what
390    /// tells an empty list item from the text it holds none of. A blank line
391    /// needs no test of its own: blank means it holds nothing once its markers
392    /// are stripped, which is the first thing the text predicate rejects.
393    fn is_latent_setext_underline(ctx: &LintContext, line_num: usize, trimmed: &str) -> bool {
394        crate::lint_context::is_setext_underline_content(trimmed)
395            && ctx.line_info(line_num - 1).is_some_and(|prev| {
396                prev.is_paragraph_context() && crate::lint_context::is_paragraph_text_line(prev.content(ctx.content))
397            })
398    }
399
400    /// Scan an item's owned range and report whether any *other* continuation
401    /// line in the item uses the content column or the post-checkbox column.
402    ///
403    /// Used exclusively for tie-breaking the auto-fix target when an
404    /// over-indented line is exactly equidistant from `content_col` and
405    /// `task_col`. In that case the author's intent is ambiguous, so we
406    /// snap to whichever valid column *they're already using* elsewhere in
407    /// the same item. When neither or both columns are in use, the caller
408    /// falls back to a canonical default.
409    fn sibling_column_usage(
410        ctx: &LintContext,
411        item_line: usize,
412        range_end: usize,
413        marker_col: usize,
414        content_col: usize,
415        task_col: usize,
416    ) -> (bool, bool) {
417        let mut uses_content = false;
418        let mut uses_task = false;
419
420        Self::walk_item_continuation(ctx, item_line, range_end, marker_col, |line| {
421            if line.actual == content_col {
422                uses_content = true;
423            }
424            if line.actual == task_col {
425                uses_task = true;
426            }
427            if uses_content && uses_task {
428                ControlFlow::Break(())
429            } else {
430                ControlFlow::Continue(())
431            }
432        });
433
434        (uses_content, uses_task)
435    }
436
437    /// Compute the auto-fix target for an over-indented continuation line.
438    /// Snaps to the nearer of the two valid columns (content_col / task_col)
439    /// for task items, and on an exact tie uses sibling-column context to
440    /// pick whichever column the author is already using elsewhere in this
441    /// item. Non-task items always snap to `required`.
442    fn compute_fix_target(
443        actual: usize,
444        required: usize,
445        task_col: Option<usize>,
446        uses_content_col: bool,
447        uses_task_col: bool,
448    ) -> usize {
449        let Some(t) = task_col else { return required };
450        match actual.abs_diff(t).cmp(&actual.abs_diff(required)) {
451            std::cmp::Ordering::Less => t,
452            std::cmp::Ordering::Greater => required,
453            std::cmp::Ordering::Equal => match (uses_task_col, uses_content_col) {
454                (true, false) => t,
455                _ => required,
456            },
457        }
458    }
459
460    /// Check if a line should be skipped (inside code, HTML, frontmatter, etc.)
461    ///
462    /// Code block *content* is skipped, but fence opener/closer lines are not —
463    /// their indentation matters for list continuation in MkDocs.
464    ///
465    /// Footnote-definition lines (the `[^id]:` line and its indented body) are
466    /// skipped too: a footnote body that follows a list is its own block, not
467    /// list continuation, so its indentation is MD068's concern, not MD077's.
468    /// Treating it as continuation produced false over-indent warnings and a
469    /// damaging auto-fix that reindented the body and then tripped MD068.
470    fn should_skip_line(info: &crate::lint_context::LineInfo, trimmed: &str) -> bool {
471        if info.in_code_block && !Self::is_code_fence(trimmed) {
472            return true;
473        }
474        info.in_front_matter
475            || info.in_footnote_definition
476            || info.in_html_block
477            || info.in_html_comment
478            || info.in_mdx_comment
479            || info.in_mkdocstrings
480            || info.in_esm_block
481            || info.in_math_block
482            || info.in_admonition
483            || info.in_content_tab
484            || info.in_pymdown_block
485            || info.in_definition_list
486            || info.in_mkdocs_html_markdown
487            || info.in_kramdown_extension_block
488    }
489
490    /// Build the warning for an over-indented continuation line. The fix is a
491    /// single-line rewrite of the leading whitespace to `fix_target`.
492    ///
493    /// The over-indent pass never delivers fenced-code lines here (it skips
494    /// anything `in_code_block`), so this builder does not need the compound
495    /// fence handling that `build_under_indent_warning` uses: moving an
496    /// over-indented fence's delimiters without its body would corrupt the code
497    /// content, so over-indented fenced blocks are deliberately left as-is.
498    fn build_over_indent_warning(
499        ctx: &LintContext,
500        line: &ContinuationLine<'_>,
501        fix_target: usize,
502        message: String,
503    ) -> LintWarning {
504        let line_content = line.info.content(ctx.content);
505        let fix_start = line.info.byte_offset;
506        let fix_end = fix_start + line.info.indent;
507        LintWarning {
508            rule_name: Some("MD077".to_string()),
509            line: line.line_num,
510            column: 1,
511            end_line: line.line_num,
512            end_column: line_content.chars().count() + 1,
513            message,
514            severity: Severity::Warning,
515            fix: Some(Fix::new(fix_start..fix_end, " ".repeat(fix_target))),
516        }
517    }
518
519    /// Build the warning for a loose-mode under-indented continuation line.
520    /// When the line is the opener of a fenced code block, emit a compound
521    /// fix that reindents opener + interior + closer atomically so MD031
522    /// doesn't see a transiently-broken fence pair (see #574).
523    ///
524    /// Returns the warning plus, when the fix is compound, the closer
525    /// line number so the caller can mark it flagged (preventing the
526    /// main loop from double-flagging the closer as its own under-indent
527    /// case). Keeping the "also flag this line" signal out of band keeps
528    /// this function pure — it reads from `ctx` only and returns a
529    /// plain value.
530    fn build_under_indent_warning(
531        ctx: &LintContext,
532        line: &ContinuationLine<'_>,
533        required: usize,
534        message: String,
535    ) -> UnderIndentOutcome {
536        let line_content = line.info.content(ctx.content);
537        let is_fence_opener = line.info.in_code_block
538            && Self::is_code_fence(line.trimmed)
539            && ctx.line_info(line.line_num - 1).is_none_or(|p| !p.in_code_block);
540
541        let (fix, warn_end_line, warn_end_column, compound_closer) = if is_fence_opener {
542            let closer_line = Self::find_fence_closer(ctx, line.line_num);
543            let fix = Self::build_compound_fence_fix(ctx, line.line_num, closer_line, line.actual, required);
544            let end_column = ctx
545                .line_info(closer_line)
546                .map_or(line_content.chars().count() + 1, |ci| {
547                    ci.content(ctx.content).chars().count() + 1
548                });
549            let extra_flag = (closer_line != line.line_num).then_some(closer_line);
550            (fix, closer_line, end_column, extra_flag)
551        } else {
552            let fix_start = line.info.byte_offset;
553            let fix_end = fix_start + line.info.indent;
554            let fix = Some(Fix::new(fix_start..fix_end, " ".repeat(required)));
555            (fix, line.line_num, line_content.chars().count() + 1, None)
556        };
557
558        UnderIndentOutcome {
559            warning: LintWarning {
560                rule_name: Some("MD077".to_string()),
561                line: line.line_num,
562                column: 1,
563                end_line: warn_end_line,
564                end_column: warn_end_column,
565                message,
566                severity: Severity::Warning,
567                fix,
568            },
569            also_flag_line: compound_closer,
570        }
571    }
572}
573
574/// A continuation line yielded by `walk_item_continuation`. Bundles the
575/// per-line facts both checker branches need so helper functions don't
576/// balloon their argument lists.
577struct ContinuationLine<'a> {
578    line_num: usize,
579    info: &'a LineInfo,
580    trimmed: &'a str,
581    actual: usize,
582    saw_blank: bool,
583    /// True once a nested child item has appeared earlier in this item's scope.
584    /// Subsequent lines are lazy continuation of the deeper item (which owns
585    /// them under CommonMark), so an ancestor must not claim them.
586    saw_nested: bool,
587}
588
589/// Result of `build_under_indent_warning`. Carries both the warning and,
590/// when the fix is compound (fence opener → promote-to-required over the
591/// whole block), the closer line so the caller can record it as already
592/// handled. This keeps the warning builder free of external mutation.
593struct UnderIndentOutcome {
594    warning: LintWarning,
595    also_flag_line: Option<usize>,
596}
597
598impl Rule for MD077ListContinuationIndent {
599    fn name(&self) -> &'static str {
600        "MD077"
601    }
602
603    fn description(&self) -> &'static str {
604        "List continuation content indentation"
605    }
606
607    fn check(&self, ctx: &LintContext) -> LintResult {
608        if ctx.content.is_empty() {
609            return Ok(Vec::new());
610        }
611
612        let strict_indent = ctx.flavor.requires_strict_list_indent();
613        let total_lines = ctx.lines.len();
614        let mut warnings = Vec::new();
615        let mut flagged_lines = std::collections::HashSet::new();
616
617        // Collect all list item lines sorted, with their content_column,
618        // marker_column, and — if the item is a GFM task — its post-checkbox
619        // column. Precomputing task_col here (instead of re-reading line_info
620        // inside the hot inner loop) keeps the per-item cost O(1).
621        //
622        // We need the owned range to extend past block.end_line because the
623        // parser excludes under-indented continuation from the block, and
624        // MD077 specifically has to evaluate those escaped lines.
625        let mut items: Vec<(usize, usize, usize, Option<usize>)> = Vec::new();
626        for block in &ctx.list_blocks {
627            for &item_line in &block.item_lines {
628                if let Some(info) = ctx.line_info(item_line)
629                    && let Some(ref li) = info.list_item
630                {
631                    // Lists inside blockquotes are out of scope for MD077. Their
632                    // content column is measured on the raw line, so it is
633                    // meaningless for a lazy continuation sitting outside the
634                    // quote: scoping them lets two items with different content
635                    // columns each claim such a line and reindent it the other
636                    // way on alternating passes.
637                    if info.blockquote.is_some() {
638                        continue;
639                    }
640                    let line = info.content(ctx.content);
641                    let task_col = Self::is_task_list_item(line, li.content_column)
642                        .then_some(li.content_column + Self::TASK_CHECKBOX_PREFIX_LEN);
643                    items.push((item_line, li.marker_column, li.content_column, task_col));
644                }
645            }
646        }
647        items.sort_unstable();
648        items.dedup_by_key(|&mut (ln, _, _, _)| ln);
649
650        // Precompute each item's owned-range end. The range ends at the line
651        // before the next item whose marker column is <= this item's (its next
652        // sibling or an ancestor), or the last line if none follows.
653        //
654        // The direct "scan forward for the next marker_col <= mine" is O(n^2) on
655        // a monotonically deepening list, where no later item ever qualifies so
656        // every item scans to the end. A monotonic stack (nearest
657        // smaller-or-equal marker column to the right) computes all range ends in
658        // one linear pass. Walking right-to-left, pop every stacked item whose
659        // marker column is strictly greater than the current one (they can never
660        // be the sibling/ancestor of anything further left that is <= current),
661        // leaving the nearest qualifying item on top.
662        let mut range_ends = vec![total_lines; items.len()];
663        let mut stack: Vec<usize> = Vec::new();
664        for i in (0..items.len()).rev() {
665            let marker_col = items[i].1;
666            while let Some(&top) = stack.last() {
667                if items[top].1 > marker_col {
668                    stack.pop();
669                } else {
670                    break;
671                }
672            }
673            range_ends[i] = stack.last().map_or(total_lines, |&j| items[j].0 - 1);
674            stack.push(i);
675        }
676
677        // Precompute each item's required indent and owned line range so both
678        // passes below scope identically.
679        let scoped: Vec<(usize, usize, usize, Option<usize>, usize, usize)> = items
680            .iter()
681            .enumerate()
682            .map(|(item_idx, &(item_line, marker_col, content_col, task_col))| {
683                // A configured indent replaces the content column, but under a
684                // flavor that requires strict list indentation it may only raise
685                // the 4-space minimum. That minimum is a compatibility floor
686                // rather than a style default: below it the renderer stops
687                // treating the line as continuation, so honoring a smaller
688                // configured value would make `fix` rewrite documents into a
689                // form the flavor's renderer no longer reads as part of the item.
690                let required = match self.config.indent {
691                    Some(indent) if strict_indent => (marker_col + indent).max(4),
692                    Some(indent) => marker_col + indent,
693                    None if strict_indent => content_col.max(4),
694                    None => content_col,
695                };
696                (
697                    item_line,
698                    marker_col,
699                    content_col,
700                    task_col,
701                    required,
702                    range_ends[item_idx],
703                )
704            })
705            .collect();
706
707        // Precompute which lines can ever reach the per-line continuation
708        // callback. `walk_item_continuation` unconditionally skips (regardless of
709        // which item owns the line) blank lines, list-item lines, headings,
710        // horizontal rules, `should_skip_line` lines, and block-level constructs;
711        // only the lines that survive all of those can be flagged. An item whose
712        // owned range contains none of them produces no warning, so both passes
713        // can skip it without walking the range at all.
714        //
715        // This keeps a document of purely deeply-nested list items linear: such a
716        // document has no continuation prose, so every item skips its walk
717        // instead of re-scanning the whole tail (which was O(n^2) in the item
718        // count). Documents that do have continuation prose are unaffected - the
719        // guard only ever skips items that could not have produced a warning.
720        let prose_candidate_lines: Vec<usize> = (1..=total_lines)
721            .filter(|&line_num| {
722                let Some(info) = ctx.line_info(line_num) else {
723                    return false;
724                };
725                let trimmed = info.content(ctx.content).trim_start();
726                !Self::should_skip_line(info, trimmed)
727                    && !info.is_blank
728                    && info.list_item.is_none()
729                    && info.heading.is_none()
730                    && !info.is_horizontal_rule
731                    && !Self::is_block_level_construct(trimmed)
732            })
733            .collect();
734        // True when a continuation candidate falls in `(after_line, range_end]`,
735        // i.e. the half-open range `walk_item_continuation` actually visits.
736        let range_has_prose_candidate = |after_line: usize, range_end: usize| -> bool {
737            let start = prose_candidate_lines.partition_point(|&l| l <= after_line);
738            prose_candidate_lines.get(start).is_some_and(|&l| l <= range_end)
739        };
740
741        // Pass 1 - under-indented continuation.
742        //
743        // Loose under-indent (after a blank line) sits below the content column
744        // and would escape the list item; it is always flagged. Tight
745        // under-indent is valid CommonMark lazy continuation, so it is left
746        // alone under `style = "any"`.
747        //
748        // Under `style = "aligned"`, tight under-indent is additionally flagged
749        // and snapped up to the content column. MD077 scopes past the parser's
750        // block end (to catch escaped loose content), and the parser absorbs
751        // col-0 blockquotes, fences, and tables that sit tight under a list item
752        // as lazy continuation. Reindenting those would change the rendered
753        // structure, so the tight-aligned branch guards two ways: it skips lines
754        // the parser classified in place (code, blockquote - see
755        // `confirmed_structure`), and it bails on an item whose continuation
756        // holds a construct the parser would re-parse once indentation changes
757        // (a latent list marker, table or setext underline - see
758        // `has_latent_structure`). Headings and horizontal rules already
759        // terminate the walk; reference/footnote/abbreviation definitions are
760        // skipped inside it. Erring toward skipping yields false negatives
761        // (safe) rather than structural false positives.
762        //
763        // This pass runs first so that a deeply nested item claims an ambiguous
764        // line - one that is under-indented for it yet over-indented for a
765        // shallower ancestor - before pass 2 can mis-attribute it to the
766        // ancestor as an over-indent and snap it the wrong way.
767        let aligned = self.config.style == ContinuationStyle::Aligned;
768        for &(item_line, marker_col, _content_col, _task_col, required, range_end) in &scoped {
769            // No continuation candidate in this item's range: it cannot produce a
770            // warning, so skip the walk (and the latent-structure scan) entirely.
771            if !range_has_prose_candidate(item_line, range_end) {
772                continue;
773            }
774            // "Latent structure": a line in this item's continuation that the
775            // parser has NOT yet promoted to a list item or table, but that
776            // would be once an earlier continuation line is reindented. Such a
777            // promotion re-attributes the following lines to a different block
778            // (different content column) and breaks single-pass idempotency, so
779            // reindenting anything in the item is unsafe and the whole item is
780            // skipped. This is a *prediction* of how the parser would re-parse
781            // after a fix, which is why it stays a heuristic (markers, tables and
782            // setext underlines are the constructs the parser absorbs as lazy
783            // continuation rather than flagging on the line itself; code and
784            // blockquotes are classified in place and handled per line below).
785            // Well-formed prose continuation matches none, so this only bails
786            // on pathological input (a safe false negative). Real nested items
787            // never reach the callback (the walk handles them via `saw_nested`).
788            let has_latent_structure = aligned && Self::item_range_has_latent_structure(ctx, item_line, range_end);
789            // Which setting produced `required` decides what the message may
790            // claim. The content column carries a structural consequence (below
791            // it the content leaves the item) and the strict-flavor minimum
792            // carries a renderer requirement, but a configured indent above
793            // both is a house style, so it is reported as one.
794            let from_configured_indent = self.config.indent.is_some_and(|indent| marker_col + indent == required);
795            Self::walk_item_continuation(ctx, item_line, range_end, marker_col, |line| {
796                let actual = line.actual;
797                let under_indented = actual < required;
798                let loose_escape = line.saw_blank && under_indented;
799                // Don't reindent a line the parser classified in place as a code
800                // block (fenced or indented) or a blockquote: these are read
801                // straight from its per-line output, so the rule cannot drift
802                // from how rumdl parses the document. Headings and horizontal
803                // rules terminate the walk before the callback; latent list
804                // markers and tables are handled by `has_latent_structure`.
805                let confirmed_structure = line.info.in_code_block || line.info.blockquote.is_some();
806                let aligned_tight = aligned
807                    && !has_latent_structure
808                    && !line.saw_blank
809                    && !line.saw_nested
810                    && under_indented
811                    && !confirmed_structure;
812                if (loose_escape || aligned_tight) && flagged_lines.insert(line.line_num) {
813                    let message = if line.saw_blank {
814                        if from_configured_indent {
815                            format!(
816                                "Content after blank line in list item needs {required} spaces of \
817                                 indentation to match the configured indent (found {actual})",
818                            )
819                        } else if strict_indent {
820                            format!(
821                                "Content inside list item needs {required} spaces of indentation \
822                                 for MkDocs compatibility (found {actual})",
823                            )
824                        } else {
825                            format!(
826                                "Content after blank line in list item needs {required} spaces of \
827                                 indentation to remain part of the list (found {actual})",
828                            )
829                        }
830                    } else {
831                        format!("Continuation line under-indented (expected {required}, found {actual})")
832                    };
833                    let outcome = Self::build_under_indent_warning(ctx, line, required, message);
834                    if let Some(closer_line) = outcome.also_flag_line {
835                        flagged_lines.insert(closer_line);
836                    }
837                    warnings.push(outcome.warning);
838                }
839                ControlFlow::Continue(())
840            });
841        }
842
843        // Pass 2 - over-indented continuation (tight or loose): prose pushed
844        // past the content column is snapped back. Fenced code blocks are
845        // skipped here (`!in_code_block`): an over-indented fence is cosmetic
846        // (the code still renders), and reindenting only its delimiters - the
847        // body is skipped by `should_skip_line` - would alter the literal code
848        // content. Indented code blocks (content column + 4 or more) are also
849        // `in_code_block`, so a blank line before such a body does not exempt
850        // it from being recognized as code rather than over-indented prose.
851        for &(item_line, marker_col, content_col, task_col, required, range_end) in &scoped {
852            // No continuation candidate in this item's range: nothing to flag.
853            if !range_has_prose_candidate(item_line, range_end) {
854                continue;
855            }
856            // For task items, gather sibling-column usage once so the auto-fix
857            // can tie-break equidistant over-indents toward whichever valid
858            // column the author is already using.
859            let (uses_content_col, uses_task_col) = match task_col {
860                Some(t) => Self::sibling_column_usage(ctx, item_line, range_end, marker_col, content_col, t),
861                None => (false, false),
862            };
863
864            Self::walk_item_continuation(ctx, item_line, range_end, marker_col, |line| {
865                let actual = line.actual;
866                if actual > required
867                    && !line.info.in_code_block
868                    && Some(actual) != task_col
869                    && !Self::starts_with_list_marker(line.trimmed)
870                    && flagged_lines.insert(line.line_num)
871                {
872                    let fix_target =
873                        Self::compute_fix_target(actual, required, task_col, uses_content_col, uses_task_col);
874                    let message = match task_col {
875                        Some(t) => format!(
876                            "Continuation line over-indented \
877                             (expected {required} or {t}, found {actual})"
878                        ),
879                        None => {
880                            format!("Continuation line over-indented (expected {required}, found {actual})")
881                        }
882                    };
883                    warnings.push(Self::build_over_indent_warning(ctx, line, fix_target, message));
884                }
885                ControlFlow::Continue(())
886            });
887        }
888
889        // The two passes emit independently, so order by position before
890        // returning - callers and tests expect document order.
891        warnings.sort_by_key(|w| (w.line, w.column));
892
893        Ok(warnings)
894    }
895
896    fn fix(&self, ctx: &LintContext) -> Result<String, LintError> {
897        let warnings = self.check(ctx)?;
898        let warnings =
899            crate::utils::fix_utils::filter_warnings_by_inline_config(warnings, ctx.inline_config(), self.name());
900        if warnings.is_empty() {
901            return Ok(ctx.content.to_string());
902        }
903
904        // Sort fixes by byte position descending to apply from end to start
905        let mut fixes: Vec<Fix> = warnings.into_iter().filter_map(|w| w.fix).collect();
906        fixes.sort_by_key(|f| std::cmp::Reverse(f.range.start));
907
908        let mut content = ctx.content.to_string();
909        for fix in fixes {
910            if fix.range.start <= content.len() && fix.range.end <= content.len() {
911                content.replace_range(fix.range, &fix.replacement);
912            }
913        }
914
915        Ok(content)
916    }
917
918    fn category(&self) -> RuleCategory {
919        RuleCategory::List
920    }
921
922    fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
923        ctx.content.is_empty() || ctx.list_blocks.is_empty()
924    }
925
926    fn as_any(&self) -> &dyn std::any::Any {
927        self
928    }
929
930    crate::impl_rule_config_methods!(MD077Config);
931}
932
933#[cfg(test)]
934mod tests {
935    use super::*;
936    use crate::config::MarkdownFlavor;
937
938    fn check(content: &str) -> Vec<LintWarning> {
939        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
940        let rule = MD077ListContinuationIndent::default();
941        rule.check(&ctx).unwrap()
942    }
943
944    fn check_mkdocs(content: &str) -> Vec<LintWarning> {
945        let ctx = LintContext::new(content, MarkdownFlavor::MkDocs, None);
946        let rule = MD077ListContinuationIndent::default();
947        rule.check(&ctx).unwrap()
948    }
949
950    fn fix(content: &str) -> String {
951        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
952        let rule = MD077ListContinuationIndent::default();
953        rule.fix(&ctx).unwrap()
954    }
955
956    fn fix_mkdocs(content: &str) -> String {
957        let ctx = LintContext::new(content, MarkdownFlavor::MkDocs, None);
958        let rule = MD077ListContinuationIndent::default();
959        rule.fix(&ctx).unwrap()
960    }
961
962    fn aligned_rule() -> MD077ListContinuationIndent {
963        MD077ListContinuationIndent::new(ContinuationStyle::Aligned)
964    }
965
966    fn check_aligned(content: &str) -> Vec<LintWarning> {
967        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
968        aligned_rule().check(&ctx).unwrap()
969    }
970
971    fn check_aligned_mkdocs(content: &str) -> Vec<LintWarning> {
972        let ctx = LintContext::new(content, MarkdownFlavor::MkDocs, None);
973        aligned_rule().check(&ctx).unwrap()
974    }
975
976    fn fix_aligned(content: &str) -> String {
977        let ctx = LintContext::new(content, MarkdownFlavor::Standard, None);
978        aligned_rule().fix(&ctx).unwrap()
979    }
980
981    fn fix_aligned_quarto(content: &str) -> String {
982        let ctx = LintContext::new(content, MarkdownFlavor::Quarto, None);
983        aligned_rule().fix(&ctx).unwrap()
984    }
985
986    #[test]
987    fn aligned_idempotent_with_latent_marker_behind_unstable_heading() {
988        // Regression: an empty backtick run (`` ``\n`` ``) is parsed as a setext
989        // heading. That heading used to terminate the latent-structure walk
990        // before it reached the `- ` marker on the next line, so the marker was
991        // not detected as latent. Reindenting the `1. ` continuation then
992        // promoted `- ` to a real list item, producing a fresh under-indent
993        // violation on the second pass and breaking single-pass idempotency.
994        // The latent-structure scan now spans the full owned range, detects the
995        // marker, and leaves the (ambiguous) item alone.
996        let input = "1. \n``\n``\n- \n``";
997        let once = fix_aligned_quarto(input);
998        let twice = fix_aligned_quarto(&once);
999        assert_eq!(once, twice, "MD077 aligned fix must be idempotent (Quarto)");
1000    }
1001
1002    #[test]
1003    fn latent_underline_needs_paragraph_text_above_it() {
1004        // Only paragraph text can become a setext heading's text line, so an
1005        // underline sitting under an ATX heading or a thematic break is latent
1006        // nothing and must not hold the whole item back.
1007        for (label, content) in [
1008            ("ATX heading", "- item\nwrap\n# Heading\n===\n"),
1009            ("thematic break", "- item\nwrap\n***\n===\n"),
1010            ("HTML block", "- item\nwrap\n<div>\n===\n"),
1011        ] {
1012            assert_eq!(
1013                check_aligned(content).len(),
1014                1,
1015                "{label}: reindenting cannot make a setext heading here, so the under-indent is reportable"
1016            );
1017        }
1018
1019        // A closing code fence is the case only the block flag can answer: its
1020        // text is indistinguishable from prose, and nothing but the block it
1021        // sits in says the line cannot be a setext heading's text.
1022        assert_eq!(
1023            check_aligned("- item\nwrap\n```\ncode\n```\n===\n").len(),
1024            2,
1025            "a closing fence is not paragraph text, so both under-indents are reportable"
1026        );
1027
1028        // An empty list item is the case only the line's own markers can answer:
1029        // it is written inside a paragraph context and holds no text at all, and
1030        // an underline below it underlines nothing. A lone `-` is not this case
1031        // (under a paragraph it is itself an underline), so each marker carries
1032        // the space that opens an item holding nothing.
1033        for (label, content) in [
1034            ("empty bullet", "- item\n wrap\n > - \n ===\n"),
1035            ("empty ordered item", "- item\n wrap\n > 1. \n ===\n"),
1036            ("empty item in a nested quote", "- item\n wrap\n > > - \n ===\n"),
1037        ] {
1038            assert_eq!(
1039                check_aligned(content).len(),
1040                1,
1041                "{label}: an item holding no text cannot become a heading's text line"
1042            );
1043        }
1044
1045        // A blank line above the underline is the same answer for the same
1046        // reason, and needs no test of its own in the rule: stripped of its
1047        // markers it holds nothing, which is what the text predicate rejects.
1048        for (label, content) in [
1049            ("bare blank line", "- item\n wrap\n\n ===\n"),
1050            ("blank line in a quote", "- item\n wrap\n >\n ===\n"),
1051            ("quoted whitespace", "- item\n wrap\n >   \n ===\n"),
1052        ] {
1053            assert_eq!(
1054                check_aligned(content).len(),
1055                2,
1056                "{label}: nothing above the underline can become a heading's text line"
1057            );
1058        }
1059
1060        // Paired control: prose above the underline IS latent structure, because
1061        // reindenting the item would give the underline the content column and
1062        // turn the pair into a real heading.
1063        assert!(
1064            check_aligned("- item\nwrap\ntext\n===\n").is_empty(),
1065            "prose above the underline is latent structure, so the item is left alone"
1066        );
1067    }
1068
1069    #[test]
1070    fn aligned_idempotent_with_lazy_continuation_out_of_a_blockquote() {
1071        // Regression: a lazy continuation that follows a blockquote-nested list
1072        // item gained one space of indent on every pass, so the formatter never
1073        // converged.
1074        let input = "- \n> *\n> a\n``";
1075        let once = fix_aligned(input);
1076        let twice = fix_aligned(&once);
1077        assert_eq!(once, twice, "MD077 aligned fix must be idempotent");
1078    }
1079
1080    // ── Tight continuation (no blank line) ─────────────────────────────
1081
1082    #[test]
1083    fn tight_lazy_continuation_zero_indent_not_flagged() {
1084        // Zero-indent lazy continuation is valid CommonMark
1085        let content = "- Item\ncontinuation\n";
1086        assert!(check(content).is_empty());
1087    }
1088
1089    #[test]
1090    fn tight_continuation_correct_indent_not_flagged() {
1091        // Correctly indented tight continuation (aligns with content column)
1092        let content = "1. Item\n   continuation\n";
1093        assert!(check(content).is_empty());
1094    }
1095
1096    #[test]
1097    fn tight_continuation_over_indented_ordered() {
1098        // "1. " = 3 chars, but continuation has 4 spaces
1099        let content = "1. This is a list item with multiple lines.\n    The second line is over-indented.\n";
1100        let warnings = check(content);
1101        assert_eq!(warnings.len(), 1);
1102        assert_eq!(warnings[0].line, 2);
1103        assert!(warnings[0].message.contains("over-indented"));
1104    }
1105
1106    #[test]
1107    fn tight_continuation_over_indented_unordered() {
1108        // "- " = 2 chars, but continuation has 3 spaces
1109        let content = "- Item\n   over-indented\n";
1110        let warnings = check(content);
1111        assert_eq!(warnings.len(), 1);
1112        assert_eq!(warnings[0].line, 2);
1113    }
1114
1115    #[test]
1116    fn tight_continuation_multiple_over_indented_lines() {
1117        let content = "1. Item\n    line one\n    line two\n    line three\n";
1118        let warnings = check(content);
1119        assert_eq!(warnings.len(), 3);
1120    }
1121
1122    #[test]
1123    fn tight_continuation_mixed_correct_and_over() {
1124        let content = "1. Item\n   correct\n    over-indented\n   correct again\n";
1125        let warnings = check(content);
1126        assert_eq!(warnings.len(), 1);
1127        assert_eq!(warnings[0].line, 3);
1128    }
1129
1130    #[test]
1131    fn tight_continuation_nested_over_indented() {
1132        // L2 "- " at column 2, content_column = 4. Continuation at 5 is over-indented for L2.
1133        let content = "- L1\n  - L2\n     over-indented continuation of L2\n";
1134        let warnings = check(content);
1135        assert_eq!(warnings.len(), 1);
1136        assert_eq!(warnings[0].line, 3);
1137        // Must report expected=4 (L2's content_col), not expected=2 (L1's)
1138        assert!(warnings[0].message.contains("expected 4"));
1139        assert!(warnings[0].message.contains("found 5"));
1140    }
1141
1142    #[test]
1143    fn tight_continuation_nested_correct_indent_not_flagged() {
1144        // Continuation at 4 spaces is correct for L2 (content_col=4). Must NOT be
1145        // flagged as over-indented relative to L1 (content_col=2).
1146        let content = "- L1\n  - L2\n    correctly indented continuation of L2\n";
1147        assert!(check(content).is_empty());
1148    }
1149
1150    #[test]
1151    fn fix_tight_continuation_nested_over_indented() {
1152        // Fix should reduce to 4 spaces (L2's content_col), not 2 (L1's)
1153        let content = "- L1\n  - L2\n     over-indented continuation of L2\n";
1154        let fixed = fix(content);
1155        assert_eq!(fixed, "- L1\n  - L2\n    over-indented continuation of L2\n");
1156    }
1157
1158    #[test]
1159    fn tight_continuation_under_indented_not_flagged() {
1160        // 2 spaces instead of 3 for "1. " — under-indented, not over-indented.
1161        // Valid lazy continuation in CommonMark, so not flagged.
1162        let content = "1. Item\n  under-indented\n";
1163        assert!(check(content).is_empty());
1164    }
1165
1166    #[test]
1167    fn tight_continuation_tab_over_indented() {
1168        // A tab expands to 4 visual columns, which exceeds content_col=2 for "- "
1169        let content = "- Item\n\tover-indented\n";
1170        let warnings = check(content);
1171        assert_eq!(warnings.len(), 1);
1172    }
1173
1174    #[test]
1175    fn fix_tight_continuation_over_indented_ordered() {
1176        let content = "1. This is a list item with multiple lines.\n    The second line is over-indented.\n";
1177        let fixed = fix(content);
1178        assert_eq!(
1179            fixed,
1180            "1. This is a list item with multiple lines.\n   The second line is over-indented.\n"
1181        );
1182    }
1183
1184    #[test]
1185    fn fix_tight_continuation_over_indented_unordered() {
1186        let content = "- Item\n   over-indented\n";
1187        let fixed = fix(content);
1188        assert_eq!(fixed, "- Item\n  over-indented\n");
1189    }
1190
1191    #[test]
1192    fn fix_tight_continuation_multiple_lines() {
1193        let content = "1. Item\n    line one\n    line two\n";
1194        let fixed = fix(content);
1195        assert_eq!(fixed, "1. Item\n   line one\n   line two\n");
1196    }
1197
1198    #[test]
1199    fn tight_continuation_mkdocs_4space_ordered_not_flagged() {
1200        // MkDocs requires max(3, 4) = 4 spaces for "1. " items.
1201        // 4-space tight continuation is correct, not over-indented.
1202        let content = "1. Item\n    continuation\n";
1203        assert!(check_mkdocs(content).is_empty());
1204    }
1205
1206    #[test]
1207    fn tight_continuation_mkdocs_5space_ordered_flagged() {
1208        // 5 spaces exceeds the MkDocs required indent of 4
1209        let content = "1. Item\n     over-indented\n";
1210        let warnings = check_mkdocs(content);
1211        assert_eq!(warnings.len(), 1);
1212        assert!(warnings[0].message.contains("expected 4"));
1213        assert!(warnings[0].message.contains("found 5"));
1214    }
1215
1216    #[test]
1217    fn fix_tight_continuation_mkdocs_over_indented() {
1218        let content = "1. Item\n     over-indented\n";
1219        let fixed = fix_mkdocs(content);
1220        assert_eq!(fixed, "1. Item\n    over-indented\n");
1221    }
1222
1223    #[test]
1224    fn tight_continuation_deeply_indented_list_markers_not_flagged() {
1225        // Deeply indented list markers (e.g., indent=8 in MD007) may not be
1226        // recognized as list items by the parser. MD077 must not flag them.
1227        let content = "* Level 0\n        * Level 1\n                * Level 2\n";
1228        assert!(check(content).is_empty());
1229    }
1230
1231    #[test]
1232    fn tight_continuation_ordered_marker_not_flagged() {
1233        // Indented ordered list marker should not be flagged
1234        let content = "- Parent\n      1. Child item\n";
1235        assert!(check(content).is_empty());
1236    }
1237
1238    // ── Unordered list: correct indent after blank ────────────────────
1239
1240    #[test]
1241    fn unordered_correct_indent_no_warning() {
1242        let content = "- Item\n\n  continuation\n";
1243        assert!(check(content).is_empty());
1244    }
1245
1246    #[test]
1247    fn unordered_partial_indent_warns() {
1248        // Content with some indent (above marker column) but less than
1249        // content_column is likely an indentation mistake.
1250        let content = "- Item\n\n continuation\n";
1251        let warnings = check(content);
1252        assert_eq!(warnings.len(), 1);
1253        assert_eq!(warnings[0].line, 3);
1254        assert!(warnings[0].message.contains("2 spaces"));
1255        assert!(warnings[0].message.contains("found 1"));
1256    }
1257
1258    #[test]
1259    fn unordered_zero_indent_is_new_paragraph() {
1260        // Content at 0 indent after a top-level list is a new paragraph, not
1261        // under-indented continuation.
1262        let content = "- Item\n\ncontinuation\n";
1263        assert!(check(content).is_empty());
1264    }
1265
1266    // ── Ordered list: CommonMark W+N ──────────────────────────────────
1267
1268    #[test]
1269    fn ordered_3space_correct_commonmark() {
1270        // "1. " is 3 chars, content_column = 3
1271        let content = "1. Item\n\n   continuation\n";
1272        assert!(check(content).is_empty());
1273    }
1274
1275    #[test]
1276    fn ordered_2space_under_indent_commonmark() {
1277        let content = "1. Item\n\n  continuation\n";
1278        let warnings = check(content);
1279        assert_eq!(warnings.len(), 1);
1280        assert!(warnings[0].message.contains("3 spaces"));
1281        assert!(warnings[0].message.contains("found 2"));
1282    }
1283
1284    // ── Multi-digit ordered markers ───────────────────────────────────
1285
1286    #[test]
1287    fn multi_digit_marker_correct() {
1288        // "10. " is 4 chars, content_column = 4
1289        let content = "10. Item\n\n    continuation\n";
1290        assert!(check(content).is_empty());
1291    }
1292
1293    #[test]
1294    fn multi_digit_marker_under_indent() {
1295        let content = "10. Item\n\n   continuation\n";
1296        let warnings = check(content);
1297        assert_eq!(warnings.len(), 1);
1298        assert!(warnings[0].message.contains("4 spaces"));
1299    }
1300
1301    // ── MkDocs flavor: 4-space minimum ────────────────────────────────
1302
1303    #[test]
1304    fn mkdocs_3space_ordered_warns() {
1305        // In MkDocs mode, 3-space indent on "1. " is not enough
1306        let content = "1. Item\n\n   continuation\n";
1307        let warnings = check_mkdocs(content);
1308        assert_eq!(warnings.len(), 1);
1309        assert!(warnings[0].message.contains("4 spaces"));
1310        assert!(warnings[0].message.contains("MkDocs"));
1311    }
1312
1313    #[test]
1314    fn mkdocs_4space_ordered_no_warning() {
1315        let content = "1. Item\n\n    continuation\n";
1316        assert!(check_mkdocs(content).is_empty());
1317    }
1318
1319    #[test]
1320    fn mkdocs_unordered_2space_ok() {
1321        // Unordered "- " has content_column = 2; max(2, 4) = 4 in mkdocs
1322        let content = "- Item\n\n    continuation\n";
1323        assert!(check_mkdocs(content).is_empty());
1324    }
1325
1326    #[test]
1327    fn mkdocs_unordered_2space_warns() {
1328        // "- " has content_column 2; MkDocs requires max(2,4) = 4
1329        let content = "- Item\n\n  continuation\n";
1330        let warnings = check_mkdocs(content);
1331        assert_eq!(warnings.len(), 1);
1332        assert!(warnings[0].message.contains("4 spaces"));
1333    }
1334
1335    // ── Auto-fix ──────────────────────────────────────────────────────
1336
1337    #[test]
1338    fn fix_unordered_indent() {
1339        // Partial indent (above marker column, below content column) gets fixed
1340        let content = "- Item\n\n continuation\n";
1341        let fixed = fix(content);
1342        assert_eq!(fixed, "- Item\n\n  continuation\n");
1343    }
1344
1345    #[test]
1346    fn fix_ordered_indent() {
1347        let content = "1. Item\n\n continuation\n";
1348        let fixed = fix(content);
1349        assert_eq!(fixed, "1. Item\n\n   continuation\n");
1350    }
1351
1352    #[test]
1353    fn fix_mkdocs_indent() {
1354        let content = "1. Item\n\n   continuation\n";
1355        let fixed = fix_mkdocs(content);
1356        assert_eq!(fixed, "1. Item\n\n    continuation\n");
1357    }
1358
1359    // ── Nested lists: only flag continuation, not sub-items ───────────
1360
1361    #[test]
1362    fn nested_list_items_not_flagged() {
1363        let content = "- Parent\n\n  - Child\n";
1364        assert!(check(content).is_empty());
1365    }
1366
1367    #[test]
1368    fn nested_list_zero_indent_is_new_paragraph() {
1369        // Content at 0 indent ends the list, not continuation
1370        let content = "- Parent\n  - Child\n\ncontinuation of parent\n";
1371        assert!(check(content).is_empty());
1372    }
1373
1374    #[test]
1375    fn nested_list_partial_indent_flagged() {
1376        // Content with partial indent (above parent marker, below content col)
1377        let content = "- Parent\n  - Child\n\n continuation of parent\n";
1378        let warnings = check(content);
1379        assert_eq!(warnings.len(), 1);
1380        assert!(warnings[0].message.contains("2 spaces"));
1381    }
1382
1383    // ── Code blocks inside items ─────────────────────────────────────
1384
1385    #[test]
1386    fn code_block_correctly_indented_no_warning() {
1387        // Fence lines and content all at correct indent for "- " (content_column = 2)
1388        let content = "- Item\n\n  ```\n  code\n  ```\n";
1389        assert!(check(content).is_empty());
1390    }
1391
1392    #[test]
1393    fn code_fence_under_indented_warns() {
1394        // Fence opener has 1-space indent, but "- " needs 2.
1395        // Only the opener is flagged — its compound fix also covers the
1396        // interior content and the matching closer (see issue #574).
1397        let content = "- Item\n\n ```\n code\n ```\n";
1398        let warnings = check(content);
1399        assert_eq!(warnings.len(), 1);
1400        assert_eq!(warnings[0].line, 3);
1401    }
1402
1403    #[test]
1404    fn code_fence_under_indented_ordered_mkdocs() {
1405        // Ordered list in MkDocs: "1. " needs max(3, 4) = 4 spaces
1406        // Fence at 3 spaces is correct for CommonMark but wrong for MkDocs
1407        let content = "1. Item\n\n   ```toml\n   key = \"value\"\n   ```\n";
1408        assert!(check(content).is_empty()); // Standard mode: 3 is fine
1409        let warnings = check_mkdocs(content);
1410        assert_eq!(warnings.len(), 1); // MkDocs: opener's compound fix covers the whole block
1411        assert_eq!(warnings[0].line, 3);
1412        assert!(warnings[0].message.contains("4 spaces"));
1413        assert!(warnings[0].message.contains("MkDocs"));
1414    }
1415
1416    #[test]
1417    fn code_fence_tilde_under_indented() {
1418        let content = "- Item\n\n ~~~\n code\n ~~~\n";
1419        let warnings = check(content);
1420        assert_eq!(warnings.len(), 1); // Tilde fences: single compound-fix warning on opener
1421        assert_eq!(warnings[0].line, 3);
1422    }
1423
1424    // ── Multiple blank lines ──────────────────────────────────────────
1425
1426    #[test]
1427    fn multiple_blank_lines_zero_indent_is_new_paragraph() {
1428        // Even with multiple blanks, 0-indent content is a new paragraph
1429        let content = "- Item\n\n\ncontinuation\n";
1430        assert!(check(content).is_empty());
1431    }
1432
1433    #[test]
1434    fn multiple_blank_lines_partial_indent_flags() {
1435        let content = "- Item\n\n\n continuation\n";
1436        let warnings = check(content);
1437        assert_eq!(warnings.len(), 1);
1438    }
1439
1440    // ── Empty items: no continuation to check ─────────────────────────
1441
1442    #[test]
1443    fn empty_item_no_warning() {
1444        let content = "- \n- Second\n";
1445        assert!(check(content).is_empty());
1446    }
1447
1448    // ── Multiple items, only some under-indented ──────────────────────
1449
1450    #[test]
1451    fn multiple_items_mixed_indent() {
1452        let content = "1. First\n\n   correct continuation\n\n2. Second\n\n  wrong continuation\n";
1453        let warnings = check(content);
1454        assert_eq!(warnings.len(), 1);
1455        assert_eq!(warnings[0].line, 7);
1456    }
1457
1458    // ── Task list items ───────────────────────────────────────────────
1459
1460    #[test]
1461    fn task_list_correct_indent() {
1462        // "- [ ] " = content_column is typically at col 6
1463        let content = "- [ ] Task\n\n      continuation\n";
1464        assert!(check(content).is_empty());
1465    }
1466
1467    // ── Frontmatter skipped ───────────────────────────────────────────
1468
1469    #[test]
1470    fn frontmatter_not_flagged() {
1471        let content = "---\ntitle: test\n---\n\n- Item\n\n  continuation\n";
1472        assert!(check(content).is_empty());
1473    }
1474
1475    // ── Fix produces valid output with multiple fixes ─────────────────
1476
1477    #[test]
1478    fn fix_multiple_items() {
1479        let content = "1. First\n\n wrong1\n\n2. Second\n\n wrong2\n";
1480        let fixed = fix(content);
1481        assert_eq!(fixed, "1. First\n\n   wrong1\n\n2. Second\n\n   wrong2\n");
1482    }
1483
1484    #[test]
1485    fn fix_multiline_loose_continuation_all_lines() {
1486        let content = "1. Item\n\n  line one\n  line two\n  line three\n";
1487        let fixed = fix(content);
1488        assert_eq!(fixed, "1. Item\n\n   line one\n   line two\n   line three\n");
1489    }
1490
1491    // ── No false positive when content is after sibling item ──────────
1492
1493    #[test]
1494    fn sibling_item_boundary_respected() {
1495        // The "continuation" after a blank belongs to "- Second", not "- First"
1496        let content = "- First\n- Second\n\n  continuation\n";
1497        assert!(check(content).is_empty());
1498    }
1499
1500    // ── Blockquote-nested lists ────────────────────────────────────────
1501
1502    #[test]
1503    fn blockquote_list_correct_indent_no_warning() {
1504        // Lists inside blockquotes: visual_indent includes the blockquote
1505        // prefix, so comparisons work on raw line columns.
1506        let content = "> - Item\n>\n>   continuation\n";
1507        assert!(check(content).is_empty());
1508    }
1509
1510    #[test]
1511    fn blockquote_list_under_indent_no_false_positive() {
1512        // Under-indented continuation inside a blockquote: visual_indent
1513        // starts at 0 (the `>` char) which is <= marker_col, so the scan
1514        // breaks and no warning is emitted. This is a known false negative
1515        // (not a false positive), which is the safer default.
1516        let content = "> - Item\n>\n> continuation\n";
1517        assert!(check(content).is_empty());
1518    }
1519
1520    // ── Deep nesting (3+ levels) ──────────────────────────────────────
1521
1522    #[test]
1523    fn deep_nesting_correct_indent() {
1524        let content = "- L1\n  - L2\n    - L3\n\n      continuation of L3\n";
1525        assert!(check(content).is_empty());
1526    }
1527
1528    #[test]
1529    fn deep_nesting_under_indent() {
1530        // L3 starts at column 4 with "- " marker, content_column = 6
1531        // Continuation with 5 spaces is under-indented for L3.
1532        let content = "- L1\n  - L2\n    - L3\n\n     continuation of L3\n";
1533        let warnings = check(content);
1534        assert_eq!(warnings.len(), 1);
1535        assert!(warnings[0].message.contains("6 spaces"));
1536        assert!(warnings[0].message.contains("found 5"));
1537    }
1538
1539    #[test]
1540    fn deep_nesting_middle_level_continuation_bullets() {
1541        // A loose continuation of the MIDDLE level (L2, content_column = 4)
1542        // after a deeper L3 list is valid; the L1 walk must not claim it as
1543        // its own over-indented continuation (issue #739).
1544        let content = "- L1\n  - L2\n    - L3\n\n    continuation of L2\n";
1545        assert!(check(content).is_empty());
1546    }
1547
1548    #[test]
1549    fn deep_nesting_middle_level_continuation_ordered() {
1550        // Issue #739 repro: ordered lists, continuation of the level-2 item
1551        // (content_column = 6) after a level-3 bullet list.
1552        let content = "1. Level 1 item.\n1. Level 1 item:\n   1. Level 2 item.\n   1. Level 2 item.\n   1. Level 2 item:\n      - Level 3 item.\n      - Level 3 item.\n\n      Level 2 list continuation.\n1. Level 1 item.\n";
1553        assert!(check(content).is_empty());
1554    }
1555
1556    #[test]
1557    fn deep_nesting_outermost_continuation() {
1558        // A loose continuation of the OUTERMOST level (L1, content_column = 2)
1559        // past two open nested levels escapes both and stays valid.
1560        let content = "- L1\n  - L2\n    - L3\n\n  continuation of L1\n";
1561        assert!(check(content).is_empty());
1562    }
1563
1564    #[test]
1565    fn deep_nesting_between_levels_still_flagged() {
1566        // Col 3 sits strictly between L1's content column (2) and L2's (4):
1567        // the deepest applicable item (L2) claims it as loose under-indent.
1568        let content = "- L1\n  - L2\n    - L3\n\n   continuation\n";
1569        let warnings = check(content);
1570        assert_eq!(warnings.len(), 1);
1571        assert!(warnings[0].message.contains("4 spaces"));
1572        assert!(warnings[0].message.contains("found 3"));
1573    }
1574
1575    #[test]
1576    fn deep_nesting_beyond_deepest_still_flagged() {
1577        // Col 7 exceeds even L3's content column (6): over-indent for L3.
1578        let content = "- L1\n  - L2\n    - L3\n\n       continuation\n";
1579        let warnings = check(content);
1580        assert_eq!(warnings.len(), 1);
1581        assert!(warnings[0].message.contains("over-indented"));
1582        assert!(warnings[0].message.contains("expected 6, found 7"));
1583    }
1584
1585    #[test]
1586    fn four_levels_middle_continuation() {
1587        // Continuation of L2 (content_column = 4) after open L3 and L4 lists:
1588        // the walk must pop two deeper levels and still defer to L2.
1589        let content = "- L1\n  - L2\n    - L3\n      - L4\n\n    continuation of L2\n";
1590        assert!(check(content).is_empty());
1591    }
1592
1593    #[test]
1594    fn nested_sibling_closes_deeper_level() {
1595        // L2b closes L2a's deeper L3 list; a continuation at L2b's content
1596        // column belongs to L2b and stays valid.
1597        let content = "- L1\n  - L2a\n    - L3\n  - L2b\n\n    continuation of L2b\n";
1598        assert!(check(content).is_empty());
1599    }
1600
1601    #[test]
1602    fn deep_nesting_middle_level_continuation_fix_preserved() {
1603        // The valid middle-level continuation must survive fix() unchanged.
1604        let content = "- L1\n  - L2\n    - L3\n\n    continuation of L2\n";
1605        assert_eq!(fix(content), content);
1606    }
1607
1608    // ── Tab indentation ───────────────────────────────────────────────
1609
1610    #[test]
1611    fn loose_tab_continuation_over_indented() {
1612        // A tab expands to 4 visual columns, exceeding content_column = 2 for
1613        // "- ". Loose over-indent is flagged just like the tight tab case
1614        // (`tight_continuation_tab_over_indented`), and the fix normalizes the
1615        // tab down to the content-column indent.
1616        let content = "- Item\n\n\tcontinuation\n";
1617        let warnings = check(content);
1618        assert_eq!(warnings.len(), 1);
1619        assert_eq!(warnings[0].line, 3);
1620        assert_eq!(fix(content), "- Item\n\n  continuation\n");
1621    }
1622
1623    // ── Multiple continuation paragraphs ──────────────────────────────
1624
1625    #[test]
1626    fn multiple_continuations_correct() {
1627        let content = "- Item\n\n  para 1\n\n  para 2\n\n  para 3\n";
1628        assert!(check(content).is_empty());
1629    }
1630
1631    #[test]
1632    fn multiple_continuations_second_under_indent() {
1633        // First continuation is correct, second is under-indented
1634        let content = "- Item\n\n  para 1\n\n continuation 2\n";
1635        let warnings = check(content);
1636        assert_eq!(warnings.len(), 1);
1637        assert_eq!(warnings[0].line, 5);
1638    }
1639
1640    // ── Ordered list with `)` marker style ────────────────────────────
1641
1642    #[test]
1643    fn ordered_paren_marker_correct() {
1644        // "1) " is 3 chars, content_column = 3
1645        let content = "1) Item\n\n   continuation\n";
1646        assert!(check(content).is_empty());
1647    }
1648
1649    #[test]
1650    fn ordered_paren_marker_under_indent() {
1651        let content = "1) Item\n\n  continuation\n";
1652        let warnings = check(content);
1653        assert_eq!(warnings.len(), 1);
1654        assert!(warnings[0].message.contains("3 spaces"));
1655    }
1656
1657    // ── Star and plus markers ─────────────────────────────────────────
1658
1659    #[test]
1660    fn star_marker_correct() {
1661        let content = "* Item\n\n  continuation\n";
1662        assert!(check(content).is_empty());
1663    }
1664
1665    #[test]
1666    fn star_marker_under_indent() {
1667        let content = "* Item\n\n continuation\n";
1668        let warnings = check(content);
1669        assert_eq!(warnings.len(), 1);
1670    }
1671
1672    #[test]
1673    fn plus_marker_correct() {
1674        let content = "+ Item\n\n  continuation\n";
1675        assert!(check(content).is_empty());
1676    }
1677
1678    // ── Heading breaks scan ───────────────────────────────────────────
1679
1680    #[test]
1681    fn heading_after_list_no_warning() {
1682        let content = "- Item\n\n# Heading\n";
1683        assert!(check(content).is_empty());
1684    }
1685
1686    // ── Horizontal rule breaks scan ───────────────────────────────────
1687
1688    #[test]
1689    fn hr_after_list_no_warning() {
1690        let content = "- Item\n\n---\n";
1691        assert!(check(content).is_empty());
1692    }
1693
1694    // ── Reference link definitions skip ───────────────────────────────
1695
1696    #[test]
1697    fn reference_link_def_not_flagged() {
1698        let content = "- Item\n\n [link]: https://example.com\n";
1699        assert!(check(content).is_empty());
1700    }
1701
1702    // ── Footnote definitions skip ─────────────────────────────────────
1703
1704    #[test]
1705    fn footnote_def_not_flagged() {
1706        let content = "- Item\n\n [^1]: footnote text\n";
1707        assert!(check(content).is_empty());
1708    }
1709
1710    #[test]
1711    fn footnote_multiline_body_after_list_not_flagged() {
1712        // A footnote definition whose indented body follows a list must not be
1713        // read as over-indented list continuation. The 4-space body lines
1714        // belong to the footnote, not to `- This is a list.` (content col 2).
1715        let content = "# A list followed by a footnote\n\n\
1716                       Here is a paragraph.[^fn]\n\n\
1717                       - This is a list.\n\n\
1718                       [^fn]:\n\
1719                       \x20\x20\x20\x20Here is a footnote that spans multiple lines.\n\
1720                       \x20\x20\x20\x20It should thus be indented by at least four spaces.\n";
1721        assert!(check(content).is_empty());
1722    }
1723
1724    #[test]
1725    fn fix_footnote_multiline_body_after_list_is_noop() {
1726        // The false positive also produced a damaging auto-fix: reindenting the
1727        // footnote body from 4 spaces to 2 broke the footnote (then tripped
1728        // MD068). The fix must leave the footnote untouched.
1729        let content = "# A list followed by a footnote\n\n\
1730                       Here is a paragraph.[^fn]\n\n\
1731                       - This is a list.\n\n\
1732                       [^fn]:\n\
1733                       \x20\x20\x20\x20Here is a footnote that spans multiple lines.\n\
1734                       \x20\x20\x20\x20It should thus be indented by at least four spaces.\n";
1735        assert_eq!(fix(content), content);
1736    }
1737
1738    #[test]
1739    fn footnote_body_indented_past_list_content_col_not_flagged() {
1740        // Footnote body indented well past the list's content column (here a
1741        // single-space list marker would put content col at 2, body at 4) is
1742        // footnote content, not over-indented continuation.
1743        let content = "- Item\n\n[^fn]:\n    Body line one.\n    Body line two.\n";
1744        assert!(check(content).is_empty());
1745    }
1746
1747    #[test]
1748    fn list_inside_footnote_body_continuation_not_flagged() {
1749        // Accepted trade-off: a list living *inside* a footnote body is skipped
1750        // wholesale, so MD077 does not police its continuation indentation.
1751        // Consistent with MD007, which also skips footnote-internal content.
1752        let content = "Text.[^fn]\n\n[^fn]:\n\
1753                       \x20\x20\x20\x20- nested item\n\
1754                       \x20\x20\x20\x20\x20\x20\x20over-indented continuation\n";
1755        assert!(check(content).is_empty());
1756    }
1757
1758    #[test]
1759    fn footnote_multiline_body_after_list_not_flagged_mkdocs() {
1760        // Same protection under the MkDocs flavor. The body sits at 6 spaces,
1761        // past MkDocs's required indent of 4 for `- `, so without skipping
1762        // footnote content it would be flagged as over-indented continuation.
1763        let content = "Here is a paragraph.[^fn]\n\n\
1764                       - This is a list.\n\n\
1765                       [^fn]:\n\
1766                       \x20\x20\x20\x20\x20\x20Footnote body that spans\n\
1767                       \x20\x20\x20\x20\x20\x20multiple indented lines.\n";
1768        assert!(check_mkdocs(content).is_empty());
1769    }
1770
1771    // ── Fix preserves correct content ─────────────────────────────────
1772
1773    #[test]
1774    fn fix_deep_nesting() {
1775        let content = "- L1\n  - L2\n    - L3\n\n     under-indented\n";
1776        let fixed = fix(content);
1777        assert_eq!(fixed, "- L1\n  - L2\n    - L3\n\n      under-indented\n");
1778    }
1779
1780    #[test]
1781    fn fix_mkdocs_unordered() {
1782        // MkDocs: "- " has content_column 2, but MkDocs requires max(2,4) = 4
1783        let content = "- Item\n\n  continuation\n";
1784        let fixed = fix_mkdocs(content);
1785        assert_eq!(fixed, "- Item\n\n    continuation\n");
1786    }
1787
1788    #[test]
1789    fn fix_code_fence_indent() {
1790        // Fence opener, interior, and closer all shift by the same delta so
1791        // the parser keeps pairing the fences and MD031 doesn't misfire.
1792        let content = "- Item\n\n ```\n code\n ```\n";
1793        let fixed = fix(content);
1794        assert_eq!(fixed, "- Item\n\n  ```\n  code\n  ```\n");
1795    }
1796
1797    #[test]
1798    fn fix_mkdocs_code_fence_indent() {
1799        // MkDocs ordered list: fence at 3 spaces needs 4; interior shifts too
1800        let content = "1. Item\n\n   ```toml\n   key = \"val\"\n   ```\n";
1801        let fixed = fix_mkdocs(content);
1802        assert_eq!(fixed, "1. Item\n\n    ```toml\n    key = \"val\"\n    ```\n");
1803    }
1804
1805    // ── Empty document / whitespace-only ──────────────────────────────
1806
1807    #[test]
1808    fn empty_document_no_warning() {
1809        assert!(check("").is_empty());
1810    }
1811
1812    #[test]
1813    fn whitespace_only_no_warning() {
1814        assert!(check("   \n\n  \n").is_empty());
1815    }
1816
1817    // ── No list at all ────────────────────────────────────────────────
1818
1819    #[test]
1820    fn no_list_no_warning() {
1821        let content = "# Heading\n\nSome paragraph.\n\nAnother paragraph.\n";
1822        assert!(check(content).is_empty());
1823    }
1824
1825    // ── Multi-line continuation (additional coverage) ──────────────
1826
1827    #[test]
1828    fn multiline_continuation_all_lines_flagged() {
1829        let content = "1. This is a list item.\n\n  This is continuation text and\n  it has multiple lines.\n  This is yet another line.\n";
1830        let warnings = check(content);
1831        assert_eq!(warnings.len(), 3);
1832        assert_eq!(warnings[0].line, 3);
1833        assert_eq!(warnings[1].line, 4);
1834        assert_eq!(warnings[2].line, 5);
1835    }
1836
1837    #[test]
1838    fn multiline_continuation_with_frontmatter_fix() {
1839        let content = "---\ntitle: Heading\n---\n\nSome introductory text:\n\n1. This is a list item.\n\n  This is list continuation text and\n  it has multiple lines that aren't indented properly.\n  This is yet another line that isn't indented properly.\n1. This is a list item.\n\n  This is list continuation text and\n  it has multiple lines that aren't indented properly.\n  This is yet another line that isn't indented properly.\n";
1840        let fixed = fix(content);
1841        assert_eq!(
1842            fixed,
1843            "---\ntitle: Heading\n---\n\nSome introductory text:\n\n1. This is a list item.\n\n   This is list continuation text and\n   it has multiple lines that aren't indented properly.\n   This is yet another line that isn't indented properly.\n1. This is a list item.\n\n   This is list continuation text and\n   it has multiple lines that aren't indented properly.\n   This is yet another line that isn't indented properly.\n"
1844        );
1845    }
1846
1847    #[test]
1848    fn multiline_continuation_correct_indent_no_warning() {
1849        let content = "1. Item\n\n   line one\n   line two\n   line three\n";
1850        assert!(check(content).is_empty());
1851    }
1852
1853    #[test]
1854    fn multiline_continuation_mixed_indent() {
1855        let content = "1. Item\n\n   correct\n  wrong\n   correct\n";
1856        let warnings = check(content);
1857        assert_eq!(warnings.len(), 1);
1858        assert_eq!(warnings[0].line, 4);
1859    }
1860
1861    #[test]
1862    fn multiline_continuation_unordered() {
1863        let content = "- Item\n\n continuation 1\n continuation 2\n continuation 3\n";
1864        let warnings = check(content);
1865        assert_eq!(warnings.len(), 3);
1866        let fixed = fix(content);
1867        assert_eq!(
1868            fixed,
1869            "- Item\n\n  continuation 1\n  continuation 2\n  continuation 3\n"
1870        );
1871    }
1872
1873    #[test]
1874    fn multiline_continuation_two_items_fix() {
1875        let content = "1. First\n\n  cont a\n  cont b\n\n2. Second\n\n  cont c\n  cont d\n";
1876        let fixed = fix(content);
1877        assert_eq!(
1878            fixed,
1879            "1. First\n\n   cont a\n   cont b\n\n2. Second\n\n   cont c\n   cont d\n"
1880        );
1881    }
1882
1883    #[test]
1884    fn fence_fix_does_not_break_pairing_for_md031() {
1885        // Regression for issue #574: previously MD077 only reindented the
1886        // fence delimiter lines while leaving the code block's interior at
1887        // the old indent. Between iterations of the fix loop the parser
1888        // saw an opener-closer mismatch, and MD031 then injected stray
1889        // blank lines at the fence boundaries. MD077's compound fix must
1890        // now rewrite the whole block atomically so the fences stay paired.
1891        let content = "#### title\n\nabc\n\n\
1892                       1. ab\n\n\
1893                       \x20\x20`aabbccdd`\n\n\
1894                       2. cd\n\n\
1895                       \x20\x20`bbcc dd ee`\n\n\
1896                       \x20\x20```\n\
1897                       \x20\x20abcd\n\
1898                       \x20\x20ef gh\n\
1899                       \x20\x20```\n\n\
1900                       \x20\x20uu\n\n\
1901                       \x20\x20```\n\
1902                       \x20\x20cdef\n\
1903                       \x20\x20gh ij\n\
1904                       \x20\x20```\n";
1905        let expected = "#### title\n\nabc\n\n\
1906                        1. ab\n\n\
1907                        \x20\x20\x20`aabbccdd`\n\n\
1908                        2. cd\n\n\
1909                        \x20\x20\x20`bbcc dd ee`\n\n\
1910                        \x20\x20\x20```\n\
1911                        \x20\x20\x20abcd\n\
1912                        \x20\x20\x20ef gh\n\
1913                        \x20\x20\x20```\n\n\
1914                        \x20\x20\x20uu\n\n\
1915                        \x20\x20\x20```\n\
1916                        \x20\x20\x20cdef\n\
1917                        \x20\x20\x20gh ij\n\
1918                        \x20\x20\x20```\n";
1919        assert_eq!(fix(content), expected);
1920    }
1921
1922    #[test]
1923    fn multiline_continuation_separated_by_blank() {
1924        let content = "1. Item\n\n  para1 line1\n  para1 line2\n\n  para2 line1\n  para2 line2\n";
1925        let warnings = check(content);
1926        assert_eq!(warnings.len(), 4);
1927        let fixed = fix(content);
1928        assert_eq!(
1929            fixed,
1930            "1. Item\n\n   para1 line1\n   para1 line2\n\n   para2 line1\n   para2 line2\n"
1931        );
1932    }
1933
1934    #[test]
1935    fn tab_indented_fence_is_normalized_to_spaces() {
1936        // Leading tabs expand to the next multiple-of-4 column under
1937        // CommonMark, so simply prepending spaces before a tab would
1938        // silently no-op (the tab snaps back to column 4). The compound
1939        // fence fix must replace the leading whitespace with a fresh
1940        // (visual_indent + delta) run of spaces. A `100. ` item has
1941        // content_column = 5, so a tab-indented fence (visual col 4) is
1942        // under-indented by 1 and must end up at 5 spaces after the fix.
1943        let content = "100. ab\n\n\t```\n\tabcd\n\t```\n";
1944        let expected = "100. ab\n\n     ```\n     abcd\n     ```\n";
1945        assert_eq!(fix(content), expected);
1946    }
1947
1948    // ── Loose continuation (after a blank line): over-indent ──────────
1949    //
1950    // Over-indentation is a mistake in both tight and loose continuation:
1951    // the body looks aligned but isn't. A blank line between the marker and
1952    // the body must not exempt it. The only over-indent that is intentional
1953    // after a blank line is an indented code block (content column + 4 or
1954    // more), which the parser marks `in_code_block` and the rule skips.
1955
1956    #[test]
1957    fn loose_continuation_over_indented_flagged() {
1958        // "* " content column is 2; 3 spaces after a blank is over-indented
1959        // (the code-block threshold is content_col + 4 = 6).
1960        let content = "* Item\n\n   over-indented\n";
1961        let warnings = check(content);
1962        assert_eq!(warnings.len(), 1);
1963        assert_eq!(warnings[0].line, 3);
1964        assert!(warnings[0].message.contains("over-indented"));
1965        assert!(warnings[0].message.contains("expected 2"));
1966        assert!(warnings[0].message.contains("found 3"));
1967    }
1968
1969    #[test]
1970    fn loose_continuation_over_indented_multiline_mixed() {
1971        // Over, correct, over — only the two over-indented lines are flagged.
1972        let content = "* Item\n\n   over one\n  correct\n   over two\n";
1973        let warnings = check(content);
1974        assert_eq!(warnings.len(), 2);
1975        assert_eq!(warnings[0].line, 3);
1976        assert_eq!(warnings[1].line, 5);
1977    }
1978
1979    #[test]
1980    fn fix_loose_continuation_over_indented() {
1981        let content = "* Item\n\n   over one\n  correct\n   over two\n";
1982        let fixed = fix(content);
1983        assert_eq!(fixed, "* Item\n\n  over one\n  correct\n  over two\n");
1984    }
1985
1986    #[test]
1987    fn fix_tight_and_loose_items_normalized_identically() {
1988        // The reported document: a tight item and a loose item with the same
1989        // over-indented body must both normalize to the content column.
1990        let content = "---\ntitle: Heading\n---\n\nSome introductory text:\n\n\
1991                       * This is a list item.\n   This is list continuation text and\n  it has multiple lines that aren't indented properly.\n   This is yet another line that isn't indented properly.\n\n\
1992                       * This is a list item.\n\n   This is list continuation text and\n  it has multiple lines that aren't indented properly.\n   This is yet another line that isn't indented properly.\n";
1993        let expected = "---\ntitle: Heading\n---\n\nSome introductory text:\n\n\
1994                        * This is a list item.\n  This is list continuation text and\n  it has multiple lines that aren't indented properly.\n  This is yet another line that isn't indented properly.\n\n\
1995                        * This is a list item.\n\n  This is list continuation text and\n  it has multiple lines that aren't indented properly.\n  This is yet another line that isn't indented properly.\n";
1996        assert_eq!(fix(content), expected);
1997    }
1998
1999    #[test]
2000    fn multi_paragraph_item_loose_paragraph_over_indented() {
2001        // A tight first paragraph and a loose second paragraph (after an
2002        // internal blank line) are both over-indented; both must be flagged.
2003        let content = "* Item.\n   tight over\n\n   loose over\n";
2004        let warnings = check(content);
2005        assert_eq!(warnings.len(), 2);
2006        assert_eq!(warnings[0].line, 2);
2007        assert_eq!(warnings[1].line, 4);
2008    }
2009
2010    #[test]
2011    fn loose_indented_code_block_not_flagged() {
2012        // content_col = 2; a loose line at content_col + 4 (6 spaces) is a
2013        // CommonMark indented code block, not over-indented prose. The over-
2014        // indent check must never reach it (it is `in_code_block` and skipped).
2015        let content = "- Item\n\n      code line\n";
2016        assert!(check(content).is_empty());
2017    }
2018
2019    #[test]
2020    fn mkdocs_loose_over_indented_flagged() {
2021        // MkDocs requires max(3, 4) = 4 for "1. ". A loose line at 5 spaces is
2022        // over-indented (code-block threshold is content_col + 4 = 7).
2023        let content = "1. Item\n\n     over\n";
2024        let warnings = check_mkdocs(content);
2025        assert_eq!(warnings.len(), 1);
2026        assert_eq!(warnings[0].line, 3);
2027        assert!(warnings[0].message.contains("over-indented"));
2028        assert!(warnings[0].message.contains("expected 4"));
2029        assert!(warnings[0].message.contains("found 5"));
2030    }
2031
2032    #[test]
2033    fn task_list_loose_over_indented_flagged() {
2034        // "- [ ] " content_col = 2, task_col = 6. A loose line at 4 spaces is
2035        // neither valid column and below the code-block threshold (6); flagged.
2036        let content = "- [ ] Task\n\n    over\n";
2037        let warnings = check(content);
2038        assert_eq!(warnings.len(), 1);
2039        assert_eq!(warnings[0].line, 3);
2040    }
2041
2042    #[test]
2043    fn loose_over_indent_boundary_below_code_block_threshold_flagged() {
2044        // content_col = 2; 5 spaces (= content_col + 3) is the deepest loose
2045        // over-indent that is still prose. content_col + 4 (6 spaces) would be
2046        // an indented code block - see `loose_indented_code_block_not_flagged`.
2047        // This pins the boundary so a shift in the parser's threshold is caught.
2048        let content = "- Item\n\n     over\n";
2049        let warnings = check(content);
2050        assert_eq!(warnings.len(), 1);
2051        assert_eq!(warnings[0].line, 3);
2052        assert!(warnings[0].message.contains("expected 2"));
2053        assert!(warnings[0].message.contains("found 5"));
2054    }
2055
2056    #[test]
2057    fn loose_over_indent_does_not_steal_nested_under_indent() {
2058        // Inner content_col = 4, marker_col = 2. A loose continuation at column
2059        // 3 is under-indented for Inner yet over-indented for Outer (content_col
2060        // 2). The under-indent pass must claim it for Inner (snap *up* to 4,
2061        // preserving the apparent nesting), never letting the over-indent pass
2062        // mis-attribute it to Outer and snap it *down* to 2. This is the exact
2063        // ambiguity the two-pass ordering exists to resolve.
2064        let content = "- Outer\n  - Inner\n\n   continuation\n";
2065        let warnings = check(content);
2066        assert_eq!(warnings.len(), 1);
2067        assert_eq!(warnings[0].line, 4);
2068        assert!(warnings[0].message.contains("4 spaces"));
2069        assert!(warnings[0].message.contains("found 3"));
2070        assert_eq!(fix(content), "- Outer\n  - Inner\n\n    continuation\n");
2071    }
2072
2073    #[test]
2074    fn loose_over_indent_attributes_to_deepest_enclosing_item() {
2075        // Inner content_col = 4. A loose continuation at column 5 over-indents
2076        // Inner (the deepest item it sits within), so it is flagged against
2077        // Inner's column 4 - not Outer's column 2 - and snapped to 4.
2078        let content = "- Outer\n  - Inner\n\n     continuation\n";
2079        let warnings = check(content);
2080        assert_eq!(warnings.len(), 1);
2081        assert_eq!(warnings[0].line, 4);
2082        assert!(warnings[0].message.contains("expected 4"));
2083        assert!(warnings[0].message.contains("found 5"));
2084        assert_eq!(fix(content), "- Outer\n  - Inner\n\n    continuation\n");
2085    }
2086
2087    // ── Over-indented fenced code blocks are left untouched ───────────
2088    //
2089    // An over-indented fence is cosmetic: the code still renders inside the
2090    // list item. Reindenting only its delimiters (the body is skipped as code)
2091    // would change the literal code content, so the over-indent pass skips
2092    // anything `in_code_block`. The under-indent path still fixes fences, where
2093    // moving the block up is required to keep it inside the item.
2094
2095    #[test]
2096    fn loose_over_indented_fence_not_flagged() {
2097        let content = "- Item\n\n   ```\n   code\n   ```\n";
2098        assert!(check(content).is_empty());
2099        assert_eq!(fix(content), content);
2100    }
2101
2102    #[test]
2103    fn tight_over_indented_fence_not_flagged() {
2104        let content = "- Item\n   ```\n   code\n   ```\n";
2105        assert!(check(content).is_empty());
2106        assert_eq!(fix(content), content);
2107    }
2108
2109    #[test]
2110    fn over_indented_tilde_fence_not_flagged() {
2111        let content = "- Item\n\n   ~~~\n   code\n   ~~~\n";
2112        assert!(check(content).is_empty());
2113        assert_eq!(fix(content), content);
2114    }
2115
2116    #[test]
2117    fn fence_like_code_content_inside_fenced_block_not_flagged() {
2118        // A ``` line that is the *body* of a ~~~ block must not be treated as
2119        // over-indented continuation; rewriting it would corrupt code content.
2120        let content = "- Item\n\n  ~~~\n   ```\n  ~~~\n";
2121        assert!(check(content).is_empty());
2122        assert_eq!(fix(content), content);
2123    }
2124
2125    #[test]
2126    fn unterminated_over_indented_fence_not_flagged() {
2127        // No closing fence: the last code line must not be mistaken for a
2128        // closer and snapped to the content column.
2129        let content = "- Item\n\n   ```\n   code1\n     code2deeper\n";
2130        assert!(check(content).is_empty());
2131        assert_eq!(fix(content), content);
2132    }
2133
2134    // ── GFM task list items: post-checkbox continuation column ───────
2135    //
2136    // MD013's reflow indents wrapped task-list lines at `content_col + 4`
2137    // (the column after the checkbox). MD077 must accept that column for
2138    // both tight and loose continuation, for every marker flavour, so the
2139    // two rules don't fight over well-formed task items (issue #579).
2140
2141    #[test]
2142    fn task_list_tight_continuation_post_checkbox_reproducer_579() {
2143        // Exact reproducer from the bug report: content wraps to the
2144        // post-checkbox column (6) with no blank line.
2145        let content = "- [ ] Lorem ipsum dolor sit amet, consectetur adipiscing\n      tempor incididunt ut labore.\n";
2146        assert!(check(content).is_empty());
2147    }
2148
2149    #[test]
2150    fn task_list_tight_continuation_dash_unchecked() {
2151        let content = "- [ ] Task\n      continuation\n";
2152        assert!(check(content).is_empty());
2153    }
2154
2155    #[test]
2156    fn task_list_tight_continuation_dash_checked_lower() {
2157        let content = "- [x] Task\n      continuation\n";
2158        assert!(check(content).is_empty());
2159    }
2160
2161    #[test]
2162    fn task_list_tight_continuation_dash_checked_upper() {
2163        let content = "- [X] Task\n      continuation\n";
2164        assert!(check(content).is_empty());
2165    }
2166
2167    #[test]
2168    fn task_list_tight_continuation_star_marker() {
2169        let content = "* [ ] Task\n      continuation\n";
2170        assert!(check(content).is_empty());
2171    }
2172
2173    #[test]
2174    fn task_list_tight_continuation_plus_marker() {
2175        let content = "+ [ ] Task\n      continuation\n";
2176        assert!(check(content).is_empty());
2177    }
2178
2179    #[test]
2180    fn task_list_tight_continuation_content_column_still_valid() {
2181        // Column 2 is the CommonMark-canonical indent for "- " and remains
2182        // valid for task items too.
2183        let content = "- [ ] Task\n  continuation\n";
2184        assert!(check(content).is_empty());
2185    }
2186
2187    #[test]
2188    fn task_list_tight_continuation_between_columns_still_flagged() {
2189        // Column 4 matches neither content_col (2) nor post-checkbox (6).
2190        // A genuine indentation mistake — must remain flagged.
2191        let content = "- [ ] Task\n    continuation\n";
2192        let warnings = check(content);
2193        assert_eq!(warnings.len(), 1);
2194        // Task items advertise both valid columns to the user.
2195        assert!(warnings[0].message.contains("expected 2 or 6"));
2196        assert!(warnings[0].message.contains("found 4"));
2197    }
2198
2199    #[test]
2200    fn task_list_tight_continuation_overshoot_still_flagged() {
2201        // Column 7 overshoots the post-checkbox column. Genuine mistake.
2202        let content = "- [ ] Task\n       continuation\n";
2203        let warnings = check(content);
2204        assert_eq!(warnings.len(), 1);
2205        assert!(warnings[0].message.contains("expected 2 or 6"));
2206        assert!(warnings[0].message.contains("found 7"));
2207    }
2208
2209    // ── Task-list fix output: snap to nearer valid column ────────────
2210
2211    #[test]
2212    fn fix_task_list_overshoot_snaps_to_task_col() {
2213        // Col 7 is 1 away from post-checkbox (6), 5 away from content (2).
2214        // Snap to 6 — the author's intent was almost certainly the
2215        // post-checkbox alignment, not the content column.
2216        let content = "- [ ] Task\n       continuation\n";
2217        let fixed = fix(content);
2218        assert_eq!(fixed, "- [ ] Task\n      continuation\n");
2219    }
2220
2221    #[test]
2222    fn fix_task_list_col_5_snaps_to_task_col() {
2223        // Col 5 is 1 away from post-checkbox (6), 3 away from content (2).
2224        let content = "- [ ] Task\n     continuation\n";
2225        let fixed = fix(content);
2226        assert_eq!(fixed, "- [ ] Task\n      continuation\n");
2227    }
2228
2229    #[test]
2230    fn fix_task_list_col_3_snaps_to_content_col() {
2231        // Col 3 is 1 away from content (2), 3 away from post-checkbox (6).
2232        let content = "- [ ] Task\n   continuation\n";
2233        let fixed = fix(content);
2234        assert_eq!(fixed, "- [ ] Task\n  continuation\n");
2235    }
2236
2237    #[test]
2238    fn fix_task_list_col_4_ties_to_content_col() {
2239        // Col 4 is equidistant (±2) from both columns. Tie breaks to the
2240        // CommonMark-canonical content column — that's the default indent
2241        // MD077 would produce for a non-task item, so prefer it when the
2242        // author's intent is ambiguous.
2243        let content = "- [ ] Task\n    continuation\n";
2244        let fixed = fix(content);
2245        assert_eq!(fixed, "- [ ] Task\n  continuation\n");
2246    }
2247
2248    #[test]
2249    fn fix_task_list_ordered_overshoot_snaps_to_task_col() {
2250        // "1. [ ] " → content_col = 3, post-checkbox = 7.
2251        // Col 8 is nearer to 7.
2252        let content = "1. [ ] Task\n        continuation\n";
2253        let fixed = fix(content);
2254        assert_eq!(fixed, "1. [ ] Task\n       continuation\n");
2255    }
2256
2257    #[test]
2258    fn fix_task_list_ordered_under_overshoot_snaps_to_content_col() {
2259        // "1. [ ] " → content_col = 3, post-checkbox = 7.
2260        // Col 4 is nearer to 3.
2261        let content = "1. [ ] Task\n    continuation\n";
2262        let fixed = fix(content);
2263        assert_eq!(fixed, "1. [ ] Task\n   continuation\n");
2264    }
2265
2266    #[test]
2267    fn task_list_tight_continuation_ordered_single_digit() {
2268        // "1. [ ] " → content_col = 3, post-checkbox = 7
2269        let content = "1. [ ] Task\n       continuation\n";
2270        assert!(check(content).is_empty());
2271    }
2272
2273    #[test]
2274    fn task_list_tight_continuation_ordered_multi_digit() {
2275        // "10. [ ] " → content_col = 4, post-checkbox = 8
2276        let content = "10. [ ] Task\n        continuation\n";
2277        assert!(check(content).is_empty());
2278    }
2279
2280    #[test]
2281    fn task_list_tight_continuation_nested_dash() {
2282        // Nested "  - [ ] " at marker_col=2 → content_col=4, post-checkbox=8
2283        let content = "- Parent\n  - [ ] Nested task\n        continuation\n";
2284        assert!(check(content).is_empty());
2285    }
2286
2287    #[test]
2288    fn task_list_loose_continuation_post_checkbox_column_not_flagged() {
2289        // Loose continuation (blank line) at col 6 is also valid. This
2290        // already passed before the fix, but pin the intent: the 6-space
2291        // indent is accepted because it's the task-alignment column, not
2292        // because the under-indent check happens to let it through.
2293        let content = "- [ ] Task\n\n      continuation\n";
2294        assert!(check(content).is_empty());
2295    }
2296
2297    #[test]
2298    fn task_list_empty_body_is_not_a_task() {
2299        // "- [ ]" with nothing after is an empty regular list item, not a
2300        // task. Column 4 continuation has no task alignment to justify it
2301        // and must still be flagged as over-indented. (Col 6 would turn
2302        // the continuation into an indented code block inside the item,
2303        // which is a different code path.)
2304        let content = "- [ ]\n    continuation\n";
2305        let warnings = check(content);
2306        assert_eq!(warnings.len(), 1);
2307        assert!(warnings[0].message.contains("found 4"));
2308    }
2309
2310    #[test]
2311    fn task_list_malformed_checkbox_is_not_a_task() {
2312        // `[~] ` is not a GFM checkbox; only `[ ] `, `[x] `, `[X] ` count.
2313        let content = "- [~] Not a task\n      continuation\n";
2314        let warnings = check(content);
2315        assert_eq!(warnings.len(), 1);
2316    }
2317
2318    // ── MkDocs flavor × task checkbox ─────────────────────────────────
2319    //
2320    // MkDocs strict-indent and task alignment interact: required_min is
2321    // max(content_col, 4), and post-checkbox is content_col + 4. Both are
2322    // independently valid; values between them are flagged.
2323
2324    #[test]
2325    fn task_list_mkdocs_unordered_required_min_valid() {
2326        // "- [ ]" MkDocs: required_min = max(2, 4) = 4, post-checkbox = 6.
2327        let content = "- [ ] Task\n    continuation\n";
2328        assert!(check_mkdocs(content).is_empty());
2329    }
2330
2331    #[test]
2332    fn task_list_mkdocs_unordered_post_checkbox_valid() {
2333        let content = "- [ ] Task\n      continuation\n";
2334        assert!(check_mkdocs(content).is_empty());
2335    }
2336
2337    #[test]
2338    fn task_list_mkdocs_unordered_between_flagged() {
2339        // Column 5 is between required_min=4 and post-checkbox=6.
2340        let content = "- [ ] Task\n     continuation\n";
2341        let warnings = check_mkdocs(content);
2342        assert_eq!(warnings.len(), 1);
2343    }
2344
2345    #[test]
2346    fn task_list_mkdocs_ordered_both_columns_valid() {
2347        // "1. [ ]" MkDocs: required_min = max(3, 4) = 4, post-checkbox = 7.
2348        let at_4 = "1. [ ] Task\n    continuation\n";
2349        assert!(check_mkdocs(at_4).is_empty());
2350        let at_7 = "1. [ ] Task\n       continuation\n";
2351        assert!(check_mkdocs(at_7).is_empty());
2352    }
2353
2354    #[test]
2355    fn task_list_mkdocs_ordered_between_flagged() {
2356        // Column 5 and 6 are between required_min=4 and post-checkbox=7.
2357        let at_5 = "1. [ ] Task\n     continuation\n";
2358        assert_eq!(check_mkdocs(at_5).len(), 1);
2359        let at_6 = "1. [ ] Task\n      continuation\n";
2360        assert_eq!(check_mkdocs(at_6).len(), 1);
2361    }
2362
2363    // ── Context-aware tie-break ──────────────────────────────────────
2364    //
2365    // When a flagged line is exactly equidistant from `content_col` and
2366    // `task_col`, the author's intent is ambiguous. Before picking a
2367    // canonical default, look at whether other continuation lines in the
2368    // same item already use one of the valid columns — if so, snap to the
2369    // column they're using so the fix preserves the author's visible
2370    // convention.
2371
2372    #[test]
2373    fn fix_task_list_tie_sibling_at_task_col_snaps_to_task_col() {
2374        // Col 4 is equidistant from content_col (2) and task_col (6).
2375        // A valid sibling at col 6 proves the author is aligning under the
2376        // checkbox, so the tie resolves to col 6.
2377        let content = "- [ ] Task\n      aligned continuation\n    tied continuation\n";
2378        let fixed = fix(content);
2379        assert_eq!(
2380            fixed,
2381            "- [ ] Task\n      aligned continuation\n      tied continuation\n"
2382        );
2383    }
2384
2385    #[test]
2386    fn fix_task_list_tie_sibling_at_content_col_snaps_to_content_col() {
2387        // Valid sibling at col 2 proves the author is aligning to the
2388        // content column, so the col-4 tie resolves to col 2.
2389        let content = "- [ ] Task\n  aligned continuation\n    tied continuation\n";
2390        let fixed = fix(content);
2391        assert_eq!(fixed, "- [ ] Task\n  aligned continuation\n  tied continuation\n");
2392    }
2393
2394    #[test]
2395    fn fix_task_list_tie_both_siblings_snaps_to_content_col() {
2396        // When siblings exist at both valid columns, the author's pattern
2397        // is self-contradictory. Fall back to the CommonMark-canonical
2398        // content column.
2399        let content = "- [ ] Task\n  at content col\n      at task col\n    tied continuation\n";
2400        let fixed = fix(content);
2401        assert_eq!(
2402            fixed,
2403            "- [ ] Task\n  at content col\n      at task col\n  tied continuation\n"
2404        );
2405    }
2406
2407    #[test]
2408    fn fix_task_list_tie_sees_task_col_through_tight_lazy_continuation() {
2409        // CommonMark allows tight lazy continuation at col ≤ marker_col
2410        // (zero-indent continuation) inside a list item. The pre-pass
2411        // must MIRROR the main check loop's termination semantics: in
2412        // tight mode (no preceding blank) col ≤ marker_col is NOT a
2413        // termination signal — the lazy line still belongs to the item.
2414        //
2415        // This test pins that mirroring: a `lazy` line at col 0 is
2416        // followed by a legitimate task-col sibling at col 6, then a
2417        // tied col-4 line. If the pre-pass terminated eagerly at the
2418        // lazy line, the task-col sibling would be missed and the tied
2419        // line would fall back to content column. With correct
2420        // mirroring, the task-col sibling is seen and the tie resolves
2421        // to col 6.
2422        let content = concat!("- [ ] Task\n", "lazy\n", "      aligned at task col\n", "    tied\n",);
2423        let fixed = fix(content);
2424        assert!(
2425            fixed.contains("\n      tied\n"),
2426            "tied line should snap to col 6 (task col) because a task-col \
2427             sibling is visible past the tight lazy-continuation line; got:\n{fixed}"
2428        );
2429    }
2430
2431    // ── Tab-indented task continuation ───────────────────────────────
2432    //
2433    // Leading tabs expand to the next column that's a multiple of 4 under
2434    // CommonMark. The fix replaces the leading whitespace bytes wholesale,
2435    // turning tabs into space-indented output.
2436
2437    #[test]
2438    fn task_list_tab_indented_continuation_flagged() {
2439        // Two tabs → visual col 8, which overshoots both valid columns
2440        // for `- [ ] ` (content_col=2, task_col=6).
2441        let content = "- [ ] Task\n\t\twrap\n";
2442        let warnings = check(content);
2443        assert_eq!(warnings.len(), 1);
2444        assert!(warnings[0].message.contains("expected 2 or 6"));
2445        assert!(warnings[0].message.contains("found 8"));
2446    }
2447
2448    #[test]
2449    fn fix_task_list_tab_indented_snaps_to_task_col() {
2450        // abs_diff(8, 6) = 2 < abs_diff(8, 2) = 6 → snap to task_col (6).
2451        let content = "- [ ] Task\n\t\twrap\n";
2452        let fixed = fix(content);
2453        assert_eq!(fixed, "- [ ] Task\n      wrap\n");
2454    }
2455
2456    #[test]
2457    fn fix_task_list_single_tab_equidistant_snaps_to_content_col() {
2458        // One tab → visual col 4, equidistant from content_col (2) and
2459        // task_col (6). No siblings → tie-break to content_col.
2460        let content = "- [ ] Task\n\twrap\n";
2461        let fixed = fix(content);
2462        assert_eq!(fixed, "- [ ] Task\n  wrap\n");
2463    }
2464
2465    // ── Blockquote × task-list ───────────────────────────────────────
2466    //
2467    // Blockquote-nested lists are a known limitation on MD077: the list
2468    // parser doesn't always expose them with the same column semantics as
2469    // top-level lists, and the rule prefers a false-negative default to
2470    // avoid spurious warnings inside blockquotes (see
2471    // `blockquote_list_under_indent_no_false_positive`). These tests pin
2472    // the current behavior so any future change is intentional.
2473
2474    #[test]
2475    fn task_list_blockquote_post_checkbox_not_flagged() {
2476        // Post-checkbox alignment inside a blockquote — accepted as valid.
2477        let content = "> - [ ] Task\n>       continuation\n";
2478        assert!(check(content).is_empty());
2479    }
2480
2481    #[test]
2482    fn task_list_blockquote_between_cols_documented_limitation() {
2483        // Col-4-equivalent inside a blockquote is silently accepted — a
2484        // known MD077 limitation on blockquote-nested lists, not a task-
2485        // list-specific choice. Pinning the current behavior.
2486        let content = "> - [ ] Task\n>     continuation\n";
2487        assert!(check(content).is_empty());
2488    }
2489
2490    #[test]
2491    fn task_list_blockquote_overshoot_documented_limitation() {
2492        // Overshoot inside a blockquote — same known limitation.
2493        let content = "> - [ ] Task\n>        continuation\n";
2494        assert!(check(content).is_empty());
2495    }
2496
2497    // ── MkDocs × task × fix output ───────────────────────────────────
2498    //
2499    // MkDocs strict-indent raises `required` to max(content_col, 4) while
2500    // task_col stays at content_col + 4. The snap logic operates on the
2501    // raised required, not on the underlying content_col.
2502
2503    #[test]
2504    fn fix_task_list_mkdocs_unordered_overshoot_snaps_to_task_col() {
2505        // `- [ ]` MkDocs: required=4, task_col=6. Col 7 → abs_diff(7,6)=1
2506        // < abs_diff(7,4)=3. Snap to task_col.
2507        let content = "- [ ] Task\n       continuation\n";
2508        let fixed = fix_mkdocs(content);
2509        assert_eq!(fixed, "- [ ] Task\n      continuation\n");
2510    }
2511
2512    #[test]
2513    fn fix_task_list_mkdocs_unordered_tie_snaps_to_required() {
2514        // `- [ ]` MkDocs: required=4, task_col=6. Col 5 → abs_diff(5,6)=1
2515        // == abs_diff(5,4)=1. Tie with no siblings → required (4).
2516        let content = "- [ ] Task\n     continuation\n";
2517        let fixed = fix_mkdocs(content);
2518        assert_eq!(fixed, "- [ ] Task\n    continuation\n");
2519    }
2520
2521    #[test]
2522    fn fix_task_list_mkdocs_ordered_overshoot_snaps_to_task_col() {
2523        // `1. [ ]` MkDocs: required=4, task_col=7. Col 8 → abs_diff(8,7)=1
2524        // < abs_diff(8,4)=4. Snap to task_col.
2525        let content = "1. [ ] Task\n        continuation\n";
2526        let fixed = fix_mkdocs(content);
2527        assert_eq!(fixed, "1. [ ] Task\n       continuation\n");
2528    }
2529
2530    #[test]
2531    fn fix_task_list_mkdocs_ordered_near_required_snaps_to_required() {
2532        // `1. [ ]` MkDocs: required=4, task_col=7. Col 5 → abs_diff(5,7)=2
2533        // > abs_diff(5,4)=1. Snap to required (4). `1. [ ] Task\n     wrap`
2534        // has actual=5 which is over `required=4` so it's flagged in
2535        // strict mode, while in standard mode it falls under the lazy-
2536        // continuation window and isn't flagged at all.
2537        let content = "1. [ ] Task\n     continuation\n";
2538        let fixed = fix_mkdocs(content);
2539        assert_eq!(fixed, "1. [ ] Task\n    continuation\n");
2540    }
2541
2542    #[test]
2543    fn fix_task_list_mkdocs_ordered_between_cols_snaps_to_task_col() {
2544        // `1. [ ]` MkDocs: required=4, task_col=7. Col 6 → abs_diff(6,7)=1
2545        // < abs_diff(6,4)=2. Snap to task_col (7).
2546        let content = "1. [ ] Task\n      continuation\n";
2547        let fixed = fix_mkdocs(content);
2548        assert_eq!(fixed, "1. [ ] Task\n       continuation\n");
2549    }
2550
2551    // ── Fix idempotency (property test) ──────────────────────────────
2552    //
2553    // A fix pass on already-fixed content must produce the same content
2554    // — otherwise MD077 would oscillate on repeated invocations. This is
2555    // the core property that issue #579 was about (MD077 vs. MD013 fix
2556    // loop), and the integration test covers the MD013 interaction. The
2557    // property tests below pin the *internal* idempotency of MD077's own
2558    // fix, so any future change that introduces oscillation fails fast.
2559
2560    fn assert_idempotent(content: &str) {
2561        let once = fix(content);
2562        let twice = fix(&once);
2563        assert_eq!(once, twice, "MD077 fix was not idempotent on input: {content:?}");
2564    }
2565
2566    fn assert_idempotent_mkdocs(content: &str) {
2567        let once = fix_mkdocs(content);
2568        let twice = fix_mkdocs(&once);
2569        assert_eq!(
2570            once, twice,
2571            "MD077 (MkDocs) fix was not idempotent on input: {content:?}"
2572        );
2573    }
2574
2575    #[test]
2576    fn idempotent_task_list_between_cols() {
2577        assert_idempotent("- [ ] Task\n    continuation\n");
2578    }
2579
2580    #[test]
2581    fn idempotent_task_list_overshoot() {
2582        assert_idempotent("- [ ] Task\n       continuation\n");
2583    }
2584
2585    #[test]
2586    fn idempotent_task_list_under_post_checkbox() {
2587        assert_idempotent("- [ ] Task\n   continuation\n");
2588    }
2589
2590    #[test]
2591    fn idempotent_task_list_near_post_checkbox() {
2592        assert_idempotent("- [ ] Task\n     continuation\n");
2593    }
2594
2595    #[test]
2596    fn idempotent_task_list_tab_overshoot() {
2597        assert_idempotent("- [ ] Task\n\t\twrap\n");
2598    }
2599
2600    #[test]
2601    fn idempotent_task_list_single_tab() {
2602        assert_idempotent("- [ ] Task\n\twrap\n");
2603    }
2604
2605    #[test]
2606    fn idempotent_task_list_ordered_overshoot() {
2607        assert_idempotent("1. [ ] Task\n        continuation\n");
2608    }
2609
2610    #[test]
2611    fn idempotent_task_list_ordered_under() {
2612        assert_idempotent("1. [ ] Task\n    continuation\n");
2613    }
2614
2615    #[test]
2616    fn idempotent_task_list_tie_with_sibling_at_task_col() {
2617        assert_idempotent("- [ ] Task\n      aligned\n    tied\n");
2618    }
2619
2620    #[test]
2621    fn idempotent_task_list_tie_with_sibling_at_content_col() {
2622        assert_idempotent("- [ ] Task\n  aligned\n    tied\n");
2623    }
2624
2625    #[test]
2626    fn idempotent_task_list_mkdocs_unordered_overshoot() {
2627        assert_idempotent_mkdocs("- [ ] Task\n       continuation\n");
2628    }
2629
2630    #[test]
2631    fn idempotent_task_list_mkdocs_unordered_tie() {
2632        assert_idempotent_mkdocs("- [ ] Task\n     continuation\n");
2633    }
2634
2635    #[test]
2636    fn idempotent_task_list_mkdocs_ordered_overshoot() {
2637        assert_idempotent_mkdocs("1. [ ] Task\n        continuation\n");
2638    }
2639
2640    #[test]
2641    fn idempotent_task_list_mkdocs_ordered_between() {
2642        assert_idempotent_mkdocs("1. [ ] Task\n      continuation\n");
2643    }
2644
2645    #[test]
2646    fn idempotent_task_list_reproducer_579() {
2647        // The exact reproducer from issue #579 already has correct indent
2648        // (col 6 = post-checkbox), so idempotency is trivially true. Pin
2649        // it anyway as a smoke test against future regressions.
2650        assert_idempotent(
2651            "- [ ] Lorem ipsum dolor sit amet, consectetur adipiscing\n      tempor incididunt ut labore.\n",
2652        );
2653    }
2654
2655    #[test]
2656    fn idempotent_non_task_list_still_holds() {
2657        // Non-task items never enter the task_col code path; sanity-check
2658        // that idempotency is preserved for them too.
2659        assert_idempotent("1. Item\n    over-indented\n");
2660        assert_idempotent("- Item\n\n continuation\n");
2661    }
2662
2663    // ── Non-task idempotency: loose-mode under-indent ────────────────
2664    //
2665    // When a blank line precedes the continuation (loose mode),
2666    // under-indented content is flagged and fixed up to the content
2667    // column. Idempotency pins that one pass of the fix is sufficient.
2668
2669    #[test]
2670    fn idempotent_non_task_loose_under_indent_ordered() {
2671        // 1. Item → content col 3; "  x" is 2 spaces, under content col.
2672        assert_idempotent("1. Item\n\n  continuation\n");
2673    }
2674
2675    #[test]
2676    fn idempotent_non_task_loose_under_indent_multi_digit() {
2677        // 10. Item → content col 4; single-space continuation needs 4.
2678        assert_idempotent("10. Item\n\n continuation\n");
2679    }
2680
2681    #[test]
2682    fn idempotent_non_task_tight_over_indent_ordered() {
2683        // Tight-mode over-indent: 5 spaces where content col is 3.
2684        assert_idempotent("1. Item\n     over-indented\n");
2685    }
2686
2687    // ── Non-task idempotency: fenced code block compound fix ─────────
2688    //
2689    // A fence opener that needs re-indenting is repaired by the
2690    // compound-fence fix which shifts opener + interior + closer
2691    // together. Idempotency pins that the compound fix settles in one
2692    // pass and does not oscillate between runs.
2693
2694    #[test]
2695    fn idempotent_non_task_fence_ordered_loose() {
2696        // 1. Item → content col 3; fence at col 2 needs to shift to 3.
2697        assert_idempotent("1. Item\n\n  ```rust\n  let x = 1;\n  ```\n");
2698    }
2699
2700    #[test]
2701    fn idempotent_non_task_fence_tilde_under_indent() {
2702        // Tilde fences use the same compound-fix path as backtick fences.
2703        // Interior below the list scope (col 0 here, required col 3) must
2704        // be promoted up in the same pass as the fence delimiters —
2705        // otherwise a second pass would flag the interior individually
2706        // and defeat idempotency.
2707        assert_idempotent("1. Item\n\n  ~~~\nplain text\n  ~~~\n");
2708    }
2709
2710    #[test]
2711    fn idempotent_non_task_fence_interior_above_required() {
2712        // Interior already above the required column must not be pushed
2713        // further up by the compound fix — authored interior indentation
2714        // is preserved when it doesn't threaten fence pairing.
2715        assert_idempotent("1. Item\n\n  ```\n    deeply indented code\n  ```\n");
2716    }
2717
2718    #[test]
2719    fn fence_fix_promotes_interior_below_scope_in_single_pass() {
2720        // Concrete behavioral check, not just idempotency:
2721        // interior at col 0 with opener at col 2, required 3, must land
2722        // at col 3 (same as opener) so fence pairing is preserved.
2723        let content = "1. Item\n\n  ```\ncode\n  ```\n";
2724        let fixed = fix(content);
2725        assert_eq!(fixed, "1. Item\n\n   ```\n   code\n   ```\n");
2726    }
2727
2728    #[test]
2729    fn fence_fix_preserves_interior_offset_from_the_fence() {
2730        // Opener at col 2 → col 3 (required). Interior at col 4 sits two
2731        // columns inside the fence and must still sit two columns inside it
2732        // afterwards, at col 5: leaving it at col 4 would silently flatten
2733        // one level of the code block's own indentation.
2734        let content = "1. Item\n\n  ```\n    code\n  ```\n";
2735        let fixed = fix(content);
2736        assert_eq!(fixed, "1. Item\n\n   ```\n     code\n   ```\n");
2737    }
2738
2739    // ── Non-task idempotency: MkDocs strict-indent ───────────────────
2740    //
2741    // Under MkDocs flavor, continuation requires max(content_col, 4),
2742    // which can force a fix even when CommonMark would accept the
2743    // content. Pin idempotency for the non-task path there too.
2744
2745    #[test]
2746    fn idempotent_non_task_mkdocs_ordered_at_3_spaces() {
2747        // CommonMark-valid (3 spaces) but MkDocs demands 4 → fix runs.
2748        assert_idempotent_mkdocs("1. Item\n\n   continuation\n");
2749    }
2750
2751    #[test]
2752    fn idempotent_non_task_mkdocs_unordered_at_2_spaces() {
2753        // "- Item" → content col 2, but MkDocs raises the floor to 4.
2754        assert_idempotent_mkdocs("- Item\n\n  continuation\n");
2755    }
2756
2757    #[test]
2758    fn idempotent_non_task_mkdocs_fence_compound() {
2759        // MkDocs non-task fence: opener/interior/closer shift together.
2760        assert_idempotent_mkdocs("1. Item\n\n   ```toml\n   k = 1\n   ```\n");
2761    }
2762
2763    // ── style = "aligned" ──────────────────────────────────────────────
2764
2765    #[test]
2766    fn aligned_tight_zero_indent_continuation_flagged() {
2767        // The core #682 case: a tight 0-indent lazy continuation is valid
2768        // CommonMark (and accepted by `any`), but `aligned` requires it to sit
2769        // at the content column.
2770        let content = "- this is a long line\nthat continues on a second line\n";
2771        let warnings = check_aligned(content);
2772        assert_eq!(warnings.len(), 1);
2773        assert_eq!(warnings[0].line, 2);
2774        assert_eq!(
2775            fix_aligned(content),
2776            "- this is a long line\n  that continues on a second line\n"
2777        );
2778    }
2779
2780    #[test]
2781    fn aligned_full_issue_example_made_consistent() {
2782        // The full #682 example: every continuation line is snapped to the
2783        // content column, producing the consistent result the reporter wants.
2784        let content = "- this is a long line\n\
2785                       that continues on a second line\n\
2786                       - this is another long line\n\
2787                       \x20\x20that continues on the next line\n\
2788                       - yet again a long line\n\
2789                       and still inconsistently spaced\n\
2790                       \x20\x20and even worse\n";
2791        let expected = "- this is a long line\n\
2792                        \x20\x20that continues on a second line\n\
2793                        - this is another long line\n\
2794                        \x20\x20that continues on the next line\n\
2795                        - yet again a long line\n\
2796                        \x20\x20and still inconsistently spaced\n\
2797                        \x20\x20and even worse\n";
2798        assert_eq!(fix_aligned(content), expected);
2799        // And the fix is a fixpoint.
2800        assert_eq!(fix_aligned(expected), expected);
2801    }
2802
2803    #[test]
2804    fn aligned_already_aligned_not_flagged() {
2805        let content = "- item\n  continuation at content column\n";
2806        assert!(check_aligned(content).is_empty());
2807    }
2808
2809    #[test]
2810    fn aligned_tight_partial_indent_flagged() {
2811        // 1 space under "- " (content col 2): tight partial under-indent.
2812        let content = "- item\n continuation\n";
2813        let warnings = check_aligned(content);
2814        assert_eq!(warnings.len(), 1);
2815        assert_eq!(fix_aligned(content), "- item\n  continuation\n");
2816    }
2817
2818    #[test]
2819    fn aligned_post_blank_zero_indent_still_new_paragraph() {
2820        // After a blank line, 0-indent content is a NEW paragraph that has left
2821        // the list - aligned mode must not reindent it back into the item.
2822        let content = "- item\n\nnew paragraph\n";
2823        assert!(check_aligned(content).is_empty());
2824        assert_eq!(fix_aligned(content), content);
2825    }
2826
2827    // ── aligned: structural blocks must NOT be pulled into the list ────
2828
2829    #[test]
2830    fn aligned_top_level_blockquote_after_list_untouched() {
2831        // A blockquote tight after a list item is a list-breaking separator,
2832        // not continuation. `in_list_block` is false for it, so aligned mode
2833        // must leave it alone (no reindent into the item).
2834        let content = "- item\n> quote\n";
2835        assert!(check_aligned(content).is_empty());
2836        assert_eq!(fix_aligned(content), content);
2837    }
2838
2839    #[test]
2840    fn aligned_top_level_fence_after_list_untouched() {
2841        let content = "- item\n```\ncode\n```\n";
2842        assert!(check_aligned(content).is_empty());
2843        assert_eq!(fix_aligned(content), content);
2844    }
2845
2846    #[test]
2847    fn aligned_top_level_table_after_list_untouched() {
2848        let content = "- item\n| a | b |\n|---|---|\n| 1 | 2 |\n";
2849        assert!(check_aligned(content).is_empty());
2850        assert_eq!(fix_aligned(content), content);
2851    }
2852
2853    // ── aligned: nested lists ──────────────────────────────────────────
2854
2855    #[test]
2856    fn aligned_nested_tight_lazy_continuation_aligns_to_inner() {
2857        // A tight lazy continuation after a nested item is, per CommonMark,
2858        // continuation of the innermost open item, so it aligns to the inner
2859        // item's content column (4), not the outer's (2). The outer item must
2860        // defer to the inner one.
2861        let content = "- Outer\n  - Inner\ncontinuation\n";
2862        let warnings = check_aligned(content);
2863        assert_eq!(warnings.len(), 1);
2864        assert_eq!(fix_aligned(content), "- Outer\n  - Inner\n    continuation\n");
2865    }
2866
2867    #[test]
2868    fn aligned_nested_continuation_already_aligned_not_flagged() {
2869        let content = "- L1\n  - L2\n    cont of L2 at 4\n";
2870        assert!(check_aligned(content).is_empty());
2871    }
2872
2873    #[test]
2874    fn aligned_nested_idempotent() {
2875        let content = "- Outer\n  - Inner\ncontinuation\n";
2876        let once = fix_aligned(content);
2877        assert_eq!(fix_aligned(&once), once);
2878    }
2879
2880    #[test]
2881    fn aligned_three_level_nesting_aligns_to_innermost() {
2882        // The saw_nested cascade defers through every level, so the tight lazy
2883        // line aligns to L3's content column (6), the innermost open item.
2884        let content = "- L1\n  - L2\n    - L3\ncont\n";
2885        assert_eq!(fix_aligned(content), "- L1\n  - L2\n    - L3\n      cont\n");
2886    }
2887
2888    #[test]
2889    fn aligned_continuation_after_sibling_owned_by_last_item() {
2890        // Tight lazy continuation after the second sibling belongs to that
2891        // sibling (no nesting), so it aligns to its content column (2).
2892        let content = "- a\n- b\nlazy\n";
2893        assert_eq!(fix_aligned(content), "- a\n- b\n  lazy\n");
2894    }
2895
2896    #[test]
2897    fn aligned_multi_digit_ordered_marker_aligns_to_content_column() {
2898        let content = "10. Item\nwrap\n";
2899        assert_eq!(fix_aligned(content), "10. Item\n    wrap\n");
2900    }
2901
2902    #[test]
2903    fn aligned_latent_setext_underline_is_left_alone() {
2904        // The underline is a lazy continuation, so the whole item renders as one
2905        // paragraph and no heading exists. Reindenting `Text` into the item would
2906        // promote the pair to a setext heading, changing the rendered structure,
2907        // so the item is skipped.
2908        let content = "- item\nText\n===\n";
2909        assert!(check_aligned(content).is_empty());
2910        assert_eq!(fix_aligned(content), content);
2911    }
2912
2913    #[test]
2914    fn aligned_reindents_prose_that_only_looks_like_an_underline() {
2915        // Positive control for the guard above: `= = =` has internal spaces, so
2916        // it is not an underline and cannot be promoted. The item is reindented.
2917        let content = "- item\nText\n= = =\n";
2918        assert_eq!(fix_aligned(content), "- item\n  Text\n  = = =\n");
2919    }
2920
2921    #[test]
2922    fn aligned_latent_marker_in_continuation_is_idempotent() {
2923        // Regression (fuzzer-found): a list-marker-looking line ("2. ") that the
2924        // parser absorbs as lazy continuation can be promoted to a real list
2925        // item once an earlier continuation line is reindented, re-attributing
2926        // later lines and breaking single-pass idempotency. Such items are not
2927        // reindented at all.
2928        let content = "# \n- \n``\n2. \n![]()";
2929        let once = fix_aligned(content);
2930        assert_eq!(fix_aligned(&once), once, "fix must be idempotent in one pass");
2931        assert_eq!(once, content, "item with a latent marker is left untouched");
2932    }
2933
2934    #[test]
2935    fn aligned_latent_table_in_continuation_is_idempotent() {
2936        // Regression (fuzzer-found): a table-looking line that the parser
2937        // absorbs as lazy continuation (so it is NOT yet `in_table_block`) can
2938        // be promoted to a real table once an earlier line is reindented. Like
2939        // a latent marker, this re-parses the block, so the item is left alone.
2940        let content = "- \n![`]()\n|  | ` |\n| --- | --- |";
2941        let once = fix_aligned(content);
2942        assert_eq!(fix_aligned(&once), once, "fix must be idempotent in one pass");
2943        assert_eq!(once, content, "item with a latent table is left untouched");
2944    }
2945
2946    #[test]
2947    fn aligned_blockquote_nested_list_not_touched() {
2948        // Lists inside blockquotes are a documented MD077 limitation (the scan
2949        // breaks at the blockquote prefix). Aligned inherits that conservative
2950        // no-false-positive behavior.
2951        let content = "> - item\n> wrap\n";
2952        assert!(check_aligned(content).is_empty());
2953        assert_eq!(fix_aligned(content), content);
2954    }
2955
2956    // ── aligned: GFM task list items ───────────────────────────────────
2957
2958    #[test]
2959    fn aligned_task_post_checkbox_column_accepted() {
2960        // Continuation aligned to the post-checkbox column (content_col + 4 = 6)
2961        // stays valid so aligned mode doesn't fight MD013 reflow.
2962        let content = "- [ ] Task\n      wrap\n";
2963        assert!(check_aligned(content).is_empty());
2964        assert_eq!(fix_aligned(content), content);
2965    }
2966
2967    #[test]
2968    fn aligned_task_under_indent_snaps_to_content_column() {
2969        let content = "- [ ] Task\nwrap\n";
2970        let warnings = check_aligned(content);
2971        assert_eq!(warnings.len(), 1);
2972        assert_eq!(fix_aligned(content), "- [ ] Task\n  wrap\n");
2973    }
2974
2975    // ── aligned: MkDocs flavor ─────────────────────────────────────────
2976
2977    #[test]
2978    fn aligned_mkdocs_tight_under_indent_snaps_to_four() {
2979        // MkDocs raises the required indent to max(content_col, 4) = 4 for "- ".
2980        let content = "- item\nwrap\n";
2981        let warnings = check_aligned_mkdocs(content);
2982        assert_eq!(warnings.len(), 1);
2983        let ctx = LintContext::new(content, MarkdownFlavor::MkDocs, None);
2984        assert_eq!(aligned_rule().fix(&ctx).unwrap(), "- item\n    wrap\n");
2985    }
2986
2987    // ── any (default) regression: tight lazy continuation untouched ────
2988
2989    #[test]
2990    fn any_default_does_not_flag_tight_lazy_continuation() {
2991        // The default style must preserve CommonMark lazy continuation.
2992        let content = "- item\nwrapped at zero indent\n";
2993        assert!(check(content).is_empty());
2994        assert_eq!(fix(content), content);
2995    }
2996
2997    #[test]
2998    fn from_config_aligned_enables_tight_flagging() {
2999        // End-to-end: `[MD077] style = "aligned"` wires through from_config.
3000        let mut config = crate::config::Config::default();
3001        let mut rule_config = crate::config::RuleConfig::default();
3002        rule_config
3003            .values
3004            .insert("style".to_string(), toml::Value::String("aligned".to_string()));
3005        config.rules.insert("MD077".to_string(), rule_config);
3006
3007        let rule = MD077ListContinuationIndent::from_config(&config);
3008        let ctx = LintContext::new("- item\nwrap\n", MarkdownFlavor::Standard, None);
3009        assert_eq!(rule.check(&ctx).unwrap().len(), 1);
3010    }
3011
3012    #[test]
3013    fn from_config_default_is_any() {
3014        // No config -> default `any` -> tight lazy continuation untouched.
3015        let config = crate::config::Config::default();
3016        let rule = MD077ListContinuationIndent::from_config(&config);
3017        let ctx = LintContext::new("- item\nwrap\n", MarkdownFlavor::Standard, None);
3018        assert!(rule.check(&ctx).unwrap().is_empty());
3019    }
3020
3021    #[test]
3022    fn from_config_indent_sets_fixed_requirement() {
3023        // End-to-end: `[MD077] indent = 4` wires through from_config and
3024        // requires continuation content to sit 4 spaces past the marker.
3025        let mut config = crate::config::Config::default();
3026        let mut rule_config = crate::config::RuleConfig::default();
3027        rule_config.values.insert("indent".to_string(), toml::Value::Integer(4));
3028        config.rules.insert("MD077".to_string(), rule_config);
3029
3030        let rule = MD077ListContinuationIndent::from_config(&config);
3031
3032        // "- item\n    wrap\n" -> continuation at 4 spaces: accepted.
3033        let ok_ctx = LintContext::new("- item\n    wrap\n", MarkdownFlavor::Standard, None);
3034        assert!(rule.check(&ok_ctx).unwrap().is_empty());
3035        assert_eq!(rule.fix(&ok_ctx).unwrap(), "- item\n    wrap\n");
3036
3037        // "- item\n\n  wrap\n" -> continuation at 2 spaces after a blank line:
3038        // flagged as under-indented (would escape the list item).
3039        let bad_ctx = LintContext::new("- item\n\n  wrap\n", MarkdownFlavor::Standard, None);
3040        let warnings = rule.check(&bad_ctx).unwrap();
3041        assert_eq!(warnings.len(), 1);
3042        assert!(warnings[0].message.contains("needs 4 spaces"));
3043    }
3044
3045    #[test]
3046    fn from_config_indent_applies_per_nested_marker() {
3047        // With a fixed indent, each item's requirement is its own marker
3048        // column plus the configured indent (nested item marker at 2 -> 6).
3049        let mut config = crate::config::Config::default();
3050        let mut rule_config = crate::config::RuleConfig::default();
3051        rule_config.values.insert("indent".to_string(), toml::Value::Integer(4));
3052        config.rules.insert("MD077".to_string(), rule_config);
3053
3054        let rule = MD077ListContinuationIndent::from_config(&config);
3055        let ctx = LintContext::new("- a\n  - b\n      wrap\n", MarkdownFlavor::Standard, None);
3056        let warnings = rule.check(&ctx).unwrap();
3057        assert!(
3058            warnings.is_empty(),
3059            "continuation at 6 spaces should pass: {warnings:?}"
3060        );
3061    }
3062
3063    /// A rule configured through the real `[MD077]` config path.
3064    fn rule_with(settings: &[(&str, toml::Value)]) -> Box<dyn Rule> {
3065        let mut config = crate::config::Config::default();
3066        let mut rule_config = crate::config::RuleConfig::default();
3067        for (key, value) in settings {
3068            rule_config.values.insert((*key).to_string(), value.clone());
3069        }
3070        config.rules.insert("MD077".to_string(), rule_config);
3071        MD077ListContinuationIndent::from_config(&config)
3072    }
3073
3074    #[test]
3075    fn configured_indent_cannot_lower_the_strict_flavor_minimum() {
3076        // The MkDocs 4-space minimum is what the renderer needs to read a line
3077        // as part of the item, so `indent = 2` may not pull it down to 2 - that
3078        // would have `fix` rewrite a rendering document into a broken one.
3079        let rule = rule_with(&[("indent", toml::Value::Integer(2))]);
3080
3081        let two = LintContext::new("- item\n\n  wrap\n", MarkdownFlavor::MkDocs, None);
3082        let warnings = rule.check(&two).unwrap();
3083        assert_eq!(warnings.len(), 1, "2 spaces is below the MkDocs minimum: {warnings:?}");
3084        assert!(
3085            warnings[0].message.contains("needs 4 spaces") && warnings[0].message.contains("MkDocs"),
3086            "the requirement comes from MkDocs, so the message must say so: {}",
3087            warnings[0].message
3088        );
3089        assert_eq!(rule.fix(&two).unwrap(), "- item\n\n    wrap\n");
3090
3091        // Control: a document already at the minimum is left alone.
3092        let four = LintContext::new("- item\n\n    wrap\n", MarkdownFlavor::MkDocs, None);
3093        assert!(rule.check(&four).unwrap().is_empty());
3094
3095        // Control: the clamp is scoped to the strict flavor. Under the standard
3096        // flavor the same setting means exactly what it says, and 4 is then an
3097        // over-indent to be snapped back to 2.
3098        let standard = LintContext::new("- item\n\n    wrap\n", MarkdownFlavor::Standard, None);
3099        assert_eq!(rule.check(&standard).unwrap().len(), 1);
3100        assert_eq!(rule.fix(&standard).unwrap(), "- item\n\n  wrap\n");
3101    }
3102
3103    #[test]
3104    fn configured_indent_can_raise_the_strict_flavor_minimum() {
3105        // The clamp is a floor, not a fixed value: above 4 the configured indent
3106        // still governs under the MkDocs flavor.
3107        let rule = rule_with(&[("indent", toml::Value::Integer(6))]);
3108        let ctx = LintContext::new("- item\n\n    wrap\n", MarkdownFlavor::MkDocs, None);
3109        let warnings = rule.check(&ctx).unwrap();
3110        assert_eq!(warnings.len(), 1);
3111        assert!(
3112            warnings[0].message.contains("needs 6 spaces"),
3113            "configured 6 must win over the 4-space floor: {}",
3114            warnings[0].message
3115        );
3116        assert_eq!(rule.fix(&ctx).unwrap(), "- item\n\n      wrap\n");
3117    }
3118
3119    #[test]
3120    fn configured_indent_message_does_not_claim_a_structural_consequence() {
3121        // Two spaces under a `- ` marker keeps the content in the list item, so
3122        // a message blaming list membership would be false. The requirement is
3123        // the configured one and the message has to say which.
3124        let rule = rule_with(&[("indent", toml::Value::Integer(4))]);
3125        let ctx = LintContext::new("- item\n\n  wrap\n", MarkdownFlavor::Standard, None);
3126        let warnings = rule.check(&ctx).unwrap();
3127        assert_eq!(warnings.len(), 1);
3128        assert!(
3129            warnings[0].message.contains("match the configured indent"),
3130            "expected the configured-indent wording, got: {}",
3131            warnings[0].message
3132        );
3133        assert!(
3134            !warnings[0].message.contains("remain part of the list"),
3135            "the content does remain part of the list here: {}",
3136            warnings[0].message
3137        );
3138
3139        // Control: the default rule reports the same document as clean, which is
3140        // what makes the structural claim wrong above.
3141        assert!(check("- item\n\n  wrap\n").is_empty());
3142
3143        // Control: content that really would escape the item still gets the
3144        // structural message, so the wording was narrowed and not replaced.
3145        let escaping = check("- item\n\n wrap\n");
3146        assert_eq!(escaping.len(), 1);
3147        assert!(
3148            escaping[0].message.contains("remain part of the list"),
3149            "unconfigured under-indent keeps its structural message, got: {}",
3150            escaping[0].message
3151        );
3152    }
3153
3154    #[test]
3155    fn configured_indent_leaves_tight_lazy_continuation_to_style() {
3156        // `indent` sets what the requirement is; `style` decides which lines are
3157        // measured against it. A tight continuation is CommonMark lazy
3158        // continuation, so `any` accepts it however the requirement was derived.
3159        let any = rule_with(&[("indent", toml::Value::Integer(4))]);
3160        let ctx = LintContext::new("- item\n  wrap\n", MarkdownFlavor::Standard, None);
3161        assert!(
3162            any.check(&ctx).unwrap().is_empty(),
3163            "style = any accepts tight lazy continuation"
3164        );
3165
3166        let aligned = rule_with(&[
3167            ("indent", toml::Value::Integer(4)),
3168            ("style", toml::Value::String("aligned".to_string())),
3169        ]);
3170        let warnings = aligned.check(&ctx).unwrap();
3171        assert_eq!(warnings.len(), 1, "style = aligned raises it: {warnings:?}");
3172        assert!(warnings[0].message.contains("expected 4"));
3173        assert_eq!(aligned.fix(&ctx).unwrap(), "- item\n    wrap\n");
3174    }
3175
3176    #[test]
3177    fn aligned_tight_underindented_fence_inside_item_left_alone() {
3178        // A fenced block is a structural construct; aligned mode does not
3179        // reindent it (the `in_code_block` guard), so a tight under-indented
3180        // fence is a no-op rather than a risky delimiter-only rewrite.
3181        let content = "- item\n ```\n code\n ```\n";
3182        assert!(check_aligned(content).is_empty());
3183        assert_eq!(fix_aligned(content), content);
3184    }
3185
3186    #[test]
3187    fn aligned_task_under_indent_fix_is_idempotent() {
3188        let content = "- [ ] Task\nwrap\n";
3189        let once = fix_aligned(content);
3190        assert_eq!(fix_aligned(&once), once);
3191    }
3192
3193    #[test]
3194    fn aligned_partial_indent_fix_is_idempotent() {
3195        let content = "- item\n continuation\n";
3196        let once = fix_aligned(content);
3197        assert_eq!(fix_aligned(&once), once);
3198    }
3199
3200    #[test]
3201    fn fence_shift_preserves_one_space_of_interior_nesting() {
3202        // Reindenting the fence moves the whole block: an interior line keeps
3203        // its offset from the opener, so one space of JSON nesting survives.
3204        let content = "1. Configure:\n\n  ```json\n  {\n   \"a\": 1\n  }\n  ```\n";
3205        let expected = "1. Configure:\n\n   ```json\n   {\n    \"a\": 1\n   }\n   ```\n";
3206        assert_eq!(fix(content), expected);
3207    }
3208
3209    #[test]
3210    fn fence_shift_preserves_every_interior_nesting_level() {
3211        let content = "1. Configure:\n\n  ```json\n  {\n    \"a\": {\n      \"b\": 1\n    }\n  }\n  ```\n";
3212        let expected = "1. Configure:\n\n   ```json\n   {\n     \"a\": {\n       \"b\": 1\n     }\n   }\n   ```\n";
3213        assert_eq!(fix(content), expected);
3214    }
3215
3216    #[test]
3217    fn fence_shift_lifts_interior_below_the_list_scope_all_the_way() {
3218        // The case the promote-up rule was written for: an interior line
3219        // starting below the item's content column must land at that column,
3220        // not one space above where it began.
3221        let content = "1. Configure:\n\n  ```json\n{\n  ```\n";
3222        let expected = "1. Configure:\n\n   ```json\n   {\n   ```\n";
3223        assert_eq!(fix(content), expected);
3224        assert_eq!(fix(expected), expected, "and the result is stable");
3225    }
3226
3227    #[test]
3228    fn fence_shift_is_idempotent() {
3229        for content in [
3230            "1. Configure:\n\n  ```json\n  {\n   \"a\": 1\n  }\n  ```\n",
3231            "1. Configure:\n\n  ```json\n{\n    deep\n  }\n  ```\n",
3232            "- item\n\n ```\n  nested\n ```\n",
3233        ] {
3234            let once = fix(content);
3235            assert_eq!(fix(&once), once, "MD077 fence fix must be idempotent: {content:?}");
3236        }
3237    }
3238
3239    #[test]
3240    fn fence_already_at_the_content_column_is_left_alone() {
3241        let content = "1. Configure:\n\n   ```json\n   {\n    \"a\": 1\n   }\n   ```\n";
3242        assert!(check(content).is_empty());
3243        assert_eq!(fix(content), content);
3244    }
3245
3246    #[test]
3247    fn over_indented_fence_keeps_its_interior_untouched() {
3248        // The over-indent direction deliberately leaves fenced blocks alone.
3249        let content = "1. Configure:\n\n       ```json\n       {\n        \"a\": 1\n       }\n       ```\n";
3250        assert_eq!(fix(content), content);
3251    }
3252}