tconbeer / sqlfmt

sqlfmt formats your dbt SQL files so you don't have to
https://sqlfmt.com
Apache License 2.0
364 stars 15 forks source link

feat: Support parsing of SQL code blocks in Markdown files #598

Open michael-the1 opened 1 month ago

michael-the1 commented 1 month ago

Closes https://github.com/tconbeer/sqlfmt/issues/593

There are two caveats that would be good to consider:

  1. Because we parse to AST -> rebuild from AST, we can actually lose some trivial whitespace characters. mistletoe keeps all the important whitespace, but can toss out some unnecessary whitespace, such as trailing whitespace or double newlines. A good example is in the README.md of this very repo:
 ~/sqlfmt
❯ poetry run sqlfmt README.md
1 file formatted.
0 files left unchanged.
README.md formatted.

 ~/sqlfmt
❯ git diff
diff --git a/README.md b/README.md
index 7d292f9..5a3f457 100755
--- a/README.md
+++ b/README.md
@@ -9,7 +9,7 @@

 sqlfmt formats your dbt SQL files so you don't have to. It is similar in nature to black, gofmt,
-and rustfmt (but for SQL).
+and rustfmt (but for SQL).

 1. **sqlfmt promotes collaboration.** An auto-formatter makes it easier to collaborate with your team and solicit contributions from new people. You will never have to mention (or argue about) code style in code reviews again.
 2. **sqlfmt is fast.** Forget about formatting your code, and spend your time on business logic instead. sqlfmt processes hundreds of files per second and only operates on files that have changed since the last run.
@@ -138,7 +138,6 @@ Config for other integrations is detailed in the docs linked below:
 - [SQLFluff](https://docs.sqlfmt.com/integrations/sqlfluff)
 - [VSCode](https://docs.sqlfmt.com/integrations/vs-code)

-
 ## The sqlfmt style
 The only thing you can configure with sqlfmt is the desired line length of the formatted file. You can do this with the `--line-length` or `-l` options. The default is 88.

I think this is acceptable since it does not affect readability and should not affect output (i.e. when it's rendered to HTML).

  1. Nested sql blocks are not formatted. I think this is acceptable. See example:

``````markdown

# Hello

```sql
SELECT 1 -- this is not formatted
tconbeer commented 1 month ago

Thank you for this! I'm mostly away from my computer this week but I'll try to review next week.

tconbeer commented 1 day ago

@michael-the1 Sorry I dropped the ball on this. I'd love to get this finalized. Looks like there are quite a few failing tests and lints. Do you want to work through those? Let me know if you get stuck.

michael-the1 commented 17 hours ago

@tconbeer Life can get busy which I totally understand.

The reason the tests are failing is because mistletoe is not installed in the test workflows. Should I add it to the dev or test group in pyproject.toml? Feels a bit... not neat 😅