fix: dedent indented code blocks before formatting - #1722
Open
mokevnin wants to merge 1 commit into
Open
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1702.
Problem
A code block nested in a list item keeps its list indentation when it is handed to the formatter, and
indent_codeblockputs the same indentation back afterwards. Every line except the first is indented twice, so the file never converges —mdsf verifyreports changes however many timesmdsf formathas run.After one
mdsf formatthe block readsa = 1at 4 spaces andb = 2at 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:
ruffreads the extra indentation asIndentationError, so the block is silently left unformatted.Change
dedent_line);Checks
cargo test -p mdsf --libpasses, including four new tests forparse_generic_codeblockandindent_codeblock.custom::test_custom_tool::with_stdin_truefails 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 formatpass followed bymdsf verifyleft 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.