Skip to content

Commit

Permalink
feat(ruby): add support for rubyfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen committed Mar 21, 2024
1 parent a84abc9 commit c9d59b3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub mod purs_tidy;
pub mod rescript_format;
pub mod roc_format;
pub mod rubocop;
pub mod rubyfmt;
pub mod ruff;
pub mod rufo;
pub mod rustfmt;
Expand Down
43 changes: 43 additions & 0 deletions src/formatters/rubyfmt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use super::execute_command;

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

cmd.arg("-i").arg(snippet_path);

execute_command(&mut cmd, snippet_path)
}

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

#[test_with::executable(rubyfmt)]
#[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_rubyfmt(snippet.path())
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(expected_output, output);
}
}
9 changes: 8 additions & 1 deletion src/languages/ruby.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
use schemars::JsonSchema;

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

use super::{Lang, LanguageFormatter};

#[derive(Debug, Default, serde::Serialize, serde::Deserialize, JsonSchema)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum Ruby {
#[default]
#[serde(rename = "rubyfmt")]
RubyFmt,
#[serde(rename = "rubocop")]
RuboCop,
#[serde(rename = "rufo")]
Expand All @@ -30,6 +35,7 @@ impl Default for MdsfFormatter<Ruby> {
Self::Multiple(vec![Self::Multiple(vec![
Self::Single(Ruby::RuboCop),
Self::Single(Ruby::Rufo),
Self::Single(Ruby::RubyFmt),
])])
}
}
Expand All @@ -43,6 +49,7 @@ impl LanguageFormatter for Ruby {
match self {
Self::RuboCop => format_using_rubocop(snippet_path),
Self::Rufo => format_using_rufo(snippet_path),
Self::RubyFmt => format_using_rubyfmt(snippet_path),
}
}
}
Expand Down

0 comments on commit c9d59b3

Please sign in to comment.