Repository navigation
feat: add --lines flag to dprint fmt for range formatting - #1329
Merged
Merged
Conversation
Adds `--lines [file-path:]first:last[,first:last]...` to only format ranges of lines in files. It may be specified multiple times for multiple files, the file path may be omitted when formatting a single file or `--stdin`, and lines of `--stdin-files` may end with the lines to format. Closes #1312
The path of a line range is also used to find the files to format, so a glob or directory would have had its files formatted in their entirety. This is detected using the existing canonicalize call and the found files in order to not make any additional system calls.
jakubkaczor
reviewed
Oct 11, 2026
| } | ||
|
|
||
| // a single line number is a range of that one line | ||
| let (first, last) = text.split_once(':').unwrap_or((text, text)); |
There was a problem hiding this comment.
The choice of : as first line–last line range separator is a bit unfortunate. In text editors it usually stands for row-column conjunction, e.g. in [Neo]Vi[m], Helix and Zed. It is already used in Ruff for this specific purpose, e.g. ruff format —lines 2:4-4:2. I think - would be better instead of :.
An excerpt from Ruff’s —help for reference:
--range <RANGE>
When specified, Ruff will try to only format the code in the given range.
It might be necessary to extend the start backwards or the end forwards, to fully enclose a logical line.
The `<RANGE>` uses the format `<start_line>:<start_column>-<end_line>:<end_column>`.
- The line and column numbers are 1 based.
- The column specifies the nth-unicode codepoint on that line.
- The end offset is exclusive.
- The column numbers are optional. You can write `--range=1-2` instead of `--range=1:1-2:1`.
- The end position is optional. You can write `--range=2` to format the entire document starting from the second line.
- The start position is optional. You can write `--range=-3` to format the first three lines of the document.
The option can only be used when formatting a single file. Range formatting of notebooks is unsupported.
Member
Author
There was a problem hiding this comment.
Oh, I didn't know there was prior art. I'll pattern off ruff. I think it's fine to break this because nobody is using it (and I'll do a release today).
Member
Author
There was a problem hiding this comment.
I opened #1341 to break this and use a dash. Thanks for the feedback!
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.
Closes #1312
Adds a
--linesflag todprint fmtfor only formatting ranges of lines, which makes it usable with tools that format only modified lines (ex.jj fixwithline-range-args).--linesare added to the files to format. Other files provided on the command line are formatted in their entirety.Tested manually with
npm:@dprint/[email protected], which only formatted the requested lines for multiple files, multiple ranges in a file,--stdin-files, and--stdin.