Skip to content

fix: dedent indented code blocks before formatting - #1722

Open
mokevnin wants to merge 1 commit into
hougesen:mainfrom
Hexlet:fix-indented-codeblock
Open

fix: dedent indented code blocks before formatting#1722
mokevnin wants to merge 1 commit into
hougesen:mainfrom
Hexlet:fix-indented-codeblock

Conversation

@mokevnin

@mokevnin mokevnin commented Aug 29, 2026

Copy link
Copy Markdown

Fixes #1702.

Problem

A code block nested in a list item keeps its list indentation when it is handed to the formatter, and indent_codeblock puts the same indentation back afterwards. Every line except the first is indented twice, so the file never converges — mdsf verify reports changes however many times mdsf format has run.

1. Step

    ```python
    a = 1
    b = 2
    ```

After one mdsf format the block reads a = 1 at 4 spaces and b = 2 at 8.

Formatters that normalize indentation themselves (oxfmt, shfmt) mask it — they receive the broken snippet and straighten it out. Ones that do not, reject it: ruff reads the extra indentation as IndentationError, so the block is silently left unformatted.

Change

  • strip the fence indentation from every snippet line before it reaches the formatter (dedent_line);
  • stop adding indentation to empty lines — they used to come back as trailing whitespace.

Checks

cargo test -p mdsf --lib passes, including four new tests for parse_generic_codeblock and indent_codeblock. custom::test_custom_tool::with_stdin_true fails on my machine both with and without this change (missing tool binary), so it is unrelated.

Measured on a corpus of 247 markdown files that contain indented code blocks. With 0.12.1, one mdsf format pass followed by mdsf verify left 108 of them still reporting changes; with this change, 17 do. Those 17 all have a fence nested inside another fence, which is a separate limitation — the inner closing fence ends the outer block for the line scanner.

A code block nested in a list item is passed to the formatter with the
list indentation still attached, and `indent_codeblock` adds the same
indentation again afterwards. Every line but the first ends up indented
twice, so the file never converges: `mdsf verify` keeps reporting
changes no matter how many times `mdsf format` runs.

Formatters that normalize indentation themselves (oxfmt, shfmt) hide the
damage. Ones that do not, reject the snippet: `ruff` reads the extra
indentation as `IndentationError`, and the block is left unformatted.

Strip the fence indentation from every line of the snippet before it
reaches the formatter, and stop indenting empty lines, which used to
gain trailing whitespace.

Closes hougesen#1702

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect indent inside indented code block

1 participant