Skip to content

Commit

Permalink
feat(python): add support for isort (#67)
Browse files Browse the repository at this point in the history
Closes #63
  • Loading branch information
hougesen authored Mar 13, 2024
1 parent e5ada18 commit 88967d2
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,6 @@ jobs:
- name: Install clang-format
run: pip install clang-format && clang-format --version

- run: pip install isort

- run: cargo test
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ build-local:

lint:
cargo fmt -- --check --color always
cargo clippy --all-targets --all-features -- -D warnings
cargo clippy --all-targets --all-features

lint-aggressive:
cargo clean
Expand All @@ -29,9 +29,9 @@ precommit:
make build
make lint
make test
cargo run --bin init-schema
cargo run -- schema
npx --yes prettier --write .
git restore tests/markdown.md
git restore tests/
typos .


Expand Down
2 changes: 1 addition & 1 deletion schemas/v0.0.1/mdsf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@
},
"PythonFormatter": {
"type": "string",
"enum": ["ruff", "black", "yapf", "blue", "autopep8"]
"enum": ["ruff", "black", "yapf", "blue", "autopep8", "isort"]
},
"Roc": {
"type": "object",
Expand Down
52 changes: 52 additions & 0 deletions src/formatters/isort.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use super::execute_command;

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

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

execute_command(&mut cmd, snippet_path)
}

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

use super::format_using_isort;

#[test]
fn it_should_format_python() {
let input = "from q import d
import b
import a
import c
def add(a: int, b: int) -> int:
return a + b
";

let expected_output = "import a
import b
import c
from q import d
def add(a: int, b: int) -> int:
return a + b
";

let snippet = setup_snippet(input, Language::Python.to_file_ext())
.expect("it to create a snippet file");

let output = format_using_isort(snippet.path())
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(expected_output, output);
}
}
1 change: 1 addition & 0 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub mod deno_format;
pub mod gleam_format;
pub mod gofmt;
pub mod gofumpt;
pub mod isort;
pub mod mix_format;
pub mod nimpretty;
pub mod prettier;
Expand Down
54 changes: 48 additions & 6 deletions src/languages/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
config::default_enabled,
formatters::{
autopep8::format_using_autopep8, black::format_using_black, blue::format_using_blue,
ruff::format_using_ruff, yapf::format_using_yapf,
isort::format_using_isort, ruff::format_using_ruff, yapf::format_using_yapf,
},
};

Expand All @@ -24,6 +24,8 @@ pub enum PythonFormatter {
Blue,
#[serde(rename = "autopep8")]
Autopep8,
#[serde(rename = "isort")]
Isort,
}

#[derive(Debug, serde::Serialize, serde::Deserialize, JsonSchema)]
Expand Down Expand Up @@ -53,12 +55,14 @@ impl LanguageFormatter for Python {
}

match self.formatter {
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),
PythonFormatter::Autopep8 => format_using_autopep8(snippet_path),
PythonFormatter::Black => format_using_black(snippet_path),
PythonFormatter::Blue => format_using_blue(snippet_path),
PythonFormatter::Ruff => format_using_ruff(snippet_path),
PythonFormatter::Yapf => format_using_yapf(snippet_path),
PythonFormatter::Isort => format_using_isort(snippet_path),
}
.map(|(_modified, output)| output)
}
}

Expand Down Expand Up @@ -190,4 +194,42 @@ mod test_python {

assert_eq!(output, expected_output);
}

#[test]
fn test_isort() {
let input = "from q import d
import b
import a
import c
def add(a: int, b: int) -> int:
return a + b
";

let expected_output = "import a
import b
import c
from q import d
def add(a: int, b: int) -> int:
return a + b
";

let l = Python {
enabled: true,
formatter: PythonFormatter::Isort,
};

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);
}
}

0 comments on commit 88967d2

Please sign in to comment.