feat: --inline-attributes option for autofmt

This commit is contained in:
marc2332 committed 2023-12-09 11:16:26 +01:00
1 parent 544ca55596
commit 52cb5ff951
5 files changed
+48 -20

No files matched your search

+16 -7
View File
@@ -22,6 +22,10 @@ pub struct Autoformat {
/// Input file
#[clap(short, long)]
pub file: Option<String>,
/// 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<Path>) -> Result<IndentOptions> {
fn indentation_for(
file_or_dir: impl AsRef<Path>,
inline_attributes: bool,
) -> Result<IndentOptions> {
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<Path>) -> Result<IndentOptions> {
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();