Skip to content

Commit 3dc6d27

Browse files
committed
add format_cargo_toml option & integrate to cargo-fmt
1 parent 02df831 commit 3dc6d27

File tree

8 files changed

+65
-13
lines changed

8 files changed

+65
-13
lines changed

Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ readme = "README.md"
99
repository = "https://github.com/rust-lang/rustfmt"
1010
description = "Tool to find and fix Rust formatting issues"
1111

12+
# Rustc dependencies are loaded from the sysroot, Cargo doesn't know about them.
13+
[package.metadata.rust-analyzer]
14+
# This package uses #[feature(rustc_private)]
15+
rustc_private = true
16+
1217
[[bin]]
1318
name = "rustfmt"
1419
path = "src/bin/main.rs"
@@ -61,8 +66,3 @@ toml_edit = "0.13"
6166
unicode-segmentation = "1.9"
6267
unicode-width = "0.1"
6368
unicode_categories = "0.1"
64-
65-
# Rustc dependencies are loaded from the sysroot, Cargo doesn't know about them.
66-
[package.metadata.rust-analyzer]
67-
# This package uses #[feature(rustc_private)]
68-
rustc_private = true

Configurations.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,14 @@ fn add_one(x: i32) -> i32 {
10401040
}
10411041
```
10421042

1043+
## `format_cargo_toml`
1044+
1045+
Format `Cargo.toml` files.
1046+
1047+
- **Default value**: `false`
1048+
- **Possible values**: `true`, `false`
1049+
- **Stable**: No (tracking issue: [#4091](https://github.com/rust-lang/rustfmt/issues/4091))
1050+
10431051
## `doc_comment_code_block_width`
10441052

10451053
Max width for code snippets included in doc comments. Only used if [`format_code_in_doc_comments`](#format_code_in_doc_comments) is true.

rustfmt.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
error_on_line_overflow = true
22
error_on_unformatted = true
33
version = "Two"
4+
format_cargo_toml = true

src/cargo-fmt/main.rs

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,15 @@ impl Target {
284284
edition: target.edition.clone(),
285285
}
286286
}
287+
288+
/// `Cargo.toml` file to format.
289+
pub fn manifest_target(manifest_path: PathBuf) -> Self {
290+
Target {
291+
path: manifest_path,
292+
kind: String::from("manifest"),
293+
edition: String::from("2021"), // The value doesn't matter for formatting Cargo.toml.
294+
}
295+
}
287296
}
288297

289298
impl PartialEq for Target {
@@ -365,6 +374,9 @@ fn get_targets_root_only(
365374
) -> Result<(), io::Error> {
366375
let metadata = get_cargo_metadata(manifest_path)?;
367376
let workspace_root_path = PathBuf::from(&metadata.workspace_root).canonicalize()?;
377+
targets.insert(Target::manifest_target(
378+
workspace_root_path.join("Cargo.toml"),
379+
));
368380
let (in_workspace_root, current_dir_manifest) = if let Some(target_manifest) = manifest_path {
369381
(
370382
workspace_root_path == target_manifest,
@@ -379,7 +391,15 @@ fn get_targets_root_only(
379391
};
380392

381393
let package_targets = match metadata.packages.len() {
382-
1 => metadata.packages.into_iter().next().unwrap().targets,
394+
1 => {
395+
let p = metadata.packages.into_iter().next().unwrap();
396+
targets.insert(Target::manifest_target(
397+
PathBuf::from(&p.manifest_path)
398+
.canonicalize()
399+
.unwrap_or_default(),
400+
));
401+
p.targets
402+
}
383403
_ => metadata
384404
.packages
385405
.into_iter()
@@ -390,13 +410,18 @@ fn get_targets_root_only(
390410
.unwrap_or_default()
391411
== current_dir_manifest
392412
})
393-
.flat_map(|p| p.targets)
413+
.flat_map(|p| {
414+
targets.insert(Target::manifest_target(
415+
PathBuf::from(&p.manifest_path)
416+
.canonicalize()
417+
.unwrap_or_default(),
418+
));
419+
p.targets
420+
})
394421
.collect(),
395422
};
396423

397-
for target in package_targets {
398-
targets.insert(Target::from_target(&target));
399-
}
424+
add_targets(&package_targets, targets);
400425

401426
Ok(())
402427
}
@@ -408,6 +433,11 @@ fn get_targets_recursive(
408433
) -> Result<(), io::Error> {
409434
let metadata = get_cargo_metadata(manifest_path)?;
410435
for package in &metadata.packages {
436+
targets.insert(Target::manifest_target(
437+
PathBuf::from(&package.manifest_path)
438+
.canonicalize()
439+
.unwrap_or_default(),
440+
));
411441
add_targets(&package.targets, targets);
412442

413443
// Look for local dependencies using information available since cargo v1.51
@@ -447,6 +477,11 @@ fn get_targets_with_hitlist(
447477

448478
for package in metadata.packages {
449479
if workspace_hitlist.remove(&package.name) {
480+
targets.insert(Target::manifest_target(
481+
PathBuf::from(&package.manifest_path)
482+
.canonicalize()
483+
.unwrap_or_default(),
484+
));
450485
for target in package.targets {
451486
targets.insert(Target::from_target(&target));
452487
}

src/cargo-fmt/test/targets.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ mod all_targets {
5757
manifest_suffix,
5858
"divergent-crate-dir-names",
5959
&exp_targets,
60-
3,
60+
3 + 3, // include 3 Cargo.toml files
6161
);
6262
}
6363

@@ -112,7 +112,7 @@ mod all_targets {
112112
manifest_suffix,
113113
"workspaces/path-dep-above",
114114
&exp_targets,
115-
6,
115+
6 + 6, // include 6 Cargo.toml files,
116116
);
117117
}
118118

src/config/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ create_config! {
173173
or they are left with trailing whitespaces";
174174
ignore: IgnoreList, IgnoreList::default(), false,
175175
"Skip formatting the specified files and directories";
176+
format_cargo_toml: bool, false, false, "Format Cargo.toml files";
176177

177178
// Not user-facing
178179
verbose: Verbosity, Verbosity::Normal, false, "How much to information to emit to the user";
@@ -681,6 +682,7 @@ hide_parse_errors = false
681682
error_on_line_overflow = false
682683
error_on_unformatted = false
683684
ignore = []
685+
format_cargo_toml = false
684686
emit_mode = "Files"
685687
make_backup = false
686688
"#,

src/formatting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ fn format_cargo_toml<T: FormatHandler>(
196196

197197
let ignore_path_set = IgnorePathSet::from_ignore_list(&config.ignore())?;
198198
let file_name = FileName::Real(path.clone());
199-
if ignore_path_set.is_match(&file_name) {
199+
if !config.format_cargo_toml() || ignore_path_set.is_match(&file_name) {
200200
return Ok(report);
201201
}
202202

src/test/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,12 @@ fn print_mismatches<T: Fn(u32) -> String>(
696696
}
697697

698698
fn read_config(filename: &Path) -> Config {
699+
if filename.file_name().map_or(false, |f| f == "Cargo.toml") {
700+
let mut config = Config::default();
701+
config.set().format_cargo_toml(true);
702+
return config;
703+
}
704+
699705
let sig_comments = read_significant_comments(filename);
700706
// Look for a config file. If there is a 'config' property in the significant comments, use
701707
// that. Otherwise, if there are no significant comments at all, look for a config file with

0 commit comments

Comments
 (0)