Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support formatting Cargo.toml #5240

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
2628172
formatting logic of Cargo.toml
xxchan Feb 25, 2022
933ce85
test comments
xxchan Feb 25, 2022
22bcae2
impl From<ignore::Error> for ErrorKind
ytmimi Apr 19, 2022
a64778e
Add `format_cargo_toml` to the code path called by rustfmt
ytmimi Apr 19, 2022
784bb05
remove debug things
xxchan Jul 16, 2022
baf8fa6
fmt
xxchan Jul 16, 2022
f188f1d
use [&mut dyn] for rules
xxchan Jul 16, 2022
30edae8
fix
xxchan Jul 16, 2022
beda310
self apply
xxchan Jul 24, 2022
71609c5
move test to integration test
xxchan Jul 24, 2022
2cb56ee
typo
xxchan Jul 24, 2022
77ad49f
don't panic
xxchan Jul 24, 2022
835eb39
add TrimSpaces & tests
xxchan Aug 5, 2022
02df831
test key with many dots
xxchan Aug 6, 2022
3dc6d27
add format_cargo_toml option & integrate to cargo-fmt
xxchan Nov 12, 2022
6ae3c97
revert formatting Cargo.toml on the repo
xxchan Nov 19, 2022
ffbb3ba
add example in Configuration doc
xxchan Nov 19, 2022
56a2c30
use significant comments to set format_cargo_toml: true
xxchan Nov 20, 2022
b0565d8
a newline at the end of each file
xxchan Nov 20, 2022
c17e2b9
remove CargoTomlError
xxchan Nov 20, 2022
1ad83c1
move /cargo-toml to /configs/format_cargo_toml
xxchan Nov 20, 2022
093e19f
Merge branch 'master' into cargo_toml
xxchan Apr 24, 2023
e3b05e1
Merge branch 'master' into cargo_toml
xxchan Aug 7, 2023
4724d3e
Migrate to latest toml_edit crate
nyurik Aug 8, 2023
09b43a1
update toml_edit to 0.20
xxchan Oct 24, 2023
c9e5a94
improve RawString handling
xxchan Oct 24, 2023
f244a91
Merge branch 'master' into cargo_toml
xxchan Oct 24, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 51 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ thiserror = "1.0.40"
toml = "0.7.4"
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
toml_edit = "0.20.4"
unicode-segmentation = "1.9"
unicode-width = "0.1"
unicode-properties = { version = "0.1", default-features = false, features = ["general-category"] }
Expand Down
38 changes: 38 additions & 0 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,44 @@ fn add_one(x: i32) -> i32 {
}
```

## `format_cargo_toml`

Format `Cargo.toml` files according to the [Cargo.toml conventions](https://github.com/rust-dev-tools/fmt-rfcs/blob/master/guide/cargo.md).

- **Default value**: `false`
- **Possible values**: `true`, `false`
- **Stable**: No (tracking issue: [#4091](https://github.com/rust-lang/rustfmt/issues/4091))

#### `false` (default):

```toml
[package]
edition="2018"

version="0.1.0"
name="e"
[dependencies]
a="0.1"


c="0.3"
b="0.2"
```

#### `true`:

```toml
[package]
name = "e"
version = "0.1.0"
edition = "2018"

[dependencies]
a = "0.1"
b = "0.2"
c = "0.3"
```

## `doc_comment_code_block_width`

Max width for code snippets included in doc comments. Only used if [`format_code_in_doc_comments`](#format_code_in_doc_comments) is true.
Expand Down
45 changes: 40 additions & 5 deletions src/cargo-fmt/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,15 @@ impl Target {
edition: target.edition,
}
}

/// `Cargo.toml` file to format.
pub fn manifest_target(manifest_path: PathBuf) -> Self {
Target {
path: manifest_path,
kind: String::from("manifest"),
edition: Edition::E2021, // The value doesn't matter for formatting Cargo.toml.
}
}
}

impl PartialEq for Target {
Expand Down Expand Up @@ -366,6 +375,9 @@ fn get_targets_root_only(
) -> Result<(), io::Error> {
let metadata = get_cargo_metadata(manifest_path)?;
let workspace_root_path = PathBuf::from(&metadata.workspace_root).canonicalize()?;
targets.insert(Target::manifest_target(
workspace_root_path.join("Cargo.toml"),
));
let (in_workspace_root, current_dir_manifest) = if let Some(target_manifest) = manifest_path {
(
workspace_root_path == target_manifest,
Expand All @@ -380,7 +392,15 @@ fn get_targets_root_only(
};

let package_targets = match metadata.packages.len() {
1 => metadata.packages.into_iter().next().unwrap().targets,
1 => {
let p = metadata.packages.into_iter().next().unwrap();
targets.insert(Target::manifest_target(
PathBuf::from(&p.manifest_path)
.canonicalize()
.unwrap_or_default(),
));
p.targets
}
_ => metadata
.packages
.into_iter()
Expand All @@ -391,13 +411,18 @@ fn get_targets_root_only(
.unwrap_or_default()
== current_dir_manifest
})
.flat_map(|p| p.targets)
.flat_map(|p| {
targets.insert(Target::manifest_target(
PathBuf::from(&p.manifest_path)
.canonicalize()
.unwrap_or_default(),
));
p.targets
})
.collect(),
};

for target in package_targets {
targets.insert(Target::from_target(&target));
}
add_targets(&package_targets, targets);

Ok(())
}
Expand All @@ -409,6 +434,11 @@ fn get_targets_recursive(
) -> Result<(), io::Error> {
let metadata = get_cargo_metadata(manifest_path)?;
for package in &metadata.packages {
targets.insert(Target::manifest_target(
PathBuf::from(&package.manifest_path)
.canonicalize()
.unwrap_or_default(),
));
add_targets(&package.targets, targets);

// Look for local dependencies using information available since cargo v1.51
Expand Down Expand Up @@ -448,6 +478,11 @@ fn get_targets_with_hitlist(

for package in metadata.packages {
if workspace_hitlist.remove(&package.name) {
targets.insert(Target::manifest_target(
PathBuf::from(&package.manifest_path)
.canonicalize()
.unwrap_or_default(),
));
for target in package.targets {
targets.insert(Target::from_target(&target));
}
Expand Down
4 changes: 2 additions & 2 deletions src/cargo-fmt/test/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ mod all_targets {
manifest_suffix,
"divergent-crate-dir-names",
&exp_targets,
3,
3 + 3, // include 3 Cargo.toml files
);
}

Expand Down Expand Up @@ -112,7 +112,7 @@ mod all_targets {
manifest_suffix,
"workspaces/path-dep-above",
&exp_targets,
6,
6 + 6, // include 6 Cargo.toml files,
);
}

Expand Down
2 changes: 2 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ create_config! {
or they are left with trailing whitespaces";
ignore: IgnoreList, IgnoreList::default(), false,
"Skip formatting the specified files and directories";
format_cargo_toml: bool, false, false, "Format Cargo.toml files";

// Not user-facing
verbose: Verbosity, Verbosity::Normal, false, "How much to information to emit to the user";
Expand Down Expand Up @@ -685,6 +686,7 @@ hide_parse_errors = false
error_on_line_overflow = false
error_on_unformatted = false
ignore = []
format_cargo_toml = false
emit_mode = "Files"
make_backup = false
"#,
Expand Down
Loading
Loading