Skip to content

Commit

Permalink
feat(dart): support dart format (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen authored Mar 9, 2024
1 parent f516bd2 commit dd7015f
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 11 deletions.
17 changes: 10 additions & 7 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,34 +43,37 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
# Used by gleam format, mix format
# gleam_format, mix_format
# NOTE: should be first since it is sometimes crashes (?)
- uses: erlef/setup-beam@v1
with:
otp-version: "26"
gleam-version: "1.0.0"
elixir-version: "1.16.1"
# Used by ruff
# ruff
- uses: actions/setup-python@v5
with:
cache: "pip"
# Used by biome
# biome, prettier, stylua
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
# Used by nimpretty
# nimpretty
- uses: jiro4989/setup-nim-action@v1
# Used by zigfmt
# zigfmt
- uses: goto-bus-stop/setup-zig@v2
# taplo
- uses: uncenter/setup-taplo@v1
# Used go shfmt
# shfmt, gofmt, gofumpt
- uses: actions/setup-go@v5
with:
go-version: "stable"
# Used by rubocop
# rubocop
- uses: ruby/setup-ruby@v1
with:
ruby-version: "3.3"
# dart_format
- uses: dart-lang/setup-dart@v1

- 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 @@ -48,6 +48,7 @@ mdsf init
| Language | Formatters |
| ---------- | ------------------- |
| CSS | `prettier` |
| Dart | `dart_format` |
| Elixir | `mix_format` |
| Go | `gofmt`, `gofumpt` |
| Gleam | `gleam_format` |
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 @@ -14,6 +14,17 @@
}
]
},
"dart": {
"default": {
"enabled": true,
"formatter": "dart_format"
},
"allOf": [
{
"$ref": "#/definitions/Dart"
}
]
},
"elixir": {
"default": {
"enabled": true,
Expand Down Expand Up @@ -235,6 +246,27 @@
"type": "string",
"enum": ["prettier"]
},
"Dart": {
"type": "object",
"properties": {
"enabled": {
"default": true,
"type": "boolean"
},
"formatter": {
"default": "dart_format",
"allOf": [
{
"$ref": "#/definitions/DartFormatter"
}
]
}
}
},
"DartFormatter": {
"type": "string",
"enum": ["dart_format"]
},
"Elixir": {
"type": "object",
"properties": {
Expand Down
10 changes: 7 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use schemars::JsonSchema;

use crate::languages::{
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,
css::Css, dart::Dart, 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,
};

#[derive(Debug, serde::Serialize, serde::Deserialize, JsonSchema)]
Expand All @@ -15,6 +15,9 @@ pub struct MdsfConfig {
#[serde(default)]
pub css: Css,

#[serde(default)]
pub dart: Dart,

#[serde(default)]
pub elixir: Elixir,

Expand Down Expand Up @@ -76,6 +79,7 @@ impl Default for MdsfConfig {
Self {
schema: default_schema_location(),
css: Css::default(),
dart: Dart::default(),
elixir: Elixir::default(),
go: Go::default(),
gleam: Gleam::default(),
Expand Down
39 changes: 39 additions & 0 deletions src/formatters/dart_format.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use super::execute_command;

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

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

execute_command(&mut cmd, snippet_path)
}

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

#[test]
fn it_should_format_dart() {
let input = "class Adder { int add(int a, int b) { return a + b; } } ";

let expected_output = "class Adder {
int add(int a, int b) {
return a + b;
}
}
";

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

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

assert_eq!(expected_output, output);
}
}
2 changes: 2 additions & 0 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
};

pub mod biome;
pub mod dart_format;
pub mod gleam_format;
pub mod gofmt;
pub mod gofumpt;
Expand Down Expand Up @@ -87,6 +88,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::Dart => config.dart.format(snippet_path),
Language::Elixir => config.elixir.format(snippet_path),
Language::Go => config.go.format(snippet_path),
Language::Gleam => config.gleam.format(snippet_path),
Expand Down
43 changes: 43 additions & 0 deletions src/languages/dart.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use schemars::JsonSchema;

use crate::{config::default_enabled, formatters::dart_format::format_using_dart_format};

use super::LanguageFormatter;

#[derive(Debug, Default, serde::Serialize, serde::Deserialize, JsonSchema)]
pub enum DartFormatter {
#[default]
#[serde(rename = "dart_format")]
DartFormat,
}

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

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

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

match self.formatter {
DartFormatter::DartFormat => format_using_dart_format(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,5 +1,6 @@
pub enum Language {
Css,
Dart,
Elixir,
Go,
Gleam,
Expand Down Expand Up @@ -30,7 +31,6 @@ pub enum Language {
// TODO: FSharp,
// TODO: Swift,
// TODO: Svelte,
// TODO: Dart,
// TODO: Julia,
// TODO: Dockerfile,
// TODO: XML,
Expand All @@ -45,6 +45,7 @@ pub enum Language {
}

pub mod css;
pub mod dart;
pub mod elixir;
pub mod gleam;
pub mod go;
Expand Down Expand Up @@ -73,6 +74,7 @@ impl Language {
pub fn maybe_from_str(input: &str) -> Option<Self> {
match input {
"css" | "scss" => Some(Self::Css),
"dart" => Some(Self::Dart),
"elixir" => Some(Self::Elixir),
"go" | "golang" => Some(Self::Go),
"gleam" => Some(Self::Gleam),
Expand Down Expand Up @@ -100,6 +102,7 @@ impl Language {
match self {
// NOTE: since scss is a superset of css we might as well support both at the same time
Self::Css => ".scss",
Self::Dart => ".dart",
Self::Elixir => ".ex",
Self::Go => ".go",
Self::Gleam => ".gleam",
Expand Down
6 changes: 6 additions & 0 deletions tests/dart.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
```dart
class Adder {int add(int a, int b) {return a + b;}}
```

0 comments on commit dd7015f

Please sign in to comment.