ruff-pre-commit icon indicating copy to clipboard operation
ruff-pre-commit copied to clipboard

--range support

Open skairunner opened this issue 1 year ago • 4 comments
trafficstars

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?

skairunner avatar Aug 13 '24 08:08 skairunner

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"]

AlexWaygood avatar Aug 13 '24 12:08 AlexWaygood

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.

skairunner avatar Aug 13 '24 12:08 skairunner

Ahh I see, thanks for explaining

AlexWaygood avatar Aug 13 '24 12:08 AlexWaygood

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

MichaReiser avatar Aug 13 '24 12:08 MichaReiser