Skip to content

Commit e7707c3

Browse files
oriori1703fernandomv3
authored andcommitted
Auto install treesitter parsers when opening a file
closes nvim-lua#1951
1 parent 4c33874 commit e7707c3

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

init.lua

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,18 @@ require('lazy').setup({
917917
local language = vim.treesitter.language.get_lang(filetype)
918918
if not language then return end
919919

920-
treesitter_try_attach(buf, language)
920+
local installed_parsers = require('nvim-treesitter').get_installed 'parsers'
921+
922+
if vim.tbl_contains(installed_parsers, language) then
923+
-- enable the parser if it is installed
924+
treesitter_try_attach(buf, language)
925+
elseif vim.tbl_contains(available_parsers, language) then
926+
-- if a parser is available in `nvim-treesitter` auto install it, and enable it after the installation is done
927+
require('nvim-treesitter').install(language):await(function() treesitter_try_attach(buf, language) end)
928+
else
929+
-- try to enable treesitter features in case the parser exists but is not available from `nvim-treesitter`
930+
treesitter_try_attach(buf, language)
931+
end
921932
end,
922933
})
923934
end,

0 commit comments

Comments
 (0)