Skip to content

Commit

Permalink
feat(erlang): add support for efmt
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen committed Mar 22, 2024
1 parent c56b8e8 commit 78056ab
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down
2 changes: 1 addition & 1 deletion schemas/v0.0.2/mdsf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@
},
"Erlang": {
"type": "string",
"enum": ["erlfmt"]
"enum": ["erlfmt", "efmt"]
},
"Gleam": {
"type": "string",
Expand Down
40 changes: 40 additions & 0 deletions src/formatters/efmt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use super::execute_command;

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

Expand All @@ -10,6 +10,8 @@ pub enum Erlang {
#[default]
#[serde(rename = "erlfmt")]
Erlfmt,
#[serde(rename = "efmt")]
Efmt,
}

impl Default for Lang<Erlang> {
Expand Down Expand Up @@ -37,6 +39,7 @@ impl LanguageFormatter for Erlang {
) -> std::io::Result<(bool, Option<String>)> {
match self {
Self::Erlfmt => format_using_erlfmt(snippet_path),
Self::Efmt => format_using_efmt(snippet_path),
}
}
}
Expand Down Expand Up @@ -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::<Erlang> {
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);
}
}

0 comments on commit 78056ab

Please sign in to comment.