Treesitter parser in neovim

Does anyone have experience using the tree-sitter parser for Stan with neovim? I followed the instructions here and added the queries manually, as suggested here, but I still don’t get any highlighting. Thanks!

This is something that @WardBrian may know. He recently went through and tried to unify a lot of our syntax highlighters and bring them up to date.

I am an Emacs user, so neovim is a bit foreign to me. If you figure something out @Jakob I would be happy to add instructions to the README of that repo

A-ha! I was able to get highlighting by running :TSBufEnable highlight on my buffer.

Like I said, not really a neovim user, so I mostly copied and pasted a config from someone else:

local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({
        "git",
        "clone",
        "--filter=blob:none",
        "https://github.com/folke/lazy.nvim.git",
        "--branch=stable", -- latest stable release
        lazypath,
    })
end
vim.opt.rtp:prepend(lazypath)

require("lazy").setup({
    {
        "nvim-treesitter/nvim-treesitter",
        build = ":TSUpdate",
        version = false,
        config = function()
            require("nvim-treesitter.configs").setup({
                ensure_installed = {
                    "c",
                    "lua",
                    "vim",
                    "vimdoc",
                },
            })
        end,
    },
})

local parser_config = require "nvim-treesitter.parsers".get_parser_configs()
parser_config.stan = {
  install_info = {
    url = "~/nvim-test/node_modules/@wardbrian/tree-sitter-stan", -- local path or git repo
    files = {"src/parser.c"}, -- note that some parsers also require src/scanner.c or src/scanner.cc
    -- optional entries:
    branch = "main", -- default branch in case of git repo if different from master
    generate_requires_npm = false, -- if stand-alone parser without npm dependencies
    requires_generate_from_grammar = false, -- if folder contains pre-generated src/parser.c
  },
  filetype = "stan", -- if filetype does not match the parser name
}


vim.filetype.add({
 extension = {
  stan="stan"
}
})

I’m not sure why this is necessary, but I’m sure someone more experienced with vim could figure out a way to set it by default.

1 Like

Perfect, thanks a lot, I got it working now. The relevant bit is only the bottom part and what I was missing was the vim.filetype.add().

For posterity (or in case you want to add it to the repo), here’s the minimal addition to my init.lua which got me working stan syntax highlighting (together with manually adding the queries):

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' },
}

2 Likes

Glad to hear it! I’ll try to add some installation notes.

I realized that the tree-sitter package isn’t listed on the Stan website under highlighting - how did you find it if I may ask?

3 Likes

I just googled for stan tree-sitter support and came across your repo, which was newer than the other one.