Skip to content

Commit

Permalink
feat(go): add support for gofumpt
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen committed Mar 9, 2024
1 parent 7b63576 commit 32d386d
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 6 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ mdsf init
| ---------- | ------------------- |
| CSS | `prettier` |
| Elixir | `mix_format` |
| Go | `gofmt` |
| Go | `gofmt`, `gofumpt` |
| Gleam | `gleam_format` |
| HTML | `prettier` |
| JSON | `prettier`, `biome` |
Expand Down
2 changes: 1 addition & 1 deletion schemas/v0.0.0/mdsf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@
},
"GoFormatter": {
"type": "string",
"enum": ["gofmt"]
"enum": ["gofmt", "gofumpt"]
},
"Html": {
"type": "object",
Expand Down
45 changes: 45 additions & 0 deletions src/formatters/gofumpt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use super::execute_command;

#[inline]
pub fn format_using_gofumpt(
snippet_path: &std::path::Path,
) -> std::io::Result<(bool, Option<String>)> {
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);
}
}
1 change: 1 addition & 0 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion src/languages/go.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -9,6 +12,8 @@ pub enum GoFormatter {
#[default]
#[serde(rename = "gofmt")]
GoFmt,
#[serde(rename = "gofumpt")]
GoFumpt,
}

#[derive(Debug, serde::Serialize, serde::Deserialize, JsonSchema)]
Expand Down Expand Up @@ -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),
}
}
}

0 comments on commit 32d386d

Please sign in to comment.