diff --git a/.config/nvim/lua/plugins/nvim-treesitter.lua b/.config/nvim/lua/plugins/nvim-treesitter.lua index 9ce5f78..25774cc 100644 --- a/.config/nvim/lua/plugins/nvim-treesitter.lua +++ b/.config/nvim/lua/plugins/nvim-treesitter.lua @@ -1,11 +1,37 @@ return { "nvim-treesitter/nvim-treesitter", + lazy = false, build = ":TSUpdate", config = function() - require("nvim-treesitter.configs").setup({ - auto_install = true, - highlight = { enable = true }, - indent = { enable = true }, + local ts = require("nvim-treesitter") + local available = {} + for _, lang in ipairs(ts.get_available()) do + available[lang] = true + end + + local function enable(buf) + if not vim.api.nvim_buf_is_valid(buf) or not pcall(vim.treesitter.start, buf) then + return + end + vim.bo[buf].indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()" + end + + vim.api.nvim_create_autocmd("FileType", { + callback = function(args) + local lang = vim.treesitter.language.get_lang(args.match) + if not lang or not available[lang] then + return + end + if vim.list_contains(ts.get_installed(), lang) then + enable(args.buf) + else + ts.install(lang):await(function() + vim.schedule(function() + enable(args.buf) + end) + end) + end + end, }) end, }