Skip to content

Commit

Permalink
feat: add support for mix format (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen authored Mar 8, 2024
1 parent 9fc3c18 commit 7dfcbae
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 5 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ jobs:
# Used by gleam format
- uses: erlef/setup-beam@v1
with:
otp-version: false
otp-version: "26"
gleam-version: "1.0.0"
elixir-version: "1.16.1"

- run: rustup toolchain install stable --profile minimal
- run: rustup component add rustfmt clippy
Expand All @@ -80,6 +81,9 @@ jobs:
- name: Validate biome
run: npx --yes @biomejs/biome --version

- name: Validate prettier
run: npx --yes prettier --version

- name: Validate nimpretty
run: nimpretty --version

Expand All @@ -89,6 +93,9 @@ jobs:
- name: Validate gleam format
run: gleam format --help

- name: Validate mix format
run: mix help format

- name: Install stylua
run: cargo install stylua

Expand Down
32 changes: 32 additions & 0 deletions schemas/v0.0.0/mdsf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
}
]
},
"elixir": {
"default": {
"enabled": true,
"formatter": "mix_format"
},
"allOf": [
{
"$ref": "#/definitions/Elixir"
}
]
},
"gleam": {
"default": {
"enabled": true,
Expand Down Expand Up @@ -180,6 +191,27 @@
"type": "string",
"enum": ["prettier"]
},
"Elixir": {
"type": "object",
"properties": {
"enabled": {
"default": true,
"type": "boolean"
},
"formatter": {
"default": "mix_format",
"allOf": [
{
"$ref": "#/definitions/ElixirFormatter"
}
]
}
}
},
"ElixirFormatter": {
"type": "string",
"enum": ["mix_format"]
},
"Gleam": {
"type": "object",
"properties": {
Expand Down
24 changes: 21 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,59 @@
use schemars::JsonSchema;

use crate::languages::{
css::Css, gleam::Gleam, html::Html, javascript::JavaScript, json::Json, lua::Lua,
markdown::Markdown, nim::Nim, python::Python, rust::Rust, toml::Toml, typescript::TypeScript,
yaml::Yaml, zig::Zig,
css::Css, elixir::Elixir, gleam::Gleam, html::Html, javascript::JavaScript, json::Json,
lua::Lua, markdown::Markdown, nim::Nim, python::Python, rust::Rust, toml::Toml,
typescript::TypeScript, yaml::Yaml, zig::Zig,
};

#[derive(Debug, serde::Serialize, serde::Deserialize, JsonSchema)]
pub struct MdsfConfig {
#[schemars(skip)]
#[serde(rename = "$schema", default = "default_schema_location")]
pub schema: String,

#[serde(default)]
pub css: Css,

#[serde(default)]
pub elixir: Elixir,

#[serde(default)]
pub gleam: Gleam,

#[serde(default)]
pub html: Html,

#[serde(default)]
pub javascript: JavaScript,

#[serde(default)]
pub json: Json,

#[serde(default)]
pub lua: Lua,

#[serde(default)]
pub markdown: Markdown,

#[serde(default)]
pub nim: Nim,

#[serde(default)]
pub python: Python,

#[serde(default)]
pub rust: Rust,

#[serde(default)]
pub toml: Toml,

#[serde(default)]
pub typescript: TypeScript,

#[serde(default)]
pub yaml: Yaml,

#[serde(default)]
pub zig: Zig,
}
Expand All @@ -47,6 +64,7 @@ impl Default for MdsfConfig {
Self {
schema: default_schema_location(),
css: Css::default(),
elixir: Elixir::default(),
gleam: Gleam::default(),
html: Html::default(),
javascript: JavaScript::default(),
Expand Down
1 change: 0 additions & 1 deletion src/formatters/gleam_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use super::{execute_command, read_snippet};

#[inline]
pub fn format_using_gleam_format(file_path: &std::path::Path) -> std::io::Result<Option<String>> {
// TODO: use installed biome instead
let mut cmd = std::process::Command::new("gleam");

// Incase the use hasn't installed biome
Expand Down
44 changes: 44 additions & 0 deletions src/formatters/mix_format.rs
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);
}
}
2 changes: 2 additions & 0 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{

pub mod biome;
pub mod gleam_format;
pub mod mix_format;
pub mod nimpretty;
pub mod prettier;
pub mod ruff;
Expand Down Expand Up @@ -55,6 +56,7 @@ pub fn format_snippet(config: &MdsfConfig, language: &Language, code: &str) -> S

if let Ok(Some(formatted_code)) = match language {
Language::Css => config.css.format(snippet_path),
Language::Elixir => config.elixir.format(snippet_path),
Language::Gleam => config.gleam.format(snippet_path),
Language::Html => config.html.format(snippet_path),
Language::JavaScript => config.javascript.format(snippet_path),
Expand Down
43 changes: 43 additions & 0 deletions src/languages/elixir.rs
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),
}
}
}
4 changes: 4 additions & 0 deletions src/languages/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub enum Language {
Css,
Elixir,
Gleam,
Html,
JavaScript,
Expand All @@ -16,6 +17,7 @@ pub enum Language {
}

pub mod css;
pub mod elixir;
pub mod gleam;
pub mod html;
pub mod javascript;
Expand All @@ -39,6 +41,7 @@ impl Language {
pub fn maybe_from_str(input: &str) -> Option<Self> {
match input {
"css" | "scss" => Some(Self::Css),
"elixir" => Some(Self::Elixir),
"gleam" => Some(Self::Gleam),
"html" => Some(Self::Html),
"js" | "jsx" | "javascript" => Some(Self::JavaScript),
Expand All @@ -61,6 +64,7 @@ impl Language {
match self {
// NOTE: since scss is a superset of css we might as well support both at the same time
Self::Css => ".scss",
Self::Elixir => ".ex",
Self::Gleam => ".gleam",
Self::Html => ".html",
Self::JavaScript => ".js",
Expand Down
6 changes: 6 additions & 0 deletions tests/elixir.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
```elixir

def add(a , b ) do a + b end


```

0 comments on commit 7dfcbae

Please sign in to comment.