diff --git a/packages/autofmt/src/component.rs b/packages/autofmt/src/component.rs index 8ff59283a..1eea2eba2 100644 --- a/packages/autofmt/src/component.rs +++ b/packages/autofmt/src/component.rs @@ -67,7 +67,7 @@ impl Writer<'_> { } // multiline handlers bump everything down - if attr_len > 1000 || self.out.indent.inline_attributes() { + if attr_len > 1000 || self.out.indent.split_line_attributes() { opt_level = ShortOptimization::NoOpt; } diff --git a/packages/autofmt/src/element.rs b/packages/autofmt/src/element.rs index e7b9c027a..be3288019 100644 --- a/packages/autofmt/src/element.rs +++ b/packages/autofmt/src/element.rs @@ -102,7 +102,7 @@ impl Writer<'_> { } // multiline handlers bump everything down - if attr_len > 1000 || self.out.indent.inline_attributes() { + if attr_len > 1000 || self.out.indent.split_line_attributes() { opt_level = ShortOptimization::NoOpt; } @@ -254,7 +254,6 @@ impl Writer<'_> { ElementAttr::EventTokens { name, tokens } => { let out = self.retrieve_formatted_expr(tokens).to_string(); - let mut lines = out.split('\n').peekable(); let first = lines.next().unwrap(); @@ -297,6 +296,10 @@ impl Writer<'_> { beginning.is_empty() } + pub fn is_empty_children(&self, children: &[BodyNode]) -> bool { + children.is_empty() + } + // 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 // returns the total line length if it's short diff --git a/packages/autofmt/src/indent.rs b/packages/autofmt/src/indent.rs index 524c626fe..f7d514865 100644 --- a/packages/autofmt/src/indent.rs +++ b/packages/autofmt/src/indent.rs @@ -8,11 +8,11 @@ pub enum IndentType { pub struct IndentOptions { width: usize, indent_string: String, - inline_attributes: bool, + split_line_attributes: bool, } impl IndentOptions { - pub fn new(typ: IndentType, width: usize, inline_attributes: bool) -> Self { + pub fn new(typ: IndentType, width: usize, split_line_attributes: bool) -> Self { assert_ne!(width, 0, "Cannot have an indent width of 0"); Self { width, @@ -20,7 +20,7 @@ impl IndentOptions { IndentType::Tabs => "\t".into(), IndentType::Spaces => " ".repeat(width), }, - inline_attributes, + split_line_attributes, } } @@ -65,8 +65,8 @@ impl IndentOptions { indent } - pub fn inline_attributes(&self) -> bool { - self.inline_attributes + pub fn split_line_attributes(&self) -> bool { + self.split_line_attributes } } diff --git a/packages/autofmt/src/lib.rs b/packages/autofmt/src/lib.rs index 19d870653..e40b873d6 100644 --- a/packages/autofmt/src/lib.rs +++ b/packages/autofmt/src/lib.rs @@ -140,7 +140,9 @@ pub fn write_block_out(body: CallBody) -> Option { } fn write_body(buf: &mut Writer, body: &CallBody) { - if buf.is_short_children(&body.roots).is_some() && !buf.out.indent.inline_attributes() { + let is_short = buf.is_short_children(&body.roots).is_some(); + let is_empty = buf.is_empty_children(&body.roots); + if (is_short && !buf.out.indent.split_line_attributes()) || is_empty { // write all the indents with spaces and commas between for idx in 0..body.roots.len() - 1 { let ident = &body.roots[idx]; diff --git a/packages/cli/src/cli/autoformat.rs b/packages/cli/src/cli/autoformat.rs index 4e3a529f7..23ce85b98 100644 --- a/packages/cli/src/cli/autoformat.rs +++ b/packages/cli/src/cli/autoformat.rs @@ -23,9 +23,9 @@ pub struct Autoformat { #[clap(short, long)] pub file: Option, - /// Inline attributes or not - #[clap(short, long)] - pub inline_attributes: bool, + /// Split attributes in lines or not + #[clap(short, long, default_value = "false")] + pub split_line_attributes: bool, } impl Autoformat { @@ -33,14 +33,14 @@ impl Autoformat { pub async fn autoformat(self) -> Result<()> { // Default to formatting the project if self.raw.is_none() && self.file.is_none() { - if let Err(e) = autoformat_project(self.check, self.inline_attributes).await { + if let Err(e) = autoformat_project(self.check, self.split_line_attributes).await { eprintln!("error formatting project: {}", e); exit(1); } } if let Some(raw) = self.raw { - let indent = indentation_for(".", self.inline_attributes)?; + let indent = indentation_for(".", self.split_line_attributes)?; if let Some(inner) = dioxus_autofmt::fmt_block(&raw, 0, indent) { println!("{}", inner); } else { @@ -55,12 +55,12 @@ impl Autoformat { let file_content; let indent; if file == "-" { - indent = indentation_for(".", self.inline_attributes)?; + indent = indentation_for(".", self.split_line_attributes)?; let mut contents = String::new(); std::io::stdin().read_to_string(&mut contents)?; file_content = Ok(contents); } else { - indent = indentation_for(".", self.inline_attributes)?; + indent = indentation_for(".", self.split_line_attributes)?; file_content = fs::read_to_string(&file); }; @@ -97,7 +97,7 @@ impl Autoformat { /// Runs using Tokio for multithreading, so it should be really really fast /// /// Doesn't do mod-descending, so it will still try to format unreachable files. TODO. -async fn autoformat_project(check: bool, inline_attributes: bool) -> Result<()> { +async fn autoformat_project(check: bool, split_line_attributes: bool) -> Result<()> { let crate_config = crate::CrateConfig::new(None)?; let mut files_to_format = vec![]; @@ -107,7 +107,7 @@ async fn autoformat_project(check: bool, inline_attributes: bool) -> Result<()> return Ok(()); } - let indent = indentation_for(&files_to_format[0], inline_attributes)?; + let indent = indentation_for(&files_to_format[0], split_line_attributes)?; let counts = files_to_format .into_iter() @@ -170,7 +170,7 @@ async fn autoformat_project(check: bool, inline_attributes: bool) -> Result<()> fn indentation_for( file_or_dir: impl AsRef, - inline_attributes: bool, + split_line_attributes: bool, ) -> Result { let out = std::process::Command::new("cargo") .args(["fmt", "--", "--print-config", "current"]) @@ -211,7 +211,7 @@ fn indentation_for( IndentType::Spaces }, tab_spaces, - inline_attributes, + split_line_attributes, )) } @@ -262,7 +262,7 @@ async fn test_auto_fmt() { check: false, raw: Some(test_rsx), file: None, - inline_attributes: false, + split_line_attributes: false, }; fmt.autoformat().await.unwrap();