From fe1d82d9db6afd30b394f8e6dcc3a951ede7c068 Mon Sep 17 00:00:00 2001 From: Bram Hoendervangers Date: Fri, 30 Aug 2024 14:27:57 +0200 Subject: [PATCH] fix: use view! indentation settings when formatting syn expressions (rust code embedded in the view! macro) --- Cargo.lock | 4 +- formatter/src/formatter/attribute.rs | 6 +- formatter/src/formatter/element.rs | 4 +- formatter/src/formatter/expr.rs | 6 +- formatter/src/source_file.rs | 171 ++++++++++++++++++++++----- prettyplease | 2 +- printer/src/algorithm.rs | 7 +- 7 files changed, 155 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4191d57..d268374 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -541,9 +541,9 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "syn" -version = "2.0.66" +version = "2.0.76" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5" +checksum = "578e081a14e0cefc3279b0472138c513f37b41a08d5a3cca9b6e4e8ceb6cd525" dependencies = [ "proc-macro2", "quote", diff --git a/formatter/src/formatter/attribute.rs b/formatter/src/formatter/attribute.rs index e5d2176..69f559f 100644 --- a/formatter/src/formatter/attribute.rs +++ b/formatter/src/formatter/attribute.rs @@ -131,10 +131,14 @@ mod tests { #[test] fn key_value_expr_attr_always_unless_lit_braces() { - // sinle expr without braces + // single expr without braces let f = format_attr_with_brace_style! { AlwaysUnlessLit => on:click=move |_| set_value(0) }; assert_snapshot!(f, @"on:click={move |_| set_value(0)}"); + // single expr without braces + let f = format_attr_with_brace_style! { AlwaysUnlessLit => foo=bar }; + assert_snapshot!(f, @"foo={bar}"); + // single expr with braces let f = format_attr_with_brace_style! { AlwaysUnlessLit => on:click={move |_| set_value(0)} }; diff --git a/formatter/src/formatter/element.rs b/formatter/src/formatter/element.rs index df811dc..df2ad6a 100644 --- a/formatter/src/formatter/element.rs +++ b/formatter/src/formatter/element.rs @@ -253,7 +253,7 @@ mod tests { let formatted = format_element_from_string!(indoc! {"
+ width=100> "}); insta::assert_snapshot!(formatted, @r###" @@ -361,7 +361,7 @@ mod tests { let formatted = format_element_from_string!(indoc! {"
Unquoted text - with spaces + with spaces
"}); diff --git a/formatter/src/formatter/expr.rs b/formatter/src/formatter/expr.rs index ad10d57..062703f 100644 --- a/formatter/src/formatter/expr.rs +++ b/formatter/src/formatter/expr.rs @@ -153,12 +153,10 @@ impl Formatter<'_> { #[cfg(test)] mod tests { - use rstml::node::Node; + use crate::formatter::*; - use crate::test_helpers::{ - element, element_from_string, format_element_from_string, format_with, - }; + use crate::test_helpers::format_element_from_string; macro_rules! format_element { ($($tt:tt)*) => {{ diff --git a/formatter/src/source_file.rs b/formatter/src/source_file.rs index 607a16f..dedb379 100644 --- a/formatter/src/source_file.rs +++ b/formatter/src/source_file.rs @@ -118,7 +118,7 @@ mod tests { fn it_works() { let source = indoc! {r#" fn main() { - view! { cx ,
"hello"
}; + view! { cx ,
"hello"
}; } "#}; @@ -129,7 +129,7 @@ mod tests {
"hello"
- }; + }; } "#); @@ -139,7 +139,7 @@ mod tests { fn fully_qualified_macro_path() { let source = indoc! {r#" fn main() { - leptos::view! { cx ,
"hello"
}; + leptos::view! { cx ,
"hello"
}; } "#}; @@ -150,7 +150,7 @@ mod tests {
"hello"
- }; + }; } "#); @@ -160,7 +160,7 @@ mod tests { fn ignore_other_macros() { let source = indoc! {r#" fn main() { - leptos::view! { cx ,
"hello"
}; + leptos::view! { cx ,
"hello"
}; } "#}; @@ -171,7 +171,7 @@ mod tests {
"hello"
- }; + }; } "#); @@ -186,14 +186,14 @@ mod tests {

Hello Kanna

- + { style! { h1 { background-color: red; color: white; } - + @media (orientation: portrait) { h1 { background-color: green; @@ -219,7 +219,7 @@ mod tests { background-color: red; color: white; } - + @media (orientation: portrait) { h1 { background-color: green; @@ -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! { cx ,
"hello"
}; } "#}; @@ -254,7 +254,7 @@ mod tests {
"hello"
- }; + }; } "#); @@ -268,8 +268,8 @@ mod tests { let a = 12; - foo::bar::some_view! { cx, - + foo::bar::some_view! { cx, + {a} } } }; @@ -310,8 +310,8 @@ mod tests { let a = 12; - html! { cx, - + html! { cx, + {a} } } }; @@ -427,8 +427,8 @@ mod tests { let a = 12; - view! { cx, - + view! { cx, + {a} } } }; @@ -457,15 +457,15 @@ mod tests { fn nested_with_comments() { let source = indoc! {r#" fn main() { - view! { cx , + view! { cx , // parent div -
+
// parent span { //ok let a = 12; - view! { cx, + view! { cx, // wow, a span {a} } @@ -502,8 +502,8 @@ mod tests { fn multiple() { let source = indoc! {r#" fn main() { - view! { cx ,
"hello"
}; - view! { cx ,
"hello"
}; + view! { cx ,
"hello"
}; + view! { cx ,
"hello"
}; } "#}; @@ -514,12 +514,12 @@ mod tests {
"hello"
- }; + }; view! { cx,
"hello"
- }; + }; } "#); } @@ -528,7 +528,7 @@ mod tests { fn with_special_characters() { let source = indoc! {r#" fn main() { - view! { cx ,
"hello²💣"
}; + view! { cx ,
"hello²💣"
}; } "#}; @@ -539,7 +539,7 @@ mod tests {
"hello²💣"
- }; + }; } "#); } @@ -579,7 +579,7 @@ mod tests { #[component] fn Component(cx: Scope, val: ExampleEnum) -> impl IntoView { match val { - ExampleEnum::ValueOneWithAReallyLongName => + ExampleEnum::ValueOneWithAReallyLongName => view! { cx,
"Value One"
@@ -606,7 +606,7 @@ mod tests { #[component] fn Component(cx: Scope, val: ExampleEnum) -> impl IntoView { match val { - ExampleEnum::ValueOneWithAReallyLongName => + ExampleEnum::ValueOneWithAReallyLongName => view! { cx,
"Value One"
@@ -654,7 +654,7 @@ mod tests { fn indent_with_tabs() { let source = indoc! {" fn main() { - \u{0020}view! { cx, + \tview! { cx,
Example
@@ -665,7 +665,7 @@ mod tests { let result = format_file_source( source, &FormatterSettings { - tab_spaces: 1, + tab_spaces: 4, indentation_style: IndentationStyle::Tabs, ..Default::default() }, @@ -674,7 +674,7 @@ mod tests { let expected = indoc! {" fn main() { - \u{0020}view! { cx, + \tview! { cx, \t\t
\t\t\t
Example
\t\t
@@ -685,6 +685,48 @@ mod tests { assert_eq!(result, expected); } + #[test] + fn indent_with_tabs_including_code_blocks() { + let source = indoc! {" + fn main() { + \tview! { cx, +
+ +
+ } + } + "}; + + let result = format_file_source( + source, + &FormatterSettings { + tab_spaces: 4, + indentation_style: IndentationStyle::Tabs, + ..Default::default() + }, + ) + .unwrap(); + + let expected = indoc! {" + fn main() { + \tview! { cx, + \t\t
+ \t\t\t + \t\t
+ \t} + } + "}; + + print!("{result}"); + assert_eq!(result, expected); + } + #[test] fn auto_detect_tabs() { let source = indoc! {" @@ -757,7 +799,7 @@ mod tests { #[test] fn tailwind() { let source = indoc! {r#" - view! { + view! { @@ -783,4 +825,69 @@ mod tests { } "###); } + + #[test] + fn indent_issue_140() { + let source = indoc! {r#" + #[component] + pub fn QrScanner() -> impl IntoView { + view! { + + +
+ +
+ + } + }"#}; + + let result = format_file_source( + source, + &FormatterSettings { + indentation_style: IndentationStyle::Spaces, + tab_spaces: 2, + max_width: 80, + ..Default::default() + }, + ) + .unwrap(); + + insta::assert_snapshot!(result, @r###" + #[component] + pub fn QrScanner() -> impl IntoView { + view! { + + +
+ +
+ + } + } + "###); + } } diff --git a/prettyplease b/prettyplease index f9800e0..6a0eb2d 160000 --- a/prettyplease +++ b/prettyplease @@ -1 +1 @@ -Subproject commit f9800e0878bb2ec6433fd81862ca6b432ea718c2 +Subproject commit 6a0eb2d862fa60430f32c69bebca5a572cdcdcad diff --git a/printer/src/algorithm.rs b/printer/src/algorithm.rs index 80dfb37..da63e40 100644 --- a/printer/src/algorithm.rs +++ b/printer/src/algorithm.rs @@ -46,6 +46,7 @@ enum PrintFrame { pub(crate) const SIZE_INFINITY: isize = 0xffff; +#[derive(Debug)] pub struct PrinterSettings { // Target line width. pub margin: isize, @@ -61,7 +62,7 @@ pub struct PrinterSettings { pub struct Printer { pub settings: PrinterSettings, - out: String, + pub out: String, // Number of spaces left on line space: isize, // Ring-buffer of tokens and calculated sizes @@ -361,8 +362,8 @@ impl Printer { if !self.settings.hard_tabs { self.out.reserve(self.pending_indentation); } else { - let tabs = self.pending_indentation / self.settings.tab_spaces as usize; - let remaining_spaces = self.pending_indentation % self.settings.tab_spaces as usize; + let tabs = self.pending_indentation / 4; + let remaining_spaces = self.pending_indentation % 4; self.out.reserve(tabs + remaining_spaces); self.out.extend(iter::repeat('\t').take(tabs)); self.pending_indentation = remaining_spaces