From 3f96d220641f1be2f644943720b71ff7890e044b Mon Sep 17 00:00:00 2001 From: Bram Hoendervangers Date: Mon, 7 Oct 2024 16:25:06 +0200 Subject: [PATCH 1/2] feat: support let bindings --- formatter/src/formatter/attribute.rs | 53 +++++++++++++++++++++++----- formatter/src/formatter/element.rs | 2 +- formatter/src/formatter/expr.rs | 4 +-- formatter/src/formatter/mod.rs | 9 +++++ prettyplease | 2 +- 5 files changed, 57 insertions(+), 13 deletions(-) diff --git a/formatter/src/formatter/attribute.rs b/formatter/src/formatter/attribute.rs index 69f559f..60f1760 100644 --- a/formatter/src/formatter/attribute.rs +++ b/formatter/src/formatter/attribute.rs @@ -1,4 +1,4 @@ -use rstml::node::{KeyedAttribute, NodeAttribute}; +use rstml::node::{FnBinding, KeyedAttribute, KeyedAttributeValue, NodeAttribute}; use syn::{spanned::Spanned, Expr}; use crate::{formatter::Formatter, AttributeValueBraceStyle as Braces}; @@ -17,16 +17,33 @@ impl Formatter<'_> { pub fn keyed_attribute(&mut self, attribute: &KeyedAttribute) { self.node_name(&attribute.key); - if let Some(value) = attribute.value() { - let formatter = self - .settings - .attr_values - .get(&attribute.key.to_string()) - .copied(); + match &attribute.possible_value { + KeyedAttributeValue::None => {} + KeyedAttributeValue::Binding(binding) => self.attribute_binding(binding), + KeyedAttributeValue::Value(expr) => { + let formatter = self + .settings + .attr_values + .get(&attribute.key.to_string()) + .copied(); + + self.printer.word("="); + self.attribute_value(&expr.value, formatter); + } + } + } - self.printer.word("="); - self.attribute_value(value, formatter); + fn attribute_binding(&mut self, binding: &FnBinding) { + self.printer.word("("); + let mut iterator = binding.inputs.iter().peekable(); + while let Some(input) = iterator.next() { + self.format_syn_pat(input); + if iterator.peek().is_some() { + self.printer.word(","); + self.printer.space(); + } } + self.printer.word(")"); } fn attribute_value(&mut self, value: &Expr, formatter: Option) { @@ -190,4 +207,22 @@ mod tests { let f = format_attr_with_brace_style! { WhenRequired => alt={"test img"} }; assert_snapshot!(f, @r#"alt="test img""#); } + + #[test] + fn let_bindings_single() { + let f = format_attribute! { let(name) }; + assert_snapshot!(f, @r#"let(name)"#) + } + + #[test] + fn let_bindings_multiple() { + let f = format_attribute! { let(name, foo, bar) }; + assert_snapshot!(f, @r#"let(name, foo, bar)"#) + } + + #[test] + fn let_bindings_destructuring() { + let f = format_attribute! { let(Item { name, value }) }; + assert_snapshot!(f, @r#"let(Item { name, value })"#) + } } diff --git a/formatter/src/formatter/element.rs b/formatter/src/formatter/element.rs index df2ad6a..e80c22b 100644 --- a/formatter/src/formatter/element.rs +++ b/formatter/src/formatter/element.rs @@ -20,7 +20,7 @@ impl Formatter<'_> { fn opening_tag(&mut self, element: &NodeElement, is_self_closing: bool) { self.printer.word("<"); self.node_name(&element.open_tag.name); - leptosfmt_prettyplease::unparse_generics(&element.open_tag.generics, self.printer); + self.format_syn_generics(&element.open_tag.generics); self.attributes(element.attributes(), is_self_closing); diff --git a/formatter/src/formatter/expr.rs b/formatter/src/formatter/expr.rs index ef1bd7f..4c6a04f 100644 --- a/formatter/src/formatter/expr.rs +++ b/formatter/src/formatter/expr.rs @@ -138,8 +138,7 @@ impl Formatter<'_> { .map(|line| (line, self.whitespace_and_comments.remove(&line).unwrap())) .collect::>(); - leptosfmt_prettyplease::unparse_expr( - expr, + leptosfmt_prettyplease::unparse_fn( self.printer, Some(&mut ViewMacroFormatter::new( self.settings, @@ -147,6 +146,7 @@ impl Formatter<'_> { &mut self.line_offset, comments_or_whitespace, )), + |p| p.expr(expr), ); } } diff --git a/formatter/src/formatter/mod.rs b/formatter/src/formatter/mod.rs index a2d1420..a232e7b 100644 --- a/formatter/src/formatter/mod.rs +++ b/formatter/src/formatter/mod.rs @@ -18,6 +18,7 @@ pub use mac::{ParentIndent, ViewMacro}; use serde::Deserialize; use serde::Serialize; +use syn::{Expr, Generics, Pat}; #[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)] pub enum ClosingTagStyle { @@ -233,4 +234,12 @@ impl<'a> Formatter<'a> { self.line_offset = Some(line_index); } + + pub fn format_syn_pat(&mut self, pat: &Pat) { + leptosfmt_prettyplease::unparse_fn(&mut self.printer, None, |p| p.pat(pat)); + } + + pub fn format_syn_generics(&mut self, generics: &Generics) { + leptosfmt_prettyplease::unparse_fn(&mut self.printer, None, |p| p.generics(generics)); + } } diff --git a/prettyplease b/prettyplease index 7aa7b6d..b233625 160000 --- a/prettyplease +++ b/prettyplease @@ -1 +1 @@ -Subproject commit 7aa7b6dbb6e0f56d2afb809c43e3c8894eb1cfd0 +Subproject commit b233625a311f019831b1b565fd97004427f41e47 From ac15e5425c440d3b29fb81b2320967600147ef16 Mon Sep 17 00:00:00 2001 From: Bram Hoendervangers Date: Mon, 7 Oct 2024 16:26:39 +0200 Subject: [PATCH 2/2] clippy --- formatter/src/formatter/mod.rs | 6 +++--- prettyplease | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/formatter/src/formatter/mod.rs b/formatter/src/formatter/mod.rs index a232e7b..590e703 100644 --- a/formatter/src/formatter/mod.rs +++ b/formatter/src/formatter/mod.rs @@ -18,7 +18,7 @@ pub use mac::{ParentIndent, ViewMacro}; use serde::Deserialize; use serde::Serialize; -use syn::{Expr, Generics, Pat}; +use syn::{Generics, Pat}; #[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)] pub enum ClosingTagStyle { @@ -236,10 +236,10 @@ impl<'a> Formatter<'a> { } pub fn format_syn_pat(&mut self, pat: &Pat) { - leptosfmt_prettyplease::unparse_fn(&mut self.printer, None, |p| p.pat(pat)); + leptosfmt_prettyplease::unparse_fn(self.printer, None, |p| p.pat(pat)); } pub fn format_syn_generics(&mut self, generics: &Generics) { - leptosfmt_prettyplease::unparse_fn(&mut self.printer, None, |p| p.generics(generics)); + leptosfmt_prettyplease::unparse_fn(self.printer, None, |p| p.generics(generics)); } } diff --git a/prettyplease b/prettyplease index b233625..bebc259 160000 --- a/prettyplease +++ b/prettyplease @@ -1 +1 @@ -Subproject commit b233625a311f019831b1b565fd97004427f41e47 +Subproject commit bebc259d12bb0f9a53132f3700017b56d32a4dad