Skip to content

Commit

Permalink
feat: implement zigfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen committed Mar 7, 2024
1 parent fc64c11 commit 7ed0647
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ use tempfile::NamedTempFile;

use self::{
nimpretty::format_using_nimpretty, ruff::format_using_ruff, rustfmt::format_using_rustfmt,
stylua::format_using_stylua,
stylua::format_using_stylua, zigfmt::format_using_zigfmt,
};

mod nimpretty;
mod ruff;
mod rustfmt;
mod stylua;
mod zigfmt;

pub fn setup_snippet(code: &str) -> std::io::Result<NamedTempFile> {
let mut f = NamedTempFile::new()?;
Expand All @@ -35,6 +36,7 @@ pub fn format_snippet(language: &str, code: String) -> String {
"lua" => format_using_stylua(&code),
"python" => format_using_ruff(&code),
"nim" => format_using_nimpretty(&code),
"zig" => format_using_zigfmt(&code),
_ => Ok(None),
} {
return formatted_code;
Expand Down
18 changes: 18 additions & 0 deletions src/formatters/zigfmt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use super::{execute_command, read_snippet, setup_snippet};

pub fn format_using_zigfmt(code: &str) -> std::io::Result<Option<String>> {
let file = setup_snippet(code)?;
let file_path = file.path();

let mut cmd = std::process::Command::new("zig");

cmd.arg("fmt");

cmd.arg(file_path);

if execute_command(&mut cmd)? {
return read_snippet(file_path).map(Some);
}

Ok(None)
}
9 changes: 9 additions & 0 deletions test/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,12 @@ proc add( a :int , b:int ) : int =
return a + b
```

```zig
fn add (a : i32 , b : i32 ) i32 {
return a + b ;
}
```

0 comments on commit 7ed0647

Please sign in to comment.