add rustfmt support
This commit is contained in:
1 parent
a3d6e9d07a
commit
2eec2bc673
2 files changed
+50
-3
No files matched your search
@@ -10,6 +10,10 @@ use super::*;
|
|||||||
/// Format some rsx
|
/// Format some rsx
|
||||||
#[derive(Clone, Debug, Parser)]
|
#[derive(Clone, Debug, Parser)]
|
||||||
pub struct Autoformat {
|
pub struct Autoformat {
|
||||||
|
/// Run rustfmt before the dioxus formatter`
|
||||||
|
#[clap(long)]
|
||||||
|
pub rustfmt: bool,
|
||||||
|
|
||||||
/// Run in 'check' mode. Exits with 0 if input is formatted correctly. Exits
|
/// Run in 'check' mode. Exits with 0 if input is formatted correctly. Exits
|
||||||
/// with 1 and prints a diff if formatting is required.
|
/// with 1 and prints a diff if formatting is required.
|
||||||
#[clap(short, long)]
|
#[clap(short, long)]
|
||||||
@@ -36,6 +40,7 @@ impl Autoformat {
|
|||||||
raw,
|
raw,
|
||||||
file,
|
file,
|
||||||
split_line_attributes,
|
split_line_attributes,
|
||||||
|
rustfmt,
|
||||||
..
|
..
|
||||||
} = self;
|
} = self;
|
||||||
|
|
||||||
@@ -60,14 +65,47 @@ impl Autoformat {
|
|||||||
|
|
||||||
// Format single file
|
// Format single file
|
||||||
if let Some(file) = file {
|
if let Some(file) = file {
|
||||||
refactor_file(file, split_line_attributes)?;
|
refactor_file(file, split_line_attributes, rustfmt)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn refactor_file(file: String, split_line_attributes: bool) -> Result<(), Error> {
|
/// Call rustfmt to format code, i32 as exitcode
|
||||||
|
fn rustfmt(input: String) -> Result<String> {
|
||||||
|
use std::thread;
|
||||||
|
|
||||||
|
let mut child = Command::new("rustfmt")
|
||||||
|
.stdin(Stdio::piped())
|
||||||
|
.stdout(Stdio::piped())
|
||||||
|
.spawn()
|
||||||
|
.map_err(|e| Error::IO(e))?;
|
||||||
|
|
||||||
|
let mut stdin = child.stdin.take().unwrap();
|
||||||
|
let pass_in = input.to_string();
|
||||||
|
let handle = thread::spawn(move || -> Result<()> {
|
||||||
|
stdin
|
||||||
|
.write_all(pass_in.as_bytes())
|
||||||
|
.map_err(|e| Error::IO(e))?;
|
||||||
|
Ok(())
|
||||||
|
});
|
||||||
|
handle.join().unwrap()?;
|
||||||
|
|
||||||
|
let output = child.wait_with_output().map_err(|e| Error::IO(e))?;
|
||||||
|
|
||||||
|
if let Some(exit_code) = output.status.code() {
|
||||||
|
if exit_code != 0 {
|
||||||
|
return Err(Error::ExitCodeNotZero(exit_code));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let output = String::from_utf8(output.stdout).map_err(|e| Error::Other(e.into()))?;
|
||||||
|
|
||||||
|
Ok(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn refactor_file(file: String, split_line_attributes: bool, do_rustfmt: bool) -> Result<(), Error> {
|
||||||
let indent = indentation_for(".", split_line_attributes)?;
|
let indent = indentation_for(".", split_line_attributes)?;
|
||||||
let file_content = if file == "-" {
|
let file_content = if file == "-" {
|
||||||
let mut contents = String::new();
|
let mut contents = String::new();
|
||||||
@@ -76,10 +114,15 @@ fn refactor_file(file: String, split_line_attributes: bool) -> Result<(), Error>
|
|||||||
} else {
|
} else {
|
||||||
fs::read_to_string(&file)
|
fs::read_to_string(&file)
|
||||||
};
|
};
|
||||||
let Ok(s) = file_content else {
|
let Ok(mut s) = file_content else {
|
||||||
eprintln!("failed to open file: {}", file_content.unwrap_err());
|
eprintln!("failed to open file: {}", file_content.unwrap_err());
|
||||||
exit(1);
|
exit(1);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if do_rustfmt {
|
||||||
|
s = rustfmt(s)?;
|
||||||
|
}
|
||||||
|
|
||||||
let edits = dioxus_autofmt::fmt_file(&s, indent);
|
let edits = dioxus_autofmt::fmt_file(&s, indent);
|
||||||
let out = dioxus_autofmt::apply_formats(&s, edits);
|
let out = dioxus_autofmt::apply_formats(&s, edits);
|
||||||
|
|
||||||
@@ -280,6 +323,7 @@ async fn test_auto_fmt() {
|
|||||||
.to_string();
|
.to_string();
|
||||||
|
|
||||||
let fmt = Autoformat {
|
let fmt = Autoformat {
|
||||||
|
rustfmt: false,
|
||||||
check: false,
|
check: false,
|
||||||
raw: Some(test_rsx),
|
raw: Some(test_rsx),
|
||||||
file: None,
|
file: None,
|
||||||
|
|||||||
@@ -46,6 +46,9 @@ pub enum Error {
|
|||||||
|
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
Other(#[from] anyhow::Error),
|
Other(#[from] anyhow::Error),
|
||||||
|
|
||||||
|
#[error("Child process exited with {0}")]
|
||||||
|
ExitCodeNotZero(i32),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&str> for Error {
|
impl From<&str> for Error {
|
||||||
|
|||||||
Reference in new issue
Block a user