Skip to content

Commit 56a2c30

Browse files
committed
use significant comments to set format_cargo_toml: true
1 parent ffbb3ba commit 56a2c30

File tree

9 files changed

+116
-10
lines changed

9 files changed

+116
-10
lines changed

src/test/mod.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -696,12 +696,6 @@ 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-
705699
let sig_comments = read_significant_comments(filename);
706700
// Look for a config file. If there is a 'config' property in the significant comments, use
707701
// that. Otherwise, if there are no significant comments at all, look for a config file with
@@ -794,15 +788,24 @@ fn get_config(config_file: Option<&Path>) -> Config {
794788

795789
// Reads significant comments of the form: `// rustfmt-key: value` into a hash map.
796790
fn read_significant_comments(file_name: &Path) -> HashMap<String, String> {
791+
let is_cargo_toml = file_name.file_name().map_or(false, |f| f == "Cargo.toml");
797792
let file = fs::File::open(file_name)
798793
.unwrap_or_else(|_| panic!("couldn't read file {}", file_name.display()));
799794
let reader = BufReader::new(file);
800-
let pattern = r"^\s*//\s*rustfmt-([^:]+):\s*(\S+)";
795+
let pattern = if is_cargo_toml {
796+
r"^\s*#\s*rustfmt-([^:]+):\s*(\S+)"
797+
} else {
798+
r"^\s*//\s*rustfmt-([^:]+):\s*(\S+)"
799+
};
801800
let regex = regex::Regex::new(pattern).expect("failed creating pattern 1");
802801

803802
// Matches lines containing significant comments or whitespace.
804-
let line_regex = regex::Regex::new(r"(^\s*$)|(^\s*//\s*rustfmt-[^:]+:\s*\S+)")
805-
.expect("failed creating pattern 2");
803+
let line_regex = regex::Regex::new(if is_cargo_toml {
804+
r"(^\s*$)|(^\s*#\s*rustfmt-[^:]+:\s*\S+)"
805+
} else {
806+
r"(^\s*$)|(^\s*//\s*rustfmt-[^:]+:\s*\S+)"
807+
})
808+
.expect("failed creating pattern 2");
806809

807810
reader
808811
.lines()
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# rustfmt-format_cargo_toml: false
2+
3+
# comment before [[bin]], not necessarily at the beginning
4+
5+
6+
[[bin]] # comment after [[bin]]
7+
"aa" = 1 # comment for "aa"
8+
'bb' = 2
9+
"啊"=1
10+
11+
# comment before package
12+
13+
[package]
14+
version = 1
15+
description = "a\nb\nhaha"
16+
name = 3
17+
18+
# comment 1 for arr1 line1
19+
20+
# comment 1 for arr1 line 2
21+
arr1 = [1,
22+
2,3]
23+
24+
# comment 2 for arr2
25+
26+
arr2 = ["11111111111111111111111111111111111111111111111111111111111111111111111111111111","1111111111111111111111111111111111111111111111111111111111111111111111111111111"]
27+
28+
# comment before [[dependencies]], not after [package]
29+
30+
31+
[dependencies]
32+
extremely_long_crate_name_goes_here = {path = "extremely_long_path_name_goes_right_here",version = "4.5.6"}
33+
34+
35+
crate1 = { path = "crate1",version = "1.2.3" }
36+
37+
# comment before the second [[bin]]
38+
39+
[[bin]]
40+
d = "git-rustfmt"
41+
c = "src/git-rustfmt/main.rs"
42+
43+
44+
45+
46+
# comment at the end of the file
47+
48+
49+

tests/source/cargo-toml/basic/Cargo.toml renamed to tests/source/cargo-toml/true/basic/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
# rustfmt-format_cargo_toml: true
22

33
# comment before [[bin]], not necessarily at the beginning
44

tests/source/cargo-toml/dotted-key/Cargo.toml renamed to tests/source/cargo-toml/true/dotted-key/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# rustfmt-format_cargo_toml: true
12

23
[a.c]
34
ac =1

tests/source/cargo-toml/virtual-manifest/Cargo.toml renamed to tests/source/cargo-toml/true/virtual-manifest/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# rustfmt-format_cargo_toml: true
12

23

34
# Works correctly for Cargo.toml without [package]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# rustfmt-format_cargo_toml: false
2+
3+
# comment before [[bin]], not necessarily at the beginning
4+
5+
6+
[[bin]] # comment after [[bin]]
7+
"aa" = 1 # comment for "aa"
8+
'bb' = 2
9+
"啊"=1
10+
11+
# comment before package
12+
13+
[package]
14+
version = 1
15+
description = "a\nb\nhaha"
16+
name = 3
17+
18+
# comment 1 for arr1 line1
19+
20+
# comment 1 for arr1 line 2
21+
arr1 = [1,
22+
2,3]
23+
24+
# comment 2 for arr2
25+
26+
arr2 = ["11111111111111111111111111111111111111111111111111111111111111111111111111111111","1111111111111111111111111111111111111111111111111111111111111111111111111111111"]
27+
28+
# comment before [[dependencies]], not after [package]
29+
30+
31+
[dependencies]
32+
extremely_long_crate_name_goes_here = {path = "extremely_long_path_name_goes_right_here",version = "4.5.6"}
33+
34+
35+
crate1 = { path = "crate1",version = "1.2.3" }
36+
37+
# comment before the second [[bin]]
38+
39+
[[bin]]
40+
d = "git-rustfmt"
41+
c = "src/git-rustfmt/main.rs"
42+
43+
44+
45+
46+
# comment at the end of the file
47+
48+
49+

tests/target/cargo-toml/basic/Cargo.toml renamed to tests/target/cargo-toml/true/basic/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ a
1515
b
1616
haha"""
1717

18+
# rustfmt-format_cargo_toml: true
1819
# comment before [[bin]], not necessarily at the beginning
1920
[[bin]] # comment after [[bin]]
2021
aa = 1 # comment for "aa"

tests/target/cargo-toml/dotted-key/Cargo.toml renamed to tests/target/cargo-toml/true/dotted-key/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ aac = 1
2626
[a.a.c.d]
2727
aacd = 1
2828

29+
# rustfmt-format_cargo_toml: true
2930
[a.c]
3031
ac = 1
3132

tests/target/cargo-toml/virtual-manifest/Cargo.toml renamed to tests/target/cargo-toml/true/virtual-manifest/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# rustfmt-format_cargo_toml: true
12
# Works correctly for Cargo.toml without [package]
23
[workspace]
34
exclude = ["crates/proc-macro-test/imp"]

0 commit comments

Comments
 (0)