feat: manual props
This commit is contained in:
1 parent
5bab835843
commit
fa756ba245
6 files changed
+245
-57
No files matched your search
@@ -83,6 +83,10 @@ impl Buffer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if comments.len() == 1 && self.src[comments[0]].is_empty() {
|
||||||
|
comments.pop();
|
||||||
|
}
|
||||||
|
|
||||||
let mut last_was_empty = false;
|
let mut last_was_empty = false;
|
||||||
for comment_line in comments.drain(..).rev() {
|
for comment_line in comments.drain(..).rev() {
|
||||||
let line = &self.src[comment_line];
|
let line = &self.src[comment_line];
|
||||||
|
|||||||
@@ -2,45 +2,151 @@ use crate::Buffer;
|
|||||||
use dioxus_rsx::*;
|
use dioxus_rsx::*;
|
||||||
use quote::ToTokens;
|
use quote::ToTokens;
|
||||||
use std::fmt::{Result, Write};
|
use std::fmt::{Result, Write};
|
||||||
|
use syn::AngleBracketedGenericArguments;
|
||||||
|
|
||||||
|
enum ShortOptimization {
|
||||||
|
// Special because we want to print the closing bracket immediately
|
||||||
|
Empty,
|
||||||
|
|
||||||
|
// Special optimization to put everything on the same line
|
||||||
|
Oneliner,
|
||||||
|
|
||||||
|
// Optimization where children flow but props remain fixed on top
|
||||||
|
PropsOnTop,
|
||||||
|
|
||||||
|
// The noisiest optimization where everything flows
|
||||||
|
NoOpt,
|
||||||
|
}
|
||||||
|
|
||||||
impl Buffer {
|
impl Buffer {
|
||||||
pub fn write_component(
|
pub fn write_component(
|
||||||
&mut self,
|
&mut self,
|
||||||
Component {
|
Component {
|
||||||
name,
|
name,
|
||||||
body,
|
fields,
|
||||||
children,
|
children,
|
||||||
manual_props,
|
manual_props,
|
||||||
prop_gen_args,
|
prop_gen_args,
|
||||||
}: &Component,
|
}: &Component,
|
||||||
|
) -> Result {
|
||||||
|
self.write_component_name(name, prop_gen_args)?;
|
||||||
|
|
||||||
|
// decide if we have any special optimizations
|
||||||
|
// Default with none, opt the cases in one-by-one
|
||||||
|
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_small_children = self.is_short_children(children).is_some();
|
||||||
|
|
||||||
|
// if we have few attributes and a lot of children, place the attrs on top
|
||||||
|
if is_short_attr_list && !is_small_children {
|
||||||
|
opt_level = ShortOptimization::PropsOnTop;
|
||||||
|
}
|
||||||
|
|
||||||
|
// even if the attr is long, it should be put on one line
|
||||||
|
if !is_short_attr_list && (fields.len() <= 1 && manual_props.is_none()) {
|
||||||
|
if children.is_empty() {
|
||||||
|
opt_level = ShortOptimization::Oneliner;
|
||||||
|
} else {
|
||||||
|
opt_level = ShortOptimization::PropsOnTop;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if we have few children and few attributes, make it a one-liner
|
||||||
|
if is_short_attr_list && is_small_children {
|
||||||
|
opt_level = ShortOptimization::Oneliner;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there's nothing at all, empty optimization
|
||||||
|
if fields.is_empty() && children.is_empty() {
|
||||||
|
opt_level = ShortOptimization::Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
match opt_level {
|
||||||
|
ShortOptimization::Empty => {}
|
||||||
|
ShortOptimization::Oneliner => {
|
||||||
|
write!(self.buf, " ")?;
|
||||||
|
|
||||||
|
self.write_component_fields(fields, manual_props, true)?;
|
||||||
|
|
||||||
|
if !children.is_empty() && !fields.is_empty() {
|
||||||
|
write!(self.buf, ", ")?;
|
||||||
|
}
|
||||||
|
|
||||||
|
for child in children {
|
||||||
|
self.write_ident(child)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
write!(self.buf, " ")?;
|
||||||
|
}
|
||||||
|
|
||||||
|
ShortOptimization::PropsOnTop => {
|
||||||
|
write!(self.buf, " ")?;
|
||||||
|
self.write_component_fields(fields, manual_props, true)?;
|
||||||
|
|
||||||
|
if !children.is_empty() && !fields.is_empty() {
|
||||||
|
write!(self.buf, ",")?;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.write_body_indented(children)?;
|
||||||
|
self.tabbed_line()?;
|
||||||
|
}
|
||||||
|
|
||||||
|
ShortOptimization::NoOpt => {
|
||||||
|
self.write_component_fields(fields, manual_props, false)?;
|
||||||
|
self.write_body_indented(children)?;
|
||||||
|
self.tabbed_line()?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
write!(self.buf, "}}")?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_component_name(
|
||||||
|
&mut self,
|
||||||
|
name: &syn::Path,
|
||||||
|
generics: &Option<AngleBracketedGenericArguments>,
|
||||||
) -> Result {
|
) -> Result {
|
||||||
let mut name = name.to_token_stream().to_string();
|
let mut name = name.to_token_stream().to_string();
|
||||||
name.retain(|c| !c.is_whitespace());
|
name.retain(|c| !c.is_whitespace());
|
||||||
self.tab()?;
|
|
||||||
write!(self.buf, "{name}")?;
|
write!(self.buf, "{name}")?;
|
||||||
|
|
||||||
if let Some(generics) = prop_gen_args {
|
if let Some(generics) = generics {
|
||||||
let mut written = generics.to_token_stream().to_string();
|
let mut written = generics.to_token_stream().to_string();
|
||||||
written.retain(|c| !c.is_whitespace());
|
written.retain(|c| !c.is_whitespace());
|
||||||
|
|
||||||
write!(self.buf, "{}", written)?;
|
write!(self.buf, "{}", written)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
write!(self.buf, " {{")?;
|
write!(self.buf, " {{")?;
|
||||||
|
|
||||||
if !body.is_empty() || !children.is_empty() {
|
Ok(())
|
||||||
self.new_line()?;
|
}
|
||||||
}
|
|
||||||
|
fn write_component_fields(
|
||||||
|
&mut self,
|
||||||
|
fields: &[ComponentField],
|
||||||
|
manual_props: &Option<syn::Expr>,
|
||||||
|
sameline: bool,
|
||||||
|
) -> Result {
|
||||||
|
let mut field_iter = fields.iter().peekable();
|
||||||
|
|
||||||
|
while let Some(field) = field_iter.next() {
|
||||||
|
if !sameline {
|
||||||
|
self.indented_tabbed_line()?;
|
||||||
|
}
|
||||||
|
|
||||||
for field in body {
|
|
||||||
self.indented_tab()?;
|
|
||||||
let name = &field.name;
|
let name = &field.name;
|
||||||
match &field.content {
|
match &field.content {
|
||||||
ContentField::ManExpr(exp) => {
|
ContentField::ManExpr(exp) => {
|
||||||
let out = prettyplease::unparse_expr(exp);
|
let out = prettyplease::unparse_expr(exp);
|
||||||
writeln!(self.buf, "{}: {},", name, out)?;
|
write!(self.buf, "{}: {}", name, out)?;
|
||||||
}
|
}
|
||||||
ContentField::Formatted(s) => {
|
ContentField::Formatted(s) => {
|
||||||
writeln!(self.buf, "{}: \"{}\",", name, s.value())?;
|
write!(self.buf, "{}: \"{}\"", name, s.value())?;
|
||||||
}
|
}
|
||||||
ContentField::OnHandlerRaw(exp) => {
|
ContentField::OnHandlerRaw(exp) => {
|
||||||
let out = prettyplease::unparse_expr(exp);
|
let out = prettyplease::unparse_expr(exp);
|
||||||
@@ -52,33 +158,71 @@ impl Buffer {
|
|||||||
self.indented_tab()?;
|
self.indented_tab()?;
|
||||||
write!(self.buf, "{}", line)?;
|
write!(self.buf, "{}", line)?;
|
||||||
}
|
}
|
||||||
writeln!(self.buf, ",")?;
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if field_iter.peek().is_some() || manual_props.is_some() {
|
||||||
|
write!(self.buf, ",")?;
|
||||||
|
|
||||||
|
if sameline {
|
||||||
|
write!(self.buf, " ")?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(exp) = manual_props {
|
if let Some(exp) = manual_props {
|
||||||
self.indented_tab()?;
|
if !sameline {
|
||||||
let out = prettyplease::unparse_expr(exp);
|
self.indented_tabbed_line()?;
|
||||||
let mut lines = out.split('\n').peekable();
|
|
||||||
let first = lines.next().unwrap();
|
|
||||||
write!(self.buf, "..{}", first)?;
|
|
||||||
for line in lines {
|
|
||||||
self.new_line()?;
|
|
||||||
self.indented_tab()?;
|
|
||||||
write!(self.buf, "{}", line)?;
|
|
||||||
}
|
}
|
||||||
self.new_line()?;
|
self.write_manual_props(exp)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
for child in children {
|
Ok(())
|
||||||
self.write_indented_ident(child)?;
|
}
|
||||||
|
fn is_short_fields(&self, fields: &[ComponentField], manual_props: &Option<syn::Expr>) -> bool {
|
||||||
|
let attr_len = fields
|
||||||
|
.iter()
|
||||||
|
.map(|field| match &field.content {
|
||||||
|
ContentField::ManExpr(exp) => exp.to_token_stream().to_string().len(),
|
||||||
|
ContentField::Formatted(s) => s.value().len() ,
|
||||||
|
ContentField::OnHandlerRaw(_) => 100000,
|
||||||
|
} + 10)
|
||||||
|
.sum::<usize>() + self.indent * 4;
|
||||||
|
|
||||||
|
match manual_props {
|
||||||
|
Some(p) => {
|
||||||
|
let content = prettyplease::unparse_expr(p);
|
||||||
|
if content.len() + attr_len > 80 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
let mut lines = content.lines();
|
||||||
|
lines.next().unwrap();
|
||||||
|
|
||||||
|
lines.next().is_none()
|
||||||
|
}
|
||||||
|
None => attr_len < 80,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_manual_props(&mut self, exp: &syn::Expr) -> Result {
|
||||||
|
/*
|
||||||
|
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();
|
||||||
|
|
||||||
|
let first_line = lines.next().unwrap();
|
||||||
|
|
||||||
|
write!(self.buf, "..{first_line}")?;
|
||||||
|
for line in lines {
|
||||||
|
self.indented_tabbed_line()?;
|
||||||
|
write!(self.buf, "{line}")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if !body.is_empty() || !children.is_empty() {
|
|
||||||
self.tab()?;
|
|
||||||
}
|
|
||||||
writeln!(self.buf, "}}")?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -42,7 +42,7 @@ impl Buffer {
|
|||||||
|
|
||||||
// check if we have a lot of attributes
|
// check if we have a lot of attributes
|
||||||
let is_short_attr_list = is_short_attrs(attributes);
|
let is_short_attr_list = is_short_attrs(attributes);
|
||||||
let is_small_children = self.is_short_children(children);
|
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
|
// if we have few attributes and a lot of children, place the attrs on top
|
||||||
if is_short_attr_list && !is_small_children {
|
if is_short_attr_list && !is_small_children {
|
||||||
@@ -201,36 +201,37 @@ impl Buffer {
|
|||||||
|
|
||||||
// check if the children are short enough to be on the same line
|
// check if the children are short enough to be on the same line
|
||||||
// We don't have the notion of current line depth - each line tries to be < 80 total
|
// We don't have the notion of current line depth - each line tries to be < 80 total
|
||||||
fn is_short_children(&self, children: &[BodyNode]) -> bool {
|
// returns the total line length if it's short
|
||||||
|
// returns none if the length exceeds the limit
|
||||||
|
// I think this eventually becomes quadratic :(
|
||||||
|
pub fn is_short_children(&self, children: &[BodyNode]) -> Option<usize> {
|
||||||
if children.is_empty() {
|
if children.is_empty() {
|
||||||
// todo: allow elements with comments but no children
|
// todo: allow elements with comments but no children
|
||||||
// like div { /* comment */ }
|
// like div { /* comment */ }
|
||||||
return true;
|
return Some(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
for child in children {
|
for child in children {
|
||||||
'line: for line in self.src[..child.span().start().line - 1].iter().rev() {
|
'line: for line in self.src[..child.span().start().line - 1].iter().rev() {
|
||||||
if line.trim().starts_with("//") {
|
match (line.trim().starts_with("//"), line.is_empty()) {
|
||||||
return false;
|
(true, _) => return None,
|
||||||
} else if line.is_empty() {
|
(_, true) => continue 'line,
|
||||||
continue;
|
_ => break 'line,
|
||||||
} else {
|
|
||||||
break 'line;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
match children {
|
match children {
|
||||||
[BodyNode::Text(ref text)] => text.value().len() < 80,
|
[BodyNode::Text(ref text)] => Some(text.value().len()),
|
||||||
[BodyNode::Element(ref el)] => {
|
[BodyNode::Element(ref el)] => self
|
||||||
extract_attr_len(&el.attributes) < 80 && self.is_short_children(&el.children)
|
.is_short_children(&el.children)
|
||||||
}
|
.map(|f| f + extract_attr_len(&el.attributes))
|
||||||
_ => false,
|
.and_then(|new_len| if new_len > 80 { None } else { Some(new_len) }),
|
||||||
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_short_attrs(attrs: &[ElementAttrNamed]) -> bool {
|
fn is_short_attrs(attrs: &[ElementAttrNamed]) -> bool {
|
||||||
let total_attr_len = extract_attr_len(attrs);
|
extract_attr_len(attrs) < 80
|
||||||
total_attr_len < 80
|
|
||||||
}
|
}
|
||||||
@@ -19,7 +19,6 @@ impl Buffer {
|
|||||||
let num_spaces_desired = (self.indent * 4) as isize;
|
let num_spaces_desired = (self.indent * 4) as isize;
|
||||||
|
|
||||||
let first = &self.src[start.line - 1];
|
let first = &self.src[start.line - 1];
|
||||||
// let first = lines[start.line - 1];
|
|
||||||
let num_spaces_real = first.chars().take_while(|c| c.is_whitespace()).count() as isize;
|
let num_spaces_real = first.chars().take_while(|c| c.is_whitespace()).count() as isize;
|
||||||
|
|
||||||
let offset = num_spaces_real - num_spaces_desired;
|
let offset = num_spaces_real - num_spaces_desired;
|
||||||
|
|||||||
@@ -1,11 +1,5 @@
|
|||||||
use dioxus_autofmt::*;
|
use dioxus_autofmt::*;
|
||||||
use proc_macro2::TokenStream as TokenStream2;
|
use proc_macro2::TokenStream as TokenStream2;
|
||||||
use syn::{Attribute, Meta};
|
|
||||||
|
|
||||||
fn test_block(wrong: &str, right: &str) {
|
|
||||||
let formatted = fmt_block(wrong).unwrap();
|
|
||||||
assert_eq!(formatted, right);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn print_block(wrong: &str) {
|
fn print_block(wrong: &str) {
|
||||||
let formatted = fmt_block(wrong).unwrap();
|
let formatted = fmt_block(wrong).unwrap();
|
||||||
@@ -165,10 +159,12 @@ fn format_comments() {
|
|||||||
fn formats_component() {
|
fn formats_component() {
|
||||||
let block = r#"
|
let block = r#"
|
||||||
Component {
|
Component {
|
||||||
adsasd: "asd", // this is a comment
|
adsasd: "asd",
|
||||||
|
|
||||||
|
// this is a comment
|
||||||
onclick: move |_| {
|
onclick: move |_| {
|
||||||
let blah = 120;
|
let blah = 120;
|
||||||
let blah = 120;
|
let blah = 122;
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
"#;
|
"#;
|
||||||
@@ -178,6 +174,49 @@ fn formats_component() {
|
|||||||
println!("{formatted}");
|
println!("{formatted}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn formats_component_complex() {
|
||||||
|
let block = r#"
|
||||||
|
div {
|
||||||
|
Component {
|
||||||
|
adsasd: "asd",
|
||||||
|
onclick: move |_| {
|
||||||
|
let a = a;
|
||||||
|
}
|
||||||
|
div {
|
||||||
|
"thing"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Component {
|
||||||
|
asdasd: "asdasd",
|
||||||
|
asdasd: "asdasdasdasdasdasdasdasdasdasd",
|
||||||
|
..Props {
|
||||||
|
a: 10,
|
||||||
|
b: 20
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Component {
|
||||||
|
//
|
||||||
|
asdasd: "asdasd",
|
||||||
|
..Props {
|
||||||
|
a: 10,
|
||||||
|
b: 20,
|
||||||
|
c: {
|
||||||
|
fn main() {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"content"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#;
|
||||||
|
|
||||||
|
let formatted = fmt_block(block).unwrap();
|
||||||
|
|
||||||
|
println!("{formatted}");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn formats_element() {
|
fn formats_element() {
|
||||||
let block = r#"
|
let block = r#"
|
||||||
@@ -239,6 +278,7 @@ fn formats_element_nested() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn formats_element_props_on_top() {
|
fn formats_element_props_on_top() {
|
||||||
|
// Gets compressed because there's no real comments
|
||||||
let block = r#"
|
let block = r#"
|
||||||
h3 {
|
h3 {
|
||||||
class: "mb-2 text-xl font-bold mb-2 text-xl font-bold mb-2 text-xl font-bold mb-2 text-xl font-bold mb-2 text-xl font-bold",
|
class: "mb-2 text-xl font-bold mb-2 text-xl font-bold mb-2 text-xl font-bold mb-2 text-xl font-bold mb-2 text-xl font-bold",
|
||||||
@@ -580,7 +620,7 @@ fn NavItem<'a>(cx: Scope, to: &'static str, children: Element<'a>, icon: Shape)
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn empty_blocks() {
|
fn empty_blocks() {
|
||||||
let mut src = r###"
|
let src = r###"
|
||||||
pub fn Alert(cx: Scope) -> Element {
|
pub fn Alert(cx: Scope) -> Element {
|
||||||
cx.render(rsx! {
|
cx.render(rsx! {
|
||||||
div { }
|
div { }
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ use syn::{
|
|||||||
pub struct Component {
|
pub struct Component {
|
||||||
pub name: syn::Path,
|
pub name: syn::Path,
|
||||||
pub prop_gen_args: Option<AngleBracketedGenericArguments>,
|
pub prop_gen_args: Option<AngleBracketedGenericArguments>,
|
||||||
pub body: Vec<ComponentField>,
|
pub fields: Vec<ComponentField>,
|
||||||
pub children: Vec<BodyNode>,
|
pub children: Vec<BodyNode>,
|
||||||
pub manual_props: Option<Expr>,
|
pub manual_props: Option<Expr>,
|
||||||
}
|
}
|
||||||
@@ -105,7 +105,7 @@ impl Parse for Component {
|
|||||||
Ok(Self {
|
Ok(Self {
|
||||||
name,
|
name,
|
||||||
prop_gen_args,
|
prop_gen_args,
|
||||||
body,
|
fields: body,
|
||||||
children,
|
children,
|
||||||
manual_props,
|
manual_props,
|
||||||
})
|
})
|
||||||
@@ -124,7 +124,7 @@ impl ToTokens for Component {
|
|||||||
let mut toks = quote! {
|
let mut toks = quote! {
|
||||||
let mut __manual_props = #manual_props;
|
let mut __manual_props = #manual_props;
|
||||||
};
|
};
|
||||||
for field in &self.body {
|
for field in &self.fields {
|
||||||
if field.name == "key" {
|
if field.name == "key" {
|
||||||
has_key = Some(field);
|
has_key = Some(field);
|
||||||
} else {
|
} else {
|
||||||
@@ -147,7 +147,7 @@ impl ToTokens for Component {
|
|||||||
Some(gen_args) => quote! { fc_to_builder #gen_args(#name) },
|
Some(gen_args) => quote! { fc_to_builder #gen_args(#name) },
|
||||||
None => quote! { fc_to_builder(#name) },
|
None => quote! { fc_to_builder(#name) },
|
||||||
};
|
};
|
||||||
for field in &self.body {
|
for field in &self.fields {
|
||||||
match field.name.to_string().as_str() {
|
match field.name.to_string().as_str() {
|
||||||
"key" => {
|
"key" => {
|
||||||
//
|
//
|
||||||
|
|||||||
Reference in new issue
Block a user