Skip to content

Commit

Permalink
Merge pull request #1701 from marc2332/feat/inline-attributes-fmt
Browse files Browse the repository at this point in the history
feat: `--split-line-attributes` parameter for CLI `fmt`
  • Loading branch information
jkelleyrtp authored Jan 9, 2024
2 parents dd4dc93 + 2876a7d commit 18a56bf
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 30 deletions.
6 changes: 3 additions & 3 deletions packages/autofmt/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl Writer<'_> {
}

// multiline handlers bump everything down
if attr_len > 1000 {
if attr_len > 1000 || self.out.indent.split_line_attributes() {
opt_level = ShortOptimization::NoOpt;
}

Expand Down Expand Up @@ -158,7 +158,7 @@ impl Writer<'_> {

while let Some(field) = field_iter.next() {
if !sameline {
self.out.indented_tabbed_line()?;
self.out.indented_tabbed_line().unwrap();
}

let name = &field.name;
Expand Down Expand Up @@ -206,7 +206,7 @@ impl Writer<'_> {

if let Some(exp) = manual_props {
if !sameline {
self.out.indented_tabbed_line()?;
self.out.indented_tabbed_line().unwrap();
}
self.write_manual_props(exp)?;
}
Expand Down
7 changes: 5 additions & 2 deletions packages/autofmt/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl Writer<'_> {
}

// multiline handlers bump everything down
if attr_len > 1000 {
if attr_len > 1000 || self.out.indent.split_line_attributes() {
opt_level = ShortOptimization::NoOpt;
}

Expand Down Expand Up @@ -262,7 +262,6 @@ impl Writer<'_> {
}
ElementAttrValue::EventTokens(tokens) => {
let out = self.retrieve_formatted_expr(tokens).to_string();

let mut lines = out.split('\n').peekable();
let first = lines.next().unwrap();

Expand Down Expand Up @@ -327,6 +326,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
Expand Down
24 changes: 15 additions & 9 deletions packages/autofmt/src/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ pub enum IndentType {
pub struct IndentOptions {
width: usize,
indent_string: String,
split_line_attributes: bool,
}

impl IndentOptions {
pub fn new(typ: IndentType, width: usize) -> 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,
indent_string: match typ {
IndentType::Tabs => "\t".into(),
IndentType::Spaces => " ".repeat(width),
},
split_line_attributes,
}
}

Expand Down Expand Up @@ -62,11 +64,15 @@ impl IndentOptions {
}
indent
}

pub fn split_line_attributes(&self) -> bool {
self.split_line_attributes
}
}

impl Default for IndentOptions {
fn default() -> Self {
Self::new(IndentType::Spaces, 4)
Self::new(IndentType::Spaces, 4, false)
}
}

Expand All @@ -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
);
}
Expand Down
4 changes: 3 additions & 1 deletion packages/autofmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ pub fn write_block_out(body: CallBody) -> Option<String> {
}

fn write_body(buf: &mut Writer, body: &CallBody) {
if buf.is_short_children(&body.roots).is_some() {
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];
Expand Down
12 changes: 6 additions & 6 deletions packages/autofmt/tests/wrong.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
33 changes: 24 additions & 9 deletions packages/cli/src/cli/autoformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,33 @@ pub struct Autoformat {
/// Input file
#[clap(short, long)]
pub file: Option<String>,

/// Split attributes in lines or not
#[clap(short, long, default_value = "false")]
pub split_line_attributes: bool,
}

impl Autoformat {
// Todo: autoformat the entire crate
pub async fn autoformat(self) -> Result<()> {
let Autoformat { check, raw, file } = self;
let Autoformat {
check,
raw,
file,
split_line_attributes,
..
} = self;

// Default to formatting the project
if raw.is_none() && file.is_none() {
if let Err(e) = autoformat_project(check).await {
if let Err(e) = autoformat_project(check, split_line_attributes).await {
eprintln!("error formatting project: {}", e);
exit(1);
}
}

if let Some(raw) = raw {
let indent = indentation_for(".")?;
let indent = indentation_for(".", self.split_line_attributes)?;
if let Some(inner) = dioxus_autofmt::fmt_block(&raw, 0, indent) {
println!("{}", inner);
} else {
Expand All @@ -50,15 +60,15 @@ impl Autoformat {

// Format single file
if let Some(file) = file {
refactor_file(file)?;
refactor_file(file, split_line_attributes)?;
}

Ok(())
}
}

fn refactor_file(file: String) -> Result<(), Error> {
let indent = indentation_for(".")?;
fn refactor_file(file: String, split_line_attributes: bool) -> Result<(), Error> {
let indent = indentation_for(".", split_line_attributes)?;
let file_content = if file == "-" {
let mut contents = String::new();
std::io::stdin().read_to_string(&mut contents)?;
Expand Down Expand Up @@ -138,7 +148,7 @@ async fn format_file(
/// 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, split_line_attributes: bool) -> Result<()> {
let crate_config = dioxus_cli_config::CrateConfig::new(None)?;

let files_to_format = get_project_files(&crate_config);
Expand All @@ -147,7 +157,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], split_line_attributes)?;

let counts = files_to_format
.into_iter()
Expand Down Expand Up @@ -181,7 +191,10 @@ async fn autoformat_project(check: bool) -> Result<()> {
Ok(())
}

fn indentation_for(file_or_dir: impl AsRef<Path>) -> Result<IndentOptions> {
fn indentation_for(
file_or_dir: impl AsRef<Path>,
split_line_attributes: bool,
) -> Result<IndentOptions> {
let out = std::process::Command::new("cargo")
.args(["fmt", "--", "--print-config", "current"])
.arg(file_or_dir.as_ref())
Expand Down Expand Up @@ -221,6 +234,7 @@ fn indentation_for(file_or_dir: impl AsRef<Path>) -> Result<IndentOptions> {
IndentType::Spaces
},
tab_spaces,
split_line_attributes,
))
}

Expand Down Expand Up @@ -269,6 +283,7 @@ async fn test_auto_fmt() {
check: false,
raw: Some(test_rsx),
file: None,
split_line_attributes: false,
};

fmt.autoformat().await.unwrap();
Expand Down
3 changes: 3 additions & 0 deletions packages/extension/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub fn format_rsx(raw: String, use_tabs: bool, indent_size: usize) -> String {
IndentType::Spaces
},
indent_size,
false,
),
);
block.unwrap()
Expand All @@ -32,6 +33,7 @@ pub fn format_selection(raw: String, use_tabs: bool, indent_size: usize) -> Stri
IndentType::Spaces
},
indent_size,
false,
),
);
block.unwrap()
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 18a56bf

Please sign in to comment.