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(csharp): add support for csharpier #130

Merged
merged 3 commits into from
Mar 23, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,11 @@ jobs:
- name: standardrb
run: gem install standardrb

- name: fantomas
run: dotnet tool install -g fantomas

- name: csharpier
run: dotnet tool install -g csharpier

- name: run tests
run: cargo test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/target

.mdsf-cache
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);
}
}
32 changes: 18 additions & 14 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 Expand Up @@ -63,20 +64,23 @@ pub mod zigfmt;

#[inline]
pub fn setup_snippet(code: &str, file_ext: &str) -> std::io::Result<NamedTempFile> {
let _ = std::fs::create_dir_all(".mdsf-cache");

let mut f = tempfile::Builder::new()
.rand_bytes(12)
.suffix(file_ext)
.prefix(
// ktlint wants PascalCase file names
if file_ext == Language::Kotlin.to_file_ext() {
"MdsfFile"
} else {
"mdsf"
},
)
.tempfile_in(".mdsf-cache")?;
let mut b = tempfile::Builder::new();

b.rand_bytes(12).suffix(file_ext).prefix(
// ktlint wants PascalCase file names
if file_ext == Language::Kotlin.to_file_ext() {
"MdsfFile"
} else {
"mdsf"
},
);

let mut f = if file_ext == ".cs" {
let _ = std::fs::create_dir_all(".mdsf-cache");
b.tempfile_in(".mdsf-cache")
} else {
b.tempfile()
}?;

f.write_all(code.as_bytes())?;
f.flush()?;
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/usort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn format_using_usort(
}

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

use super::format_using_usort;
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);
}
}