Skip to content

Commit 835eb39

Browse files
committed
add TrimSpaces & tests
1 parent 77ad49f commit 835eb39

File tree

6 files changed

+202
-13
lines changed

6 files changed

+202
-13
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ unicode-width = "0.1"
6363
unicode_categories = "0.1"
6464

6565
# Rustc dependencies are loaded from the sysroot, Cargo doesn't know about them.
66-
6766
[package.metadata.rust-analyzer]
6867
# This package uses #[feature(rustc_private)]
6968
rustc_private = true

src/formatting/cargo_toml.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub(crate) fn format_cargo_toml_inner(content: &str, config: &Config) -> Result<
2626
long_tables: vec![],
2727
current_section: String::new(),
2828
},
29+
&mut TrimSpaces,
2930
];
3031
for rule in rules.into_iter() {
3132
rule.visit_document_mut(&mut doc);
@@ -75,6 +76,11 @@ struct BlankLine {
7576
trimming: bool,
7677
}
7778

79+
/// Trim unnecessary spaces.
80+
///
81+
/// Note: this is not included in the Style Guide.
82+
struct TrimSpaces;
83+
7884
/// Don't use quotes around any standard key names; use bare keys. Only use quoted
7985
/// keys for non-standard keys whose names require them, and avoid introducing such
8086
/// key names when possible.
@@ -349,3 +355,96 @@ impl VisitMut for FormatInlineTable {
349355
});
350356
}
351357
}
358+
359+
impl TrimSpaces {
360+
fn trim_block(s: &str) -> String {
361+
let s = s.trim();
362+
if s.is_empty() {
363+
return String::new();
364+
}
365+
366+
let s: String = s
367+
.lines()
368+
.into_iter()
369+
.filter_map(|line| {
370+
let trimmed = line.trim();
371+
if trimmed.is_empty() {
372+
None
373+
} else {
374+
Some(format!("{trimmed}"))
375+
}
376+
})
377+
.join("\n");
378+
379+
format!("{}\n", s)
380+
}
381+
382+
fn trim_suffix(s: &str) -> String {
383+
let s = s.trim();
384+
if s.is_empty() {
385+
String::new()
386+
} else {
387+
format!(" {}", s)
388+
}
389+
}
390+
}
391+
392+
impl VisitMut for TrimSpaces {
393+
fn visit_document_mut(&mut self, node: &mut Document) {
394+
self.visit_table_mut(node);
395+
396+
let set_prefix = |decor: &mut Decor, i: usize| {
397+
let prefix = format!(
398+
"{}{}",
399+
if i == 0 { "" } else { "\n" },
400+
Self::trim_block(decor.prefix().unwrap_or_default())
401+
);
402+
decor.set_prefix(prefix);
403+
};
404+
let table = node.as_table_mut();
405+
for (i, (_, item)) in table.iter_mut().enumerate() {
406+
if let Some(table) = item.as_table_mut() {
407+
set_prefix(table.decor_mut(), i);
408+
} else if let Some(arr) = item.as_array_of_tables_mut() {
409+
for table in arr.iter_mut() {
410+
set_prefix(table.decor_mut(), i);
411+
}
412+
}
413+
}
414+
415+
if !node.trailing().trim().is_empty() {
416+
let trailing: String = Self::trim_block(node.trailing());
417+
node.set_trailing(&format!("\n{trailing}"));
418+
} else {
419+
node.set_trailing("");
420+
}
421+
}
422+
423+
fn visit_table_mut(&mut self, node: &mut Table) {
424+
let decor = node.decor_mut();
425+
if let Some(prefix) = decor.prefix() {
426+
decor.set_prefix(format!("\n{}", Self::trim_block(prefix)));
427+
}
428+
if let Some(suffix) = decor.suffix() {
429+
decor.set_suffix(Self::trim_suffix(suffix));
430+
}
431+
432+
self.visit_table_like_mut(node);
433+
}
434+
435+
fn visit_table_like_kv_mut(&mut self, mut key: KeyMut<'_>, value: &mut Item) {
436+
let decor = key.decor_mut();
437+
if let Some(prefix) = decor.prefix() {
438+
decor.set_prefix(format!("{}", Self::trim_block(prefix)));
439+
}
440+
441+
if let Some(value) = value.as_value_mut() {
442+
let decor = value.decor_mut();
443+
if let Some(suffix) = decor.suffix() {
444+
decor.set_suffix(Self::trim_suffix(suffix));
445+
}
446+
}
447+
448+
self.visit_item_mut(value);
449+
}
450+
}
Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,49 @@
11

22

3-
[[bin]]
4-
"aa" = 1
5-
'bb' = 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
69
"啊"=1
10+
11+
# comment before package
12+
713
[package]
814
version = 1
915
description = "a\nb\nhaha"
1016
name = 3
1117

12-
# comment 1
13-
18+
# comment 1 for arr1 line1
19+
20+
# comment 1 for arr1 line 2
1421
arr1 = [1,
15-
2,3]
22+
2,3]
1623

17-
# comment 2
24+
# comment 2 for arr2
1825

1926
arr2 = ["11111111111111111111111111111111111111111111111111111111111111111111111111111111","1111111111111111111111111111111111111111111111111111111111111111111111111111111"]
2027

28+
# comment before [[dependencies]], not after [package]
29+
30+
2131
[dependencies]
2232
extremely_long_crate_name_goes_here = {path = "extremely_long_path_name_goes_right_here",version = "4.5.6"}
33+
34+
2335
crate1 = { path = "crate1",version = "1.2.3" }
36+
37+
# comment before the second [[bin]]
2438

2539
[[bin]]
2640
d = "git-rustfmt"
27-
c = "src/git-rustfmt/main.rs"
41+
c = "src/git-rustfmt/main.rs"
42+
43+
44+
45+
46+
# comment at the end of the file
47+
48+
49+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
3+
# Works correctly for Cargo.toml without [package]
4+
5+
6+
[workspace]
7+
8+
9+
members = ["xtask/", "lib/*", "crates/*"]
10+
exclude = ["crates/proc-macro-test/imp"]
11+
12+
13+
[profile.dev]
14+
15+
# Disabling debug info speeds up builds a bunch,
16+
# and we don't rely on it for debugging that much.
17+
18+
debug1 = 0
19+
20+
21+
[profile.dev.package]
22+
23+
# These speed up local tests.
24+
rowan.opt-level = 3
25+
rustc-hash.opt-level = 3
26+
smol_str.opt-level = 3
27+
text-size.opt-level = 3
28+
# This speeds up `cargo xtask dist`.
29+
miniz_oxide.opt-level = 3
30+
31+
32+
[profile.release]
33+
34+
incremental = true
35+
# Set this to 1 or 2 to get more useful backtraces in debugger.
36+
debug2 = 0
37+
38+
39+
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
# comment before package
12
[package]
23
name = 3
34
version = 1
4-
# comment 1
5+
# comment 1 for arr1 line1
6+
# comment 1 for arr1 line 2
57
arr1 = [1, 2, 3]
6-
# comment 2
8+
# comment 2 for arr2
79
arr2 = [
810
"11111111111111111111111111111111111111111111111111111111111111111111111111111111",
911
"1111111111111111111111111111111111111111111111111111111111111111111111111111111"
@@ -13,18 +15,23 @@ a
1315
b
1416
haha"""
1517

16-
[[bin]]
17-
aa = 1
18+
# comment before [[bin]], not necessarily at the beginning
19+
[[bin]] # comment after [[bin]]
20+
aa = 1 # comment for "aa"
1821
bb = 2
1922
"啊" = 1
2023

24+
# comment before the second [[bin]]
2125
[[bin]]
2226
c = "src/git-rustfmt/main.rs"
2327
d = "git-rustfmt"
2428

29+
# comment before [[dependencies]], not after [package]
2530
[dependencies]
2631
crate1 = { path = "crate1", version = "1.2.3" }
2732

2833
[dependencies.extremely_long_crate_name_goes_here]
2934
path = "extremely_long_path_name_goes_right_here"
3035
version = "4.5.6"
36+
37+
# comment at the end of the file
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Works correctly for Cargo.toml without [package]
2+
[workspace]
3+
exclude = ["crates/proc-macro-test/imp"]
4+
members = ["xtask/", "lib/*", "crates/*"]
5+
6+
[profile.dev]
7+
# Disabling debug info speeds up builds a bunch,
8+
# and we don't rely on it for debugging that much.
9+
debug1 = 0
10+
11+
[profile.dev.package]
12+
# These speed up local tests.
13+
rowan.opt-level = 3
14+
rustc-hash.opt-level = 3
15+
smol_str.opt-level = 3
16+
text-size.opt-level = 3
17+
# This speeds up `cargo xtask dist`.
18+
miniz_oxide.opt-level = 3
19+
20+
[profile.release]
21+
incremental = true
22+
# Set this to 1 or 2 to get more useful backtraces in debugger.
23+
debug2 = 0

0 commit comments

Comments
 (0)