feat: more cleanup

This commit is contained in:
Jonathan Kelley 2022-06-28 17:29:10 -04:00
parent fa756ba245
commit bd936d3af2
3 changed files with 36 additions and 8 deletions

View file

@ -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<syn::Expr>) -> bool {
pub fn is_short_fields(
&self,
fields: &[ComponentField],
manual_props: &Option<syn::Expr>,
) -> Option<usize> {
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();

View file

@ -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))

View file

@ -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| {