From 7ed06470e0f0f26817938db9bf2dd5e397134e92 Mon Sep 17 00:00:00 2001 From: Mads Hougesen Date: Thu, 7 Mar 2024 11:26:16 +0100 Subject: [PATCH] feat: implement zigfmt --- src/formatters/mod.rs | 4 +++- src/formatters/zigfmt.rs | 18 ++++++++++++++++++ test/input.md | 9 +++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/formatters/zigfmt.rs diff --git a/src/formatters/mod.rs b/src/formatters/mod.rs index 8c1f6692..ef4c11bc 100644 --- a/src/formatters/mod.rs +++ b/src/formatters/mod.rs @@ -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 { let mut f = NamedTempFile::new()?; @@ -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; diff --git a/src/formatters/zigfmt.rs b/src/formatters/zigfmt.rs new file mode 100644 index 00000000..14010c49 --- /dev/null +++ b/src/formatters/zigfmt.rs @@ -0,0 +1,18 @@ +use super::{execute_command, read_snippet, setup_snippet}; + +pub fn format_using_zigfmt(code: &str) -> std::io::Result> { + 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) +} diff --git a/test/input.md b/test/input.md index 88911b63..549b57b3 100644 --- a/test/input.md +++ b/test/input.md @@ -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 ; + + } + + +```