Skip to content

Commit

Permalink
fix: duplicated comments in consecutive macros (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
bram209 authored Sep 10, 2024
1 parent 1c3768b commit 5240eda
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 9 deletions.
6 changes: 3 additions & 3 deletions formatter/src/formatter/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl Formatter<'_> {
}
}

self.expr(value, formatter)
self.expr(value, formatter);
}

fn expr(&mut self, expr: &syn::Expr, formatter: Option<ExpressionFormatter>) {
Expand Down Expand Up @@ -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,
)),
);
Expand Down
2 changes: 1 addition & 1 deletion formatter/src/formatter/mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct ViewMacro<'a> {
pub comma: Option<TokenTree>,
}

#[derive(Default)]
#[derive(Default, Debug)]
pub struct ParentIndent {
pub tabs: usize,
pub spaces: usize,
Expand Down
4 changes: 4 additions & 0 deletions formatter/src/formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
56 changes: 56 additions & 0 deletions formatter/src/source_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
<div>dummy text</div>
}
.into_view()
} else {
view! {
// comment in else condition.
<div>dummy text</div>
}
.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.
<div>dummy text</div>
}
.into_view()
} else {
view! {
// comment in else condition.
<div>dummy text</div>
}
.into_view()
}
}}
}
})
}
"###);
}

#[test]
fn multiple() {
let source = indoc! {r#"
Expand Down
14 changes: 10 additions & 4 deletions formatter/src/view_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ use crate::{Formatter, FormatterSettings, ViewMacro};
pub struct ViewMacroFormatter<'a> {
settings: &'a FormatterSettings,
source: Option<&'a Rope>,
line_offset: Option<usize>,
line_offset: &'a mut Option<usize>,
comments: HashMap<usize, Option<String>>,
}

impl ViewMacroFormatter<'_> {
pub fn new<'a>(
settings: &'a FormatterSettings,
source: Option<&'a Rope>,
line_offset: Option<usize>,
line_offset: &'a mut Option<usize>,
comments: HashMap<usize, Option<String>>,
) -> ViewMacroFormatter<'a> {
ViewMacroFormatter {
Expand All @@ -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 {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion prettyplease

0 comments on commit 5240eda

Please sign in to comment.