diff --git a/formatter/src/formatter/mac.rs b/formatter/src/formatter/mac.rs index f7bdbd1..b104ab8 100644 --- a/formatter/src/formatter/mac.rs +++ b/formatter/src/formatter/mac.rs @@ -1,6 +1,7 @@ use crop::Rope; use leptosfmt_pretty_printer::Printer; use proc_macro2::{token_stream, Span, TokenStream, TokenTree}; +use quote::ToTokens; use rstml::node::Node; use syn::{spanned::Spanned, Macro}; @@ -84,7 +85,15 @@ impl Formatter<'_> { self.printer .cbox((parent_indent.tabs * self.settings.tab_spaces + parent_indent.spaces) as isize); - self.flush_comments(cx.span().start().line - 1, false); + self.flush_comments( + cx.as_ref() + .map(|cx| cx.span()) + .unwrap_or_else(|| view_mac.mac.delimiter.span().open()) + .start() + .line + - 1, + false, + ); let macro_word = format!("{}! {{", get_macro_full_path(view_mac.mac)); self.printer.word(macro_word); @@ -169,7 +178,7 @@ pub fn format_macro( Some(source) => { let whitespace = crate::collect_comments::extract_whitespace_and_comments( source, - mac.mac.tokens.clone(), + mac.mac.to_token_stream(), ); Formatter::with_source(settings, &mut printer, source, whitespace) @@ -197,15 +206,15 @@ mod tests { #[test] fn one_liner() { - let formatted = view_macro!(view! { cx,
"hi"
}); - insta::assert_snapshot!(formatted, @r#"view! { cx,
"hi"
}"#); + let formatted = view_macro!(view! {
"hi"
}); + insta::assert_snapshot!(formatted, @r#"view! {
"hi"
}"#); } #[test] fn with_nested_nodes() { - let formatted = view_macro!(view! { cx,
"hi"
}); + let formatted = view_macro!(view! {
"hi"
}); insta::assert_snapshot!(formatted, @r#" - view! { cx, + view! {
"hi"
@@ -215,9 +224,9 @@ mod tests { #[test] fn with_global_class() { - let formatted = view_macro!(view! { cx, class = STYLE,
"hi"
}); + let formatted = view_macro!(view! { class = STYLE,
"hi"
}); insta::assert_snapshot!(formatted, @r#" - view! { cx, class=STYLE, + view! { class=STYLE,
"hi"
diff --git a/formatter/src/formatter/mod.rs b/formatter/src/formatter/mod.rs index 2a6d1ac..a2d1420 100644 --- a/formatter/src/formatter/mod.rs +++ b/formatter/src/formatter/mod.rs @@ -196,10 +196,6 @@ impl<'a> Formatter<'a> { pub fn flush_comments(&mut self, line_index: usize, skip_trailing_whitespace: bool) { let last = self.line_offset.unwrap_or(0); - if last > line_index { - return; - } - let comments_or_empty_lines: Vec<_> = (last..=line_index) .filter_map(|l| self.whitespace_and_comments.remove(&l)) .collect(); diff --git a/formatter/src/source_file.rs b/formatter/src/source_file.rs index 3a3eb74..9ae462d 100644 --- a/formatter/src/source_file.rs +++ b/formatter/src/source_file.rs @@ -89,7 +89,7 @@ mod tests { // Valid Rust formatted code #[component] pub(crate) fn Error(cx: Scope, message: Option) -> impl IntoView { - view! { cx, + view! {
Example
@@ -109,7 +109,7 @@ mod tests { // Valid Rust formatted code #[component] pub(crate) fn Error(cx: Scope, message: Option) -> impl IntoView { - view! { cx,
Example
} + view! {
Example
} } "###); } @@ -118,14 +118,14 @@ mod tests { fn it_works() { let source = indoc! {r#" fn main() { - view! { cx ,
"hello"
}; + view! {
"hello"
}; } "#}; let result = format_file_source(source, &Default::default()).unwrap(); insta::assert_snapshot!(result, @r#" fn main() { - view! { cx, + view! {
"hello"
@@ -139,14 +139,14 @@ mod tests { fn fully_qualified_macro_path() { let source = indoc! {r#" fn main() { - leptos::view! { cx ,
"hello"
}; + leptos::view! {
"hello"
}; } "#}; let result = format_file_source(source, &Default::default()).unwrap(); insta::assert_snapshot!(result, @r#" fn main() { - leptos::view! { cx, + leptos::view! {
"hello"
@@ -160,14 +160,14 @@ mod tests { fn ignore_other_macros() { let source = indoc! {r#" fn main() { - leptos::view! { cx ,
"hello"
}; + leptos::view! {
"hello"
}; } "#}; let result = format_file_source(source, &Default::default()).unwrap(); insta::assert_snapshot!(result, @r#" fn main() { - leptos::view! { cx, + leptos::view! {
"hello"
@@ -236,7 +236,7 @@ mod tests { fn fully_qualified_macro_path_overridden() { let source = indoc! {r#" fn main() { - foo::bar::some_view! { cx ,
"hello"
}; + foo::bar::some_view! {
"hello"
}; } "#}; @@ -250,7 +250,7 @@ mod tests { .unwrap(); insta::assert_snapshot!(result, @r#" fn main() { - foo::bar::some_view! { cx, + foo::bar::some_view! {
"hello"
@@ -264,11 +264,11 @@ mod tests { fn fully_qualified_macro_path_with_indent() { let source = indoc! {r#" fn main() { - foo::bar::some_view! { cx ,
{ + foo::bar::some_view! {
{ let a = 12; - foo::bar::some_view! { cx, + foo::bar::some_view! { {a} } @@ -286,13 +286,13 @@ mod tests { .unwrap(); insta::assert_snapshot!(result, @r#" fn main() { - foo::bar::some_view! { cx, + foo::bar::some_view! {
{ let a = 12; - foo::bar::some_view! { cx, {a} } + foo::bar::some_view! { {a} } }
@@ -306,11 +306,11 @@ mod tests { fn override_macro_names() { let source = indoc! {r#" fn main() { - html! { cx ,
{ + html! {
{ let a = 12; - html! { cx, + html! { {a} } @@ -328,13 +328,13 @@ mod tests { .unwrap(); insta::assert_snapshot!(result, @r#" fn main() { - html! { cx, + html! {
{ let a = 12; - html! { cx, {a} } + html! { {a} } }
@@ -349,7 +349,7 @@ mod tests { let source = indoc! {r#" // comment outside view macro fn main() { - view! { cx , + view! { // Top level comment
// This is one beautiful message @@ -383,7 +383,7 @@ mod tests { insta::assert_snapshot!(result, @r###" // comment outside view macro fn main() { - view! { cx, + view! { // Top level comment
// This is one beautiful message @@ -423,11 +423,11 @@ mod tests { fn nested() { let source = indoc! {r#" fn main() { - view! { cx ,
{ + view! {
{ let a = 12; - view! { cx, + view! { {a} } @@ -438,13 +438,13 @@ mod tests { let result = format_file_source(source, &Default::default()).unwrap(); insta::assert_snapshot!(result, @r###" fn main() { - view! { cx, + view! {
{ let a = 12; - view! { cx, {a} } + view! { {a} } }
@@ -457,7 +457,8 @@ mod tests { fn nested_with_comments() { let source = indoc! {r#" fn main() { - view! { cx , + view! { + // parent div
@@ -465,7 +466,7 @@ mod tests { { //ok let a = 12; - view! { cx, + view! { // wow, a span {a} } @@ -476,7 +477,7 @@ mod tests { let result = format_file_source(source, &Default::default()).unwrap(); insta::assert_snapshot!(result, @r###" fn main() { - view! { cx, + view! { // parent div
@@ -486,7 +487,7 @@ mod tests { { let a = 12; - view! { cx, + view! { // wow, a span {a} } @@ -558,20 +559,20 @@ mod tests { fn multiple() { let source = indoc! {r#" fn main() { - view! { cx ,
"hello"
}; - view! { cx ,
"hello"
}; + view! {
"hello"
}; + view! {
"hello"
}; } "#}; let result = format_file_source(source, &Default::default()).unwrap(); insta::assert_snapshot!(result, @r#" fn main() { - view! { cx, + view! {
"hello"
}; - view! { cx, + view! {
"hello"
@@ -584,14 +585,14 @@ mod tests { fn with_special_characters() { let source = indoc! {r#" fn main() { - view! { cx ,
"hello²💣"
}; + view! {
"hello²💣"
}; } "#}; let result = format_file_source(source, &Default::default()).unwrap(); insta::assert_snapshot!(result, @r#" fn main() { - view! { cx, + view! {
"hello²💣"
@@ -605,7 +606,7 @@ mod tests { let source = indoc! {r#" #[component] fn test2(cx: Scope) -> impl IntoView { - let x = view! { cx,
Hello
}; + let x = view! {
Hello
}; } "#}; @@ -613,7 +614,7 @@ mod tests { insta::assert_snapshot!(result, @r###" #[component] fn test2(cx: Scope) -> impl IntoView { - let x = view! { cx, + let x = view! {
Hello
@@ -636,12 +637,12 @@ mod tests { fn Component(cx: Scope, val: ExampleEnum) -> impl IntoView { match val { ExampleEnum::ValueOneWithAReallyLongName => - view! { cx, + view! {
"Value One"
}.into_view(cx), - ExampleEnum::ValueTwoWithAReallyLongName => view! { cx, + ExampleEnum::ValueTwoWithAReallyLongName => view! {
"Value Two"
@@ -663,12 +664,12 @@ mod tests { fn Component(cx: Scope, val: ExampleEnum) -> impl IntoView { match val { ExampleEnum::ValueOneWithAReallyLongName => - view! { cx, + view! {
"Value One"
}.into_view(cx), - ExampleEnum::ValueTwoWithAReallyLongName => view! { cx, + ExampleEnum::ValueTwoWithAReallyLongName => view! {
"Value Two"
@@ -710,7 +711,7 @@ mod tests { fn indent_with_tabs() { let source = indoc! {" fn main() { - \tview! { cx, + \tview! {
Example
@@ -730,7 +731,7 @@ mod tests { let expected = indoc! {" fn main() { - \tview! { cx, + \tview! { \t\t
\t\t\t
Example
\t\t
@@ -745,7 +746,7 @@ mod tests { fn indent_with_tabs_including_code_blocks() { let source = indoc! {" fn main() { - \tview! { cx, + \tview! {
@@ -805,7 +806,7 @@ mod tests { let expected = indoc! {" fn main() { - \tview! { cx, + \tview! { \t\t
\t\t\t
Example
\t\t
@@ -820,7 +821,7 @@ mod tests { fn auto_detect_spaces() { let source = indoc! {" fn main() { - \u{0020}view! { cx, + \u{0020}view! {
Example
@@ -840,7 +841,7 @@ mod tests { let expected = indoc! {" fn main() { - \u{0020}view! { cx, + \u{0020}view! { \u{0020}\u{0020}
\u{0020}\u{0020}\u{0020}
Example
\u{0020}\u{0020}