Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(clojure): support cljstyle #96

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,8 @@ jobs:
- name: beautysh
run: pip install beautysh

- name: cljstyle
run: curl -sLO https://raw.githubusercontent.com/greglook/cljstyle/main/util/install-cljstyle && chmod +x install-cljstyle && sudo ./install-cljstyle

- name: run tests
run: cargo test
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ mdsf init
| Blade | `blade-formatter` |
| C | `clang-format` |
| CSharp | `clang-format` |
| Clojure | `cljstyle` |
| Cpp | `clang-format` |
| Crystal | `crystal_format` |
| Css | `prettier` |
Expand Down
40 changes: 40 additions & 0 deletions schemas/v0.0.2/mdsf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@
}
]
},
"clojure": {
"default": {
"enabled": true,
"formatter": "cljstyle"
},
"allOf": [
{
"$ref": "#/definitions/Lang_for_Clojure"
}
]
},
"cpp": {
"default": {
"enabled": true,
Expand Down Expand Up @@ -449,6 +460,10 @@
"type": "string",
"enum": ["clang-format"]
},
"Clojure": {
"type": "string",
"enum": ["cljstyle"]
},
"Cpp": {
"type": "string",
"enum": ["clang-format"]
Expand Down Expand Up @@ -545,6 +560,18 @@
}
}
},
"Lang_for_Clojure": {
"type": "object",
"required": ["enabled", "formatter"],
"properties": {
"enabled": {
"type": "boolean"
},
"formatter": {
"$ref": "#/definitions/MdsfFormatter_for_Clojure"
}
}
},
"Lang_for_Cpp": {
"type": "object",
"required": ["enabled", "formatter"],
Expand Down Expand Up @@ -1024,6 +1051,19 @@
}
]
},
"MdsfFormatter_for_Clojure": {
"anyOf": [
{
"$ref": "#/definitions/Clojure"
},
{
"type": "array",
"items": {
"$ref": "#/definitions/MdsfFormatter_for_Clojure"
}
}
]
},
"MdsfFormatter_for_Cpp": {
"anyOf": [
{
Expand Down
18 changes: 11 additions & 7 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use schemars::JsonSchema;

use crate::languages::{
blade::Blade, c::C, cpp::Cpp, crystal::Crystal, csharp::CSharp, css::Css, dart::Dart,
elixir::Elixir, elm::Elm, gleam::Gleam, go::Go, graphql::GraphQL, groovy::Groovy, html::Html,
java::Java, javascript::JavaScript, json::Json, just::Just, lua::Lua, markdown::Markdown,
nim::Nim, objective_c::ObjectiveC, ocaml::OCaml, perl::Perl, protobuf::Protobuf,
purescript::PureScript, python::Python, rescript::ReScript, roc::Roc, ruby::Ruby, rust::Rust,
shell::Shell, sql::Sql, toml::Toml, typescript::TypeScript, vue::Vue, xml::Xml, yaml::Yaml,
zig::Zig, Lang,
blade::Blade, c::C, clojure::Clojure, cpp::Cpp, crystal::Crystal, csharp::CSharp, css::Css,
dart::Dart, elixir::Elixir, elm::Elm, gleam::Gleam, go::Go, graphql::GraphQL, groovy::Groovy,
html::Html, java::Java, javascript::JavaScript, json::Json, just::Just, lua::Lua,
markdown::Markdown, nim::Nim, objective_c::ObjectiveC, ocaml::OCaml, perl::Perl,
protobuf::Protobuf, purescript::PureScript, python::Python, rescript::ReScript, roc::Roc,
ruby::Ruby, rust::Rust, shell::Shell, sql::Sql, toml::Toml, typescript::TypeScript, vue::Vue,
xml::Xml, yaml::Yaml, zig::Zig, Lang,
};

#[derive(Debug, serde::Serialize, serde::Deserialize, JsonSchema)]
Expand All @@ -23,6 +23,9 @@ pub struct MdsfConfig {
#[serde(default)]
pub c: Lang<C>,

#[serde(default)]
pub clojure: Lang<Clojure>,

#[serde(default)]
pub cpp: Lang<Cpp>,

Expand Down Expand Up @@ -143,6 +146,7 @@ impl Default for MdsfConfig {

blade: Lang::<Blade>::default(),
c: Lang::<C>::default(),
clojure: Lang::<Clojure>::default(),
cpp: Lang::<Cpp>::default(),
crystal: Lang::<Crystal>::default(),
csharp: Lang::<CSharp>::default(),
Expand Down
40 changes: 40 additions & 0 deletions src/formatters/buf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use super::execute_command;

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

cmd.arg("format").arg("--write").arg(snippet_path);

execute_command(&mut cmd, snippet_path)
}

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

use super::format_using_buf;

#[test_with::executable(buf)]
#[test]
fn it_should_format_protobuf() {
let input = "service SearchService {
rpc Search (SearchRequest) returns (SearchResponse);
}";

let expected_output = "service SearchService {
rpc Search(SearchRequest) returns (SearchResponse);
}
";

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

let output = format_using_buf(snippet.path())
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(expected_output, output);
}
}
69 changes: 69 additions & 0 deletions src/formatters/cljstyle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use super::execute_command;

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

cmd.arg("fix").arg(snippet_path);

execute_command(&mut cmd, snippet_path)
}

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

use super::format_using_cljstyle;

#[test_with::executable(cljstyle)]
#[test]
fn it_should_format_clojure() {
let input = "( ns
foo.bar.baz \"some doc\"
(:require (foo.bar [abc :as abc]
def))
(:use foo.bar.qux)
(:import foo.bar.qux.Foo
;; Need this for the thing
foo.bar.qux.Bar)
)

(defn hello \"says hi\" (
[] (hello \"world\")
) ([name]
( println \"Hello,\" name )
))";

let expected_output = "(ns foo.bar.baz
\"some doc\"
(:require
[foo.bar.abc :as abc]
[foo.bar.def]
[foo.bar.qux :refer :all])
(:import
(foo.bar.qux
;; Need this for the thing
Bar
Foo)))


(defn hello
\"says hi\"
([] (hello \"world\"))
([name]
(println \"Hello,\" name)))
";

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

let output = format_using_cljstyle(snippet.path())
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(output, expected_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 @@ pub mod black;
pub mod blade_formatter;
pub mod blue;
pub mod clang_format;
pub mod cljstyle;
pub mod crystal_format;
pub mod dart_format;
pub mod deno_fmt;
Expand Down Expand Up @@ -108,6 +109,7 @@ pub fn format_snippet(config: &MdsfConfig, language: &Language, code: &str) -> S
if let Ok(Some(formatted_code)) = match language {
Language::Blade => config.blade.format(snippet_path),
Language::C => config.c.format(snippet_path),
Language::Clojure => config.clojure.format(snippet_path),
Language::CSharp => config.csharp.format(snippet_path),
Language::Cpp => config.cpp.format(snippet_path),
Language::Crystal => config.crystal.format(snippet_path),
Expand Down
128 changes: 128 additions & 0 deletions src/languages/clojure.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
use schemars::JsonSchema;

use crate::formatters::{cljstyle::format_using_cljstyle, MdsfFormatter};

use super::{Lang, LanguageFormatter};

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

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

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

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

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

use super::Clojure;

const INPUT: &str = "( ns
foo.bar.baz \"some doc\"
(:require (foo.bar [abc :as abc]
def))
(:use foo.bar.qux)
(:import foo.bar.qux.Foo
;; Need this for the thing
foo.bar.qux.Bar)
)

(defn hello \"says hi\" (
[] (hello \"world\")
) ([name]
( println \"Hello,\" name )
))";

const EXTENSION: &str = crate::languages::Language::Clojure.to_file_ext();

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

#[test_with::executable(cljstyle)]
#[test]
fn test_cljstyle() {
let l = Lang::<Clojure> {
enabled: true,
formatter: MdsfFormatter::Single(Clojure::Cljstyle),
};

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 = "(ns foo.bar.baz
\"some doc\"
(:require
[foo.bar.abc :as abc]
[foo.bar.def]
[foo.bar.qux :refer :all])
(:import
(foo.bar.qux
;; Need this for the thing
Bar
Foo)))


(defn hello
\"says hi\"
([] (hello \"world\"))
([name]
(println \"Hello,\" name)))
";

assert_eq!(output, expected_output);
}
}
Loading
Loading