Skip to content

Commit

Permalink
feat(python): support usort
Browse files Browse the repository at this point in the history
Closes #62 on merge.
  • Loading branch information
hougesen committed Mar 14, 2024
1 parent 88967d2 commit ddb6fc1
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
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", "isort"]
"enum": ["ruff", "black", "yapf", "blue", "autopep8", "isort", "usort"]
},
"Roc": {
"type": "object",
Expand Down
1 change: 1 addition & 0 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub mod sql_formatter;
pub mod sqlfluff;
pub mod stylua;
pub mod taplo;
pub mod usort;
pub mod yapf;
pub mod zigfmt;

Expand Down
52 changes: 52 additions & 0 deletions src/formatters/usort.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use super::execute_command;

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

cmd.arg("format").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_usort;

#[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_usort(snippet.path())
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(expected_output, output);
}
}
44 changes: 43 additions & 1 deletion src/languages/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use crate::{
config::default_enabled,
formatters::{
autopep8::format_using_autopep8, black::format_using_black, blue::format_using_blue,
isort::format_using_isort, ruff::format_using_ruff, yapf::format_using_yapf,
isort::format_using_isort, ruff::format_using_ruff, usort::format_using_usort,
yapf::format_using_yapf,
},
};

Expand All @@ -26,6 +27,8 @@ pub enum PythonFormatter {
Autopep8,
#[serde(rename = "isort")]
Isort,
#[serde(rename = "usort")]
Usort,
}

#[derive(Debug, serde::Serialize, serde::Deserialize, JsonSchema)]
Expand Down Expand Up @@ -61,6 +64,7 @@ impl LanguageFormatter for Python {
PythonFormatter::Ruff => format_using_ruff(snippet_path),
PythonFormatter::Yapf => format_using_yapf(snippet_path),
PythonFormatter::Isort => format_using_isort(snippet_path),
PythonFormatter::Usort => format_using_usort(snippet_path),
}
.map(|(_modified, output)| output)
}
Expand Down Expand Up @@ -232,4 +236,42 @@ def add(a: int, b: int) -> int:

assert_eq!(output, expected_output);
}

#[test]
fn test_usort() {
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::Usort,
};

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 ddb6fc1

Please sign in to comment.