Skip to content

Commit

Permalink
feat(haskell): support hindent
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen committed Mar 20, 2024
1 parent 58f7e2e commit 33e8b2a
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ mdsf init
| Go | `gofmt`, `gofumpt`, `goimports` |
| GraphQL | `prettier` |
| Groovy | `npm-groovy-lint` |
| Haskell | `fourmolu` |
| Haskell | `fourmolu`, `hindent` |
| Html | `prettier` |
| Java | `clang-format`, `google-java-format` |
| JavaScript | `biome`, `clang-format`, `deno_fmt`, `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 @@ -528,7 +528,7 @@
},
"Haskell": {
"type": "string",
"enum": ["fourmolu"]
"enum": ["fourmolu", "hindent"]
},
"Html": {
"type": "string",
Expand Down
44 changes: 44 additions & 0 deletions src/formatters/hindent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use super::execute_command;

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

cmd.arg(snippet_path);

execute_command(&mut cmd, snippet_path)
}

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

use super::format_using_hindent;

#[test_with::executable(hindent)]
#[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_hindent(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 @@ -23,6 +23,7 @@ pub mod gofmt;
pub mod gofumpt;
pub mod goimports;
pub mod google_java_format;
pub mod hindent;
pub mod isort;
pub mod just_fmt;
pub mod mix_format;
Expand Down
31 changes: 30 additions & 1 deletion src/languages/haskell.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use schemars::JsonSchema;

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

use super::{Lang, LanguageFormatter};

Expand All @@ -10,6 +12,8 @@ pub enum Haskell {
#[default]
#[serde(rename = "fourmolu")]
Fourmolu,
#[serde(rename = "hindent")]
HIndent,
}

impl Default for Lang<Haskell> {
Expand Down Expand Up @@ -37,6 +41,7 @@ impl LanguageFormatter for Haskell {
) -> std::io::Result<(bool, Option<String>)> {
match self {
Self::Fourmolu => format_using_fourmolu(snippet_path),
Self::HIndent => format_using_hindent(snippet_path),
}
}
}
Expand Down Expand Up @@ -100,4 +105,28 @@ addNumbers a b = do

assert_eq!(output, expected_output);
}

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

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);
}
}

0 comments on commit 33e8b2a

Please sign in to comment.