Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: inline unquoted text + expr block combinations #110

Merged
merged 4 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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
4 changes: 1 addition & 3 deletions formatter/src/formatter/mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand Down
Loading