diff --git a/.github/workflows/validate.yml b/.github/workflows/validate.yml index 0424122d..0233a7fc 100644 --- a/.github/workflows/validate.yml +++ b/.github/workflows/validate.yml @@ -50,7 +50,7 @@ jobs: otp-version: "26" gleam-version: "1.0.0" elixir-version: "1.16.1" - # ruff + # ruff, sqlfluff, black, blue, yapf, autopep8 - uses: actions/setup-python@v5 with: cache: "pip" @@ -82,14 +82,10 @@ jobs: run: taplo --version - name: Install ruff - run: pip install ruff - - name: Validate ruff - run: ruff --version + run: pip install ruff && ruff --version - name: Install sqlfluff - run: pip install sqlfluff - - name: Validate sqlfluff - run: sqlfluff format --version + run: pip install sqlfluff && sqlfluff format --version - name: Validate biome run: npx --yes @biomejs/biome --version @@ -110,26 +106,24 @@ jobs: run: mix help format - name: Install rubocop - run: gem install rubocop - - name: Validate rubocop - run: rubocop --version + run: gem install rubocop && rubocop --version - name: Install stylua - run: cargo install stylua - - name: Validate stylua - run: stylua --version + run: cargo install stylua && stylua --version - name: Install shfmt - run: go install mvdan.cc/sh/v3/cmd/shfmt@latest - - name: Validate shfmt - run: shfmt --version + run: go install mvdan.cc/sh/v3/cmd/shfmt@latest && shfmt --version - name: Install gofumpt - run: go install mvdan.cc/gofumpt@latest - - name: Validate gofumpt - run: gofumpt --version + run: go install mvdan.cc/gofumpt@latest && gofumpt --version - name: Install black run: pip install black && black --version + - name: Install blue + run: pip install blue && blue --version + + - name: Install yapf + run: pip install yapf && yapf --version + - run: cargo test diff --git a/README.md b/README.md index 02d03f67..37827670 100644 --- a/README.md +++ b/README.md @@ -45,25 +45,25 @@ mdsf init > > Only formatters that are already installed will be used. -| Language | Formatters | -| ---------- | --------------------------- | -| CSS | `prettier` | -| Dart | `dart_format` | -| Elixir | `mix_format` | -| Gleam | `gleam_format` | -| Go | `gofmt`, `gofumpt` | -| HTML | `prettier` | -| JSON | `prettier`, `biome` | -| JavaScript | `prettier`, `biome` | -| Lua | `stylua` | -| Nim | `nimpretty` | -| Python | `ruff`, `black` | -| Ruby | `rubocop` | -| Rust | `rustfmt` | -| SQL | `sqlfluff`, `sql-formatter` | -| Shell | `shfmt` | -| TOML | `taplo` | -| TypeScript | `prettier`, `biome` | -| Vue | `prettier` | -| YAML | `prettier` | -| Zig | `zigfmt` | +| Language | Formatters | +| ---------- | ------------------------------------------- | +| CSS | `prettier` | +| Dart | `dart_format` | +| Elixir | `mix_format` | +| Gleam | `gleam_format` | +| Go | `gofmt`, `gofumpt` | +| HTML | `prettier` | +| JSON | `prettier`, `biome` | +| JavaScript | `prettier`, `biome` | +| Lua | `stylua` | +| Nim | `nimpretty` | +| Python | `ruff`, `black`, `blue`, `yapf`, `autopep8` | +| Ruby | `rubocop` | +| Rust | `rustfmt` | +| SQL | `sqlfluff`, `sql-formatter` | +| Shell | `shfmt` | +| TOML | `taplo` | +| TypeScript | `prettier`, `biome` | +| Vue | `prettier` | +| YAML | `prettier` | +| Zig | `zigfmt` | diff --git a/schemas/v0.0.0/mdsf.schema.json b/schemas/v0.0.0/mdsf.schema.json index 13e0a7cc..3b6aeae3 100644 --- a/schemas/v0.0.0/mdsf.schema.json +++ b/schemas/v0.0.0/mdsf.schema.json @@ -486,7 +486,7 @@ }, "PythonFormatter": { "type": "string", - "enum": ["ruff", "black"] + "enum": ["ruff", "black", "yapf", "blue", "autopep8"] }, "Ruby": { "type": "object", diff --git a/src/formatters/autopep8.rs b/src/formatters/autopep8.rs new file mode 100644 index 00000000..f065a188 --- /dev/null +++ b/src/formatters/autopep8.rs @@ -0,0 +1,36 @@ +use super::execute_command; + +#[inline] +pub fn format_using_autopep8( + snippet_path: &std::path::Path, +) -> std::io::Result<(bool, Option)> { + let mut cmd = std::process::Command::new("autopep8"); + + cmd.arg("--in-place").arg(snippet_path); + + execute_command(&mut cmd, snippet_path) +} + +#[cfg(test)] +mod test_autopep8 { + use crate::{formatters::setup_snippet, languages::Language}; + + use super::format_using_autopep8; + + #[test] + fn it_should_format_python() { + let input = "def add( a: int , b:int)->int: return a+b"; + + let expected_output = "def add(a: int, b: int) -> int: return a+b\n"; + + let snippet = setup_snippet(input, Language::Python.to_file_ext()) + .expect("it to create a snippet file"); + + let output = format_using_autopep8(snippet.path()) + .expect("it to be successful") + .1 + .expect("it to be some"); + + assert_eq!(expected_output, output); + } +} diff --git a/src/formatters/blue.rs b/src/formatters/blue.rs new file mode 100644 index 00000000..2eb161e4 --- /dev/null +++ b/src/formatters/blue.rs @@ -0,0 +1,36 @@ +use super::execute_command; + +#[inline] +pub fn format_using_blue( + snippet_path: &std::path::Path, +) -> std::io::Result<(bool, Option)> { + let mut cmd = std::process::Command::new("blue"); + + cmd.arg("--quiet").arg(snippet_path); + + execute_command(&mut cmd, snippet_path) +} + +#[cfg(test)] +mod test_blue { + use crate::{formatters::setup_snippet, languages::Language}; + + use super::format_using_blue; + + #[test] + fn it_should_format_python() { + let input = "def add( a: int , b:int)->int: return a+b"; + + let expected_output = "def add(a: int, b: int) -> int:\n return a + b\n"; + + let snippet = setup_snippet(input, Language::Python.to_file_ext()) + .expect("it to create a snippet file"); + + let output = format_using_blue(snippet.path()) + .expect("it to be successful") + .1 + .expect("it to be some"); + + assert_eq!(expected_output, output); + } +} diff --git a/src/formatters/mod.rs b/src/formatters/mod.rs index 060f32e2..d3c7b89d 100644 --- a/src/formatters/mod.rs +++ b/src/formatters/mod.rs @@ -10,8 +10,10 @@ use crate::{ languages::{Language, LanguageFormatter}, }; +pub mod autopep8; pub mod biome; pub mod black; +pub mod blue; pub mod dart_format; pub mod gleam_format; pub mod gofmt; @@ -27,6 +29,7 @@ pub mod sql_formatter; pub mod sqlfluff; pub mod stylua; pub mod taplo; +pub mod yapf; pub mod zigfmt; #[inline] diff --git a/src/formatters/yapf.rs b/src/formatters/yapf.rs new file mode 100644 index 00000000..93bd3777 --- /dev/null +++ b/src/formatters/yapf.rs @@ -0,0 +1,36 @@ +use super::execute_command; + +#[inline] +pub fn format_using_yapf( + snippet_path: &std::path::Path, +) -> std::io::Result<(bool, Option)> { + let mut cmd = std::process::Command::new("yapf"); + + cmd.arg("--in-place").arg(snippet_path); + + execute_command(&mut cmd, snippet_path) +} + +#[cfg(test)] +mod test_yapf { + use crate::{formatters::setup_snippet, languages::Language}; + + use super::format_using_yapf; + + #[test] + fn it_should_format_python() { + let input = "def add( a: int , b:int)->int: return a+b"; + + let expected_output = "def add(a: int, b: int) -> int:\n return a + b\n"; + + let snippet = setup_snippet(input, Language::Python.to_file_ext()) + .expect("it to create a snippet file"); + + let output = format_using_yapf(snippet.path()) + .expect("it to be successful") + .1 + .expect("it to be some"); + + assert_eq!(expected_output, output); + } +} diff --git a/src/languages/python.rs b/src/languages/python.rs index 62680364..7f604b48 100644 --- a/src/languages/python.rs +++ b/src/languages/python.rs @@ -2,7 +2,10 @@ use schemars::JsonSchema; use crate::{ config::default_enabled, - formatters::{black::format_using_black, ruff::format_using_ruff}, + formatters::{ + autopep8::format_using_autopep8, black::format_using_black, blue::format_using_blue, + ruff::format_using_ruff, yapf::format_using_yapf, + }, }; use super::LanguageFormatter; @@ -14,6 +17,12 @@ pub enum PythonFormatter { Ruff, #[serde(rename = "black")] Black, + #[serde(rename = "yapf")] + Yapf, + #[serde(rename = "blue")] + Blue, + #[serde(rename = "autopep8")] + Autopep8, } #[derive(Debug, serde::Serialize, serde::Deserialize, JsonSchema)] @@ -42,8 +51,11 @@ impl LanguageFormatter for Python { } match self.formatter { - PythonFormatter::Ruff => format_using_ruff(snippet_path).map(|res| res.1), + PythonFormatter::Autopep8 => format_using_autopep8(snippet_path).map(|res| res.1), PythonFormatter::Black => format_using_black(snippet_path).map(|res| res.1), + PythonFormatter::Blue => format_using_blue(snippet_path).map(|res| res.1), + PythonFormatter::Ruff => format_using_ruff(snippet_path).map(|res| res.1), + PythonFormatter::Yapf => format_using_yapf(snippet_path).map(|res| res.1), } } }