Autoformat sqlx macros
Is your feature request related to a problem? Please describe. Currently, SQL query strings can not be auto-formatted.
Describe the solution you'd like Add an auto-format command to the CLI.
Describe alternatives you've considered Format all queries manually.
Additional context
As an precedence, Dioxus provides a fmt command in their CLI that can autoformat all rsx! macro invocations: https://github.com/DioxusLabs/dioxus/pull/454.
I'd love to have this! As you might imagine, though, it would be quite the lift.
You can also usually get some assistance from the IDE here. For example, in RustRover if you apply SQL language injection to the string literal, you can open the string in a new editor tab and do "reformat code" there.
Regarding IDE support: with neovim you can also make it work via:
- treesitter injections so SQL code blocks are highlighted as SQL
- a formatter plugin like
conform.nvimthat then can format the blocks
Found a relevant issue: https://github.com/launchbadge/sqlx/issues/2866. Re-using sqlfluff for this would be nice, though I'm not sure how this would work.
There's also sqruff, a native Rust port of sqlfluff made by the guys behind quary. Being written in Rust might make it easier to integrate to the CLI.
See also https://github.com/rust-lang/rustfmt/issues/5656 so the fmt from cli might also cooperate with rustfmt so a single cargo fmt could do the work.
Hi :)
I built a formatter CLI for SQL within SQLx query macros:
https://github.com/jflessau/sqlx-fmt
It uses sqruff for formatting SQL and tree-sitter for grabbing and replacing the raw string literals.
I've always wanted to have something like this, and I would be more than happy if others find it useful too.
This is also available:
https://github.com/code-trust/sqlx-inline-fmt