From 78056ab7515d5194706f299d550a05be4fdd6fa3 Mon Sep 17 00:00:00 2001 From: Mads Hougesen Date: Fri, 22 Mar 2024 01:40:41 +0100 Subject: [PATCH] feat(erlang): add support for efmt --- README.md | 2 +- schemas/v0.0.2/mdsf.schema.json | 2 +- src/formatters/efmt.rs | 40 +++++++++++++++++++++++++++++++++ src/formatters/mod.rs | 1 + src/languages/erlang.rs | 33 ++++++++++++++++++++++++++- 5 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 src/formatters/efmt.rs diff --git a/README.md b/README.md index abddcb67..c76c2895 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ mdsf init | Dart | `dart_format` | | Elixir | `mix_format` | | Elm | `elm-format` | -| Erlang | `erlfmt` | +| Erlang | `efmt`, `erlfmt` | | Gleam | `gleam_format` | | Go | `gofmt`, `gofumpt`, `goimports` | | GraphQL | `prettier` | diff --git a/schemas/v0.0.2/mdsf.schema.json b/schemas/v0.0.2/mdsf.schema.json index 599cb50c..e5122e26 100644 --- a/schemas/v0.0.2/mdsf.schema.json +++ b/schemas/v0.0.2/mdsf.schema.json @@ -545,7 +545,7 @@ }, "Erlang": { "type": "string", - "enum": ["erlfmt"] + "enum": ["erlfmt", "efmt"] }, "Gleam": { "type": "string", diff --git a/src/formatters/efmt.rs b/src/formatters/efmt.rs new file mode 100644 index 00000000..d7dc109b --- /dev/null +++ b/src/formatters/efmt.rs @@ -0,0 +1,40 @@ +use super::execute_command; + +#[inline] +pub fn format_using_efmt(file_path: &std::path::Path) -> std::io::Result<(bool, Option)> { + let mut cmd = std::process::Command::new("efmt"); + + cmd.arg("-w").arg(file_path); + + execute_command(&mut cmd, file_path) +} + +#[cfg(test)] +mod test_efmt { + use crate::{ + formatters::{efmt::format_using_efmt, setup_snippet}, + languages::Language, + }; + + #[test_with::executable(efmt)] + #[test] + fn it_should_format_erlang() { + let input = "what_is(Erlang) -> +case Erlang of movie->[hello(mike,joe,robert),credits]; language->formatting_arguments end +."; + + let expected_output = "what_is(Erlang) -> + case Erlang of movie -> [hello(mike, joe, robert), credits]; language -> formatting_arguments end. +" +; + let snippet = setup_snippet(input, Language::Erlang.to_file_ext()) + .expect("it to create a snippet file"); + + let output = format_using_efmt(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 b8e0a1f4..d451a757 100644 --- a/src/formatters/mod.rs +++ b/src/formatters/mod.rs @@ -16,6 +16,7 @@ pub mod cljstyle; pub mod crystal_format; pub mod dart_format; pub mod deno_fmt; +pub mod efmt; pub mod elm_format; pub mod erlfmt; pub mod fourmolu; diff --git a/src/languages/erlang.rs b/src/languages/erlang.rs index c4bc429c..e5d3c2c3 100644 --- a/src/languages/erlang.rs +++ b/src/languages/erlang.rs @@ -1,6 +1,6 @@ use schemars::JsonSchema; -use crate::formatters::{erlfmt::format_using_erlfmt, MdsfFormatter}; +use crate::formatters::{efmt::format_using_efmt, erlfmt::format_using_erlfmt, MdsfFormatter}; use super::{Lang, LanguageFormatter}; @@ -10,6 +10,8 @@ pub enum Erlang { #[default] #[serde(rename = "erlfmt")] Erlfmt, + #[serde(rename = "efmt")] + Efmt, } impl Default for Lang { @@ -37,6 +39,7 @@ impl LanguageFormatter for Erlang { ) -> std::io::Result<(bool, Option)> { match self { Self::Erlfmt => format_using_erlfmt(snippet_path), + Self::Efmt => format_using_efmt(snippet_path), } } } @@ -99,4 +102,32 @@ case Erlang of movie->[hello(mike,joe,robert),credits]; language->formatting_arg assert_eq!(output, expected_output); } + + #[test_with::executable(efmt)] + #[test] + fn test_efmt() { + let input = "what_is(Erlang) -> +case Erlang of movie->[hello(mike,joe,robert),credits]; language->formatting_arguments end +."; + + let l = Lang:: { + enabled: true, + formatter: MdsfFormatter::Single(Erlang::Efmt), + }; + + 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"); + + let expected_output = "what_is(Erlang) -> + case Erlang of movie -> [hello(mike, joe, robert), credits]; language -> formatting_arguments end. +" +; + + assert_eq!(output, expected_output); + } }