Skip to content

Commit 942d117

Browse files
committed
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.
1 parent 4b065ad commit 942d117

1 file changed

Lines changed: 42 additions & 46 deletions

File tree

init.lua

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -888,53 +888,49 @@ require('lazy').setup({
888888
branch = 'main',
889889
-- [[ Configure Treesitter ]] See `:help nvim-treesitter-intro`
890890
config = function()
891-
-- ensure basic parser are installed
892-
local parsers = { 'bash', 'c', 'diff', 'html', 'lua', 'luadoc', 'markdown', 'markdown_inline', 'query', 'vim', 'vimdoc' }
893-
require('nvim-treesitter').install(parsers)
894-
895-
---@param buf integer
896-
---@param language string
897-
local function treesitter_try_attach(buf, language)
898-
-- check if parser exists and load it
899-
if not vim.treesitter.language.add(language) then return end
900-
-- enables syntax highlighting and other treesitter features
901-
vim.treesitter.start(buf, language)
902-
903-
-- enables treesitter based folds
904-
-- for more info on folds see `:help folds`
905-
-- vim.wo.foldexpr = 'v:lua.vim.treesitter.foldexpr()'
906-
-- vim.wo.foldmethod = 'expr'
907-
908-
-- check if treesitter indentation is available for this language, and if so enable it
909-
-- in case there is no indent query, the indentexpr will fallback to the vim's built in one
910-
local has_indent_query = vim.treesitter.query.get(language, 'indents') ~= nil
911-
912-
-- enables treesitter based indentation
913-
if has_indent_query then vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()" end
914-
end
891+
require('nvim-treesitter.configs').setup {
892+
-- Added to appease the Lua Language Server strict typing
893+
modules = {},
894+
ignore_install = {},
895+
896+
-- Ensure these basic parsers are installed
897+
ensure_installed = {
898+
'bash',
899+
'c',
900+
'diff',
901+
'html',
902+
'lua',
903+
'luadoc',
904+
'markdown',
905+
'markdown_inline',
906+
'query',
907+
'vim',
908+
'vimdoc'
909+
},
915910

916-
local available_parsers = require('nvim-treesitter').get_available()
917-
vim.api.nvim_create_autocmd('FileType', {
918-
callback = function(args)
919-
local buf, filetype = args.buf, args.match
920-
921-
local language = vim.treesitter.language.get_lang(filetype)
922-
if not language then return end
923-
924-
local installed_parsers = require('nvim-treesitter').get_installed 'parsers'
925-
926-
if vim.tbl_contains(installed_parsers, language) then
927-
-- enable the parser if it is installed
928-
treesitter_try_attach(buf, language)
929-
elseif vim.tbl_contains(available_parsers, language) then
930-
-- if a parser is available in `nvim-treesitter` auto install it, and enable it after the installation is done
931-
require('nvim-treesitter').install(language):await(function() treesitter_try_attach(buf, language) end)
932-
else
933-
-- try to enable treesitter features in case the parser exists but is not available from `nvim-treesitter`
934-
treesitter_try_attach(buf, language)
935-
end
936-
end,
937-
})
911+
-- Automatically install missing parsers when entering a buffer
912+
auto_install = true,
913+
914+
-- Install parsers synchronously (only applied to `ensure_installed`)
915+
sync_install = false,
916+
917+
-- Enable syntax highlighting
918+
highlight = {
919+
enable = true,
920+
-- Some languages depend on vim's regex highlighting system (such as Ruby) for indent rules.
921+
-- If you are experiencing weird indenting issues, add the language to the list of additional_vim_regex_highlighting and disabled languages for indent.
922+
additional_vim_regex_highlighting = false,
923+
},
924+
925+
-- Enable treesitter-based indentation
926+
indent = {
927+
enable = true,
928+
},
929+
}
930+
931+
-- If you want to enable your treesitter-based folds, you can keep this here:
932+
-- vim.wo.foldexpr = 'v:lua.vim.treesitter.foldexpr()'
933+
-- vim.wo.foldmethod = 'expr'
938934
end,
939935
},
940936

0 commit comments

Comments
 (0)