Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(roc): add support for roc format #60

Merged
merged 2 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ jobs:
- uses: dart-lang/setup-dart@v1
# crystal_format
- uses: crystal-lang/install-crystal@v1
# roc_format
- uses: hasnep/[email protected]

- run: rustup toolchain install stable --profile minimal
- run: rustup component add rustfmt clippy
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ mdsf init
| Objective C | `clang-format` |
| Protobuf | `clang-format` |
| Python | `ruff`, `black`, `blue`, `yapf`, `autopep8` |
| Roc | `roc_format` |
| Ruby | `rubocop` |
| Rust | `rustfmt` |
| SQL | `sqlfluff`, `sql-formatter` |
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 @@ -212,6 +212,17 @@
}
]
},
"roc": {
"default": {
"enabled": true,
"formatter": "roc_format"
},
"allOf": [
{
"$ref": "#/definitions/Roc"
}
]
},
"ruby": {
"default": {
"enabled": true,
Expand Down Expand Up @@ -712,6 +723,27 @@
"type": "string",
"enum": ["ruff", "black", "yapf", "blue", "autopep8"]
},
"Roc": {
"type": "object",
"properties": {
"enabled": {
"default": true,
"type": "boolean"
},
"formatter": {
"default": "roc_format",
"allOf": [
{
"$ref": "#/definitions/RocFormatter"
}
]
}
}
},
"RocFormatter": {
"type": "string",
"enum": ["roc_format"]
},
"Ruby": {
"type": "object",
"properties": {
Expand Down
8 changes: 6 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::languages::{
c::C, cpp::Cpp, crystal::Crystal, csharp::CSharp, css::Css, dart::Dart, elixir::Elixir,
gleam::Gleam, go::Go, html::Html, java::Java, javascript::JavaScript, json::Json, lua::Lua,
markdown::Markdown, nim::Nim, objective_c::ObjectiveC, protobuf::Protobuf, python::Python,
ruby::Ruby, rust::Rust, shell::Shell, sql::Sql, toml::Toml, typescript::TypeScript, vue::Vue,
yaml::Yaml, zig::Zig,
roc::Roc, ruby::Ruby, rust::Rust, shell::Shell, sql::Sql, toml::Toml, typescript::TypeScript,
vue::Vue, yaml::Yaml, zig::Zig,
};

#[derive(Debug, serde::Serialize, serde::Deserialize, JsonSchema)]
Expand Down Expand Up @@ -72,6 +72,9 @@ pub struct MdsfConfig {
#[serde(default)]
pub python: Python,

#[serde(default)]
pub roc: Roc,

#[serde(default)]
pub ruby: Ruby,

Expand Down Expand Up @@ -125,6 +128,7 @@ impl Default for MdsfConfig {
objective_c: ObjectiveC::default(),
protobuf: Protobuf::default(),
python: Python::default(),
roc: Roc::default(),
ruby: Ruby::default(),
rust: Rust::default(),
shell: Shell::default(),
Expand Down
2 changes: 2 additions & 0 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod gofumpt;
pub mod mix_format;
pub mod nimpretty;
pub mod prettier;
pub mod roc_format;
pub mod rubocop;
pub mod ruff;
pub mod rustfmt;
Expand Down Expand Up @@ -111,6 +112,7 @@ pub fn format_snippet(config: &MdsfConfig, language: &Language, code: &str) -> S
Language::ObjectiveC => config.objective_c.format(snippet_path),
Language::Protobuf => config.protobuf.format(snippet_path),
Language::Python => config.python.format(snippet_path),
Language::Roc => config.roc.format(snippet_path),
Language::Ruby => config.ruby.format(snippet_path),
Language::Rust => config.rust.format(snippet_path),
Language::Shell => config.shell.format(snippet_path),
Expand Down
58 changes: 58 additions & 0 deletions src/formatters/roc_format.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use super::execute_command;

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

cmd.arg("format").arg(snippet_path);

execute_command(&mut cmd, snippet_path)
}

#[cfg(test)]
mod test_roc_format {
use crate::{formatters::setup_snippet, languages::Language};

use super::format_using_roc_format;

#[test]
fn it_should_format_roc() {
let input = r#"app "helloWorld"
packages { pf: "https://github.com/roc-lang/" }
imports [pf.Stdout]
provides [main] to pf






main =
Stdout.line "Hello, World!"


"#;

let expected_output = r#"app "helloWorld"
packages { pf: "https://github.com/roc-lang/" }
imports [pf.Stdout]
provides [main] to pf

main =
Stdout.line "Hello, World!"

"#;

let snippet =
setup_snippet(input, Language::Roc.to_file_ext()).expect("it to create a snippet file");

let output = format_using_roc_format(snippet.path())
.expect("it to be successful")
.1
.expect("it to be some");

assert_eq!(expected_output, output);
}
}
4 changes: 4 additions & 0 deletions src/languages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub enum Language {
ObjectiveC,
Protobuf,
Python,
Roc,
Ruby,
Rust,
Shell,
Expand Down Expand Up @@ -64,6 +65,7 @@ pub mod nim;
pub mod objective_c;
pub mod protobuf;
pub mod python;
pub mod roc;
pub mod ruby;
pub mod rust;
pub mod shell;
Expand Down Expand Up @@ -101,6 +103,7 @@ impl Language {
"objectivec" | "objective-c" | "objc" => Some(Self::ObjectiveC),
"profobuf" | "profo" => Some(Self::Protobuf),
"python" => Some(Self::Python),
"roc" => Some(Self::Roc),
"ruby" => Some(Self::Ruby),
"rust" | "rb" => Some(Self::Rust),
"shell" | "sh" | "bash" | "zsh" => Some(Self::Shell),
Expand Down Expand Up @@ -139,6 +142,7 @@ impl Language {
Self::ObjectiveC => ".m",
Self::Protobuf => ".proto",
Self::Python => ".py",
Self::Roc => ".roc",
Self::Ruby => ".rb",
Self::Rust => ".rs",
Self::Shell => ".sh",
Expand Down
117 changes: 117 additions & 0 deletions src/languages/roc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
use schemars::JsonSchema;

use crate::{config::default_enabled, formatters::roc_format::format_using_roc_format};

use super::LanguageFormatter;

#[derive(Debug, Default, serde::Serialize, serde::Deserialize, JsonSchema)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum RocFormatter {
#[default]
#[serde(rename = "roc_format")]
RocFormat,
}

#[derive(Debug, serde::Serialize, serde::Deserialize, JsonSchema)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct Roc {
#[serde(default = "default_enabled")]
pub enabled: bool,
#[serde(default)]
pub formatter: RocFormatter,
}

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

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

match self.formatter {
RocFormatter::RocFormat => format_using_roc_format(snippet_path).map(|res| res.1),
}
}
}

#[cfg(test)]
mod test_roc {
use crate::{formatters::setup_snippet, languages::LanguageFormatter};

use super::{Roc, RocFormatter};

const INPUT: &str = r#"app "helloWorld"
packages { pf: "https://github.com/roc-lang/" }
imports [pf.Stdout]
provides [main] to pf






main =
Stdout.line "Hello, World!"


"#;

const EXTENSION: &str = crate::languages::Language::Roc.to_file_ext();

#[test]
fn it_should_be_enabled_by_default() {
assert!(Roc::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!(Roc {
enabled: false,
formatter: RocFormatter::default(),
}
.format(snippet_path)
.expect("it to not fail")
.is_none());
}

#[test]
fn test_roc_format() {
let l = Roc {
enabled: true,
formatter: RocFormatter::RocFormat,
};

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");

let expected_output = r#"app "helloWorld"
packages { pf: "https://github.com/roc-lang/" }
imports [pf.Stdout]
provides [main] to pf

main =
Stdout.line "Hello, World!"

"#;

assert_eq!(output, expected_output);
}
}
17 changes: 17 additions & 0 deletions tests/roc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
```roc

app "helloWorld"
packages { pf: "https://github.com/roc-lang/" }
imports [pf.Stdout]
provides [main] to pf






main =
Stdout.line "Hello, World!"


```
Loading