Skip to content

Commit

Permalink
feat(just): support just fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen committed Mar 16, 2024
1 parent 8480458 commit 4bde325
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ jobs:
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
# just_fmt
- uses: taiki-e/install-action@just

- run: rustup toolchain install stable --profile minimal
- run: rustup component add rustfmt clippy
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ mdsf init
| Java | `clang-format` |
| JavaScript | `prettier`, `biome`, `clang-format`, `deno_fmt` |
| JSON | `prettier`, `biome`, `clang-format`, `deno_fmt` |
| Just | `just_fmt` |
| Lua | `stylua` |
| Nim | `nimpretty` |
| Objective C | `clang-format` |
Expand Down
40 changes: 40 additions & 0 deletions schemas/v0.0.1/mdsf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,17 @@
}
]
},
"just": {
"default": {
"enabled": true,
"formatter": "just_fmt"
},
"allOf": [
{
"$ref": "#/definitions/Lang_for_Just"
}
]
},
"lua": {
"default": {
"enabled": true,
Expand Down Expand Up @@ -379,6 +390,10 @@
"type": "string",
"enum": ["prettier", "biome", "deno_fmt", "clang-format"]
},
"Just": {
"type": "string",
"enum": ["just_fmt"]
},
"Lang_for_C": {
"type": "object",
"required": ["enabled", "formatter"],
Expand Down Expand Up @@ -535,6 +550,18 @@
}
}
},
"Lang_for_Just": {
"type": "object",
"required": ["enabled", "formatter"],
"properties": {
"enabled": {
"type": "boolean"
},
"formatter": {
"$ref": "#/definitions/MdsfFormatter_for_Just"
}
}
},
"Lang_for_Lua": {
"type": "object",
"required": ["enabled", "formatter"],
Expand Down Expand Up @@ -904,6 +931,19 @@
}
]
},
"MdsfFormatter_for_Just": {
"anyOf": [
{
"$ref": "#/definitions/Just"
},
{
"type": "array",
"items": {
"$ref": "#/definitions/MdsfFormatter_for_Just"
}
}
]
},
"MdsfFormatter_for_Lua": {
"anyOf": [
{
Expand Down
12 changes: 8 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use schemars::JsonSchema;

use crate::languages::{
c::C, cpp::Cpp, crystal::Crystal, csharp::CSharp, css::Css, dart::Dart, elixir::Elixir,
gleam::Gleam, go::Go, html::Html, java::Java, javascript::JavaScript, json::Json, lua::Lua,
markdown::Markdown, nim::Nim, objective_c::ObjectiveC, protobuf::Protobuf, python::Python,
roc::Roc, ruby::Ruby, rust::Rust, shell::Shell, sql::Sql, toml::Toml, typescript::TypeScript,
vue::Vue, yaml::Yaml, zig::Zig, Lang,
gleam::Gleam, go::Go, html::Html, java::Java, javascript::JavaScript, json::Json, just::Just,
lua::Lua, markdown::Markdown, nim::Nim, objective_c::ObjectiveC, protobuf::Protobuf,
python::Python, roc::Roc, ruby::Ruby, rust::Rust, shell::Shell, sql::Sql, toml::Toml,
typescript::TypeScript, vue::Vue, yaml::Yaml, zig::Zig, Lang,
};

#[derive(Debug, serde::Serialize, serde::Deserialize, JsonSchema)]
Expand Down Expand Up @@ -54,6 +54,9 @@ pub struct MdsfConfig {
#[serde(default)]
pub json: Lang<Json>,

#[serde(default)]
pub just: Lang<Just>,

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

Expand Down Expand Up @@ -122,6 +125,7 @@ impl Default for MdsfConfig {
java: Lang::<Java>::default(),
javascript: Lang::<JavaScript>::default(),
json: Lang::<Json>::default(),
just: Lang::<Just>::default(),
lua: Lang::<Lua>::default(),
markdown: Lang::<Markdown>::default(),
nim: Lang::<Nim>::default(),
Expand Down
47 changes: 47 additions & 0 deletions src/formatters/just_fmt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use super::execute_command;

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

cmd.arg("--fmt")
// TODO: remove once it is stabilized
.arg("--unstable")
.arg("--justfile")
.arg(snippet_path);

execute_command(&mut cmd, snippet_path)
}

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

#[test]
fn it_should_format_just() {
let input = "build:
cargo build
cargo build --release
";

let expected_output = "build:
cargo build
cargo build --release
";

let snippet = setup_snippet(input, Language::Gleam.to_file_ext())
.expect("it to create a snippet file");

let output = format_using_just_fmt(snippet.path())
.expect("it to be successful")
.1
.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 @@ -17,6 +17,7 @@ pub mod gleam_format;
pub mod gofmt;
pub mod gofumpt;
pub mod isort;
pub mod just_fmt;
pub mod mix_format;
pub mod nimpretty;
pub mod prettier;
Expand Down Expand Up @@ -107,6 +108,7 @@ pub fn format_snippet(config: &MdsfConfig, language: &Language, code: &str) -> S
Language::Java => config.java.format(snippet_path),
Language::JavaScript => config.javascript.format(snippet_path),
Language::Json => config.json.format(snippet_path),
Language::Just => config.just.format(snippet_path),
Language::Lua => config.lua.format(snippet_path),
Language::Markdown => config.markdown.format(snippet_path),
Language::Nim => config.nim.format(snippet_path),
Expand Down
103 changes: 103 additions & 0 deletions src/languages/just.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
use schemars::JsonSchema;

use crate::formatters::{just_fmt::format_using_just_fmt, MdsfFormatter};

use super::{Lang, LanguageFormatter};

#[derive(Debug, Default, serde::Serialize, serde::Deserialize, JsonSchema)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum Just {
#[default]
#[serde(rename = "just_fmt")]
JustFmt,
}

impl Default for Lang<Just> {
#[inline]
fn default() -> Self {
Self {
enabled: true,
formatter: MdsfFormatter::<Just>::default(),
}
}
}

impl Default for MdsfFormatter<Just> {
#[inline]
fn default() -> Self {
Self::Single(Just::JustFmt)
}
}

impl LanguageFormatter for Just {
#[inline]
fn format_snippet(
&self,
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
match self {
Self::JustFmt => format_using_just_fmt(snippet_path),
}
}
}

#[cfg(test)]
mod test_just {
use crate::{
formatters::{setup_snippet, MdsfFormatter},
languages::Lang,
};

use super::Just;

const INPUT: &str = "
build:
cargo build
cargo build --release
";
const EXTENSION: &str = crate::languages::Language::Just.to_file_ext();

#[test]
fn it_should_be_enabled_by_default() {
assert!(Lang::<Just>::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::<Just> {
enabled: false,
formatter: MdsfFormatter::Single(Just::default())
}
.format(snippet_path)
.expect("it to not fail")
.is_none());
}

#[test]
fn test_just_fmt() {
let l = Lang::<Just> {
enabled: true,
formatter: MdsfFormatter::Single(Just::JustFmt),
};

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 = "build:
cargo build
cargo build --release
";

assert_eq!(output, expected_output);
}
}
7 changes: 6 additions & 1 deletion src/languages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub enum Language {
Java,
JavaScript,
Json,
Just,
Lua,
Markdown,
Nim,
Expand Down Expand Up @@ -63,6 +64,7 @@ pub mod html;
pub mod java;
pub mod javascript;
pub mod json;
pub mod just;
pub mod lua;
pub mod markdown;
pub mod nim;
Expand Down Expand Up @@ -103,7 +105,8 @@ impl Language {
"html" => Some(Self::Html),
"java" => Some(Self::Java),
"javascript" | "js" | "jsx" => Some(Self::JavaScript),
"json" => Some(Self::Json),
"json" | "jsonc" => Some(Self::Json),
"just" | "justfile" => Some(Self::Just),
"lua" => Some(Self::Lua),
"markdown" | "md" => Some(Self::Markdown),
"nim" => Some(Self::Nim),
Expand Down Expand Up @@ -143,6 +146,8 @@ impl Language {
Self::Java => ".java",
Self::JavaScript => ".js",
Self::Json => ".jsonc",
// NOTE: just does not have any file ext
Self::Just => ".justfile",
Self::Lua => ".lua",
Self::Markdown => ".md",
Self::Nim => ".nim",
Expand Down

0 comments on commit 4bde325

Please sign in to comment.