forked from aptos-labs/aptos-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
x: introduce 'cargo xfmt', a wrapper around 'cargo fmt'
Introduce 'xfmt', a wrapper around 'cargo fmt' which works around the issue that the 'merge_imports' config of rustfmt isn't stable. If you place an unstable config in a rustfmt.toml config file and run using a stable version of rustfmt the config is ignored and you get the following: $ cargo fmt Warning: can't set `merge_imports = true`, unstable features are only available in nightly channel. But if you instead pass the config using the '--config' option rustfmt happily applies and uses the unstable config: $ cargo fmt -- --config merge_imports=true Since there are enough people on the project that would like to see imports merged the new 'xfmt' wrapper uses the abbove workaround to ensure that imports are merged when running 'cargo xfmt'. If you also want to have the following behavior with your favorite editor (assuming your editor lets you pass additional flags to rustfmt) you can simply add '--config merge_imports=true' to the list of options your editor passes rustfmt when formatting rust code. For example with vim and the rust.vim plugin you would just need to add the following to your vimrc to have imports merged when rust.vim is used to format your code: let g:rustfmt_options = '--config merge_imports=true' Closes: aptos-labs#2926 Approved by: bothra90
- Loading branch information
1 parent
e2ff6d3
commit a32b413
Showing
4 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) The Libra Core Contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use crate::{cargo::Cargo, config::Config, Result}; | ||
use std::ffi::OsString; | ||
use structopt::StructOpt; | ||
|
||
#[derive(Debug, StructOpt)] | ||
pub struct Args { | ||
#[structopt(long)] | ||
/// Run in 'check' mode. Exits with 0 if input is | ||
/// formatted correctly. Exits with 1 and prints a diff if | ||
/// formatting is required. | ||
check: bool, | ||
|
||
#[structopt(long)] | ||
/// Run check on all packages in the workspace | ||
workspace: bool, | ||
|
||
#[structopt(name = "ARGS", parse(from_os_str), last = true)] | ||
/// Pass through args to rustfmt | ||
args: Vec<OsString>, | ||
} | ||
|
||
pub fn run(args: Args, _config: Config) -> Result<()> { | ||
// Hardcode that we want imports merged | ||
let mut pass_through_args = vec!["--config".into(), "merge_imports=true".into()]; | ||
|
||
if args.check { | ||
pass_through_args.push("--check".into()); | ||
} | ||
|
||
pass_through_args.extend(args.args); | ||
|
||
let mut cmd = Cargo::new("fmt"); | ||
|
||
if args.workspace { | ||
// cargo fmt doesn't have a --workspace flag, instead it uses the | ||
// old --all flag | ||
cmd.all(); | ||
} | ||
|
||
cmd.pass_through(&pass_through_args).run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters