From f50dd35617a581239fb26b6e64b1b5a00ee784f5 Mon Sep 17 00:00:00 2001 From: Bram Date: Sat, 2 Mar 2024 18:33:50 +0100 Subject: [PATCH] fix: inline unquoted text + expr block combinations (#110) * fix: inline unquoted text + expr block combinations * clippy * clippy * formatting --- Cargo.lock | 4 +-- formatter/Cargo.toml | 2 +- formatter/src/formatter/element.rs | 42 +++++++++++++++++++++++++++--- formatter/src/formatter/mac.rs | 4 +-- 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7513537..7e5803c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -492,9 +492,9 @@ dependencies = [ [[package]] name = "rstml" -version = "0.10.6" +version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7afcc74cab5d3118523b1f75900e1fcbeae7cac6c6cb800430621bf58add0bd" +checksum = "fe542870b8f59dd45ad11d382e5339c9a1047cde059be136a7016095bbdefa77" dependencies = [ "proc-macro2", "proc-macro2-diagnostics", diff --git a/formatter/Cargo.toml b/formatter/Cargo.toml index f082e33..64e7198 100644 --- a/formatter/Cargo.toml +++ b/formatter/Cargo.toml @@ -9,7 +9,7 @@ description = "view macro formatter for the Leptos web framework" [dependencies] leptosfmt-pretty-printer.workspace = true -rstml = "0.10.6" +rstml = "0.11.2" syn = { version = "2.0.18", features = ["visit", "full", "extra-traits"] } leptosfmt-prettyplease = { features = ["verbatim"], version = "0.2.16" } proc-macro2 = { version = "1.0.68", features = ["span-locations"] } diff --git a/formatter/src/formatter/element.rs b/formatter/src/formatter/element.rs index ea7e2b7..84fe88e 100644 --- a/formatter/src/formatter/element.rs +++ b/formatter/src/formatter/element.rs @@ -1,4 +1,5 @@ use crate::formatter::Formatter; + use rstml::node::{Node, NodeAttribute, NodeElement}; use syn::spanned::Spanned; @@ -88,8 +89,17 @@ impl Formatter<'_> { while let Some(child) = iter.next() { self.node(child); - if iter.peek().is_some() { - self.printer.space() + if let Some(next_child) = iter.peek() { + let curr_end = child.span().end(); + let next_start = next_child.span().start(); + let consecutive = + curr_end.line == next_start.line && next_start.column == curr_end.column; + + if !matches!(next_child, Node::RawText(_)) && !consecutive { + self.printer.space() + } else { + self.printer.zerobreak() + } } } @@ -251,8 +261,8 @@ mod tests { #[test] fn child_element_two_textual() { - let formatted = format_element! {
"The count is" {count}
}; - insta::assert_snapshot!(formatted, @r#"
"The count is" {count}
"#); + let formatted = format_element! {
"The count is " {count}
}; + insta::assert_snapshot!(formatted, @r#"
"The count is " {count}
"#); } #[test] @@ -266,6 +276,30 @@ mod tests { "#); } + #[test] + fn child_element_two_textual_unquoted() { + let formatted = format_element_from_string! { "
The count is {count}.
" }; + insta::assert_snapshot!(formatted, @r#"
The count is {count}.
"#); + } + + #[test] + fn child_element_two_textual_unquoted_no_trailingspace() { + let formatted = format_element_from_string! { "
The count is{count}
" }; + insta::assert_snapshot!(formatted, @r#"
The count is{count}
"#); + } + + #[test] + fn child_element_many_textual_unquoted() { + let formatted = format_element_from_string! { "
The current count is: {count}. Increment by one is this: {count + 1}
" }; + insta::assert_snapshot!(formatted, @r###" +
+ The current count is: {count}. Increment by one is this: + {count + 1} +
+ "###); + } + // view! {

Something: {something} .

} + #[test] fn html_unquoted_text() { let formatted = format_element_from_string!(r##"
Unquoted text
"##); diff --git a/formatter/src/formatter/mac.rs b/formatter/src/formatter/mac.rs index ad66207..d7dc028 100644 --- a/formatter/src/formatter/mac.rs +++ b/formatter/src/formatter/mac.rs @@ -50,9 +50,7 @@ impl<'a> ViewMacro<'a> { (cx, comma) }; - let Some((tokens, global_class)) = extract_global_class(tokens) else { - return None; - }; + let (tokens, global_class) = extract_global_class(tokens)?; let span = mac.span(); let nodes = rstml::parse2(tokens).ok()?;