Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Add ability to specify --range option multiple times #12800

Open
afontenot opened this issue Aug 11, 2024 · 4 comments
Open

[feature] Add ability to specify --range option multiple times #12800

afontenot opened this issue Aug 11, 2024 · 4 comments
Labels
formatter Related to the formatter needs-design Needs further design before implementation wish Not on the current roadmap; maybe in the future

Comments

@afontenot
Copy link

This feature would increase feature parity with black, which supports multiply specifying the equivalent --line-ranges option.

Accepting multiple ranges is extremely useful for programmatically checking / modifying the format of the changed portions of a file when changes occur. For example, on some code bases a style guide has not been correctly adhered to historically, but project maintainers want to ensure that all new work adheres to ruff format checks.

Some discussion of this happened in the original --range pull request #9733, which I'll copy here:

The CLI only supports a single range. We can explore supporting multiple ranges in the future. The main challenge is that overlapping ranges invalidate the offset of whichever range gets formatted last (starting from the back helps but doesn't prevent it). This is especially a problem if the formatter has to extend the formatted range.

While I do agree that this adds some complexity, I think it's a valuable feature and I didn't see an open issue for it, so I decided to file one to hopefully push this forward.

@AlexWaygood AlexWaygood added cli Related to the command-line interface formatter Related to the formatter labels Aug 11, 2024
@MichaReiser
Copy link
Member

Thanks for opening this issue and doing the extra work of finding the PR where it was added.

Could you tell us a bit more about your workflow? It seems you're already calling ruff in an automated way.

@afontenot
Copy link
Author

Could you tell us a bit more about your workflow?

Sure! I don't have this in use on any large projects (yet), but I work on some Python projects that don't have an established code style currently. I've written a small program that calls git to extract the precise line numbers that have been changed, and turns these into ranges that can be passed into either black or ruff format.

To make sure my contributions to these projects follow a consistent style (I use the black style on all my personal stuff), I set up a pre-commit hook that runs this tool in a --check form. I can also pass --diff to it to see code style changes suggested by black or ruff or just accept the suggested changes. This can't be done without iteratively changing one section of code at a time in ruff. I think project maintainers who want to make sure PRs pass a style check without reformatting their whole code base might be interested in adding a tool like this to their test suite.

Some editors allow you to pass selections to the formatter, so that's a partial workaround for this issue. However, this doesn't allow a pre-commit check, and the editor I'm currently using the most (Zed) does not support this.

@dhruvmanila
Copy link
Member

This will also be useful in implementing the upcoming (3.18) LSP feature textDocument/rangesFormatting which allow providing multiple ranges for a formatting request (https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#textDocument_rangeFormatting).

@MichaReiser MichaReiser added wish Not on the current roadmap; maybe in the future and removed cli Related to the command-line interface labels Aug 12, 2024
@MichaReiser MichaReiser added the needs-design Needs further design before implementation label Aug 17, 2024
@okuuva
Copy link

okuuva commented Sep 19, 2024

Just to add a related note: the use case @afontenot described is the reason darker exists. Support for ruff format is incoming.

Full disclosure: I'm a darker contributor. Always feels like an icky humble brag but better to be open about it I guess.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatter Related to the formatter needs-design Needs further design before implementation wish Not on the current roadmap; maybe in the future
Projects
None yet
Development

No branches or pull requests

5 participants