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: correctly collect leading and trailing comments #145

Merged
merged 1 commit into from
Sep 11, 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
25 changes: 17 additions & 8 deletions formatter/src/formatter/mac.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -197,15 +206,15 @@ mod tests {

#[test]
fn one_liner() {
let formatted = view_macro!(view! { cx, <div>"hi"</div> });
insta::assert_snapshot!(formatted, @r#"view! { cx, <div>"hi"</div> }"#);
let formatted = view_macro!(view! { <div>"hi"</div> });
insta::assert_snapshot!(formatted, @r#"view! { <div>"hi"</div> }"#);
}

#[test]
fn with_nested_nodes() {
let formatted = view_macro!(view! { cx, <div><span>"hi"</span></div> });
let formatted = view_macro!(view! { <div><span>"hi"</span></div> });
insta::assert_snapshot!(formatted, @r#"
view! { cx,
view! {
<div>
<span>"hi"</span>
</div>
Expand All @@ -215,9 +224,9 @@ mod tests {

#[test]
fn with_global_class() {
let formatted = view_macro!(view! { cx, class = STYLE, <div><span>"hi"</span></div> });
let formatted = view_macro!(view! { class = STYLE, <div><span>"hi"</span></div> });
insta::assert_snapshot!(formatted, @r#"
view! { cx, class=STYLE,
view! { class=STYLE,
<div>
<span>"hi"</span>
</div>
Expand Down
4 changes: 0 additions & 4 deletions formatter/src/formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading
Loading