Files
nvim-dotfiles/lua/plugins/treesitter.lua
T

80 lines
2.2 KiB
Lua

local env = require 'env'
local languages = {
'bash',
'c',
'cmake',
'cpp',
'diff',
'gitcommit',
'html',
'java',
'json',
'lua',
'luadoc',
'make',
'markdown',
'markdown_inline',
'python',
'query',
'regex',
'toml',
'systemverilog', -- one parser for both .v and .sv
'vim',
'vimdoc',
'yaml',
}
return {
'nvim-treesitter/nvim-treesitter',
branch = 'main',
lazy = false, -- the main branch does not support lazy-loading
build = ':TSUpdate',
config = function()
local ts = require 'nvim-treesitter'
ts.setup()
-- The main branch compiles parsers itself: it needs the tree-sitter CLI and a C compiler.
-- mason installs the CLI (lua/plugins/lsp.lua); wait until startup is done so mason's bin
-- directory is on PATH before we check.
local function can_compile()
return env.has 'tree-sitter' and (env.has 'cc' or env.has 'gcc' or env.has 'clang')
end
if not env.minimal then
vim.schedule(function()
if can_compile() then
ts.install(languages)
end
end)
end
vim.treesitter.language.register('systemverilog', { 'verilog' }) -- .v files use the same parser
local attempted = {} -- one install attempt per language per session, so being offline isn't noisy
vim.api.nvim_create_autocmd('FileType', {
group = vim.api.nvim_create_augroup('user-treesitter', { clear = true }),
callback = function(args)
local lang = vim.treesitter.language.get_lang(args.match)
if not lang then
return
end
if not pcall(vim.treesitter.start, args.buf, lang) then
-- parser missing: install it in the background, then highlight the buffer
if not attempted[lang] and not env.minimal and can_compile() and vim.tbl_contains(ts.get_available(), lang) then
attempted[lang] = true
ts.install(lang):await(function()
if vim.api.nvim_buf_is_valid(args.buf) then
pcall(vim.treesitter.start, args.buf, lang)
end
end)
end
return
end
vim.bo[args.buf].indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
end,
})
end,
}