ruff/crates
konsti 51aa73f405
Add --diff option ruff format (#7937)
**Summary** `ruff format --diff` is similar to `ruff format --check`,
but we don't only error with the list of file that would be formatted,
but also show a diff between the unformatted input and the formatted
output.

```console
$ ruff format --diff scratch.py scratch.pyi scratch.ipynb
warning: `ruff format` is not yet stable, and subject to change in future versions.
--- scratch.ipynb
+++ scratch.ipynb
@@ -1,3 +1,4 @@
 import numpy
-maths = (numpy.arange(100)**2).sum()
-stats= numpy.asarray([1,2,3,4]).median()
+
+maths = (numpy.arange(100) ** 2).sum()
+stats = numpy.asarray([1, 2, 3, 4]).median()
--- scratch.py
+++ scratch.py
@@ -1,3 +1,3 @@
 x = 1
-y=2
+y = 2
 z = 3
2 files would be reformatted, 1 file left unchanged
```

With `--diff`, the summary message gets printed to stderr to allow e.g.
`ruff format --diff . > format.patch`.

At the moment, jupyter notebooks are formatted as code diffs, while
everything else is a real diff that could be applied. This means that
the diffs containing jupyter notebooks are not real diffs and can't be
applied. We could change this to json diffs, but they are hard to read.
We could also split the diff option into a human diff option, where we
deviate from the machine readable diff constraints, and a proper machine
readable, appliable diff output that you can pipe into other tools.

To make the tests work, the results (and errors, if any) are sorted
before printing them. Previously, the print order was random, i.e. two
identical runs could have different output.

Open question: Should this go into the markdown docs? Or will this be
subsumed by the integration of the formatter into `ruff check`?

**Test plan** Fixtures for the change and no change cases, including a
jupyter notebook and for file input and stdin.

Fixes #7231

---------

Co-authored-by: Micha Reiser <micha@reiser.io>
2023-10-18 11:55:05 +00:00
..
flake8_to_ruff Add [format|lint].exclude options (#8000) 2023-10-18 01:15:25 +00:00
ruff_benchmark Bump ureq from 2.7.1 to 2.8.0 (#7756) 2023-10-02 09:49:59 -04:00
ruff_cache Introduce FormatterSettings (#7545) 2023-09-21 08:01:24 +02:00
ruff_cli Add --diff option ruff format (#7937) 2023-10-18 11:55:05 +00:00
ruff_dev Add [format|lint].exclude options (#8000) 2023-10-18 01:15:25 +00:00
ruff_diagnostics Add settings for promoting and demoting fixes (#7841) 2023-10-10 20:04:21 +00:00
ruff_formatter Bump tracing from 0.1.37 to 0.1.39 (#7978) 2023-10-17 07:46:53 +00:00
ruff_index Add unreachable code rule (#5384) 2023-07-04 14:27:23 +00:00
ruff_linter add autofix for D301 (#7970) 2023-10-18 02:19:29 +00:00
ruff_macros Rename RuleGroup::Unspecified to Stable (#7991) 2023-10-16 14:53:27 -05:00
ruff_notebook Add cell field to JSON output format (#7664) 2023-10-13 01:06:02 +00:00
ruff_python_ast Insert newline after nested function or class statements (#7946) 2023-10-18 09:45:58 +00:00
ruff_python_codegen Add support for PEP 701 (#7376) 2023-09-29 02:55:39 +00:00
ruff_python_formatter Insert newline after nested function or class statements (#7946) 2023-10-18 09:45:58 +00:00
ruff_python_index Add support for PEP 701 (#7376) 2023-09-29 02:55:39 +00:00
ruff_python_literal Implement our own small-integer optimization (#7584) 2023-09-25 15:13:21 +00:00
ruff_python_parser Disallow f-strings in match pattern literal (#7857) 2023-10-09 10:11:08 +00:00
ruff_python_resolver Replace .map_or(false, $closure) with .is_some_and(closure) (#6244) 2023-08-01 19:29:42 +02:00
ruff_python_semantic Lazily evaluate all PEP 695 type alias values (#8033) 2023-10-17 21:50:26 -04:00
ruff_python_stdlib Improvements to RUF015 (#7848) 2023-10-08 14:49:45 +00:00
ruff_python_trivia Extend pragma comment cases (#7687) 2023-09-28 18:55:19 +00:00
ruff_shrinking Add support for PEP 701 (#7376) 2023-09-29 02:55:39 +00:00
ruff_source_file Add cell field to JSON output format (#7664) 2023-10-13 01:06:02 +00:00
ruff_text_size Unify line size settings between ruff and the formatter (#6873) 2023-08-28 06:44:56 +00:00
ruff_wasm Add [format|lint].exclude options (#8000) 2023-10-18 01:15:25 +00:00
ruff_workspace Add lint.preview (#8002) 2023-10-18 01:26:37 +00:00