From ce8cc5a718cad7848681a90c57692e8e9cb8b3d5 Mon Sep 17 00:00:00 2001 From: Jazzcort Date: Mon, 4 May 2026 11:19:22 -0400 Subject: [PATCH] fix: change config for treesitter Both `install()` and `get_available()` are nil which causes the error while spawning nvim and make the treesitter not working. Replacing the custom `FileType` autocmd and manual parser attachment logic with the standard `nvim-treesitter.configs` setup function seems to work fine. --- init.lua | 88 +++++++++++++++++++++++++++----------------------------- 1 file changed, 42 insertions(+), 46 deletions(-) diff --git a/init.lua b/init.lua index 8d1f19a10d4..f7ac72e90d3 100644 --- a/init.lua +++ b/init.lua @@ -888,53 +888,49 @@ require('lazy').setup({ branch = 'main', -- [[ Configure Treesitter ]] See `:help nvim-treesitter-intro` config = function() - -- ensure basic parser are installed - local parsers = { 'bash', 'c', 'diff', 'html', 'lua', 'luadoc', 'markdown', 'markdown_inline', 'query', 'vim', 'vimdoc' } - require('nvim-treesitter').install(parsers) - - ---@param buf integer - ---@param language string - local function treesitter_try_attach(buf, language) - -- check if parser exists and load it - if not vim.treesitter.language.add(language) then return end - -- enables syntax highlighting and other treesitter features - vim.treesitter.start(buf, language) - - -- enables treesitter based folds - -- for more info on folds see `:help folds` - -- vim.wo.foldexpr = 'v:lua.vim.treesitter.foldexpr()' - -- vim.wo.foldmethod = 'expr' - - -- check if treesitter indentation is available for this language, and if so enable it - -- in case there is no indent query, the indentexpr will fallback to the vim's built in one - local has_indent_query = vim.treesitter.query.get(language, 'indents') ~= nil - - -- enables treesitter based indentation - if has_indent_query then vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()" end - end + require('nvim-treesitter.configs').setup { + -- Added to appease the Lua Language Server strict typing + modules = {}, + ignore_install = {}, + + -- Ensure these basic parsers are installed + ensure_installed = { + 'bash', + 'c', + 'diff', + 'html', + 'lua', + 'luadoc', + 'markdown', + 'markdown_inline', + 'query', + 'vim', + 'vimdoc', + }, - local available_parsers = require('nvim-treesitter').get_available() - vim.api.nvim_create_autocmd('FileType', { - callback = function(args) - local buf, filetype = args.buf, args.match - - local language = vim.treesitter.language.get_lang(filetype) - if not language then return end - - local installed_parsers = require('nvim-treesitter').get_installed 'parsers' - - if vim.tbl_contains(installed_parsers, language) then - -- enable the parser if it is installed - treesitter_try_attach(buf, language) - elseif vim.tbl_contains(available_parsers, language) then - -- if a parser is available in `nvim-treesitter` auto install it, and enable it after the installation is done - require('nvim-treesitter').install(language):await(function() treesitter_try_attach(buf, language) end) - else - -- try to enable treesitter features in case the parser exists but is not available from `nvim-treesitter` - treesitter_try_attach(buf, language) - end - end, - }) + -- Automatically install missing parsers when entering a buffer + auto_install = true, + + -- Install parsers synchronously (only applied to `ensure_installed`) + sync_install = false, + + -- Enable syntax highlighting + highlight = { + enable = true, + -- Some languages depend on vim's regex highlighting system (such as Ruby) for indent rules. + -- If you are experiencing weird indenting issues, add the language to the list of additional_vim_regex_highlighting and disabled languages for indent. + additional_vim_regex_highlighting = false, + }, + + -- Enable treesitter-based indentation + indent = { + enable = true, + }, + } + + -- If you want to enable your treesitter-based folds, you can keep this here: + -- vim.wo.foldexpr = 'v:lua.vim.treesitter.foldexpr()' + -- vim.wo.foldmethod = 'expr' end, },