Skip to content

Commit

Permalink
fix: inline unquoted text + expr block combinations
Browse files Browse the repository at this point in the history
  • Loading branch information
bram209 committed Mar 2, 2024
1 parent 27d26e6 commit 1201980
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion formatter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
42 changes: 38 additions & 4 deletions formatter/src/formatter/element.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::formatter::Formatter;
use quote::ToTokens;

Check failure on line 2 in formatter/src/formatter/element.rs

View workflow job for this annotation

GitHub Actions / Clippy

unused import: `quote::ToTokens`

Check warning on line 2 in formatter/src/formatter/element.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `quote::ToTokens`

Check warning on line 2 in formatter/src/formatter/element.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `quote::ToTokens`
use rstml::node::{Node, NodeAttribute, NodeElement};
use syn::spanned::Spanned;

Expand Down Expand Up @@ -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()
}
}
}

Expand Down Expand Up @@ -251,8 +261,8 @@ mod tests {

#[test]
fn child_element_two_textual() {
let formatted = format_element! { <div>"The count is" {count}</div> };
insta::assert_snapshot!(formatted, @r#"<div>"The count is" {count}</div>"#);
let formatted = format_element! { <div>"The count is " {count}</div> };
insta::assert_snapshot!(formatted, @r#"<div>"The count is " {count}</div>"#);
}

#[test]
Expand All @@ -266,6 +276,30 @@ mod tests {
"#);
}

#[test]
fn child_element_two_textual_unquoted() {
let formatted = format_element_from_string! { "<div>The count is {count}.</div>" };
insta::assert_snapshot!(formatted, @r#"<div>The count is {count}.</div>"#);
}

#[test]
fn child_element_two_textual_unquoted_no_trailingspace() {
let formatted = format_element_from_string! { "<div>The count is{count}</div>" };
insta::assert_snapshot!(formatted, @r#"<div>The count is{count}</div>"#);
}

#[test]
fn child_element_many_textual_unquoted() {
let formatted = format_element_from_string! { "<div>The current count is: {count}. Increment by one is this: {count + 1}</div>" };
insta::assert_snapshot!(formatted, @r###"
<div>
The current count is: {count}. Increment by one is this:
{count + 1}
</div>
"###);
}
// view! { <p>Something: {something} .</p> }

#[test]
fn html_unquoted_text() {
let formatted = format_element_from_string!(r##"<div>Unquoted text</div>"##);
Expand Down

0 comments on commit 1201980

Please sign in to comment.