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

feat(kotlin): add support for ktfmt #127

Merged
merged 1 commit into from Mar 23, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -83,7 +83,7 @@ mdsf init
| JavaScript | `biome`, `clang-format`, `deno_fmt`, `prettier` |
| Json | `biome`, `clang-format`, `deno_fmt`, `prettier` |
| Just | `just_fmt` |
| Kotlin | `ktlint` |
| Kotlin | `ktfmt`, `ktlint` |
| Lua | `luaformatter`, `stylua` |
| Markdown | `prettier` |
| Nim | `nimpretty` |
Expand Down
4 changes: 2 additions & 2 deletions schemas/v0.0.2/mdsf.schema.json
Expand Up @@ -237,7 +237,7 @@
"kotlin": {
"default": {
"enabled": true,
"formatter": "ktlint"
"formatter": [["ktlint", "ktfmt"]]
},
"allOf": [
{
Expand Down Expand Up @@ -589,7 +589,7 @@
},
"Kotlin": {
"type": "string",
"enum": ["ktlint"]
"enum": ["ktlint", "ktfmt"]
},
"Lang_for_Blade": {
"type": "object",
Expand Down
45 changes: 45 additions & 0 deletions src/formatters/ktfmt.rs
@@ -0,0 +1,45 @@
use super::execute_command;

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

cmd.arg("--format")
.arg("--log-level=error")
.arg(snippet_path);

execute_command(&mut cmd, snippet_path)
}

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

use super::format_using_ktfmt;

#[test_with::executable(ktfmt)]
#[test]
fn it_should_format_kotlin() {
let input = " fun add(a:Int ,b:Int ):Int {
return a + b
}
";

let expected_output = "fun add(a: Int, b: Int): Int {
return a + b
}
";

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

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

assert_eq!(output, expected_output);
}
}
1 change: 1 addition & 0 deletions src/formatters/mod.rs
Expand Up @@ -28,6 +28,7 @@ pub mod google_java_format;
pub mod hindent;
pub mod isort;
pub mod just_fmt;
pub mod ktfmt;
pub mod ktlint;
pub mod luaformatter;
pub mod mix_format;
Expand Down
34 changes: 32 additions & 2 deletions src/languages/kotlin.rs
@@ -1,6 +1,6 @@
use schemars::JsonSchema;

use crate::formatters::{ktlint::format_using_ktlint, MdsfFormatter};
use crate::formatters::{ktfmt::format_using_ktfmt, ktlint::format_using_ktlint, MdsfFormatter};

use super::{Lang, LanguageFormatter};

Expand All @@ -10,6 +10,8 @@ pub enum Kotlin {
#[default]
#[serde(rename = "ktlint")]
Ktlint,
#[serde(rename = "ktfmt")]
Ktfmt,
}

impl Default for Lang<Kotlin> {
Expand All @@ -25,7 +27,10 @@ impl Default for Lang<Kotlin> {
impl Default for MdsfFormatter<Kotlin> {
#[inline]
fn default() -> Self {
Self::Single(Kotlin::Ktlint)
Self::Multiple(vec![Self::Multiple(vec![
Self::Single(Kotlin::Ktlint),
Self::Single(Kotlin::Ktfmt),
])])
}
}

Expand All @@ -37,6 +42,7 @@ impl LanguageFormatter for Kotlin {
) -> std::io::Result<(bool, Option<String>)> {
match self {
Self::Ktlint => format_using_ktlint(snippet_path),
Self::Ktfmt => format_using_ktfmt(snippet_path),
}
}
}
Expand Down Expand Up @@ -100,6 +106,30 @@ fun add(
): Int {
return a + b
}
";

assert_eq!(output, expected_output);
}

#[test_with::executable(ktfmt)]
#[test]
fn test_ktfmt() {
let l = Lang::<Kotlin> {
enabled: true,
formatter: MdsfFormatter::Single(Kotlin::Ktfmt),
};

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

let expected_output = "fun add(a: Int, b: Int): Int {
return a + b
}
";

assert_eq!(output, expected_output);
Expand Down