fix: prevent dx fmt from adding trailing whitespace to blank lines (#5224)
This fixes a conflict between dx fmt and cargo fmt (rustfmt). Previously, dx fmt would add indentation whitespace to blank lines inside RSX blocks, but cargo fmt removes trailing whitespace from all lines. This made it impossible for code to satisfy both formatters. The fix skips adding indentation to empty lines in two places: 1. When indenting formatted RSX blocks inside expressions 2. When reassembling the final formatted output Fixes #5223 Co-authored-by: Dmitry Minkovsky <dminkovsky@gmail.com>
This commit is contained in:
1 parent
8eb4322de2
commit
3a60140dff
4 files changed
+37
-3
No files matched your search
@@ -55,7 +55,14 @@ pub fn unparse_expr(expr: &Expr, src: &str, cfg: &IndentOptions) -> String {
|
||||
if multiline || formatted.contains('\n') {
|
||||
formatted = formatted
|
||||
.lines()
|
||||
.map(|line| format!("{}{line}", self.cfg.indent_str()))
|
||||
.map(|line| {
|
||||
// Don't add indentation to blank lines (avoid trailing whitespace)
|
||||
if line.is_empty() {
|
||||
String::new()
|
||||
} else {
|
||||
format!("{}{line}", self.cfg.indent_str())
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
}
|
||||
@@ -106,8 +113,8 @@ pub fn unparse_expr(expr: &Expr, src: &str, cfg: &IndentOptions) -> String {
|
||||
let mut lines = fmted.lines().enumerate().peekable();
|
||||
|
||||
while let Some((_idx, fmt_line)) = lines.next() {
|
||||
// Push the indentation
|
||||
if is_multiline {
|
||||
// Push the indentation (but not for blank lines - avoid trailing whitespace)
|
||||
if is_multiline && !fmt_line.is_empty() {
|
||||
out_fmt.push_str(&cfg.indent_str().repeat(whitespace));
|
||||
}
|
||||
|
||||
|
||||
@@ -62,4 +62,6 @@ twoway![
|
||||
asset,
|
||||
collapse,
|
||||
expr_on_conditional,
|
||||
blank_lines,
|
||||
blank_lines_preserved,
|
||||
];
|
||||
@@ -0,0 +1,16 @@
|
||||
rsx! {
|
||||
div {
|
||||
match true {
|
||||
true => rsx! {
|
||||
if true {
|
||||
span { "a" }
|
||||
}
|
||||
if true {
|
||||
span { "b" }
|
||||
}
|
||||
span { "c" }
|
||||
},
|
||||
false => rsx! {},
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
rsx! {
|
||||
div {
|
||||
span { "a" }
|
||||
|
||||
span { "b" }
|
||||
|
||||
span { "c" }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user