This patch introduces a super-simple format-diff tool, that allows you to do:
```
git diff | rustfmt-format-diff -p 1
```
To format your current changes.
For now it doesn't accept too much customisation, and it basically calls rustfmt
with the default configuration, but more customisation can be added in the
future if needed.
* Make method for searching parents for toml file public
* Make method for loading config from path directly public, tweak the
API since it was never returning None
- `--dump-default-config` outputs the default configuration to the
specified file as TOML and then exits.
- `--dump-minimal-config` is checked after formatting files as normal.
If present, any configuration options that were checked during
formatting are written to the specified file as TOML.
- These options were added only to `rustfmt`, not to `cargo fmt`. They
can be specified when using `cargo fmt` by placing them after `--`.
- It would have been nice if the filename was optional, so you could
run just `rusfmt --dump-minimal-config build.rs` to have it output to
`rustfmt.toml`. However, this doesn't do what you might expect: it
outputs the config to `build.rs`!
This API isn't fantastic, but it's the best I can come up with without
something like `concat_idents!()`. There are relatively few places where
config is set, to hopefully the ugliness isn't disastrous.
Change previous occurences of `config.item = value` to this new API,
rather than using `config.override_value()`. Undo the changes to
`override_value()`, as it's no longer important to propogate the error
to the caller. Add a test for the new interface.
Preparation for #865, which proposes adding a flag which outputs which
config options are used during formatting.
This PR should not make any difference to functionality. A lot of this
was search-and-replace.
Some areas worthy of review/discussion:
- The method for each config item returns a clone of the underlying
value. We can't simply return an immutable reference, as lots of
places in the code expect to be able to pass the returned value as
`bool` (not `&bool). It would be nice if the `bool` items could
return a copy, but the more complex types a borrowed reference... but
unfortunately, I couldn't get the macro to do this.
- A few places (mostly tests and `src/bin/rustfmt.rs`) were overriding
config items by modifying the fields of the `Config` struct directly.
They now use the existing `override_value()` method, which has been
modified to return a `Result` for use by `src/bin/rustfmt.rs`. This
benefits of this are that the complex `file_lines` and `write_mode`
strings are now parsed in one place (`Config.override_value`) instead
of multiple. The disadvantages are that it moves the compile-time
checks for config names to become run-time checks.
* Add multiple configuration file names feature
* Add '.rustfmt.toml' in README file
* Clean up configuration file code
* Make config file names constant
* Use only one blank line
This changes rustfmt to return exit code 4
when run with write mode diff and differences between
the formatted code and the original code are found.
Useful for CI to make sure your contributors actually ran rustfmt.
This will help in debugging issues as rustfmt gets more users.
If the working tree is clean, output looks like
$ target/debug/rustfmt -V
0.5.0 (9f5ed3b)
If the working tree is dirty, output looks like
$ target/debug/rustfmt -V
0.5.0 (9f5ed3b worktree dirty)
If git is unavailable, output looks like
$ target/debug/rustfmt -V
0.5.0 (git commit unavailable)
To avoid rebuilds on changing tests, the build script will only rerun if
files under src/ are changed. This means the actual git status may show
changed files and this would not show up in the version. This should not
be an issue as files not in src/ should not affect the build output.
`update_config()` was parsing the `write-mode` option once for each file
argument. This commit parses them once up front into a `CliOptions`
struct, which is then applied to the config before calling `run()`.
This commit tidies up handling of `write_mode` by setting it in the
config at the start, and removing the `write_mode` parameter threaded
throughout the formatting process.