astral-sh / ruff-pre-commit

A pre-commit hook for Ruff.
Apache License 2.0
892 stars 41 forks source link

--range support #94

Open skairunner opened 1 month ago

skairunner commented 1 month ago

Ruff introduced a new parameter --range that allows the formatter to only check the specified range. This would be useful to allow migrating from black to ruff formatting without causing whole-file diffs whenever a file is touched for the first time.

Quote from docs:

Editor options:
      --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>`.

Is there a way to have range support within the pre-commit framework?

AlexWaygood commented 1 month ago

Hi! Would you be able to do this using the args key in your .pre-commit-config.yaml file? E.g. something like

  - repo: https://github.com/astral-sh/ruff-pre-commit
    rev: v0.5.7
    hooks:
      - id: ruff
        name: Run ruff
        args: ["--range=1:2-3:4"]
skairunner commented 1 month ago

Unfortunately not -- that would hardcode all pre-commits to use that specific range. I was thinking that it would be ideal if ruff could somehow receive the line numbers edited in the git commit and pass those along to ruff formatter, per-file. So it would basically only format the lines that were touched by the commit rather than all files touched.

But I don't know if that's supported within the pre-commit framework.

AlexWaygood commented 1 month ago

Ahh I see, thanks for explaining

MichaReiser commented 1 month ago

Yeah, this requires pre-commit support and possibly support for passing multiple ranges https://github.com/astral-sh/ruff/issues/12800