Skip to content

Commit

Permalink
feat(csharp): add support for csharpier
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen committed Mar 23, 2024
1 parent 8e01e4e commit 6df8239
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down
4 changes: 2 additions & 2 deletions schemas/v0.0.2/mdsf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"csharp": {
"default": {
"enabled": true,
"formatter": "clang-format"
"formatter": [["csharpier", "clang-format"]]
},
"allOf": [
{
Expand Down Expand Up @@ -524,7 +524,7 @@
},
"CSharp": {
"type": "string",
"enum": ["clang-format"]
"enum": ["csharpier", "clang-format"]
},
"Clojure": {
"type": "string",
Expand Down
55 changes: 55 additions & 0 deletions src/formatters/csharpier.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use super::execute_command;

#[inline]
pub fn format_using_csharpier(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
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);
}
}
1 change: 1 addition & 0 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
52 changes: 46 additions & 6 deletions src/languages/csharp.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
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};

#[derive(Debug, Default, serde::Serialize, serde::Deserialize, JsonSchema)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum CSharp {
#[default]
#[serde(rename = "csharpier")]
CSharpier,
#[serde(rename = "clang-format")]
ClangFormat,
}
Expand All @@ -25,7 +29,10 @@ impl Default for Lang<CSharp> {
impl Default for MdsfFormatter<CSharp> {
#[inline]
fn default() -> Self {
Self::Single(CSharp::ClangFormat)
Self::Multiple(vec![Self::Multiple(vec![
Self::Single(CSharp::CSharpier),
Self::Single(CSharp::ClangFormat),
])])
}
}

Expand All @@ -36,6 +43,7 @@ impl LanguageFormatter for CSharp {
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
match self {
Self::CSharpier => format_using_csharpier(snippet_path),
Self::ClangFormat => format_using_clang_format(snippet_path),
}
}
Expand All @@ -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;
}
}
} ";
Expand Down Expand Up @@ -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::<CSharp> {
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);
}
}

0 comments on commit 6df8239

Please sign in to comment.