diff --git a/.github/workflows/validate.yml b/.github/workflows/validate.yml index f7a3d680..047e994e 100644 --- a/.github/workflows/validate.yml +++ b/.github/workflows/validate.yml @@ -104,20 +104,22 @@ jobs: - name: Install rubocop run: gem install rubocop - - name: Validate rubocop run: rubocop --version - name: Install stylua run: cargo install stylua - - name: Validate stylua run: stylua --version - name: Install shfmt run: go install mvdan.cc/sh/v3/cmd/shfmt@latest - - name: Validate shfmt run: shfmt --version + - name: Install gofumpt + run: go install mvdan.cc/gofumpt@latest + - name: Validate gofumpt + run: gofumpt --version + - run: cargo test diff --git a/README.md b/README.md index afe90e33..d1270185 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ mdsf init | ---------- | ------------------- | | CSS | `prettier` | | Elixir | `mix_format` | -| Go | `gofmt` | +| Go | `gofmt`, `gofumpt` | | Gleam | `gleam_format` | | HTML | `prettier` | | JSON | `prettier`, `biome` | diff --git a/schemas/v0.0.0/mdsf.schema.json b/schemas/v0.0.0/mdsf.schema.json index 175fd416..97a570a7 100644 --- a/schemas/v0.0.0/mdsf.schema.json +++ b/schemas/v0.0.0/mdsf.schema.json @@ -296,7 +296,7 @@ }, "GoFormatter": { "type": "string", - "enum": ["gofmt"] + "enum": ["gofmt", "gofumpt"] }, "Html": { "type": "object", diff --git a/src/formatters/gofumpt.rs b/src/formatters/gofumpt.rs new file mode 100644 index 00000000..58138959 --- /dev/null +++ b/src/formatters/gofumpt.rs @@ -0,0 +1,45 @@ +use super::execute_command; + +#[inline] +pub fn format_using_gofumpt( + snippet_path: &std::path::Path, +) -> std::io::Result<(bool, Option)> { + let mut cmd = std::process::Command::new("gofumpt"); + + cmd.arg("-w").arg(snippet_path); + + execute_command(&mut cmd, snippet_path) +} + +#[cfg(test)] +mod test_gofumpt { + use crate::{formatters::setup_snippet, languages::Language}; + + #[test] + fn it_should_format_go() { + let input = "package main + + func add(a int , b int ) int { + return a + b + } + + "; + + let expected_output = "package main + +func add(a int, b int) int { +\treturn a + b +} +"; + + let snippet = + setup_snippet(input, Language::Go.to_file_ext()).expect("it to create a snippet file"); + + let output = super::format_using_gofumpt(snippet.path()) + .expect("it to be successful") + .1 + .expect("it to be some"); + + assert_eq!(expected_output, output); + } +} diff --git a/src/formatters/mod.rs b/src/formatters/mod.rs index 2a562ba4..ee296247 100644 --- a/src/formatters/mod.rs +++ b/src/formatters/mod.rs @@ -13,6 +13,7 @@ use crate::{ pub mod biome; pub mod gleam_format; pub mod gofmt; +pub mod gofumpt; pub mod mix_format; pub mod nimpretty; pub mod prettier; diff --git a/src/languages/go.rs b/src/languages/go.rs index b7b80709..a7e6f211 100644 --- a/src/languages/go.rs +++ b/src/languages/go.rs @@ -1,6 +1,9 @@ use schemars::JsonSchema; -use crate::{config::default_enabled, formatters::gofmt::format_using_gofmt}; +use crate::{ + config::default_enabled, + formatters::{gofmt::format_using_gofmt, gofumpt::format_using_gofumpt}, +}; use super::LanguageFormatter; @@ -9,6 +12,8 @@ pub enum GoFormatter { #[default] #[serde(rename = "gofmt")] GoFmt, + #[serde(rename = "gofumpt")] + GoFumpt, } #[derive(Debug, serde::Serialize, serde::Deserialize, JsonSchema)] @@ -38,6 +43,7 @@ impl LanguageFormatter for Go { match self.formatter { GoFormatter::GoFmt => format_using_gofmt(snippet_path).map(|res| res.1), + GoFormatter::GoFumpt => format_using_gofumpt(snippet_path).map(|res| res.1), } } }