Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement zigfmt #5

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ;

}


```