Skip to content

Commit

Permalink
feat(kotlin): add support for ktfmt
Browse files Browse the repository at this point in the history
Closes #106
  • Loading branch information
hougesen committed Mar 23, 2024
1 parent 26b0e87 commit 59a6bf1
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 59a6bf1

Please sign in to comment.