zig
zig copied to clipboard
Proposal: add the `--diff` option to `zig fmt`
I propose to add the --diff
option to the zig fmt
command. When it is set, it will display diffs instead of rewriting files.
The --diff
and --check
options can be used together.
Rationale
Several modern code formatting tools have an option to display diffs instead of rewriting files.
I discovered this option when using ruff
(a formatter for Python code written in Rust) and I find it is very convenient.
Examples:
Go
gofmt
has the options:
-d display diffs instead of rewriting files
-l list files whose formatting differs from gofmt's
Python
black
has the options:
--check Don't write the files back, just return the
status. Return code 0 means nothing would
change. Return code 1 means some files would
be reformatted. Return code 123 means there
was an internal error.
--diff Don't write the files back, just output a
diff for each file on stdout.
Javascript
prettier
has the options
-c, --check Check if the given files are formatted, print a human-friendly summary
message and paths to unformatted files (see also --list-different).
-l, --list-different Print the names of files that are different from Prettier's formatting (see also --check).
-w, --write Edit files in-place. (Beware!)
Note: prettier
does not support setting -c
and -l
together.
Thanks.
This is the implementation in Go (I linked the Go implementation because the code is probably easier to read and the algorithm also guarantees to run in O(n log n) time instead of the standard O(n²) time.): https://github.com/golang/go/blob/master/src/internal/diff/diff.go
I wrote a Rust port of diff.go
that may or may not be easier to understand: https://gist.github.com/FnControlOption/4d36dd2043ec86bc877beff114821def
This would be really easy to implement if #18360 was accepted
This would be really easy to implement if #18360 was accepted
Thanks for the link.
I avoided creating a PR (by converting the Go code to Zig) since one of the issues was where to place the diff
implementation.