Skip to content

feat: add --lines flag to dprint fmt for range formatting - #1329

Merged
dsherret merged 2 commits into
mainfrom
feat-fmt-lines
Oct 10, 2026
Merged

dsherret merged 2 commits into
mainfrom
feat-fmt-lines

Conversation

@dsherret

Copy link
Copy Markdown
Member

Closes #1312

Adds a --lines flag to dprint fmt for only formatting ranges of lines, which makes it usable with tools that format only modified lines (ex. jj fix with line-range-args).

# several ranges in several files
dprint fmt --lines src/main.ts:10:20,35:40,52 --lines src/other.ts:5:8
# the file path may be omitted for a single file or --stdin
dprint fmt --lines 10:20,35:40 src/main.ts
dprint fmt --stdin src/main.ts --lines=10:20 < src/main.ts
# lines of --stdin-files may end with the lines to format
printf 'src/main.ts:10:20,35:40\nsrc/other.ts\n' | dprint fmt --stdin-files
  • Lines are 1-based and inclusive. A bare number is a single line, ranges in a file are comma separated, and the flag may be repeated.
  • The files in --lines are added to the files to format. Other files provided on the command line are formatted in their entirety.
  • A range without a file path errors when more than one file is found.
  • Overlapping and adjacent ranges are merged, then formatted from the last to the first so the lines of the remaining ranges don't shift.
  • The incremental cache and the stable format pass are skipped when formatting line ranges.
  • The range is passed on to the plugin the same way as the LSP's range formatting, so what's formatted is up to the plugin.

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.

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.
@dsherret
dsherret merged commit 4d93d8e into main Oct 10, 2026
22 checks passed
@dsherret
dsherret deleted the feat-fmt-lines branch October 10, 2026 21:49
}

// a single line number is a range of that one line
let (first, last) = text.split_once(':').unwrap_or((text, text));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@dsherret dsherret Oct 11, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@dsherret dsherret Oct 11, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened #1341 to break this and use a dash. Thanks for the feedback!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Released.

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.

Support range formatting in the CLI

2 participants