diff --git a/README.md b/README.md index 5c4d9325..b16a517f 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ mdsf init | ---------- | ------------------------------------------------------------- | | Blade | `blade-formatter` | | C | `clang-format` | -| CSharp | `clang-format` | +| CSharp | `clang-format`, `csharpier` | | Clojure | `cljstyle` | | Cpp | `clang-format` | | Crystal | `crystal_format` | diff --git a/schemas/v0.0.2/mdsf.schema.json b/schemas/v0.0.2/mdsf.schema.json index 26140222..9a9a322a 100644 --- a/schemas/v0.0.2/mdsf.schema.json +++ b/schemas/v0.0.2/mdsf.schema.json @@ -61,7 +61,7 @@ "csharp": { "default": { "enabled": true, - "formatter": "clang-format" + "formatter": [["csharpier", "clang-format"]] }, "allOf": [ { @@ -524,7 +524,7 @@ }, "CSharp": { "type": "string", - "enum": ["clang-format"] + "enum": ["csharpier", "clang-format"] }, "Clojure": { "type": "string", diff --git a/src/formatters/csharpier.rs b/src/formatters/csharpier.rs new file mode 100644 index 00000000..da191189 --- /dev/null +++ b/src/formatters/csharpier.rs @@ -0,0 +1,55 @@ +use super::execute_command; + +#[inline] +pub fn format_using_csharpier( + snippet_path: &std::path::Path, +) -> std::io::Result<(bool, Option)> { + let mut cmd = std::process::Command::new("dotnet"); + + cmd.arg("csharpier").arg(snippet_path); + + execute_command(&mut cmd, snippet_path) +} + +#[cfg(test)] +mod test_csharpier { + use crate::{formatters::setup_snippet, languages::Language}; + + use super::format_using_csharpier; + + #[test_with::executable(dotnet)] + #[test] + fn it_should_format_csharp() { + let input = "namespace Mdsf { + class Adder { + public static int add(int a,int b) { + var c=a+b ; + return c ; + } + } + } "; + + let expected_output = "namespace Mdsf +{ + class Adder + { + public static int add(int a, int b) + { + var c = a + b; + return c; + } + } +} +"; + + let snippet = setup_snippet(input, Language::CSharp.to_file_ext()) + .expect("it to create a snippet file"); + + let output = format_using_csharpier(snippet.path()) + .expect("it to be successful") + .1 + .expect("it to be some"); + + assert_eq!(output, expected_output); + } +} diff --git a/src/formatters/mod.rs b/src/formatters/mod.rs index a2111d82..cc27a170 100644 --- a/src/formatters/mod.rs +++ b/src/formatters/mod.rs @@ -14,6 +14,7 @@ pub mod blue; pub mod clang_format; pub mod cljstyle; pub mod crystal_format; +pub mod csharpier; pub mod dart_format; pub mod deno_fmt; pub mod efmt; diff --git a/src/languages/csharp.rs b/src/languages/csharp.rs index 956b4d04..2ce1cf23 100644 --- a/src/languages/csharp.rs +++ b/src/languages/csharp.rs @@ -1,6 +1,8 @@ use schemars::JsonSchema; -use crate::formatters::{clang_format::format_using_clang_format, MdsfFormatter}; +use crate::formatters::{ + clang_format::format_using_clang_format, csharpier::format_using_csharpier, MdsfFormatter, +}; use super::{Lang, LanguageFormatter}; @@ -8,6 +10,8 @@ use super::{Lang, LanguageFormatter}; #[cfg_attr(test, derive(PartialEq, Eq))] pub enum CSharp { #[default] + #[serde(rename = "csharpier")] + CSharpier, #[serde(rename = "clang-format")] ClangFormat, } @@ -25,7 +29,10 @@ impl Default for Lang { impl Default for MdsfFormatter { #[inline] fn default() -> Self { - Self::Single(CSharp::ClangFormat) + Self::Multiple(vec![Self::Multiple(vec![ + Self::Single(CSharp::CSharpier), + Self::Single(CSharp::ClangFormat), + ])]) } } @@ -36,6 +43,7 @@ impl LanguageFormatter for CSharp { snippet_path: &std::path::Path, ) -> std::io::Result<(bool, Option)> { match self { + Self::CSharpier => format_using_csharpier(snippet_path), Self::ClangFormat => format_using_clang_format(snippet_path), } } @@ -53,8 +61,8 @@ mod test_csharp { const INPUT: &str = "namespace Mdsf { class Adder { public static int add(int a,int b) { - a-b ; - return a + b; + var c = a+b ; + return c; } } } "; @@ -99,12 +107,44 @@ mod test_csharp { let expected_output = "namespace Mdsf { class Adder { public static int add(int a, int b) { - a - b; - return a + b; + var c = a + b; + return c; } } }"; assert_eq!(output, expected_output); } + + #[test_with::executable(dotnet)] + #[test] + fn test_csharpier() { + let l = Lang:: { + enabled: true, + formatter: MdsfFormatter::Single(CSharp::CSharpier), + }; + + 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 = "namespace Mdsf +{ + class Adder + { + public static int add(int a, int b) + { + var c = a + b; + return c; + } + } +} +"; + + assert_eq!(output, expected_output); + } }