Skip to content

About

Jinja-aware filetype for dbt models in Neovim

Topics

Resources

Stars

4 stars

Watchers

0 watching

Forks

Repository files navigation

dbt-ft.nvim

A Jinja-aware filetype for dbt models in Neovim.

Neovim opens dbt models as plain SQL, so the SQL parser trips over every {{ ref() }} and none of the Jinja is highlighted. dbt-ft.nvim gives models their own dbt filetype, which Tree-sitter parses as Jinja with the SQL between the tags handed to the SQL parser. Highlighting, folding, and indenting then understand both languages, and a dbt buffer still gets what Neovim gives a sql buffer.

The same dbt model without and with dbt-ft.nvim, with the lines SQL can't parse marked in red

There are no commands and no setup function.

Features

  • .sql files in a dbt project get the dbt filetype, except dbt's compiled output in target/.
  • The SQL around the Jinja tags, and each {{ }} expression, go to the SQL parser as one document, so from {{ ref('orders') }} where parses and highlights as SQL.
  • dbt's functions and context variables, such as ref, config, this, and target, highlight as built-ins, so they stand out from your own macros.
  • With Tree-sitter folding and indenting on, Jinja blocks fold, and SQL keeps its folds and indents.
  • The indent and 'textwidth' follow the project's SQLFluff config, with dbt Labs' four spaces where it sets no indent. A project's EditorConfig overrides both.
  • gc uses {# #} on a line that starts with a Jinja tag, indented or not, and -- elsewhere.
  • Enter or o on a -- comment line starts the new line with -- too.
  • % moves between a Jinja block's tags, such as {% if %}, {% else %}, and {% endif %}.
  • gf on a ref() opens the model it names.
  • With yaml-language-server, a project's YAML gets dbt's schemas for validation, completion, and hover, even when the project's directory doesn't have dbt in its name.

See :help dbt-ft for details and limitations.

Requirements

  • Neovim 0.12 or later.
  • The jinja, jinja_inline, and sql Tree-sitter parsers, for example from nvim-treesitter with :TSInstall jinja jinja_inline sql.

Installation

With vim.pack:

vim.pack.add({ "https://github.com/dobsontom/dbt-ft.nvim" })

With lazy.nvim:

{ "dobsontom/dbt-ft.nvim" }

Don't lazy-load it: it does almost nothing at startup, and it can't load on the dbt filetype, which only it detects.

Setup

LazyVim starts Tree-sitter for dbt buffers by itself. If your config starts it for a list of filetypes, add dbt to the list:

vim.api.nvim_create_autocmd("FileType", {
  pattern = { "dbt", "sql" },
  callback = function()
    vim.treesitter.start()
  end,
})

Language servers, formatters, and linters set up for sql don't run on dbt buffers, which keeps the ones that can't read Jinja off your models. Add dbt to those that can. See :help dbt-ft-formatting for conform.nvim and nvim-lint, as SQLFluff's dbt templater can't read the buffer, and :help dbt-ft-snippets for blink.cmp and LuaSnip.

The dbt language server, dbt lsp, comes with dbt v2, not dbt-core 1.x. With nvim-lspconfig, whose dbt config already covers the dbt filetype, enable it:

vim.lsp.enable("dbt")

See :help dbt-ft-lsp for setting it up without nvim-lspconfig.

Run :checkhealth dbt-ft to check the setup.

Options

Option Set it to To
vim.g.dbt_ft_yaml_schemas false or 0 leave yaml-language-server's schema mappings alone
vim.g.dbt_recommended_style false or 0 keep your own indent and 'textwidth' in dbt buffers
vim.g.loaded_dbt_ft 1, before it loads turn dbt-ft.nvim off

Development

make test runs the tests, and builds the parsers they need on its first run, which needs Git, a C compiler, and the tree-sitter CLI. make format and make lint also need StyLua, selene, lua-language-server, ts_query_ls, oxfmt, and jq. prek runs all three as Git hooks.

Licence

MIT

About

Jinja-aware filetype for dbt models in Neovim

Topics

Resources

Stars

4 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages