feat: support range formatting - #70
Merged
Merged
Conversation
Formats only the object properties or array elements that a range touches within the innermost object or array containing it, leaving the rest of the file as written. The whole object or array is formatted when its members would be reordered (ex. package.json conventions) or the line breaks around them change, and the whole file when the range reaches the root value's brackets.
Replacing only the touched members left their indentation, separators and trailing comments unformatted, so a selected line kept a wrong indent, a multi-line member came out indented inconsistently and trailing commas weren't added or removed. The lines of the members are replaced instead, widening to the member holding their object or array when the line breaks around them change and keeping the file's line endings. Also leaves a range outside the root value unformatted.
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.
Adds range formatting (ex. an editor's "format selection").
How it works
format_text_rangefinds the innermost object or array containing the range and the properties or elements the range touches. It formats the whole file as usual, finds the same members in the output by path (property name and occurrence, or array index), and replaces only the lines those members are on. So the first member's indentation, the separator after the last one and its trailing comment get formatted too, while everything else is left as written.It widens further when replacing just those lines would give a wrong or inconsistent result:
A range reaching the root value's brackets formats the whole file, and one entirely outside the root value (ex. a leading comment) changes nothing. The replaced text uses the file's existing line endings, since changing those is a whole-file concern.
wasm_plugin.rsnow passesrequest.rangetoformat_text_rangeinstead of formatting the whole file.Tests
Spec files in
tests/specs/rangeuse the new[|/|]range markers from dprint-development 0.12 (dprint/dprint#1255). The BOM and line ending cases are unit tests since spec files can't express them.I also ran a throwaway brute-force check of ~3.5M ranges across all spec inputs and 15 configs: no panics, results always parse to the same values, and the only non-idempotent cases come from an existing full-format bug with
preferSingleLineand trailing comments, unrelated to this change.