From 52cb5ff951ed80d8b6d801ef49268233176ab81d Mon Sep 17 00:00:00 2001 From: marc2332 Date: Sat, 9 Dec 2023 11:16:26 +0100 Subject: [PATCH 1/5] feat: `--inline-attributes` option for autofmt --- packages/autofmt/src/component.rs | 16 +++++++++++++--- packages/autofmt/src/element.rs | 2 +- packages/autofmt/src/indent.rs | 24 +++++++++++++++--------- packages/cli/src/cli/autoformat.rs | 23 ++++++++++++++++------- packages/extension/src/lib.rs | 3 +++ 5 files changed, 48 insertions(+), 20 deletions(-) diff --git a/packages/autofmt/src/component.rs b/packages/autofmt/src/component.rs index 8cf36811d..6257486a8 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 { + if attr_len > 1000 || self.out.indent.inline_attributes() { opt_level = ShortOptimization::NoOpt; } @@ -154,11 +154,17 @@ impl Writer<'_> { manual_props: &Option, sameline: bool, ) -> Result { + println!("fields {fields:?}"); + println!("manual_props {manual_props:?}"); + let mut field_iter = fields.iter().peekable(); while let Some(field) = field_iter.next() { + println!("{:?}", field); if !sameline { - self.out.indented_tabbed_line()?; + self.out.indented_tabbed_line().unwrap(); + } else { + println!("not line????"); } let name = &field.name; @@ -200,13 +206,17 @@ impl Writer<'_> { if sameline { write!(self.out, " ")?; + println!("not line-----"); } } } if let Some(exp) = manual_props { + println!("{:?}", exp); if !sameline { - self.out.indented_tabbed_line()?; + self.out.indented_tabbed_line().unwrap(); + } else { + println!("not line!!!!!!"); } self.write_manual_props(exp)?; } diff --git a/packages/autofmt/src/element.rs b/packages/autofmt/src/element.rs index 27e7b31f1..e7b9c027a 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 { + if attr_len > 1000 || self.out.indent.inline_attributes() { opt_level = ShortOptimization::NoOpt; } diff --git a/packages/autofmt/src/indent.rs b/packages/autofmt/src/indent.rs index 2cce7cf1e..524c626fe 100644 --- a/packages/autofmt/src/indent.rs +++ b/packages/autofmt/src/indent.rs @@ -8,10 +8,11 @@ pub enum IndentType { pub struct IndentOptions { width: usize, indent_string: String, + inline_attributes: bool, } impl IndentOptions { - pub fn new(typ: IndentType, width: usize) -> Self { + pub fn new(typ: IndentType, width: usize, inline_attributes: bool) -> Self { assert_ne!(width, 0, "Cannot have an indent width of 0"); Self { width, @@ -19,6 +20,7 @@ impl IndentOptions { IndentType::Tabs => "\t".into(), IndentType::Spaces => " ".repeat(width), }, + inline_attributes, } } @@ -62,11 +64,15 @@ impl IndentOptions { } indent } + + pub fn inline_attributes(&self) -> bool { + self.inline_attributes + } } impl Default for IndentOptions { fn default() -> Self { - Self::new(IndentType::Spaces, 4) + Self::new(IndentType::Spaces, 4, false) } } @@ -77,31 +83,31 @@ mod tests { #[test] fn count_indents() { assert_eq!( - IndentOptions::new(IndentType::Spaces, 4).count_indents("no indentation here!"), + IndentOptions::new(IndentType::Spaces, 4, false).count_indents("no indentation here!"), 0 ); assert_eq!( - IndentOptions::new(IndentType::Spaces, 4).count_indents(" v += 2"), + IndentOptions::new(IndentType::Spaces, 4, false).count_indents(" v += 2"), 1 ); assert_eq!( - IndentOptions::new(IndentType::Spaces, 4).count_indents(" v += 2"), + IndentOptions::new(IndentType::Spaces, 4, false).count_indents(" v += 2"), 2 ); assert_eq!( - IndentOptions::new(IndentType::Spaces, 4).count_indents(" v += 2"), + IndentOptions::new(IndentType::Spaces, 4, false).count_indents(" v += 2"), 2 ); assert_eq!( - IndentOptions::new(IndentType::Spaces, 4).count_indents("\t\tv += 2"), + IndentOptions::new(IndentType::Spaces, 4, false).count_indents("\t\tv += 2"), 2 ); assert_eq!( - IndentOptions::new(IndentType::Spaces, 4).count_indents("\t\t v += 2"), + IndentOptions::new(IndentType::Spaces, 4, false).count_indents("\t\t v += 2"), 2 ); assert_eq!( - IndentOptions::new(IndentType::Spaces, 2).count_indents(" v += 2"), + IndentOptions::new(IndentType::Spaces, 2, false).count_indents(" v += 2"), 2 ); } diff --git a/packages/cli/src/cli/autoformat.rs b/packages/cli/src/cli/autoformat.rs index 6c1ce04a1..4e3a529f7 100644 --- a/packages/cli/src/cli/autoformat.rs +++ b/packages/cli/src/cli/autoformat.rs @@ -22,6 +22,10 @@ pub struct Autoformat { /// Input file #[clap(short, long)] pub file: Option, + + /// Inline attributes or not + #[clap(short, long)] + pub inline_attributes: bool, } impl Autoformat { @@ -29,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).await { + if let Err(e) = autoformat_project(self.check, self.inline_attributes).await { eprintln!("error formatting project: {}", e); exit(1); } } if let Some(raw) = self.raw { - let indent = indentation_for(".")?; + let indent = indentation_for(".", self.inline_attributes)?; if let Some(inner) = dioxus_autofmt::fmt_block(&raw, 0, indent) { println!("{}", inner); } else { @@ -51,12 +55,12 @@ impl Autoformat { let file_content; let indent; if file == "-" { - indent = indentation_for(".")?; + indent = indentation_for(".", self.inline_attributes)?; let mut contents = String::new(); std::io::stdin().read_to_string(&mut contents)?; file_content = Ok(contents); } else { - indent = indentation_for(".")?; + indent = indentation_for(".", self.inline_attributes)?; file_content = fs::read_to_string(&file); }; @@ -93,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) -> Result<()> { +async fn autoformat_project(check: bool, inline_attributes: bool) -> Result<()> { let crate_config = crate::CrateConfig::new(None)?; let mut files_to_format = vec![]; @@ -103,7 +107,7 @@ async fn autoformat_project(check: bool) -> Result<()> { return Ok(()); } - let indent = indentation_for(&files_to_format[0])?; + let indent = indentation_for(&files_to_format[0], inline_attributes)?; let counts = files_to_format .into_iter() @@ -164,7 +168,10 @@ async fn autoformat_project(check: bool) -> Result<()> { Ok(()) } -fn indentation_for(file_or_dir: impl AsRef) -> Result { +fn indentation_for( + file_or_dir: impl AsRef, + inline_attributes: bool, +) -> Result { let out = std::process::Command::new("cargo") .args(["fmt", "--", "--print-config", "current"]) .arg(file_or_dir.as_ref()) @@ -204,6 +211,7 @@ fn indentation_for(file_or_dir: impl AsRef) -> Result { IndentType::Spaces }, tab_spaces, + inline_attributes, )) } @@ -254,6 +262,7 @@ async fn test_auto_fmt() { check: false, raw: Some(test_rsx), file: None, + inline_attributes: false, }; fmt.autoformat().await.unwrap(); diff --git a/packages/extension/src/lib.rs b/packages/extension/src/lib.rs index fb9cca8a9..6581482ca 100644 --- a/packages/extension/src/lib.rs +++ b/packages/extension/src/lib.rs @@ -15,6 +15,7 @@ pub fn format_rsx(raw: String, use_tabs: bool, indent_size: usize) -> String { IndentType::Spaces }, indent_size, + false, ), ); block.unwrap() @@ -32,6 +33,7 @@ pub fn format_selection(raw: String, use_tabs: bool, indent_size: usize) -> Stri IndentType::Spaces }, indent_size, + false, ), ); block.unwrap() @@ -67,6 +69,7 @@ pub fn format_file(contents: String, use_tabs: bool, indent_size: usize) -> Form IndentType::Spaces }, indent_size, + false, ), ); let out = dioxus_autofmt::apply_formats(&contents, _edits.clone()); From 27bfabd42580f7b934e6299102ee696506c4c0ce Mon Sep 17 00:00:00 2001 From: marc2332 Date: Sat, 9 Dec 2023 11:19:18 +0100 Subject: [PATCH 2/5] clean up --- packages/autofmt/src/component.rs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/packages/autofmt/src/component.rs b/packages/autofmt/src/component.rs index 6257486a8..8ff59283a 100644 --- a/packages/autofmt/src/component.rs +++ b/packages/autofmt/src/component.rs @@ -154,17 +154,11 @@ impl Writer<'_> { manual_props: &Option, sameline: bool, ) -> Result { - println!("fields {fields:?}"); - println!("manual_props {manual_props:?}"); - let mut field_iter = fields.iter().peekable(); while let Some(field) = field_iter.next() { - println!("{:?}", field); if !sameline { self.out.indented_tabbed_line().unwrap(); - } else { - println!("not line????"); } let name = &field.name; @@ -206,17 +200,13 @@ impl Writer<'_> { if sameline { write!(self.out, " ")?; - println!("not line-----"); } } } if let Some(exp) = manual_props { - println!("{:?}", exp); if !sameline { self.out.indented_tabbed_line().unwrap(); - } else { - println!("not line!!!!!!"); } self.write_manual_props(exp)?; } From 095805610002fabc88b44666d0d6006662811ff3 Mon Sep 17 00:00:00 2001 From: marc2332 Date: Sat, 9 Dec 2023 11:33:14 +0100 Subject: [PATCH 3/5] fix --- packages/autofmt/tests/wrong.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/autofmt/tests/wrong.rs b/packages/autofmt/tests/wrong.rs index 06a0f00d1..8f2d08452 100644 --- a/packages/autofmt/tests/wrong.rs +++ b/packages/autofmt/tests/wrong.rs @@ -18,11 +18,11 @@ macro_rules! twoway { }; } -twoway!("comments-4sp" => comments_4sp (IndentOptions::new(IndentType::Spaces, 4))); -twoway!("comments-tab" => comments_tab (IndentOptions::new(IndentType::Tabs, 4))); +twoway!("comments-4sp" => comments_4sp (IndentOptions::new(IndentType::Spaces, 4, false))); +twoway!("comments-tab" => comments_tab (IndentOptions::new(IndentType::Tabs, 4, false))); -twoway!("multi-4sp" => multi_4sp (IndentOptions::new(IndentType::Spaces, 4))); -twoway!("multi-tab" => multi_tab (IndentOptions::new(IndentType::Tabs, 4))); +twoway!("multi-4sp" => multi_4sp (IndentOptions::new(IndentType::Spaces, 4, false))); +twoway!("multi-tab" => multi_tab (IndentOptions::new(IndentType::Tabs, 4, false))); -twoway!("multiexpr-4sp" => multiexpr_4sp (IndentOptions::new(IndentType::Spaces, 4))); -twoway!("multiexpr-tab" => multiexpr_tab (IndentOptions::new(IndentType::Tabs, 4))); +twoway!("multiexpr-4sp" => multiexpr_4sp (IndentOptions::new(IndentType::Spaces, 4, false))); +twoway!("multiexpr-tab" => multiexpr_tab (IndentOptions::new(IndentType::Tabs, 4, false))); From c10cefffb6b9a7bfd0cac026fedc5bc60b3e7bed Mon Sep 17 00:00:00 2001 From: marc2332 Date: Sat, 9 Dec 2023 11:46:30 +0100 Subject: [PATCH 4/5] fix: Disabling short-children inlining when inline attributes is disabled --- packages/autofmt/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/autofmt/src/lib.rs b/packages/autofmt/src/lib.rs index 0c3fe6bb8..19d870653 100644 --- a/packages/autofmt/src/lib.rs +++ b/packages/autofmt/src/lib.rs @@ -140,7 +140,7 @@ 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() { + if buf.is_short_children(&body.roots).is_some() && !buf.out.indent.inline_attributes() { // write all the indents with spaces and commas between for idx in 0..body.roots.len() - 1 { let ident = &body.roots[idx]; From ce5d7bd11badc9d54b5d5f0cd5f399d40cc12fc6 Mon Sep 17 00:00:00 2001 From: marc2332 Date: Sat, 9 Dec 2023 16:51:41 +0100 Subject: [PATCH 5/5] rename to split-line-attributes --- packages/autofmt/src/component.rs | 2 +- packages/autofmt/src/element.rs | 7 +++++-- packages/autofmt/src/indent.rs | 10 +++++----- packages/autofmt/src/lib.rs | 4 +++- packages/cli/src/cli/autoformat.rs | 24 ++++++++++++------------ 5 files changed, 26 insertions(+), 21 deletions(-) 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();