diff --git a/src/formatters/luaformatter.rs b/src/formatters/luaformatter.rs new file mode 100644 index 00000000..10452e18 --- /dev/null +++ b/src/formatters/luaformatter.rs @@ -0,0 +1,52 @@ +use super::execute_command; + +#[inline] +pub fn format_using_luaformatter( + snippet_path: &std::path::Path, +) -> std::io::Result<(bool, Option)> { + let mut cmd = std::process::Command::new("lua-format"); + + cmd.arg("-i").arg(snippet_path); + + execute_command(&mut cmd, snippet_path) +} + +#[cfg(test)] +mod test_luaformatter { + use crate::{ + formatters::{luaformatter::format_using_luaformatter, setup_snippet}, + languages::Language, + }; + + #[test_with::executable(lua-format)] + #[test] + fn it_should_format_lua() { + let input = " + + local function add ( a , b +) +local c=a+b +return c + + +end + "; + + let expected_output = "local function add(a, b) + local c = a + b + return c + +end +"; + + let snippet = + setup_snippet(input, Language::Lua.to_file_ext()).expect("it to create a snippet file"); + + let output = format_using_luaformatter(snippet.path()) + .expect("it to be successful") + .1 + .expect("it to be some"); + + assert_eq!(expected_output, output); + } +} diff --git a/src/formatters/mod.rs b/src/formatters/mod.rs index f52570fc..9589c4e5 100644 --- a/src/formatters/mod.rs +++ b/src/formatters/mod.rs @@ -27,6 +27,7 @@ pub mod hindent; pub mod isort; pub mod just_fmt; pub mod ktlint; +pub mod luaformatter; pub mod mix_format; pub mod nimpretty; pub mod npm_groovy_lint; diff --git a/src/languages/lua.rs b/src/languages/lua.rs index a9218026..ed4c6d4a 100644 --- a/src/languages/lua.rs +++ b/src/languages/lua.rs @@ -1,6 +1,8 @@ use schemars::JsonSchema; -use crate::formatters::{stylua::format_using_stylua, MdsfFormatter}; +use crate::formatters::{ + luaformatter::format_using_luaformatter, stylua::format_using_stylua, MdsfFormatter, +}; use super::{Lang, LanguageFormatter}; @@ -10,6 +12,9 @@ pub enum Lua { #[default] #[serde(rename = "stylua")] Stylua, + + #[serde(rename = "luaformatter")] + LuaFormatter, } impl Default for Lang { @@ -25,7 +30,10 @@ impl Default for Lang { impl Default for MdsfFormatter { #[inline] fn default() -> Self { - Self::Single(Lua::Stylua) + Self::Multiple(vec![Self::Multiple(vec![ + Self::Single(Lua::Stylua), + Self::Single(Lua::LuaFormatter), + ])]) } } @@ -37,6 +45,7 @@ impl LanguageFormatter for Lua { ) -> std::io::Result<(bool, Option)> { match self { Self::Stylua => format_using_stylua(snippet_path), + Self::LuaFormatter => format_using_luaformatter(snippet_path), } } } @@ -54,12 +63,11 @@ mod test_lua { local function add ( a , b ) - -return a +b + local c = a + b +return c end - "; const EXTENSION: &str = crate::languages::Language::Lua.to_file_ext(); @@ -85,7 +93,11 @@ end #[test] fn test_stylua() { - let expected_output = "local function add(a, b)\n\treturn a + b\nend\n"; + let expected_output = "local function add(a, b) +\tlocal c = a + b +\treturn c +end +"; let l = Lang:: { enabled: true, @@ -102,4 +114,30 @@ end assert_eq!(output, expected_output); } + + #[test_with::executable(lua-format)] + #[test] + fn test_luaformatter() { + let expected_output = "local function add(a, b) + local c = a + b + return c + +end +"; + + let l = Lang:: { + enabled: true, + formatter: MdsfFormatter::Single(Lua::LuaFormatter), + }; + + let snippet = setup_snippet(INPUT, EXTENSION).expect("it to save the file"); + let snippet_path = snippet.path(); + + let output = l + .format(snippet_path) + .expect("it to not fail") + .expect("it to be a snippet"); + + assert_eq!(output, expected_output); + } }