Skip to content

Commit

Permalink
feat(swift): add support for swiftformat (#105)
Browse files Browse the repository at this point in the history
* feat(swift): add support for swiftformat

* chore: update config schema

* ci: remove update-schema

---------

Co-authored-by: hougesen <[email protected]>
  • Loading branch information
hougesen and hougesen authored Mar 21, 2024
1 parent 34b6e99 commit b6eefd8
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 44 deletions.
31 changes: 0 additions & 31 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,8 @@ env:
RUST_BACKTRACE: full

jobs:
update-schema:
strategy:
matrix:
os:
- ubuntu-latest
node:
- 20
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
- run: rustup toolchain install stable --profile minimal
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}

- name: update schema
run: cargo run -- schema

- name: update readme
run: node scripts/update-supported-languages.mjs

- name: format
run: npx prettier --write schemas/ README.md

- uses: EndBug/add-and-commit@v9
with:
message: "chore: update config schema"

lint:
name: lint
needs: [update-schema]
strategy:
matrix:
os:
Expand All @@ -66,7 +36,6 @@ jobs:

test:
name: test
needs: [update-schema]
strategy:
matrix:
os:
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ mdsf init
| Go | `gofmt`, `gofumpt`, `goimports` |
| GraphQL | `prettier` |
| Groovy | `npm-groovy-lint` |
| Haskell | `fourmolu`, `hindent` |
| Haskell | `fourmolu`, `hindent`, `ormolu` |
| Html | `prettier` |
| Java | `clang-format`, `google-java-format` |
| JavaScript | `biome`, `clang-format`, `deno_fmt`, `prettier` |
Expand All @@ -94,16 +94,17 @@ mdsf init
| Python | `autopep8`, `black`, `blue`, `isort`, `ruff`, `usort`, `yapf` |
| ReScript | `rescript_format` |
| Roc | `roc_format` |
| Ruby | `rubocop`, `rufo` |
| Ruby | `rubocop`, `rubyfmt`, `rufo` |
| Rust | `rustfmt` |
| Scala | `scalafmt` |
| Shell | `beautysh`, `shfmt` |
| Sql | `sql-formatter`, `sqlfluff` |
| Swift | `swiftformat` |
| Toml | `taplo` |
| TypeScript | `biome`, `deno_fmt`, `prettier` |
| Vue | `prettier` |
| Xml | `xmllint` |
| Yaml | `prettier` |
| Yaml | `prettier`, `yamlfmt` |
| Zig | `zigfmt` |

<!-- END_SECTION:supported-languages -->
52 changes: 46 additions & 6 deletions schemas/v0.0.2/mdsf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
"haskell": {
"default": {
"enabled": true,
"formatter": [["fourmolu", "hindent"]]
"formatter": [["fourmolu", "ormolu", "hindent"]]
},
"allOf": [
{
Expand Down Expand Up @@ -361,7 +361,7 @@
"ruby": {
"default": {
"enabled": true,
"formatter": [["rubocop", "rufo"]]
"formatter": [["rubocop", "rufo", "rubyfmt"]]
},
"allOf": [
{
Expand Down Expand Up @@ -413,6 +413,17 @@
}
]
},
"swift": {
"default": {
"enabled": true,
"formatter": "swiftformat"
},
"allOf": [
{
"$ref": "#/definitions/Lang_for_Swift"
}
]
},
"toml": {
"default": {
"enabled": true,
Expand Down Expand Up @@ -460,7 +471,7 @@
"yaml": {
"default": {
"enabled": true,
"formatter": "prettier"
"formatter": [["prettier", "yamlfmt"]]
},
"allOf": [
{
Expand Down Expand Up @@ -539,7 +550,7 @@
},
"Haskell": {
"type": "string",
"enum": ["fourmolu", "hindent"]
"enum": ["fourmolu", "ormolu", "hindent"]
},
"Html": {
"type": "string",
Expand Down Expand Up @@ -1009,6 +1020,18 @@
}
}
},
"Lang_for_Swift": {
"type": "object",
"required": ["enabled", "formatter"],
"properties": {
"enabled": {
"type": "boolean"
},
"formatter": {
"$ref": "#/definitions/MdsfFormatter_for_Swift"
}
}
},
"Lang_for_Toml": {
"type": "object",
"required": ["enabled", "formatter"],
Expand Down Expand Up @@ -1570,6 +1593,19 @@
}
]
},
"MdsfFormatter_for_Swift": {
"anyOf": [
{
"$ref": "#/definitions/Swift"
},
{
"type": "array",
"items": {
"$ref": "#/definitions/MdsfFormatter_for_Swift"
}
}
]
},
"MdsfFormatter_for_Toml": {
"anyOf": [
{
Expand Down Expand Up @@ -1686,7 +1722,7 @@
},
"Ruby": {
"type": "string",
"enum": ["rubocop", "rufo"]
"enum": ["rubyfmt", "rubocop", "rufo"]
},
"Rust": {
"type": "string",
Expand All @@ -1704,6 +1740,10 @@
"type": "string",
"enum": ["sqlfluff", "sql-formatter"]
},
"Swift": {
"type": "string",
"enum": ["swiftformat"]
},
"Toml": {
"type": "string",
"enum": ["taplo"]
Expand All @@ -1722,7 +1762,7 @@
},
"Yaml": {
"type": "string",
"enum": ["prettier"]
"enum": ["prettier", "yamlfmt"]
},
"Zig": {
"type": "string",
Expand Down
9 changes: 6 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::languages::{
haskell::Haskell, html::Html, java::Java, javascript::JavaScript, json::Json, just::Just,
kotlin::Kotlin, lua::Lua, markdown::Markdown, nim::Nim, objective_c::ObjectiveC, ocaml::OCaml,
perl::Perl, protobuf::Protobuf, purescript::PureScript, python::Python, rescript::ReScript,
roc::Roc, ruby::Ruby, rust::Rust, scala::Scala, shell::Shell, sql::Sql, toml::Toml,
typescript::TypeScript, vue::Vue, xml::Xml, yaml::Yaml, zig::Zig, Lang,
roc::Roc, ruby::Ruby, rust::Rust, scala::Scala, shell::Shell, sql::Sql, swift::Swift,
toml::Toml, typescript::TypeScript, vue::Vue, xml::Xml, yaml::Yaml, zig::Zig, Lang,
};

#[derive(Debug, serde::Serialize, serde::Deserialize, JsonSchema)]
Expand Down Expand Up @@ -125,6 +125,9 @@ pub struct MdsfConfig {
#[serde(default)]
pub shell: Lang<Shell>,

#[serde(default)]
pub swift: Lang<Swift>,

#[serde(default)]
pub sql: Lang<Sql>,

Expand All @@ -136,7 +139,6 @@ pub struct MdsfConfig {

#[serde(default)]
pub vue: Lang<Vue>,

#[serde(default)]
pub xml: Lang<Xml>,

Expand Down Expand Up @@ -189,6 +191,7 @@ impl Default for MdsfConfig {
rust: Lang::<Rust>::default(),
scala: Lang::<Scala>::default(),
shell: Lang::<Shell>::default(),
swift: Lang::<Swift>::default(),
sql: Lang::<Sql>::default(),
toml: Lang::<Toml>::default(),
typescript: Lang::<TypeScript>::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 @@ -47,6 +47,7 @@ pub mod shfmt;
pub mod sql_formatter;
pub mod sqlfluff;
pub mod stylua;
pub mod swiftformat;
pub mod taplo;
pub mod usort;
pub mod xmllint;
Expand Down Expand Up @@ -160,6 +161,7 @@ pub fn format_snippet(config: &MdsfConfig, language: &Language, code: &str) -> S
Language::Scala => config.scala.format(snippet_path),
Language::Shell => config.shell.format(snippet_path),
Language::Sql => config.sql.format(snippet_path),
Language::Swift => config.swift.format(snippet_path),
Language::Toml => config.toml.format(snippet_path),
Language::TypeScript => config.typescript.format(snippet_path),
Language::Vue => config.vue.format(snippet_path),
Expand Down
42 changes: 42 additions & 0 deletions src/formatters/swiftformat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use super::execute_command;

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

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

execute_command(&mut cmd, snippet_path)
}

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

use super::format_using_swiftformat;

#[test_with::executable(swiftformat)]
#[test]
fn it_should_format_swift() {
let input = " func add(a:Int ,b:Int)->Int {
return a + b
}";

let expected_output = "func add(a: Int, b: Int) -> Int {
return a + b
}
";

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

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

assert_eq!(expected_output, output);
}
}
5 changes: 4 additions & 1 deletion src/languages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub enum Language {
Scala,
Shell,
Sql,
Swift,
Toml,
TypeScript,
Vue,
Expand All @@ -48,7 +49,6 @@ pub enum Language {
Zig,
// TODO: PHP,
// TODO: FSharp,
// TODO: Swift,
// TODO: Svelte,
// TODO: Julia,
// TODO: Dockerfile,
Expand Down Expand Up @@ -95,6 +95,7 @@ pub mod rust;
pub mod scala;
pub mod shell;
pub mod sql;
pub mod swift;
pub mod toml;
pub mod typescript;
pub mod vue;
Expand Down Expand Up @@ -151,6 +152,7 @@ impl Language {
"sql" | "bigquery" | "db2" | "db2i" | "hive" | "mariadb" | "mysql" | "n1ql"
| "plsql" | "postgresql" | "redshift" | "singlestoredb" | "snowflake" | "spark"
| "sqlite" | "transactsql" | "trino" | "tsql" => Some(Self::Sql),
"swift" => Some(Self::Swift),
"toml" => Some(Self::Toml),
"typescript" | "ts" | "tsx" => Some(Self::TypeScript),
"vue" => Some(Self::Vue),
Expand Down Expand Up @@ -201,6 +203,7 @@ impl Language {
Self::Shell => ".sh",
Self::Scala => ".scala",
Self::Sql => ".sql",
Self::Swift => ".swift",
Self::Toml => ".toml",
Self::TypeScript => ".ts",
Self::Vue => ".vue",
Expand Down
Loading

0 comments on commit b6eefd8

Please sign in to comment.