Skip to content

feat: support range formatting - #286

Merged
dsherret merged 3 commits into
mainfrom
range-formatting
Oct 3, 2026
Merged

dsherret merged 3 commits into
mainfrom
range-formatting

Conversation

@dsherret

@dsherret dsherret commented Oct 3, 2026

Copy link
Copy Markdown
Member

Adds range formatting, like dprint/dprint-plugin-json#70, and upgrades dprint-core to 0.70.0 (and dprint-development to 0.12.1).

format_text_range finds the blocks the range touches. It starts with the top-level blocks and goes into lists → items (their children and sub-lists) → block quote children. It formats the whole file, takes the formatted text of those blocks (from the start of the first block's line to the end of the last block's line) and puts it in place of the original. The result is reparsed to check that the blocks along the path are the same kinds, that the other blocks kept their exact text, and that the replaced blocks read back as formatted. For nested blocks, every sibling's line prefix (the container's indentation and > markers) must already be what the formatter writes. If a check fails, it tries the enclosing block, and so on out to formatting the whole file (ex. changing * items to - would split the list in two, so the whole list is formatted).

  • Only code blocks (and front matter) inside the formatted region are sent to the host's formatter. If the region widens to an outer block, it's formatted again with that block's code blocks included.
  • A range that touches the first and last top-level block formats the whole file. A range that only touches blank lines between top-level blocks formats nothing.
  • The BOM and the file's line endings are kept outside the formatted region. Files with a bare \r line ending are formatted whole.

I stress-tested it with about 25M range/config combinations over the spec files, the CommonMark examples, hand-written edge cases and generated documents, checking for panics, changes outside the touched blocks, the rendered HTML staying the same, and agreement with full formatting. Known minor cases left as is:

  • A range nested inside a file's only top-level block doesn't add the final newline.
  • Empty > lines at the edges of a block quote can stay after a partial format.
  • In heavily malformed nested lists, a later range format can widen further than the first one did.

@dsherret
dsherret merged commit 46939fd into main Oct 3, 2026
2 checks passed
@dsherret
dsherret deleted the range-formatting branch October 3, 2026 16:50
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.

1 participant