diff --git a/.github/workflows/validate.yml b/.github/workflows/validate.yml index 25afbe7..db62706 100644 --- a/.github/workflows/validate.yml +++ b/.github/workflows/validate.yml @@ -83,10 +83,14 @@ jobs: - name: Install ruff run: pip install ruff - - name: Validate ruff run: ruff --version + - name: Install sqlfluff + run: pip install sqlfluff + - name: Validate sqlfluff + run: sqlfluff format --version + - name: Validate biome run: npx --yes @biomejs/biome --version diff --git a/README.md b/README.md index b453e2f..633c7ee 100644 --- a/README.md +++ b/README.md @@ -45,25 +45,25 @@ mdsf init > > Only formatters that are already installed will be used. -| Language | Formatters | -| ---------- | ------------------- | -| CSS | `prettier` | -| Dart | `dart_format` | -| Elixir | `mix_format` | -| Gleam | `gleam_format` | -| Go | `gofmt`, `gofumpt` | -| HTML | `prettier` | -| JSON | `prettier`, `biome` | -| JavaScript | `prettier`, `biome` | -| Lua | `stylua` | -| Nim | `nimpretty` | -| Python | `ruff` | -| Ruby | `rubocop` | -| Rust | `rustfmt` | -| SQL | `sql-formatter` | -| Shell | `shfmt` | -| TOML | `taplo` | -| TypeScript | `prettier`, `biome` | -| Vue | `prettier` | -| YAML | `prettier` | -| Zig | `zigfmt` | +| Language | Formatters | +| ---------- | --------------------------- | +| CSS | `prettier` | +| Dart | `dart_format` | +| Elixir | `mix_format` | +| Gleam | `gleam_format` | +| Go | `gofmt`, `gofumpt` | +| HTML | `prettier` | +| JSON | `prettier`, `biome` | +| JavaScript | `prettier`, `biome` | +| Lua | `stylua` | +| Nim | `nimpretty` | +| Python | `ruff` | +| Ruby | `rubocop` | +| Rust | `rustfmt` | +| SQL | `sqlfluff`, `sql-formatter` | +| Shell | `shfmt` | +| TOML | `taplo` | +| TypeScript | `prettier`, `biome` | +| Vue | `prettier` | +| YAML | `prettier` | +| Zig | `zigfmt` | diff --git a/schemas/v0.0.0/mdsf.schema.json b/schemas/v0.0.0/mdsf.schema.json index 47a7725..a1012d2 100644 --- a/schemas/v0.0.0/mdsf.schema.json +++ b/schemas/v0.0.0/mdsf.schema.json @@ -171,7 +171,7 @@ "sql": { "default": { "enabled": true, - "formatter": "sql-formatter" + "formatter": "sqlfluff" }, "allOf": [ { @@ -530,10 +530,6 @@ "type": "string", "enum": ["rustfmt"] }, - "SQLFormatter": { - "type": "string", - "enum": ["sql-formatter"] - }, "Shell": { "type": "object", "properties": { @@ -563,15 +559,19 @@ "type": "boolean" }, "formatter": { - "default": "sql-formatter", + "default": "sqlfluff", "allOf": [ { - "$ref": "#/definitions/SQLFormatter" + "$ref": "#/definitions/SqlFormatter" } ] } } }, + "SqlFormatter": { + "type": "string", + "enum": ["sqlfluff", "sql-formatter"] + }, "Toml": { "type": "object", "properties": { diff --git a/src/formatters/mod.rs b/src/formatters/mod.rs index 9443871..d1b00af 100644 --- a/src/formatters/mod.rs +++ b/src/formatters/mod.rs @@ -23,6 +23,7 @@ pub mod ruff; pub mod rustfmt; pub mod shfmt; pub mod sql_formatter; +pub mod sqlfluff; pub mod stylua; pub mod taplo; pub mod zigfmt; diff --git a/src/formatters/sqlfluff.rs b/src/formatters/sqlfluff.rs new file mode 100644 index 0000000..6d15362 --- /dev/null +++ b/src/formatters/sqlfluff.rs @@ -0,0 +1,52 @@ +use super::execute_command; + +#[inline] +fn set_sqlfluff_args(cmd: &mut std::process::Command, snippet_path: &std::path::Path) { + cmd.arg("format") + .arg("--dialect") + // TODO: custom dialect? + .arg("ansi") + .arg(snippet_path); +} + +#[inline] +fn invote_sqlfluff( + mut cmd: std::process::Command, + snippet_path: &std::path::Path, +) -> std::io::Result<(bool, Option)> { + set_sqlfluff_args(&mut cmd, snippet_path); + + execute_command(&mut cmd, snippet_path) +} + +#[inline] +pub fn format_using_sqlfluff( + snippet_path: &std::path::Path, +) -> std::io::Result<(bool, Option)> { + invote_sqlfluff(std::process::Command::new("sqlfluff"), snippet_path) +} + +#[cfg(test)] +mod test_sqlfluff { + use crate::{formatters::setup_snippet, languages::Language}; + + #[test] + fn it_should_format_sql() { + let input = "SELECT * FROM tbl + WHERE foo = 'bar'; "; + + let expected_output = "SELECT * FROM tbl +WHERE foo = 'bar'; +"; + + let snippet = + setup_snippet(input, Language::Sql.to_file_ext()).expect("it to create a snippet file"); + + let output = super::format_using_sqlfluff(snippet.path()) + .expect("it to be successful") + .1 + .expect("it to be some"); + + assert_eq!(expected_output, output); + } +} diff --git a/src/languages/sql.rs b/src/languages/sql.rs index 68f5ce1..1c65ac8 100644 --- a/src/languages/sql.rs +++ b/src/languages/sql.rs @@ -1,12 +1,17 @@ use schemars::JsonSchema; -use crate::{config::default_enabled, formatters::sql_formatter::format_using_sql_formatter}; +use crate::{ + config::default_enabled, formatters::sql_formatter::format_using_sql_formatter, + formatters::sqlfluff::format_using_sqlfluff, +}; use super::LanguageFormatter; #[derive(Debug, Default, serde::Serialize, serde::Deserialize, JsonSchema)] -pub enum SQLFormatter { +pub enum SqlFormatter { #[default] + #[serde(rename = "sqlfluff")] + Sqlfluff, #[serde(rename = "sql-formatter")] SQLFormatter, } @@ -16,7 +21,7 @@ pub struct Sql { #[serde(default = "default_enabled")] pub enabled: bool, #[serde(default)] - pub formatter: SQLFormatter, + pub formatter: SqlFormatter, } impl Default for Sql { @@ -24,7 +29,7 @@ impl Default for Sql { fn default() -> Self { Self { enabled: true, - formatter: SQLFormatter::default(), + formatter: SqlFormatter::default(), } } } @@ -37,7 +42,8 @@ impl LanguageFormatter for Sql { } match self.formatter { - SQLFormatter::SQLFormatter => format_using_sql_formatter(snippet_path).map(|res| res.1), + SqlFormatter::SQLFormatter => format_using_sql_formatter(snippet_path).map(|res| res.1), + SqlFormatter::Sqlfluff => format_using_sqlfluff(snippet_path).map(|res| res.1), } } }