Skip to content

Commit

Permalink
feat(python): add support for blue, yapf and autopep8
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen committed Mar 9, 2024
1 parent 8e13a91 commit b510c61
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 44 deletions.
32 changes: 13 additions & 19 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
44 changes: 22 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
2 changes: 1 addition & 1 deletion schemas/v0.0.0/mdsf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@
},
"PythonFormatter": {
"type": "string",
"enum": ["ruff", "black"]
"enum": ["ruff", "black", "yapf", "blue", "autopep8"]
},
"Ruby": {
"type": "object",
Expand Down
36 changes: 36 additions & 0 deletions src/formatters/autopep8.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use super::execute_command;

#[inline]
pub fn format_using_autopep8(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
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);
}
}
36 changes: 36 additions & 0 deletions src/formatters/blue.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use super::execute_command;

#[inline]
pub fn format_using_blue(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
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);
}
}
3 changes: 3 additions & 0 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,6 +29,7 @@ pub mod sql_formatter;
pub mod sqlfluff;
pub mod stylua;
pub mod taplo;
pub mod yapf;
pub mod zigfmt;

#[inline]
Expand Down
36 changes: 36 additions & 0 deletions src/formatters/yapf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use super::execute_command;

#[inline]
pub fn format_using_yapf(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
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);
}
}
16 changes: 14 additions & 2 deletions src/languages/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)]
Expand Down Expand Up @@ -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),
}
}
}

0 comments on commit b510c61

Please sign in to comment.