vscode-sqltools icon indicating copy to clipboard operation
vscode-sqltools copied to clipboard

Formatting support for Jinja templating

Open samkessaram opened this issue 5 years ago • 9 comments

The problem: Short version: the formatter breaks Jinja templating syntax.

I'm developing models using dbt which uses Jinja templating for interpolating variables, introducing control flow structures, and reusing macros.

e.g.

SELECT
  *
FROM {{ var('events_table') }}
WHERE
  livemode = true

dbt is pretty widely used (1.2k stars on GitHub), and there's at least one medium sized project that also uses Jinja with SQL.

Currently the formatter changes the above code from something like this:

{{ var('events_table') }}

into this:

{ { var('events_table') } }

which breaks the templating engine.

It would be awesome if there was a configuration option to preserve the Jinja formatting.

There are three cases:

Expressions {{ ... }}: Expressions are used when you want to output a string. You can use expressions to reference variable and call macros.

Statements {% ... %}: Statements are used for control flow, for example, to set up for loops and if statements, or to define macros.

Comments {# ... #}: Jinja comments are used to prevent the text within the comment from compiling.

(source: https://docs.getdbt.com/docs/getting-started-with-jinja)

The solution: A configuration option where we can declare that we'd like to preserve Jinja formatting. When enabled, it should not mess up the spacing described above.

Something like:

  "sqltools.format": {
    "indentSize": 2,
    "reservedWordCase": "upper",
    "preserveJinja": true
  }

samkessaram avatar Dec 05 '19 22:12 samkessaram

hi @samkessaram

I can't do this right now. Is a bit out of scope for the project. But you can file a PR if you want, I'll would be glad if you could. Do you have any experience with javascript?

mtxr avatar Feb 07 '20 23:02 mtxr

hi @samkessaram

I can't do this right now. Is a bit out of scope for the project. But you can file a PR if you want, I'll would be glad if you could. Do you have any experience with javascript?

Hey @mtxr! Turns out I have some time to attempt this. I'll submit a PR fairly soon. Thanks for the project by the way!

samkessaram avatar Mar 27 '20 03:03 samkessaram

As I'm struggling with the same issue I was wondering if this feature has been implemented? According to #531 it was not merged because of code formatting issues?

Closing as there's more work to be done here re: indentation.

Is it planned to implement this feature or what exactly is missing in #531. Maybe I can help out, although my javascript experience is not vast.

tepene avatar May 26 '21 07:05 tepene

Hey @tepene, I got sidetracked and this became less of a priority, though I'm game for opening it back up. I can't remember fully what indentation issues I was referring to in #531 but when I get a chance to look into it I'll let you know. Feel pull that branch and play around.

samkessaram avatar May 27 '21 19:05 samkessaram

I could also really use this!

cgstaber avatar Sep 09 '21 13:09 cgstaber

Any update here? Have folks found a workaround? I use DBT so this functionality is absolutely needed in an SQL formatter.

moleary-gsa avatar Jun 27 '23 14:06 moleary-gsa

Yes, I want to add that this breaks DBT code on save by adding a space between curly brackets. Took me a bit of debugging to figure out it was this extension causing it.

alexanderfifefd avatar Mar 18 '24 15:03 alexanderfifefd

is this shipped? or any workarounds? or any other package recommendations?

ishpreet95 avatar Mar 21 '24 07:03 ishpreet95

@ishpreet no developments here, but as a workaround you could try this to disable the SQLTools formatter:

  1. Add "sqltools.formatLanguages": [], to your settings.json
  2. Restart VS Code

Please let us know if this works.

gjsjohnmurray avatar Mar 21 '24 11:03 gjsjohnmurray