Skip to content

Commit

Permalink
feat(lua): add support for luaformatter
Browse files Browse the repository at this point in the history
Closes #111
  • Loading branch information
hougesen committed Mar 21, 2024
1 parent b6eefd8 commit 1436b5a
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 6 deletions.
52 changes: 52 additions & 0 deletions src/formatters/luaformatter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use super::execute_command;

#[inline]
pub fn format_using_luaformatter(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
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);
}
}
1 change: 1 addition & 0 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
50 changes: 44 additions & 6 deletions src/languages/lua.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -10,6 +12,9 @@ pub enum Lua {
#[default]
#[serde(rename = "stylua")]
Stylua,

#[serde(rename = "luaformatter")]
LuaFormatter,
}

impl Default for Lang<Lua> {
Expand All @@ -25,7 +30,10 @@ impl Default for Lang<Lua> {
impl Default for MdsfFormatter<Lua> {
#[inline]
fn default() -> Self {
Self::Single(Lua::Stylua)
Self::Multiple(vec![Self::Multiple(vec![
Self::Single(Lua::Stylua),
Self::Single(Lua::LuaFormatter),
])])
}
}

Expand All @@ -37,6 +45,7 @@ impl LanguageFormatter for Lua {
) -> std::io::Result<(bool, Option<String>)> {
match self {
Self::Stylua => format_using_stylua(snippet_path),
Self::LuaFormatter => format_using_luaformatter(snippet_path),
}
}
}
Expand All @@ -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();
Expand All @@ -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::<Lua> {
enabled: true,
Expand All @@ -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::<Lua> {
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);
}
}

0 comments on commit 1436b5a

Please sign in to comment.