From bd936d3af21f8f4f04df47db1c3ec48b625c61ad Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Tue, 28 Jun 2022 17:29:10 -0400 Subject: [PATCH] feat: more cleanup --- packages/autofmt/src/component.rs | 20 +++++++++++++------- packages/autofmt/src/element.rs | 22 ++++++++++++++++++++++ packages/autofmt/tests/sink.rs | 2 +- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/packages/autofmt/src/component.rs b/packages/autofmt/src/component.rs index de6e146bf..32c028247 100644 --- a/packages/autofmt/src/component.rs +++ b/packages/autofmt/src/component.rs @@ -36,7 +36,7 @@ impl Buffer { let mut opt_level = ShortOptimization::NoOpt; // check if we have a lot of attributes - let is_short_attr_list = self.is_short_fields(fields, manual_props); + let is_short_attr_list = self.is_short_fields(fields, manual_props).is_some(); let is_small_children = self.is_short_children(children).is_some(); // if we have few attributes and a lot of children, place the attrs on top @@ -179,7 +179,11 @@ impl Buffer { Ok(()) } - fn is_short_fields(&self, fields: &[ComponentField], manual_props: &Option) -> bool { + pub fn is_short_fields( + &self, + fields: &[ComponentField], + manual_props: &Option, + ) -> Option { let attr_len = fields .iter() .map(|field| match &field.content { @@ -193,14 +197,18 @@ impl Buffer { Some(p) => { let content = prettyplease::unparse_expr(p); if content.len() + attr_len > 80 { - return false; + return None; } let mut lines = content.lines(); lines.next().unwrap(); - lines.next().is_none() + if lines.next().is_none() { + Some(attr_len + content.len()) + } else { + None + } } - None => attr_len < 80, + None => Some(attr_len), } } @@ -209,8 +217,6 @@ impl Buffer { We want to normalize the expr to the appropriate indent level. */ - use syn::spanned::Spanned; - let formatted = prettyplease::unparse_expr(exp); let mut lines = formatted.lines(); diff --git a/packages/autofmt/src/element.rs b/packages/autofmt/src/element.rs index ba8a17b5d..8fe64c454 100644 --- a/packages/autofmt/src/element.rs +++ b/packages/autofmt/src/element.rs @@ -223,6 +223,28 @@ impl Buffer { match children { [BodyNode::Text(ref text)] => Some(text.value().len()), + [BodyNode::Component(ref comp)] => { + let is_short_child = self.is_short_children(&comp.children); + let is_short_attrs = self.is_short_fields(&comp.fields, &comp.manual_props); + + match (is_short_child, is_short_attrs) { + (Some(child_len), Some(attrs_len)) => Some(child_len + attrs_len), + (Some(child_len), None) => Some(child_len), + (None, Some(attrs_len)) => Some(attrs_len), + (None, None) => None, + } + } + [BodyNode::RawExpr(ref text)] => { + // TODO: let rawexprs to be inlined + // let span = syn::spanned::Spanned::span(&text); + // let (start, end) = (span.start(), span.end()); + // if start.line == end.line { + // Some(end.column - start.column) + // } else { + // None + // } + None + } [BodyNode::Element(ref el)] => self .is_short_children(&el.children) .map(|f| f + extract_attr_len(&el.attributes)) diff --git a/packages/autofmt/tests/sink.rs b/packages/autofmt/tests/sink.rs index 025b3f59e..ca1cb5c88 100644 --- a/packages/autofmt/tests/sink.rs +++ b/packages/autofmt/tests/sink.rs @@ -435,7 +435,7 @@ fn formats_complex() { icons::icon_0 {} } span { "{name}" } - children.is_some().then(|| rsx!{ + children.is_some().then(|| rsx! { span { class: "inline-block ml-auto hover:bg-gray-500", onclick: move |evt| {