Skip to content

Commit

Permalink
feat(java): add google-java-format (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen authored Mar 20, 2024
1 parent d7c718b commit 57333a8
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ mdsf init
| GraphQL | `prettier` |
| Groovy | `npm-groovy-lint` |
| Html | `prettier` |
| Java | `clang-format` |
| Java | `clang-format`, `google-java-format` |
| JavaScript | `biome`, `clang-format`, `deno_fmt`, `prettier` |
| Json | `biome`, `clang-format`, `deno_fmt`, `prettier` |
| Just | `just_fmt` |
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 @@ -160,7 +160,7 @@
"java": {
"default": {
"enabled": true,
"formatter": "clang-format"
"formatter": ["google-java-format", "clang-format"]
},
"allOf": [
{
Expand Down Expand Up @@ -484,7 +484,7 @@
},
"Java": {
"type": "string",
"enum": ["clang-format"]
"enum": ["google-java-format", "clang-format"]
},
"JavaScript": {
"type": "string",
Expand Down
48 changes: 48 additions & 0 deletions src/formatters/google_java_format.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use super::execute_command;

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

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

execute_command(&mut cmd, snippet_path)
}

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

use super::format_using_google_java_format;

#[test_with::executable(google-java-format)]
#[test]
fn it_should_format_java() {
let input = "class HelloWorld {
public static void main(String[] args) {
System.out.println(\"Hello\");
System.out.println(\"World!\");
}
}";

let expected_output = "class HelloWorld {
public static void main(String[] args) {
System.out.println(\"Hello\");
System.out.println(\"World!\");
}
}
";

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

let output = format_using_google_java_format(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 @@ -19,6 +19,7 @@ pub mod gleam_format;
pub mod gofmt;
pub mod gofumpt;
pub mod goimports;
pub mod google_java_format;
pub mod isort;
pub mod just_fmt;
pub mod mix_format;
Expand Down
39 changes: 37 additions & 2 deletions src/languages/java.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
use schemars::JsonSchema;

use crate::formatters::{clang_format::format_using_clang_format, MdsfFormatter};
use crate::formatters::{
clang_format::format_using_clang_format, google_java_format::format_using_google_java_format,
MdsfFormatter,
};

use super::{Lang, LanguageFormatter};

#[derive(Debug, Default, serde::Serialize, serde::Deserialize, JsonSchema)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum Java {
#[default]
#[serde(rename = "google-java-format")]
GoogleJavaFormat,
#[serde(rename = "clang-format")]
ClangFormat,
}
Expand All @@ -25,7 +30,10 @@ impl Default for Lang<Java> {
impl Default for MdsfFormatter<Java> {
#[inline]
fn default() -> Self {
Self::Single(Java::ClangFormat)
Self::Multiple(vec![
Self::Single(Java::GoogleJavaFormat),
Self::Single(Java::ClangFormat),
])
}
}

Expand All @@ -37,6 +45,7 @@ impl LanguageFormatter for Java {
) -> std::io::Result<(bool, Option<String>)> {
match self {
Self::ClangFormat => format_using_clang_format(snippet_path),
Self::GoogleJavaFormat => format_using_google_java_format(snippet_path),
}
}
}
Expand Down Expand Up @@ -102,4 +111,30 @@ mod test_java {

assert_eq!(expected_output, output);
}

#[test_with::executable(google-java-format)]
#[test]
fn test_google_java_format() {
let expected_output = "class HelloWorld {
public static void main(String[] args) {
System.out.println(\"Hello\");
System.out.println(\"World!\");
}
}
";
let l = Lang::<Java> {
enabled: true,
formatter: MdsfFormatter::Single(Java::GoogleJavaFormat),
};

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!(expected_output, output);
}
}

0 comments on commit 57333a8

Please sign in to comment.