-
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(crystal): support crystal format
- Loading branch information
Showing
8 changed files
with
196 additions
and
13 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,39 @@ | ||
use super::execute_command; | ||
|
||
#[inline] | ||
pub fn format_using_crystal_format( | ||
snippet_path: &std::path::Path, | ||
) -> std::io::Result<(bool, Option<String>)> { | ||
let mut cmd = std::process::Command::new("crystal"); | ||
|
||
cmd.arg("tool").arg("format").arg(snippet_path); | ||
|
||
execute_command(&mut cmd, snippet_path) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test_crystal_format { | ||
use crate::{formatters::setup_snippet, languages::Language}; | ||
|
||
use super::format_using_crystal_format; | ||
|
||
#[test] | ||
fn it_should_format_crystal() { | ||
let input = "def add(a, b) return a + b end"; | ||
|
||
let expected_output = "def add(a, b) | ||
return a + b | ||
end | ||
"; | ||
|
||
let snippet = setup_snippet(input, Language::Crystal.to_file_ext()) | ||
.expect("it to create a snippet file"); | ||
|
||
let output = format_using_crystal_format(snippet.path()) | ||
.expect("it to be successful") | ||
.1 | ||
.expect("it to be some"); | ||
|
||
assert_eq!(expected_output, 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,99 @@ | ||
use schemars::JsonSchema; | ||
|
||
use crate::{config::default_enabled, formatters::crystal_format::format_using_crystal_format}; | ||
|
||
use super::LanguageFormatter; | ||
|
||
#[derive(Debug, Default, serde::Serialize, serde::Deserialize, JsonSchema)] | ||
#[cfg_attr(test, derive(PartialEq, Eq))] | ||
pub enum CrystalFormatter { | ||
#[default] | ||
#[serde(rename = "crystal_format")] | ||
CrystalFormat, | ||
} | ||
|
||
#[derive(Debug, serde::Serialize, serde::Deserialize, JsonSchema)] | ||
#[cfg_attr(test, derive(PartialEq, Eq))] | ||
pub struct Crystal { | ||
#[serde(default = "default_enabled")] | ||
pub enabled: bool, | ||
#[serde(default)] | ||
pub formatter: CrystalFormatter, | ||
} | ||
|
||
impl Default for Crystal { | ||
#[inline] | ||
fn default() -> Self { | ||
Self { | ||
enabled: true, | ||
formatter: CrystalFormatter::default(), | ||
} | ||
} | ||
} | ||
|
||
impl LanguageFormatter for Crystal { | ||
#[inline] | ||
fn format(&self, snippet_path: &std::path::Path) -> std::io::Result<Option<String>> { | ||
if !self.enabled { | ||
return Ok(None); | ||
} | ||
|
||
match self.formatter { | ||
CrystalFormatter::CrystalFormat => { | ||
format_using_crystal_format(snippet_path).map(|res| res.1) | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test_crystal { | ||
use crate::{formatters::setup_snippet, languages::LanguageFormatter}; | ||
|
||
use super::{Crystal, CrystalFormatter}; | ||
|
||
const INPUT: &str = "def add(a, b) return a + b end"; | ||
|
||
const EXTENSION: &str = crate::languages::Language::Crystal.to_file_ext(); | ||
|
||
#[test] | ||
fn it_should_be_enabled_by_default() { | ||
assert!(Crystal::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!(Crystal { | ||
enabled: false, | ||
formatter: CrystalFormatter::CrystalFormat, | ||
} | ||
.format(snippet_path) | ||
.expect("it to not fail") | ||
.is_none()); | ||
} | ||
|
||
#[test] | ||
fn test_crystal_format() { | ||
let expected_output = "def add(a, b) | ||
return a + b | ||
end | ||
"; | ||
let l = Crystal { | ||
enabled: true, | ||
formatter: CrystalFormatter::CrystalFormat, | ||
}; | ||
|
||
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"); | ||
|
||
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