Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
220 additions
and
6 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
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,55 @@ | ||
use super::execute_command; | ||
|
||
#[inline] | ||
pub fn format_using_scalafmt( | ||
snippet_path: &std::path::Path, | ||
) -> std::io::Result<(bool, Option<String>)> { | ||
let mut cmd = std::process::Command::new("scalafmt"); | ||
|
||
#[cfg(test)] | ||
{ | ||
cmd.arg("--config-str").arg("\"version=3.8.0\""); | ||
|
||
cmd.arg("--debug"); | ||
}; | ||
|
||
cmd.arg("--quiet") | ||
.arg("--mode") | ||
.arg("any") | ||
.arg(snippet_path); | ||
|
||
execute_command(&mut cmd, snippet_path) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test_scalafmt { | ||
use crate::{formatters::setup_snippet, languages::Language}; | ||
|
||
use super::format_using_scalafmt; | ||
|
||
#[test_with::executable(scalafmt)] | ||
#[test] | ||
fn it_should_format_scala() { | ||
let input = "object Addition { | ||
def main() = { | ||
println(1 + 3) | ||
} | ||
}"; | ||
let expected_output = "object Addition { | ||
def main() = { | ||
println(1 + 3) | ||
} | ||
} | ||
"; | ||
|
||
let snippet = setup_snippet(input, Language::Scala.to_file_ext()) | ||
.expect("it to create a snippet file"); | ||
|
||
let output = format_using_scalafmt(snippet.path()) | ||
.expect("it to be successful") | ||
.1 | ||
.expect("it to be some"); | ||
|
||
assert_eq!(output, expected_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
use schemars::JsonSchema; | ||
|
||
use crate::formatters::{scalafmt::format_using_scalafmt, MdsfFormatter}; | ||
|
||
use super::{Lang, LanguageFormatter}; | ||
|
||
#[derive(Debug, Default, serde::Serialize, serde::Deserialize, JsonSchema)] | ||
#[cfg_attr(test, derive(PartialEq, Eq))] | ||
pub enum Scala { | ||
#[default] | ||
#[serde(rename = "scalafmt")] | ||
Scalafmt, | ||
} | ||
|
||
impl Default for Lang<Scala> { | ||
#[inline] | ||
fn default() -> Self { | ||
Self { | ||
enabled: true, | ||
formatter: MdsfFormatter::<Scala>::default(), | ||
} | ||
} | ||
} | ||
|
||
impl Default for MdsfFormatter<Scala> { | ||
#[inline] | ||
fn default() -> Self { | ||
Self::Single(Scala::Scalafmt) | ||
} | ||
} | ||
|
||
impl LanguageFormatter for Scala { | ||
#[inline] | ||
fn format_snippet( | ||
&self, | ||
snippet_path: &std::path::Path, | ||
) -> std::io::Result<(bool, Option<String>)> { | ||
match self { | ||
Self::Scalafmt => format_using_scalafmt(snippet_path), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test_blade { | ||
use crate::{ | ||
formatters::{setup_snippet, MdsfFormatter}, | ||
languages::Lang, | ||
}; | ||
|
||
use super::Scala; | ||
|
||
const INPUT: &str = r#" | ||
object A { println ("HELLO!" ) } | ||
// comment | ||
"#; | ||
|
||
const EXTENSION: &str = crate::languages::Language::Blade.to_file_ext(); | ||
|
||
#[test] | ||
fn it_should_be_enabled_by_default() { | ||
assert!(Lang::<Scala>::default().enabled); | ||
} | ||
|
||
#[test] | ||
fn it_should_not_format_when_enabled_is_false() { | ||
let snippet = setup_snippet(INPUT, EXTENSION).expect("it to save the file"); | ||
let snippet_path = snippet.path(); | ||
|
||
assert!(Lang::<Scala> { | ||
enabled: false, | ||
formatter: MdsfFormatter::Single(Scala::Scalafmt), | ||
} | ||
.format(snippet_path) | ||
.expect("it to not fail") | ||
.is_none()); | ||
} | ||
|
||
#[test_with::executable(scalafmt)] | ||
#[test] | ||
fn test_scalafmt() { | ||
let expected_output = r#"object A{ println("HELLO!") } | ||
// comment | ||
"#; | ||
|
||
let l = Lang::<Scala> { | ||
enabled: true, | ||
formatter: MdsfFormatter::Single(Scala::Scalafmt), | ||
}; | ||
|
||
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!(output, expected_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