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

chore(python): support pyink #225

Merged
merged 1 commit into from
May 29, 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
3 changes: 3 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ jobs:
- name: black
run: pipx install black && black --version

- name: pyink
run: pipx install pyink && pyink --version

- name: blue
run: pipx install blue && blue --version

Expand Down
120 changes: 60 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,66 +60,66 @@ mdsf init

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

| Language | Formatters |
| ---------------- | ------------------------------------------------------------- |
| Blade | `blade-formatter` |
| C | `clang-format` |
| CSharp | `clang-format`, `csharpier` |
| Cabal | `cabal_format` |
| Clojure | `cljstyle`, `joker` |
| Cpp | `clang-format` |
| Crystal | `crystal_format` |
| Css | `prettier`, `stylelint` |
| D | `dfmt` |
| Dart | `dart_format` |
| Elixir | `mix_format` |
| Elm | `elm-format` |
| Erb | `erb-formatter` |
| Erlang | `efmt`, `erlfmt` |
| FSharp | `fantomas` |
| Fortran | `fprettify` |
| Gleam | `gleam_format` |
| Go | `crlfmt`, `gofmt`, `gofumpt`, `goimports` |
| GraphQL | `prettier` |
| Groovy | `npm-groovy-lint` |
| Handlebars | `djlint`, `prettier` |
| Haskell | `fourmolu`, `hindent`, `ormolu`, `stylish-haskell` |
| Hcl | `terraform_fmt`, `tofu_fmt` |
| Html | `djlint`, `prettier` |
| Java | `clang-format`, `google-java-format` |
| JavaScript | `biome`, `clang-format`, `deno_fmt`, `prettier`, `standardjs` |
| Json | `biome`, `clang-format`, `deno_fmt`, `prettier` |
| Julia | `juliaformatter.jl` |
| Just | `just_fmt` |
| Kcl | `kcl_fmt` |
| Kotlin | `ktfmt`, `ktlint` |
| Lua | `luaformatter`, `stylua` |
| Markdown | `mdformat`, `prettier` |
| Mustache | `djlint` |
| Nim | `nimpretty` |
| Nix | `alejandra`, `nixfmt`, `nixpkgs-fmt` |
| Nunjucks | `djlint` |
| OCaml | `ocamlformat`, `ocp-indent` |
| ObjectiveC | `clang-format` |
| Perl | `perltidy` |
| Protobuf | `buf`, `clang-format` |
| PureScript | `purs-tidy` |
| Python | `autopep8`, `black`, `blue`, `isort`, `ruff`, `usort`, `yapf` |
| ReScript | `rescript_format` |
| ReStructuredText | `rstfmt` |
| Roc | `roc_format` |
| Ruby | `rubocop`, `rubyfmt`, `rufo`, `standardrb` |
| Rust | `leptosfmt`, `rustfmt`, `yew-fmt` |
| Scala | `scalafmt` |
| Shell | `beautysh`, `shfmt` |
| Sql | `sql-formatter`, `sqlfluff` |
| Swift | `swift-format`, `swiftformat` |
| Toml | `taplo` |
| TypeScript | `biome`, `deno_fmt`, `prettier` |
| Vue | `biome`, `prettier` |
| Xml | `xmlformat`, `xmllint` |
| Yaml | `prettier`, `yamlfix`, `yamlfmt` |
| Zig | `zigfmt` |
| Language | Formatters |
| ---------------- | ---------------------------------------------------------------------- |
| Blade | `blade-formatter` |
| C | `clang-format` |
| CSharp | `clang-format`, `csharpier` |
| Cabal | `cabal_format` |
| Clojure | `cljstyle`, `joker` |
| Cpp | `clang-format` |
| Crystal | `crystal_format` |
| Css | `prettier`, `stylelint` |
| D | `dfmt` |
| Dart | `dart_format` |
| Elixir | `mix_format` |
| Elm | `elm-format` |
| Erb | `erb-formatter` |
| Erlang | `efmt`, `erlfmt` |
| FSharp | `fantomas` |
| Fortran | `fprettify` |
| Gleam | `gleam_format` |
| Go | `crlfmt`, `gofmt`, `gofumpt`, `goimports` |
| GraphQL | `prettier` |
| Groovy | `npm-groovy-lint` |
| Handlebars | `djlint`, `prettier` |
| Haskell | `fourmolu`, `hindent`, `ormolu`, `stylish-haskell` |
| Hcl | `terraform_fmt`, `tofu_fmt` |
| Html | `djlint`, `prettier` |
| Java | `clang-format`, `google-java-format` |
| JavaScript | `biome`, `clang-format`, `deno_fmt`, `prettier`, `standardjs` |
| Json | `biome`, `clang-format`, `deno_fmt`, `prettier` |
| Julia | `juliaformatter.jl` |
| Just | `just_fmt` |
| Kcl | `kcl_fmt` |
| Kotlin | `ktfmt`, `ktlint` |
| Lua | `luaformatter`, `stylua` |
| Markdown | `mdformat`, `prettier` |
| Mustache | `djlint` |
| Nim | `nimpretty` |
| Nix | `alejandra`, `nixfmt`, `nixpkgs-fmt` |
| Nunjucks | `djlint` |
| OCaml | `ocamlformat`, `ocp-indent` |
| ObjectiveC | `clang-format` |
| Perl | `perltidy` |
| Protobuf | `buf`, `clang-format` |
| PureScript | `purs-tidy` |
| Python | `autopep8`, `black`, `blue`, `isort`, `pyink`, `ruff`, `usort`, `yapf` |
| ReScript | `rescript_format` |
| ReStructuredText | `rstfmt` |
| Roc | `roc_format` |
| Ruby | `rubocop`, `rubyfmt`, `rufo`, `standardrb` |
| Rust | `leptosfmt`, `rustfmt`, `yew-fmt` |
| Scala | `scalafmt` |
| Shell | `beautysh`, `shfmt` |
| Sql | `sql-formatter`, `sqlfluff` |
| Swift | `swift-format`, `swiftformat` |
| Toml | `taplo` |
| TypeScript | `biome`, `deno_fmt`, `prettier` |
| Vue | `biome`, `prettier` |
| Xml | `xmlformat`, `xmllint` |
| Yaml | `prettier`, `yamlfix`, `yamlfmt` |
| Zig | `zigfmt` |

<!-- END_SECTION:supported-languages -->

Expand Down
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ update-readme:
cargo run -- schema
cargo run -- init
node scripts/update-supported-languages.mjs
npx --yes prettier --write .
npx --yes prettier --cache --write .
git restore tests/

precommit:
Expand Down
2 changes: 1 addition & 1 deletion mdsf.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
"enabled": true,
"formatter": [
["usort", "isort"],
["ruff", "blue", "black", "yapf", "autopep8"]
["ruff", "blue", "black", "yapf", "autopep8", "pyink"]
]
},
"rescript": {
Expand Down
13 changes: 11 additions & 2 deletions schemas/v0.0.6/mdsf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@
"enabled": true,
"formatter": [
["usort", "isort"],
["ruff", "blue", "black", "yapf", "autopep8"]
["ruff", "blue", "black", "yapf", "autopep8", "pyink"]
]
},
"allOf": [
Expand Down Expand Up @@ -2349,7 +2349,16 @@
},
"Python": {
"type": "string",
"enum": ["ruff", "black", "yapf", "blue", "autopep8", "isort", "usort"]
"enum": [
"ruff",
"black",
"yapf",
"blue",
"autopep8",
"isort",
"usort",
"pyink"
]
},
"ReScript": {
"type": "string",
Expand Down
1 change: 1 addition & 0 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub mod ormolu;
pub mod perltidy;
pub mod prettier;
pub mod purs_tidy;
pub mod pyink;
pub mod rescript_format;
pub mod roc_format;
pub mod rstfmt;
Expand Down
37 changes: 37 additions & 0 deletions src/formatters/pyink.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use super::execute_command;
use crate::error::MdsfError;

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

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

execute_command(&mut cmd, snippet_path)
}

#[cfg(test)]
mod test_pyink {
use super::format_using_pyink;
use crate::{formatters::setup_snippet, languages::Language};

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

assert_eq!(expected_output, output);
}
}
30 changes: 28 additions & 2 deletions src/languages/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::{
error::MdsfError,
formatters::{
autopep8::format_using_autopep8, black::format_using_black, blue::format_using_blue,
isort::format_using_isort, ruff::format_using_ruff, usort::format_using_usort,
yapf::format_using_yapf, MdsfFormatter,
isort::format_using_isort, pyink::format_using_pyink, ruff::format_using_ruff,
usort::format_using_usort, yapf::format_using_yapf, MdsfFormatter,
},
};

Expand All @@ -28,6 +28,8 @@ pub enum Python {
Isort,
#[serde(rename = "usort")]
Usort,
#[serde(rename = "pyink")]
PyInk,
}

impl LanguageFormatter for Python {
Expand All @@ -44,6 +46,7 @@ impl LanguageFormatter for Python {
Self::Yapf => format_using_yapf(snippet_path),
Self::Isort => format_using_isort(snippet_path),
Self::Usort => format_using_usort(snippet_path),
Self::PyInk => format_using_pyink(snippet_path),
}
}
}
Expand All @@ -62,6 +65,7 @@ impl Default for MdsfFormatter<Python> {
Self::Single(Python::Black),
Self::Single(Python::Yapf),
Self::Single(Python::Autopep8),
Self::Single(Python::PyInk),
]),
])
}
Expand All @@ -88,6 +92,7 @@ impl core::fmt::Display for Python {
Self::Autopep8 => write!(f, "autopep8"),
Self::Isort => write!(f, "isort"),
Self::Usort => write!(f, "usort"),
Self::PyInk => write!(f, "pyink"),
}
}
}
Expand Down Expand Up @@ -306,4 +311,25 @@ def add(a: int, b: int) -> int:

assert_eq!(output, expected_output);
}

#[test_with::executable(pyink)]
#[test]
fn test_pyink() {
let expected_output = "def add(a: int, b: int) -> int:\n return a + b\n";

let l = Lang::<Python> {
enabled: true,
formatter: MdsfFormatter::Single(Python::PyInk),
};

let snippet = setup_snippet(INPUT, EXTENSION).expect("it to save the file");
let snippet_path = snippet.path();

let output = l
.format(snippet_path, &LineInfo::fake())
.expect("it to not fail")
.expect("it to be a snippet");

assert_eq!(output, expected_output);
}
}
Loading