From 9a42056112df39dd66f18377f25ab68e6abe3575 Mon Sep 17 00:00:00 2001 From: Mads Hougesen Date: Thu, 21 Mar 2024 14:53:35 +0100 Subject: [PATCH] feat(ruby): add support for standardrb --- .github/workflows/validate.yml | 3 +++ src/formatters/mod.rs | 1 + src/formatters/standardrb.rs | 43 ++++++++++++++++++++++++++++++++++ src/languages/ruby.rs | 30 +++++++++++++++++++++++- 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 src/formatters/standardrb.rs diff --git a/.github/workflows/validate.yml b/.github/workflows/validate.yml index 47c042b7..e48e8dcb 100644 --- a/.github/workflows/validate.yml +++ b/.github/workflows/validate.yml @@ -203,5 +203,8 @@ jobs: - name: yamlfmt run: go install github.com/google/yamlfmt/cmd/yamlfmt@latest + - name: standardrb + run: gem install standardrb + - name: run tests run: cargo test diff --git a/src/formatters/mod.rs b/src/formatters/mod.rs index f52570fc..a20d6fd4 100644 --- a/src/formatters/mod.rs +++ b/src/formatters/mod.rs @@ -46,6 +46,7 @@ pub mod scalafmt; pub mod shfmt; pub mod sql_formatter; pub mod sqlfluff; +pub mod standardrb; pub mod stylua; pub mod swiftformat; pub mod taplo; diff --git a/src/formatters/standardrb.rs b/src/formatters/standardrb.rs new file mode 100644 index 00000000..2c28ee20 --- /dev/null +++ b/src/formatters/standardrb.rs @@ -0,0 +1,43 @@ +use super::execute_command; + +#[inline] +pub fn format_using_standardrb( + snippet_path: &std::path::Path, +) -> std::io::Result<(bool, Option)> { + let mut cmd = std::process::Command::new("standardrb"); + + cmd.arg("--fix").arg(snippet_path); + + execute_command(&mut cmd, snippet_path) +} + +#[cfg(test)] +mod test_standardrb { + use crate::{ + formatters::{setup_snippet, standardrb::format_using_standardrb}, + languages::Language, + }; + + #[test_with::executable(standardrb)] + #[test] + fn it_should_format_ruby() { + let input = "def add( a , b ) + return a + b + end"; + + let expected_output = "def add(a, b) + a + b +end +"; + + let snippet = setup_snippet(input, Language::Ruby.to_file_ext()) + .expect("it to create a snippet file"); + + let output = format_using_standardrb(snippet.path()) + .expect("it to be successful") + .1 + .expect("it to be some"); + + assert_eq!(expected_output, output); + } +} diff --git a/src/languages/ruby.rs b/src/languages/ruby.rs index c17c776b..1229f616 100644 --- a/src/languages/ruby.rs +++ b/src/languages/ruby.rs @@ -2,7 +2,7 @@ use schemars::JsonSchema; use crate::formatters::{ rubocop::format_using_rubocop, rubyfmt::format_using_rubyfmt, rufo::format_using_rufo, - MdsfFormatter, + standardrb::format_using_standardrb, MdsfFormatter, }; use super::{Lang, LanguageFormatter}; @@ -17,6 +17,8 @@ pub enum Ruby { RuboCop, #[serde(rename = "rufo")] Rufo, + #[serde(rename = "standardrb")] + Standardrb, } impl Default for Lang { @@ -36,6 +38,7 @@ impl Default for MdsfFormatter { Self::Single(Ruby::RuboCop), Self::Single(Ruby::Rufo), Self::Single(Ruby::RubyFmt), + Self::Single(Ruby::Standardrb), ])]) } } @@ -50,6 +53,7 @@ impl LanguageFormatter for Ruby { Self::RuboCop => format_using_rubocop(snippet_path), Self::Rufo => format_using_rufo(snippet_path), Self::RubyFmt => format_using_rubyfmt(snippet_path), + Self::Standardrb => format_using_standardrb(snippet_path), } } } @@ -136,4 +140,28 @@ end assert_eq!(output, expected_output); } + + #[test_with::executable(standardrb)] + #[test] + fn test_standardrb() { + let expected_output = "def add(a, b) + a + b +end +"; + + let l = Lang:: { + enabled: true, + formatter: MdsfFormatter::Single(Ruby::Standardrb), + }; + + 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); + } }