Skip to content

Commit

Permalink
feat(sql): support sqlfluff (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen authored Mar 9, 2024
1 parent ae21c85 commit 14e18e3
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 35 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
44 changes: 22 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
14 changes: 7 additions & 7 deletions schemas/v0.0.0/mdsf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
"sql": {
"default": {
"enabled": true,
"formatter": "sql-formatter"
"formatter": "sqlfluff"
},
"allOf": [
{
Expand Down Expand Up @@ -530,10 +530,6 @@
"type": "string",
"enum": ["rustfmt"]
},
"SQLFormatter": {
"type": "string",
"enum": ["sql-formatter"]
},
"Shell": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -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": {
Expand Down
1 change: 1 addition & 0 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
52 changes: 52 additions & 0 deletions src/formatters/sqlfluff.rs
Original file line number Diff line number Diff line change
@@ -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<String>)> {
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<String>)> {
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);
}
}
16 changes: 11 additions & 5 deletions src/languages/sql.rs
Original file line number Diff line number Diff line change
@@ -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,
}
Expand All @@ -16,15 +21,15 @@ pub struct Sql {
#[serde(default = "default_enabled")]
pub enabled: bool,
#[serde(default)]
pub formatter: SQLFormatter,
pub formatter: SqlFormatter,
}

impl Default for Sql {
#[inline]
fn default() -> Self {
Self {
enabled: true,
formatter: SQLFormatter::default(),
formatter: SqlFormatter::default(),
}
}
}
Expand All @@ -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),
}
}
}

0 comments on commit 14e18e3

Please sign in to comment.