Skip to content

Commit

Permalink
feat(go): add support for gofmt (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen authored Mar 9, 2024
1 parent 1acff82 commit 7b63576
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ mdsf init

> \[!NOTE\]
> mdsf is not a tool for installing formatters.
> Only formatters that are installed already will be used.
>
> Only formatters that are already installed will be used.
| Language | Formatters |
| ---------- | ------------------- |
| CSS | `prettier` |
| Elixir | `mix_format` |
| Go | `gofmt` |
| Gleam | `gleam_format` |
| HTML | `prettier` |
| JSON | `prettier`, `biome` |
Expand Down
32 changes: 32 additions & 0 deletions schemas/v0.0.0/mdsf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@
}
]
},
"go": {
"default": {
"enabled": true,
"formatter": "gofmt"
},
"allOf": [
{
"$ref": "#/definitions/Go"
}
]
},
"html": {
"default": {
"enabled": true,
Expand Down Expand Up @@ -266,6 +277,27 @@
"type": "string",
"enum": ["gleam_format"]
},
"Go": {
"type": "object",
"properties": {
"enabled": {
"default": true,
"type": "boolean"
},
"formatter": {
"default": "gofmt",
"allOf": [
{
"$ref": "#/definitions/GoFormatter"
}
]
}
}
},
"GoFormatter": {
"type": "string",
"enum": ["gofmt"]
},
"Html": {
"type": "object",
"properties": {
Expand Down
6 changes: 5 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use schemars::JsonSchema;

use crate::languages::{
css::Css, elixir::Elixir, gleam::Gleam, html::Html, javascript::JavaScript, json::Json,
css::Css, elixir::Elixir, gleam::Gleam, go::Go, html::Html, javascript::JavaScript, json::Json,
lua::Lua, markdown::Markdown, nim::Nim, python::Python, ruby::Ruby, rust::Rust, shell::Shell,
toml::Toml, typescript::TypeScript, vue::Vue, yaml::Yaml, zig::Zig,
};
Expand All @@ -18,6 +18,9 @@ pub struct MdsfConfig {
#[serde(default)]
pub elixir: Elixir,

#[serde(default)]
pub go: Go,

#[serde(default)]
pub gleam: Gleam,

Expand Down Expand Up @@ -74,6 +77,7 @@ impl Default for MdsfConfig {
schema: default_schema_location(),
css: Css::default(),
elixir: Elixir::default(),
go: Go::default(),
gleam: Gleam::default(),
html: Html::default(),
javascript: JavaScript::default(),
Expand Down
48 changes: 48 additions & 0 deletions src/formatters/gofmt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use super::execute_command;

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

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

execute_command(&mut cmd, snippet_path)
}

#[cfg(test)]
mod test_gofmt {
use crate::{
formatters::{gofmt::format_using_gofmt, 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 = format_using_gofmt(snippet.path())
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(expected_output, output);
}
}
2 changes: 1 addition & 1 deletion src/formatters/mix_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn format_using_mix_format(
}

#[cfg(test)]
mod test_gleam_format {
mod test_mix_format {
use crate::{
formatters::{mix_format::format_using_mix_format, setup_snippet},
languages::Language,
Expand Down
2 changes: 2 additions & 0 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{

pub mod biome;
pub mod gleam_format;
pub mod gofmt;
pub mod mix_format;
pub mod nimpretty;
pub mod prettier;
Expand Down Expand Up @@ -86,6 +87,7 @@ pub fn format_snippet(config: &MdsfConfig, language: &Language, code: &str) -> S
if let Ok(Some(formatted_code)) = match language {
Language::Css => config.css.format(snippet_path),
Language::Elixir => config.elixir.format(snippet_path),
Language::Go => config.go.format(snippet_path),
Language::Gleam => config.gleam.format(snippet_path),
Language::Html => config.html.format(snippet_path),
Language::JavaScript => config.javascript.format(snippet_path),
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/rubocop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn format_using_rubocop(
}

#[cfg(test)]
mod test_ruff {
mod test_rubocop {
use crate::{
formatters::{rubocop::format_using_rubocop, setup_snippet},
languages::Language,
Expand Down
43 changes: 43 additions & 0 deletions src/languages/go.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use schemars::JsonSchema;

use crate::{config::default_enabled, formatters::gofmt::format_using_gofmt};

use super::LanguageFormatter;

#[derive(Debug, Default, serde::Serialize, serde::Deserialize, JsonSchema)]
pub enum GoFormatter {
#[default]
#[serde(rename = "gofmt")]
GoFmt,
}

#[derive(Debug, serde::Serialize, serde::Deserialize, JsonSchema)]
pub struct Go {
#[serde(default = "default_enabled")]
pub enabled: bool,
#[serde(default)]
pub formatter: GoFormatter,
}

impl Default for Go {
#[inline]
fn default() -> Self {
Self {
enabled: true,
formatter: GoFormatter::default(),
}
}
}

impl LanguageFormatter for Go {
#[inline]
fn format(&self, snippet_path: &std::path::Path) -> std::io::Result<Option<String>> {
if !self.enabled {
return Ok(None);
}

match self.formatter {
GoFormatter::GoFmt => format_using_gofmt(snippet_path).map(|res| res.1),
}
}
}
5 changes: 4 additions & 1 deletion src/languages/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub enum Language {
Css,
Elixir,
Go,
Gleam,
Html,
JavaScript,
Expand All @@ -17,7 +18,6 @@ pub enum Language {
Vue,
Yaml,
Zig,
// TODO: Go,
// TODO: Cpp,
// TODO: C,
// TODO: Haskell,
Expand Down Expand Up @@ -47,6 +47,7 @@ pub enum Language {
pub mod css;
pub mod elixir;
pub mod gleam;
pub mod go;
pub mod html;
pub mod javascript;
pub mod json;
Expand All @@ -73,6 +74,7 @@ impl Language {
match input {
"css" | "scss" => Some(Self::Css),
"elixir" => Some(Self::Elixir),
"go" | "golang" => Some(Self::Go),
"gleam" => Some(Self::Gleam),
"html" => Some(Self::Html),
"javascript" | "js" | "jsx" => Some(Self::JavaScript),
Expand All @@ -99,6 +101,7 @@ impl Language {
// NOTE: since scss is a superset of css we might as well support both at the same time
Self::Css => ".scss",
Self::Elixir => ".ex",
Self::Go => ".go",
Self::Gleam => ".gleam",
Self::Html => ".html",
Self::JavaScript => ".js",
Expand Down
12 changes: 12 additions & 0 deletions tests/go.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
```go



package main

func add(a int , b int ) int {
return a + b
}


```

0 comments on commit 7b63576

Please sign in to comment.