Skip to content

Commit

Permalink
feat(haskell): add support for ormolu
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen committed Mar 21, 2024
1 parent a84abc9 commit c71f520
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub mod mix_format;
pub mod nimpretty;
pub mod npm_groovy_lint;
pub mod ocamlformat;
pub mod ormolu;
pub mod perltidy;
pub mod prettier;
pub mod purs_tidy;
Expand Down
44 changes: 44 additions & 0 deletions src/formatters/ormolu.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use super::execute_command;

#[inline]
pub fn format_using_ormolu(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
let mut cmd = std::process::Command::new("ormolu");

cmd.arg("--mode").arg("inplace").arg(snippet_path);

execute_command(&mut cmd, snippet_path)
}

#[cfg(test)]
mod test_ormolu {
use crate::{formatters::setup_snippet, languages::Language};

use super::format_using_ormolu;

#[test_with::executable(ormolu)]
#[test]
fn it_should_format_haskell() {
let input = "
addNumbers::Int->Int->Int
addNumbers a b = do
a + b
";

let expected_output = "addNumbers :: Int -> Int -> Int
addNumbers a b = do
a + b
";

let snippet = setup_snippet(input, Language::Haskell.to_file_ext())
.expect("it to create a snippet file");

let output = format_using_ormolu(snippet.path())
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(output, expected_output);
}
}
33 changes: 31 additions & 2 deletions src/languages/haskell.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
use schemars::JsonSchema;

use crate::formatters::{
fourmolu::format_using_fourmolu, hindent::format_using_hindent, MdsfFormatter,
fourmolu::format_using_fourmolu, hindent::format_using_hindent, ormolu::format_using_ormolu,
MdsfFormatter,
};

use super::{Lang, LanguageFormatter};

#[derive(Debug, Default, serde::Serialize, serde::Deserialize, JsonSchema)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum Haskell {
#[default]
#[serde(rename = "fourmolu")]
Fourmolu,
#[default]
#[serde(rename = "ormolu")]
Ormolu,
#[serde(rename = "hindent")]
HIndent,
}
Expand All @@ -31,6 +34,7 @@ impl Default for MdsfFormatter<Haskell> {
fn default() -> Self {
Self::Multiple(vec![Self::Multiple(vec![
Self::Single(Haskell::Fourmolu),
Self::Single(Haskell::Ormolu),
Self::Single(Haskell::HIndent),
])])
}
Expand All @@ -44,6 +48,7 @@ impl LanguageFormatter for Haskell {
) -> std::io::Result<(bool, Option<String>)> {
match self {
Self::Fourmolu => format_using_fourmolu(snippet_path),
Self::Ormolu => format_using_ormolu(snippet_path),
Self::HIndent => format_using_hindent(snippet_path),
}
}
Expand Down Expand Up @@ -109,6 +114,30 @@ addNumbers a b = do
assert_eq!(output, expected_output);
}

#[test_with::executable(ormolu)]
#[test]
fn test_ormolu() {
let l = Lang::<Haskell> {
enabled: true,
formatter: MdsfFormatter::Single(Haskell::Ormolu),
};

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 = "addNumbers :: Int -> Int -> Int
addNumbers a b = do
a + b
";

assert_eq!(output, expected_output);
}

#[test_with::executable(hindent)]
#[test]
fn test_hindent() {
Expand Down

0 comments on commit c71f520

Please sign in to comment.