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(ruff): add format and check command #305

Merged
merged 1 commit into from
Jun 16, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ mdsf init

<!-- START_SECTION:supported-languages -->

`mdsf` currently supports 131 tools.
`mdsf` currently supports 132 tools.

| Formatter | Description |
| ------------------ | ---------------------------------------------------------------------------------------------------------------------- |
Expand Down Expand Up @@ -205,7 +205,8 @@ mdsf init
| rstfmt | [https://github.com/dzhu/rstfmt](https://github.com/dzhu/rstfmt) |
| rubocop | [https://github.com/rubocop/rubocop](https://github.com/rubocop/rubocop) |
| rubyfmt | [https://github.com/fables-tales/rubyfmt](https://github.com/fables-tales/rubyfmt) |
| ruff | [https://docs.astral.sh/ruff/](https://docs.astral.sh/ruff/) |
| ruff | [https://docs.astral.sh/ruff/formatter/](https://docs.astral.sh/ruff/formatter/) |
| ruff_check | [https://docs.astral.sh/ruff/linter/](https://docs.astral.sh/ruff/linter/) |
| rufo | [https://github.com/ruby-formatter/rufo](https://github.com/ruby-formatter/rufo) |
| rustfmt | [https://github.com/rust-lang/rustfmt](https://github.com/rust-lang/rustfmt) |
| rustywind | [https://github.com/avencera/rustywind](https://github.com/avencera/rustywind) |
Expand Down
7 changes: 6 additions & 1 deletion schemas/v0.1.1/mdsf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -535,10 +535,15 @@
"enum": ["rubyfmt"]
},
{
"description": "https://docs.astral.sh/ruff/",
"description": "https://docs.astral.sh/ruff/formatter/",
"type": "string",
"enum": ["ruff"]
},
{
"description": "https://docs.astral.sh/ruff/linter/",
"type": "string",
"enum": ["ruff_check"]
},
{
"description": "https://github.com/ruby-formatter/rufo",
"type": "string",
Expand Down
10 changes: 8 additions & 2 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,10 +649,14 @@ pub enum Tooling {
#[serde(rename = "rubyfmt")]
RubyFmt,

#[doc = "https://docs.astral.sh/ruff/"]
#[doc = "https://docs.astral.sh/ruff/formatter/"]
#[serde(rename = "ruff")]
Ruff,

#[doc = "https://docs.astral.sh/ruff/linter/"]
#[serde(rename = "ruff_check")]
RuffCheck,

#[doc = "https://github.com/ruby-formatter/rufo"]
#[serde(rename = "rufo")]
Rufo,
Expand Down Expand Up @@ -893,7 +897,8 @@ impl Tooling {
Self::RstFmt => rstfmt::run(snippet_path),
Self::RuboCop => rubocop::run(snippet_path),
Self::RubyFmt => rubyfmt::run(snippet_path),
Self::Ruff => ruff::run(snippet_path),
Self::Ruff => ruff::run_format(snippet_path),
Self::RuffCheck => ruff::run_check(snippet_path),
Self::Rufo => rufo::run(snippet_path),
Self::RustFmt => rustfmt::run(snippet_path),
Self::Rustywind => rustywind::run(snippet_path),
Expand Down Expand Up @@ -1034,6 +1039,7 @@ impl core::fmt::Display for Tooling {
Self::RuboCop => write!(f, "rubocop"),
Self::RubyFmt => write!(f, "rubyfmt"),
Self::Ruff => write!(f, "ruff"),
Self::RuffCheck => write!(f, "ruff_check"),
Self::Rufo => write!(f, "rufo"),
Self::RustFmt => write!(f, "rustfmt"),
Self::Rustywind => write!(f, "rustywind"),
Expand Down
22 changes: 16 additions & 6 deletions src/formatters/ruff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@ use super::execute_command;
use crate::error::MdsfError;

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

cmd.arg("format");
cmd.arg("--quiet");
cmd.arg(snippet_path);
cmd.arg("format").arg("--quiet").arg(snippet_path);

execute_command(&mut cmd, snippet_path)
}

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

cmd.arg("check")
.arg("--fix")
.arg("--quiet")
.arg(snippet_path);

execute_command(&mut cmd, snippet_path)
}

#[cfg(test)]
mod test_ruff {
use super::run;
use super::run_format;
use crate::{formatters::setup_snippet, generated::language_to_ext};

#[test_with::executable(ruff)]
Expand All @@ -26,7 +36,7 @@ mod test_ruff {
let snippet =
setup_snippet(input, &language_to_ext("python")).expect("it to create a snippet file");

let output = run(snippet.path())
let output = run_format(snippet.path())
.expect("it to be successful")
.1
.expect("it to be some");
Expand Down
Loading