feat: fix autoformatting around one liners

This commit is contained in:
Jonathan Kelley committed 2022-07-05 17:49:16 -04:00
1 parent 64d669e934
commit fa9ebe17cc
18 files changed
+259 -699

No files matched your search

+7 -2
View File
@@ -1,7 +1,6 @@
use std::{
collections::{HashMap, VecDeque},
fmt::{Result, Write},
rc::Rc,
};
use dioxus_rsx::{BodyNode, ElementAttr, ElementAttrNamed};
@@ -118,6 +117,13 @@ impl Buffer {
pub fn write_body_indented(&mut self, children: &[BodyNode]) -> Result {
self.indent += 1;
self.write_body_no_indent(children)?;
self.indent -= 1;
Ok(())
}
pub fn write_body_no_indent(&mut self, children: &[BodyNode]) -> Result {
for child in children {
// Exprs handle their own indenting/line breaks
if !matches!(child, BodyNode::RawExpr(_)) {
@@ -128,7 +134,6 @@ impl Buffer {
self.write_ident(child)?;
}
self.indent -= 1;
Ok(())
}
+16 -2
View File
@@ -4,6 +4,7 @@ use quote::ToTokens;
use std::fmt::{Result, Write};
use syn::{spanned::Spanned, AngleBracketedGenericArguments};
#[derive(Debug)]
enum ShortOptimization {
// Special because we want to print the closing bracket immediately
Empty,
@@ -69,6 +70,15 @@ impl Buffer {
opt_level = ShortOptimization::NoOpt;
}
// Useful for debugging
// dbg!(
// name.to_token_stream().to_string(),
// &opt_level,
// attr_len,
// is_short_attr_list,
// is_small_children
// );
match opt_level {
ShortOptimization::Empty => {}
ShortOptimization::Oneliner => {
@@ -101,6 +111,11 @@ impl Buffer {
ShortOptimization::NoOpt => {
self.write_component_fields(fields, manual_props, false)?;
if !children.is_empty() && !fields.is_empty() {
write!(self.buf, ",")?;
}
self.write_body_indented(children)?;
self.tabbed_line()?;
}
@@ -196,7 +211,6 @@ impl Buffer {
.map(|field| match &field.content {
ContentField::Formatted(s) => s.value().len() ,
ContentField::OnHandlerRaw(exp) | ContentField::ManExpr(exp) => {
let formatted = prettyplease::unparse_expr(exp);
let len = if formatted.contains('\n') {
10000
@@ -207,7 +221,7 @@ impl Buffer {
len
},
} + 10)
.sum::<usize>() + self.indent * 4;
.sum::<usize>();
match manual_props {
Some(p) => {
+10 -3
View File
@@ -95,7 +95,9 @@ impl Buffer {
}
ShortOptimization::PropsOnTop => {
write!(self.buf, " ")?;
if !attributes.is_empty() || key.is_some() {
write!(self.buf, " ")?;
}
self.write_attributes(attributes, key, true)?;
if !children.is_empty() && !attributes.is_empty() {
@@ -141,13 +143,18 @@ impl Buffer {
}
write!(self.buf, "key: \"{}\"", key.value())?;
if !attributes.is_empty() {
write!(self.buf, ", ")?;
write!(self.buf, ",")?;
if sameline {
write!(self.buf, " ")?;
}
}
}
while let Some(attr) = attr_iter.next() {
self.indent += 1;
self.write_comments(attr.attr.start())?;
if !sameline {
self.write_comments(attr.attr.start())?;
}
self.indent -= 1;
if !sameline {
+6 -1
View File
@@ -98,7 +98,12 @@ pub fn fmt_block(block: &str, indent_level: usize) -> Option<String> {
let body = syn::parse_str::<dioxus_rsx::CallBody>(block).unwrap();
buf.write_body_indented(&body.roots).unwrap();
// Oneliner optimization
if buf.is_short_children(&body.roots).is_some() {
buf.write_ident(&body.roots[0]).unwrap();
} else {
buf.write_body_indented(&body.roots).unwrap();
}
// writing idents leaves the final line ended at the end of the last ident
if buf.buf.contains('\n') {