Adding Stan parsing/highlihgting/etc to nvim-treesitter

Since I just went on this adventure I figured I’d put it somewhere for the common good; writing the blog post I wish I had three days ago.

To get nvim-treesitter support for Stan you need to do two things: i) find a parser and configure treesitter to use it; ii) add some treesitter queries for the new parser. This all is described in this post on the Stan forums but I because I’m new to treesitting I needed some more details, which I describe below:

i) get a parser

The link above provides a Stan parser on github; you can add the repo itself as a source for the stan parser. You need the below incantation in your nvim config:

local parser_config = require('nvim-treesitter.parsers').get_parser_configs()
parser_config.stan = {
  install_info = {
    url = 'https://github.com/WardBrian/tree-sitter-stan',
    files = { 'src/parser.c' }, 
    branch = 'main',
  },
}

vim.filetype.add {
  extension = { stan = 'stan' },
} 

If, like me, you have your nvim config as vim script rather than lua you need to wrap the above incantation in another incantation that does inline lua from vimscript:

lua << EOF
    -- lua code goes here
EOF

Once this is done, reload your config and do :TSInstall stan.

ii) add queries

I had no idea what was meant by this, but as it turns out what you have to do is to copy the query files from the WardBrian repo to a place where nvim will look for them. The simplest solution for me: ~/.config/nvim/queries/stan/. These files are very important as they are what treesitter use for highlighting and such.

Backlinks:

this file last touched 2024.11.20