diff --git a/formatter/src/formatter/expr.rs b/formatter/src/formatter/expr.rs index 7eb6743..ef1bd7f 100644 --- a/formatter/src/formatter/expr.rs +++ b/formatter/src/formatter/expr.rs @@ -104,7 +104,7 @@ impl Formatter<'_> { } } - self.expr(value, formatter) + self.expr(value, formatter); } fn expr(&mut self, expr: &syn::Expr, formatter: Option) { @@ -141,10 +141,10 @@ impl Formatter<'_> { leptosfmt_prettyplease::unparse_expr( expr, self.printer, - Some(&ViewMacroFormatter::new( + Some(&mut ViewMacroFormatter::new( self.settings, self.source, - self.line_offset, + &mut self.line_offset, comments_or_whitespace, )), ); diff --git a/formatter/src/formatter/mac.rs b/formatter/src/formatter/mac.rs index fcfe2f5..f7bdbd1 100644 --- a/formatter/src/formatter/mac.rs +++ b/formatter/src/formatter/mac.rs @@ -18,7 +18,7 @@ pub struct ViewMacro<'a> { pub comma: Option, } -#[derive(Default)] +#[derive(Default, Debug)] pub struct ParentIndent { pub tabs: usize, pub spaces: usize, diff --git a/formatter/src/formatter/mod.rs b/formatter/src/formatter/mod.rs index a2d1420..2a6d1ac 100644 --- a/formatter/src/formatter/mod.rs +++ b/formatter/src/formatter/mod.rs @@ -196,6 +196,10 @@ 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 a2d380c..3a3eb74 100644 --- a/formatter/src/source_file.rs +++ b/formatter/src/source_file.rs @@ -498,6 +498,62 @@ mod tests { "###); } + #[test] + fn nested_comments_in_consecutive_view_macro() { + let source = indoc! {r#" + use leptos::*; + + fn main() { + mount_to_body(|| { + view! { + {move || { + if true { + view! { + // comment in if condition. +
dummy text
+ } + .into_view() + } else { + view! { + // comment in else condition. +
dummy text
+ } + .into_view() + } + }} + } + }) + } + "#}; + + let result = format_file_source(source, &Default::default()).unwrap(); + insta::assert_snapshot!(result, @r###" + use leptos::*; + + fn main() { + mount_to_body(|| { + view! { + {move || { + if true { + view! { + // comment in if condition. +
dummy text
+ } + .into_view() + } else { + view! { + // comment in else condition. +
dummy text
+ } + .into_view() + } + }} + } + }) + } + "###); + } + #[test] fn multiple() { let source = indoc! {r#" diff --git a/formatter/src/view_macro.rs b/formatter/src/view_macro.rs index 2cad477..0fa913c 100644 --- a/formatter/src/view_macro.rs +++ b/formatter/src/view_macro.rs @@ -8,7 +8,7 @@ use crate::{Formatter, FormatterSettings, ViewMacro}; pub struct ViewMacroFormatter<'a> { settings: &'a FormatterSettings, source: Option<&'a Rope>, - line_offset: Option, + line_offset: &'a mut Option, comments: HashMap>, } @@ -16,7 +16,7 @@ impl ViewMacroFormatter<'_> { pub fn new<'a>( settings: &'a FormatterSettings, source: Option<&'a Rope>, - line_offset: Option, + line_offset: &'a mut Option, comments: HashMap>, ) -> ViewMacroFormatter<'a> { ViewMacroFormatter { @@ -38,7 +38,11 @@ pub fn get_macro_full_path(mac: &syn::Macro) -> String { } impl MacroFormatter for ViewMacroFormatter<'_> { - fn format(&self, printer: &mut leptosfmt_pretty_printer::Printer, mac: &syn::Macro) -> bool { + fn format( + &mut self, + printer: &mut leptosfmt_pretty_printer::Printer, + mac: &syn::Macro, + ) -> bool { let mut formatted = false; for macro_name in &self.settings.macro_names { @@ -49,16 +53,18 @@ impl MacroFormatter for ViewMacroFormatter<'_> { let Some(m) = ViewMacro::try_parse(Default::default(), mac) else { continue; }; + let mut formatter = Formatter { printer, settings: self.settings, source: self.source, - line_offset: self.line_offset, + line_offset: *self.line_offset, whitespace_and_comments: self.comments.clone(), }; formatter.view_macro(&m); formatted = true; + *self.line_offset = formatter.line_offset; } formatted diff --git a/prettyplease b/prettyplease index 6a0eb2d..750ad46 160000 --- a/prettyplease +++ b/prettyplease @@ -1 +1 @@ -Subproject commit 6a0eb2d862fa60430f32c69bebca5a572cdcdcad +Subproject commit 750ad4660f6b6475527b1f652ff669c808d8cc11