-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(haskell): add hindent and fourmolu (#98)
* feat(haskell): add fourmolu * feat(haskell): support hindent * refactor(haskell): try fourmolo then hindent * ci: setup fourmolu and hindent
- Loading branch information
Showing
9 changed files
with
291 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use super::execute_command; | ||
|
||
#[inline] | ||
pub fn format_using_fourmolu( | ||
snippet_path: &std::path::Path, | ||
) -> std::io::Result<(bool, Option<String>)> { | ||
let mut cmd = std::process::Command::new("fourmolu"); | ||
|
||
cmd.arg("-i").arg(snippet_path); | ||
|
||
execute_command(&mut cmd, snippet_path) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test_fourmolu { | ||
use crate::{formatters::setup_snippet, languages::Language}; | ||
|
||
use super::format_using_fourmolu; | ||
|
||
#[test_with::executable(fourmolu)] | ||
#[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_fourmolu(snippet.path()) | ||
.expect("it to be successful") | ||
.1 | ||
.expect("it to be some"); | ||
|
||
assert_eq!(output, expected_output); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!(output, expected_output); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
use schemars::JsonSchema; | ||
|
||
use crate::formatters::{ | ||
fourmolu::format_using_fourmolu, hindent::format_using_hindent, 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, | ||
#[serde(rename = "hindent")] | ||
HIndent, | ||
} | ||
|
||
impl Default for Lang<Haskell> { | ||
#[inline] | ||
fn default() -> Self { | ||
Self { | ||
enabled: true, | ||
formatter: MdsfFormatter::<Haskell>::default(), | ||
} | ||
} | ||
} | ||
|
||
impl Default for MdsfFormatter<Haskell> { | ||
#[inline] | ||
fn default() -> Self { | ||
Self::Multiple(vec![Self::Multiple(vec![ | ||
Self::Single(Haskell::Fourmolu), | ||
Self::Single(Haskell::HIndent), | ||
])]) | ||
} | ||
} | ||
|
||
impl LanguageFormatter for Haskell { | ||
#[inline] | ||
fn format_snippet( | ||
&self, | ||
snippet_path: &std::path::Path, | ||
) -> std::io::Result<(bool, Option<String>)> { | ||
match self { | ||
Self::Fourmolu => format_using_fourmolu(snippet_path), | ||
Self::HIndent => format_using_hindent(snippet_path), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test_haskell { | ||
use crate::{ | ||
formatters::{setup_snippet, MdsfFormatter}, | ||
languages::Lang, | ||
}; | ||
|
||
use super::Haskell; | ||
|
||
const INPUT: &str = " | ||
addNumbers::Int->Int->Int | ||
addNumbers a b = do | ||
a + b | ||
"; | ||
|
||
const EXTENSION: &str = crate::languages::Language::Haskell.to_file_ext(); | ||
|
||
#[test] | ||
fn it_should_be_enabled_by_default() { | ||
assert!(Lang::<Haskell>::default().enabled); | ||
} | ||
|
||
#[test] | ||
fn it_should_not_format_when_enabled_is_false() { | ||
let snippet = setup_snippet(INPUT, EXTENSION).expect("it to save the file"); | ||
let snippet_path = snippet.path(); | ||
|
||
assert!(Lang::<Haskell> { | ||
enabled: false, | ||
formatter: MdsfFormatter::Single(Haskell::default()), | ||
} | ||
.format(snippet_path) | ||
.expect("it to not fail") | ||
.is_none()); | ||
} | ||
|
||
#[test_with::executable(fourmolu)] | ||
#[test] | ||
fn test_fourmolu() { | ||
let l = Lang::<Haskell> { | ||
enabled: true, | ||
formatter: MdsfFormatter::Single(Haskell::Fourmolu), | ||
}; | ||
|
||
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() { | ||
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); | ||
} | ||
} |
Oops, something went wrong.