-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(go): add support for gofmt (#47)
- Loading branch information
Showing
10 changed files
with
151 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
|
||
``` |