Add prototype of ruff format for projects (#6871)

**Summary** Add recursive formatting based on `ruff check` file
discovery for `ruff format`, as a prototype for the formatter alpha.
This allows e.g. `format ../projects/django/`. It's still lacking
support for any settings except line length.

Note just like the existing `ruff format` this will become part of the
production build, i.e. you'll be able to use it - hidden by default and
with a prominent warning - with `ruff format .` after the next release.

Error handling works in my manual tests (the colors do also work):

```
$  target/debug/ruff format scripts/
warning: `ruff format` is a work-in-progress, subject to change at any time, and intended for internal use only.
```
(the above changes `add_rule.py` where we have the wrong bin op
breaking)

```
$ target/debug/ruff format ../projects/django/
warning: `ruff format` is a work-in-progress, subject to change at any time, and intended for internal use only.
Failed to format /home/konsti/projects/django/tests/test_runner_apps/tagged/tests_syntax_error.py: source contains syntax errors: ParseError { error: UnrecognizedToken(Name { name: "syntax_error" }, None), offset: 131, source_path: "<filename>" }
```

```
$ target/debug/ruff format a
warning: `ruff format` is a work-in-progress, subject to change at any time, and intended for internal use only.
Failed to read /home/konsti/ruff/a/d.py: Permission denied (os error 13)
```

**Test Plan** Missing! I'm not sure if it's worth building tests at this
stage or how they should look like.
This commit is contained in:
konsti
2023-08-27 21:12:18 +02:00
committed by GitHub
parent 059757a8c8
commit c2413dcd2c
7 changed files with 142 additions and 16 deletions

View File

@@ -3,6 +3,15 @@
The goal of our formatter is to be compatible with Black except for rare edge cases (mostly
involving comment placement).
You can try an experimental version of the formatter on your project with:
```shell
cargo run --bin ruff -- format path/to/your/project
```
Note that currently the only supported option is `line-length` and that both the CLI and the
formatting are a work-in-progress and will change before the stable release.
## Dev tools
**Testing your changes** You can use the `ruff_python_formatter` binary to format individual files

View File

@@ -99,9 +99,9 @@ where
#[derive(Error, Debug)]
pub enum FormatModuleError {
#[error("source contains syntax errors (lexer error): {0:?}")]
#[error("source contains syntax errors: {0:?}")]
LexError(LexicalError),
#[error("source contains syntax errors (parser error): {0:?}")]
#[error("source contains syntax errors: {0:?}")]
ParseError(ParseError),
#[error(transparent)]
FormatError(#[from] FormatError),