Skip to content

Commit e01e1eb

Browse files
committed
Auto install treesitter parsers when opening a file
closes #1951
1 parent 8ac4b12 commit e01e1eb

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

init.lua

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

906-
treesitter_try_attach(buf, language)
906+
local installed_parsers = require('nvim-treesitter').get_installed 'parsers'
907+
908+
if vim.tbl_contains(installed_parsers, language) then
909+
-- enable the parser if it is installed
910+
treesitter_try_attach(buf, language)
911+
elseif vim.tbl_contains(available_parsers, language) then
912+
-- if a parser is available in `nvim-treesitter` auto install it, and enable it after the installation is done
913+
require('nvim-treesitter').install(language):await(function() treesitter_try_attach(buf, language) end)
914+
else
915+
-- try to enable treesitter features in case the parser exists but is not available from `nvim-treesitter`
916+
treesitter_try_attach(buf, language)
917+
end
907918
end,
908919
})
909920
end,

0 commit comments

Comments
 (0)