Skip to content

Commit

Permalink
feat(ruby): add support for rufo (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen authored Mar 21, 2024
1 parent 93a28d6 commit b5fe318
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -197,5 +197,8 @@ jobs:
- name: ktlint
run: curl -sSLO https://github.com/pinterest/ktlint/releases/download/1.2.1/ktlint && chmod a+x ktlint && sudo mv ktlint /usr/local/bin/

- name: rufo
run: gem install rufo

- name: run tests
run: cargo test
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ mdsf init
| Python | `autopep8`, `black`, `blue`, `isort`, `ruff`, `usort`, `yapf` |
| ReScript | `rescript_format` |
| Roc | `roc_format` |
| Ruby | `rubocop` |
| Ruby | `rubocop`, `rufo` |
| Rust | `rustfmt` |
| Scala | `scalafmt` |
| Shell | `beautysh`, `shfmt` |
Expand Down
6 changes: 3 additions & 3 deletions schemas/v0.0.2/mdsf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
"java": {
"default": {
"enabled": true,
"formatter": ["google-java-format", "clang-format"]
"formatter": [["google-java-format", "clang-format"]]
},
"allOf": [
{
Expand Down Expand Up @@ -361,7 +361,7 @@
"ruby": {
"default": {
"enabled": true,
"formatter": "rubocop"
"formatter": [["rubocop", "rufo"]]
},
"allOf": [
{
Expand Down Expand Up @@ -1686,7 +1686,7 @@
},
"Ruby": {
"type": "string",
"enum": ["rubocop"]
"enum": ["rubocop", "rufo"]
},
"Rust": {
"type": "string",
Expand Down
23 changes: 13 additions & 10 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub mod rescript_format;
pub mod roc_format;
pub mod rubocop;
pub mod ruff;
pub mod rufo;
pub mod rustfmt;
pub mod scalafmt;
pub mod shfmt;
Expand All @@ -52,16 +53,18 @@ pub mod zigfmt;

#[inline]
pub fn setup_snippet(code: &str, file_ext: &str) -> std::io::Result<NamedTempFile> {
let mut b = tempfile::Builder::new();

b.rand_bytes(12).suffix(file_ext);

// ktlint wants PascalCase file names
if file_ext == Language::Kotlin.to_file_ext() {
b.prefix("MdsfFile");
}

let mut f = b.tempfile()?;
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()?;

f.write_all(code.as_bytes())?;
f.flush()?;
Expand Down
43 changes: 43 additions & 0 deletions src/formatters/rufo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use super::execute_command;

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

cmd.arg("--simple-exit").arg(snippet_path);

execute_command(&mut cmd, snippet_path)
}

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

#[test_with::executable(rufo)]
#[test]
fn it_should_format_ruby() {
let input = "def add( a , b )
return a + b
end";

let expected_output = "def add(a, b)
return a + b
end
";

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

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

assert_eq!(expected_output, output);
}
}
4 changes: 2 additions & 2 deletions src/languages/java.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ impl Default for Lang<Java> {
impl Default for MdsfFormatter<Java> {
#[inline]
fn default() -> Self {
Self::Multiple(vec![
Self::Multiple(vec![Self::Multiple(vec![
Self::Single(Java::GoogleJavaFormat),
Self::Single(Java::ClangFormat),
])
])])
}
}

Expand Down
34 changes: 32 additions & 2 deletions src/languages/ruby.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use schemars::JsonSchema;

use crate::formatters::{rubocop::format_using_rubocop, MdsfFormatter};
use crate::formatters::{rubocop::format_using_rubocop, rufo::format_using_rufo, MdsfFormatter};

use super::{Lang, LanguageFormatter};

Expand All @@ -10,6 +10,8 @@ pub enum Ruby {
#[default]
#[serde(rename = "rubocop")]
RuboCop,
#[serde(rename = "rufo")]
Rufo,
}

impl Default for Lang<Ruby> {
Expand All @@ -25,7 +27,10 @@ impl Default for Lang<Ruby> {
impl Default for MdsfFormatter<Ruby> {
#[inline]
fn default() -> Self {
Self::Single(Ruby::RuboCop)
Self::Multiple(vec![Self::Multiple(vec![
Self::Single(Ruby::RuboCop),
Self::Single(Ruby::Rufo),
])])
}
}

Expand All @@ -37,6 +42,7 @@ impl LanguageFormatter for Ruby {
) -> std::io::Result<(bool, Option<String>)> {
match self {
Self::RuboCop => format_using_rubocop(snippet_path),
Self::Rufo => format_using_rufo(snippet_path),
}
}
}
Expand Down Expand Up @@ -99,4 +105,28 @@ end

assert_eq!(output, expected_output);
}

#[test_with::executable(rufo)]
#[test]
fn test_rufo() {
let expected_output = "def add(a, b)
return a + b
end
";

let l = Lang::<Ruby> {
enabled: true,
formatter: MdsfFormatter::Single(Ruby::Rufo),
};

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");

assert_eq!(output, expected_output);
}
}

0 comments on commit b5fe318

Please sign in to comment.