-
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: add support for mix format (#36)
- Loading branch information
Showing
9 changed files
with
160 additions
and
5 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, read_snippet}; | ||
|
||
#[inline] | ||
pub fn format_using_mix_format(file_path: &std::path::Path) -> std::io::Result<Option<String>> { | ||
let mut cmd = std::process::Command::new("mix"); | ||
|
||
// Incase the use hasn't installed biome | ||
cmd.arg("format").arg(file_path); | ||
|
||
if execute_command(&mut cmd)? { | ||
return read_snippet(file_path).map(Some); | ||
} | ||
|
||
Ok(None) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test_gleam_format { | ||
use crate::{ | ||
formatters::{mix_format::format_using_mix_format, setup_snippet}, | ||
languages::Language, | ||
}; | ||
|
||
#[test] | ||
fn it_should_format_gleam() { | ||
let input = " | ||
def add(a , b ) do a + b end | ||
"; | ||
let expected_output = "def add(a, b) do | ||
a + b | ||
end | ||
"; | ||
|
||
let snippet = setup_snippet(input, Language::Elixir.to_file_ext()) | ||
.expect("it to create a snippet file"); | ||
|
||
let output = format_using_mix_format(snippet.path()) | ||
.expect("it to be succesful") | ||
.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,43 @@ | ||
use schemars::JsonSchema; | ||
|
||
use crate::{config::default_enabled, formatters::mix_format::format_using_mix_format}; | ||
|
||
use super::LanguageFormatter; | ||
|
||
#[derive(Debug, Default, serde::Serialize, serde::Deserialize, JsonSchema)] | ||
pub enum ElixirFormatter { | ||
#[default] | ||
#[serde(rename = "mix_format")] | ||
MixFormat, | ||
} | ||
|
||
#[derive(Debug, serde::Serialize, serde::Deserialize, JsonSchema)] | ||
pub struct Elixir { | ||
#[serde(default = "default_enabled")] | ||
pub enabled: bool, | ||
#[serde(default)] | ||
pub formatter: ElixirFormatter, | ||
} | ||
|
||
impl Default for Elixir { | ||
#[inline] | ||
fn default() -> Self { | ||
Self { | ||
enabled: true, | ||
formatter: ElixirFormatter::default(), | ||
} | ||
} | ||
} | ||
|
||
impl LanguageFormatter for Elixir { | ||
#[inline] | ||
fn format(&self, snippet_path: &std::path::Path) -> std::io::Result<Option<String>> { | ||
if !self.enabled { | ||
return Ok(None); | ||
} | ||
|
||
match self.formatter { | ||
ElixirFormatter::MixFormat => format_using_mix_format(snippet_path), | ||
} | ||
} | ||
} |
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,6 @@ | ||
```elixir | ||
|
||
def add(a , b ) do a + b end | ||
|
||
|
||
``` |