From a7a4284b302a36816b85388fc86df9746bd82add Mon Sep 17 00:00:00 2001 From: schvv31n Date: Sun, 17 Nov 2024 12:17:06 +0000 Subject: [PATCH] v0.5.3: comments now remain intact in empty macro invocations --- Cargo.lock | 4 +- Cargo.toml | 2 +- src/formatter.rs | 60 +++++++++---------- src/utils.rs | 2 +- tests/main.rs | 5 ++ .../samples/comments_in_empty_macro/source.rs | 11 ++++ .../samples/comments_in_empty_macro/target.rs | 10 ++++ 7 files changed, 60 insertions(+), 34 deletions(-) create mode 100644 tests/samples/comments_in_empty_macro/source.rs create mode 100644 tests/samples/comments_in_empty_macro/target.rs diff --git a/Cargo.lock b/Cargo.lock index e692472..c99f2ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "anstream" @@ -458,7 +458,7 @@ checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" [[package]] name = "yew-fmt" -version = "0.5.2" +version = "0.5.3" dependencies = [ "anyhow", "basic-toml", diff --git a/Cargo.toml b/Cargo.toml index d5e3032..4ffd507 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "yew-fmt" -version = "0.5.2" +version = "0.5.3" edition = "2021" authors = ["Tim Kurdov "] repository = "https://github.com/schvv31n/yew-fmt" diff --git a/src/formatter.rs b/src/formatter.rs index 6f41875..74ae890 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -37,7 +37,7 @@ fn add_last_line_len(prev: usize, new: &str) -> usize { new.last_line_len().unwrap_or(new.len() + prev) } -#[derive(Clone, Copy)] +#[derive(Debug, Clone, Copy)] enum Comment<'src> { /// the initial `//` and the newline are not included Line(&'src str), @@ -673,39 +673,39 @@ impl<'fmt, 'src: 'fmt> Visit<'_> for FmtCtx<'fmt, 'src> { }; let span = i.delimiter.span(); let (opening_span, closing_span) = (span.open(), span.close()); + let (html_start, html_end) = (opening_span.end(), closing_span.start()); self.print_source(opening_span.start())?; - let html_start = opening_span.end(); + self.print_text(opening, html_start)?; if i.tokens.is_empty() { - self.print_text(opening, html_start)?; - self.print_text(closing, closing_span.end())?; - return Ok(None); - } - - let html = match self.config.yew.html_flavor.parse_root(i.tokens.clone()) { - Ok(html) => html, - Err(e) => { - let span = e.span(); - let start = self.pos_to_byte_offset(span.start())?; - let end = self.pos_to_byte_offset(span.end())?; - return Ok(Some( - Diagnostic::error() - .with_message(e.to_string()) - .with_labels(vec![Label::primary((), start..end)]), - )); - } - }; - let mut block = FmtBlock::new( - self.alloc, - Some(root_spacing), - ChainingRule::Off, - self.pos_to_byte_offset(html_start)?, - ); - html.format(&mut block, self)?; + self.print_source(html_end)?; + } else { + let mut block = FmtBlock::new( + self.alloc, + Some(root_spacing), + ChainingRule::Off, + self.pos_to_byte_offset(html_start)?, + ); + + let html = match self.config.yew.html_flavor.parse_root(i.tokens.clone()) { + Ok(html) => html, + Err(e) => { + let span = e.span(); + let start = self.pos_to_byte_offset(span.start())?; + let end = self.pos_to_byte_offset(span.end())?; + return Ok(Some( + Diagnostic::error() + .with_message(e.to_string()) + .with_labels(vec![Label::primary((), start..end)]), + )); + } + }; + html.format(&mut block, self)?; - self.print_text(opening, html_start)?; - self.print_fmt_block(block, closing_span.start())?; + self.print_fmt_block(block, html_end)?; + } self.print_text(closing, closing_span.end())?; + Ok(None) })(); } @@ -874,7 +874,7 @@ pub struct FormatResult<'fmt, 'src> { output: Result<&'fmt str, Diagnostic<()>>, } -impl<'fmt, 'src> FormatResult<'fmt, 'src> { +impl<'fmt> FormatResult<'fmt, '_> { /// if the result is an error, write it into stderr, if it's successfully formatted code, /// return it pub fn emit_error(self, writer: &mut dyn WriteColor) -> Result> { diff --git a/src/utils.rs b/src/utils.rs index 51e099b..cb1ea49 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -93,7 +93,7 @@ pub struct WithPrevMut<'slice, T> { index: usize, } -impl<'slice, T> WithPrevMut<'slice, T> { +impl WithPrevMut<'_, T> { pub fn next(&mut self) -> Option<(&mut T, &mut [T])> { // Safety: if the slice is exhausted, the function will always return before reaching // `.unwrap_unchecked()` diff --git a/tests/main.rs b/tests/main.rs index b8a8821..8e44261 100644 --- a/tests/main.rs +++ b/tests/main.rs @@ -18,6 +18,11 @@ fn breaking_propagated() { cmp("tests/samples/breaking_propagated") } +#[test] +fn comments_in_empty_macro() { + cmp("tests/samples/comments_in_empty_macro") +} + #[test] fn elements_broken_up() { cmp("tests/samples/elements_broken_up") diff --git a/tests/samples/comments_in_empty_macro/source.rs b/tests/samples/comments_in_empty_macro/source.rs new file mode 100644 index 0000000..6a38a28 --- /dev/null +++ b/tests/samples/comments_in_empty_macro/source.rs @@ -0,0 +1,11 @@ +use +yew::prelude::*; + +#[function_component] +fn Comp() -> Html { + /* comment */ + html! { + // comment inside + // тут щось важливе + } +} diff --git a/tests/samples/comments_in_empty_macro/target.rs b/tests/samples/comments_in_empty_macro/target.rs new file mode 100644 index 0000000..9b2e73d --- /dev/null +++ b/tests/samples/comments_in_empty_macro/target.rs @@ -0,0 +1,10 @@ +use yew::prelude::*; + +#[function_component] +fn Comp() -> Html { + /* comment */ + html! { + // comment inside + // тут щось важливе + } +}