Skip to content

feat: support range formatting - #40

Merged
dsherret merged 1 commit into
mainfrom
feat-range-formatting
Oct 4, 2026
Merged

dsherret merged 1 commit into
mainfrom
feat-range-formatting

Conversation

@dsherret

@dsherret dsherret commented Oct 4, 2026

Copy link
Copy Markdown
Member

Adds range (selection) formatting, following dprint/dprint-plugin-json#70 and the same feature in dprint-plugin-markdown, and upgrades dprint-core to 0.70.0.

Behaviour

format_text_range widens the range to the lines of the instructions and comments it touches and only replaces those lines. The text outside of them is left as it was, including its line endings and BOM.

  • A cursor (empty range) formats the instruction or comment whose lines it is on, including continuation lines and heredoc bodies.
  • A range touching everything from the first instruction or comment to the last formats the whole file.
  • A range that only touches blank lines formats nothing.
  • A comment following an instruction on its last line is formatted along with it, since whole lines are replaced.
  • With indentStages, the touched lines get the indentation they'd have when formatting the whole file.
  • A file with a lone carriage return is formatted whole, since the parser breaks a line there and the line lookups don't.

Implementation

generate is split into top_level_nodes and generate_nodes, so only the touched top-level nodes are generated and printed, then spliced over their lines. Nothing is formatted and re-parsed to find the replacement. Whole-file formatting goes through the same two functions and is unchanged.

Other changes

  • dprint-core 0.69.0 -> 0.70.0 (FileMatchingInfo.additive) and dprint-development 0.11.0 -> 0.12.1 (range markers in specs).
  • Fixes cargo check --features tracing --tests, which failed on a parse_config_key_map function that no longer exists.

Tests

  • tests/specs/Range/: 23 spec files covering every instruction kind and form, comments, heredocs, the escape directive, lines that can't be parsed, and the lineWidth, indentStages, indentWidth and healthcheckCmdNewLine options.
  • Unit tests in src/format_range.rs for what specs can't express: BOM, CRLF, lone carriage returns, range clamping, a missing final newline, and None results.
  • tests/range_test.rs: formats every cursor position and line selection of every spec's text (plus CRLF, BOM and trailing-text variants) and checks that the result formats to the same text as formatting the whole file, and that line endings and the BOM are kept. Takes about 8s in a debug build.

Not addressed

On a file that already mixes line endings, the line breaks within a touched multi-line instruction are written with the file's dominant line ending.

Formats only the lines of the instructions and comments a range touches, and upgrades dprint-core to 0.70.0.
@dsherret
dsherret merged commit 2ecd931 into main Oct 4, 2026
2 checks passed
@dsherret
dsherret deleted the feat-range-formatting branch October 4, 2026 17:05
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