sqlx icon indicating copy to clipboard operation
sqlx copied to clipboard

Autoformat sqlx macros

Open musjj opened this issue 1 year ago • 7 comments

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.

musjj avatar Sep 21 '24 12:09 musjj

I'd love to have this! As you might imagine, though, it would be quite the lift.

abonander avatar Sep 22 '24 01:09 abonander

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.

abonander avatar Sep 22 '24 01:09 abonander

Regarding IDE support: with neovim you can also make it work via:

syphar avatar Sep 22 '24 05:09 syphar

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.

musjj avatar Sep 22 '24 16:09 musjj

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.

lu-zero avatar Dec 17 '24 13:12 lu-zero

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.

jflessau avatar Nov 07 '25 07:11 jflessau

This is also available:

https://github.com/code-trust/sqlx-inline-fmt

Ape avatar Nov 07 '25 13:11 Ape