|
@@ -1,3 +1,4 @@
|
|
|
+use dioxus_autofmt::{IndentOptions, IndentType};
|
|
|
use futures::{stream::FuturesUnordered, StreamExt};
|
|
|
use std::{fs, path::Path, process::exit};
|
|
|
|
|
@@ -35,7 +36,8 @@ impl Autoformat {
|
|
|
}
|
|
|
|
|
|
if let Some(raw) = self.raw {
|
|
|
- if let Some(inner) = dioxus_autofmt::fmt_block(&raw, 0) {
|
|
|
+ let indent = indentation_for(".")?;
|
|
|
+ if let Some(inner) = dioxus_autofmt::fmt_block(&raw, 0, indent) {
|
|
|
println!("{}", inner);
|
|
|
} else {
|
|
|
// exit process with error
|
|
@@ -46,17 +48,21 @@ impl Autoformat {
|
|
|
|
|
|
// Format single file
|
|
|
if let Some(file) = self.file {
|
|
|
- let file_content = if file == "-" {
|
|
|
+ let file_content;
|
|
|
+ let indent;
|
|
|
+ if file == "-" {
|
|
|
+ indent = indentation_for(".")?;
|
|
|
let mut contents = String::new();
|
|
|
std::io::stdin().read_to_string(&mut contents)?;
|
|
|
- Ok(contents)
|
|
|
+ file_content = Ok(contents);
|
|
|
} else {
|
|
|
- fs::read_to_string(&file)
|
|
|
+ indent = indentation_for(".")?;
|
|
|
+ file_content = fs::read_to_string(&file);
|
|
|
};
|
|
|
|
|
|
match file_content {
|
|
|
Ok(s) => {
|
|
|
- let edits = dioxus_autofmt::fmt_file(&s);
|
|
|
+ let edits = dioxus_autofmt::fmt_file(&s, indent);
|
|
|
let out = dioxus_autofmt::apply_formats(&s, edits);
|
|
|
if file == "-" {
|
|
|
print!("{}", out);
|
|
@@ -93,6 +99,12 @@ async fn autoformat_project(check: bool) -> Result<()> {
|
|
|
let mut files_to_format = vec![];
|
|
|
collect_rs_files(&crate_config.crate_dir, &mut files_to_format);
|
|
|
|
|
|
+ if files_to_format.is_empty() {
|
|
|
+ return Ok(());
|
|
|
+ }
|
|
|
+
|
|
|
+ let indent = indentation_for(&files_to_format[0])?;
|
|
|
+
|
|
|
let counts = files_to_format
|
|
|
.into_iter()
|
|
|
.filter(|file| {
|
|
@@ -104,10 +116,11 @@ async fn autoformat_project(check: bool) -> Result<()> {
|
|
|
})
|
|
|
.map(|path| async {
|
|
|
let _path = path.clone();
|
|
|
+ let _indent = indent.clone();
|
|
|
let res = tokio::spawn(async move {
|
|
|
let contents = tokio::fs::read_to_string(&path).await?;
|
|
|
|
|
|
- let edits = dioxus_autofmt::fmt_file(&contents);
|
|
|
+ let edits = dioxus_autofmt::fmt_file(&contents, _indent.clone());
|
|
|
let len = edits.len();
|
|
|
|
|
|
if !edits.is_empty() {
|
|
@@ -151,6 +164,49 @@ async fn autoformat_project(check: bool) -> Result<()> {
|
|
|
Ok(())
|
|
|
}
|
|
|
|
|
|
+fn indentation_for(file_or_dir: impl AsRef<Path>) -> Result<IndentOptions> {
|
|
|
+ let out = std::process::Command::new("cargo")
|
|
|
+ .args(["fmt", "--", "--print-config", "current"])
|
|
|
+ .arg(file_or_dir.as_ref())
|
|
|
+ .stdout(std::process::Stdio::piped())
|
|
|
+ .stderr(std::process::Stdio::inherit())
|
|
|
+ .output()?;
|
|
|
+ if !out.status.success() {
|
|
|
+ return Err(Error::CargoError("cargo fmt failed".into()));
|
|
|
+ }
|
|
|
+
|
|
|
+ let config = String::from_utf8_lossy(&out.stdout);
|
|
|
+
|
|
|
+ let hard_tabs = config
|
|
|
+ .lines()
|
|
|
+ .find(|line| line.starts_with("hard_tabs "))
|
|
|
+ .and_then(|line| line.split_once('='))
|
|
|
+ .map(|(_, value)| value.trim() == "true")
|
|
|
+ .ok_or_else(|| {
|
|
|
+ Error::RuntimeError("Could not find hard_tabs option in rustfmt config".into())
|
|
|
+ })?;
|
|
|
+ let tab_spaces = config
|
|
|
+ .lines()
|
|
|
+ .find(|line| line.starts_with("tab_spaces "))
|
|
|
+ .and_then(|line| line.split_once('='))
|
|
|
+ .map(|(_, value)| value.trim().parse::<usize>())
|
|
|
+ .ok_or_else(|| {
|
|
|
+ Error::RuntimeError("Could not find tab_spaces option in rustfmt config".into())
|
|
|
+ })?
|
|
|
+ .map_err(|_| {
|
|
|
+ Error::RuntimeError("Could not parse tab_spaces option in rustfmt config".into())
|
|
|
+ })?;
|
|
|
+
|
|
|
+ Ok(IndentOptions::new(
|
|
|
+ if hard_tabs {
|
|
|
+ IndentType::Tabs
|
|
|
+ } else {
|
|
|
+ IndentType::Spaces
|
|
|
+ },
|
|
|
+ tab_spaces,
|
|
|
+ ))
|
|
|
+}
|
|
|
+
|
|
|
fn collect_rs_files(folder: &Path, files: &mut Vec<PathBuf>) {
|
|
|
let Ok(folder) = folder.read_dir() else {
|
|
|
return;
|