Skip to content

Commit 1202ab6

Browse files
oriori1703tiagobnoronha
authored andcommitted
Auto install treesitter parsers when opening a file
closes nvim-lua#1951
1 parent aa5e8a0 commit 1202ab6

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
@@ -1200,7 +1200,18 @@ require('lazy').setup({
12001200
local language = vim.treesitter.language.get_lang(filetype)
12011201
if not language then return end
12021202

1203-
treesitter_try_attach(buf, language)
1203+
local installed_parsers = require('nvim-treesitter').get_installed 'parsers'
1204+
1205+
if vim.tbl_contains(installed_parsers, language) then
1206+
-- enable the parser if it is installed
1207+
treesitter_try_attach(buf, language)
1208+
elseif vim.tbl_contains(available_parsers, language) then
1209+
-- if a parser is available in `nvim-treesitter` auto install it, and enable it after the installation is done
1210+
require('nvim-treesitter').install(language):await(function() treesitter_try_attach(buf, language) end)
1211+
else
1212+
-- try to enable treesitter features in case the parser exists but is not available from `nvim-treesitter`
1213+
treesitter_try_attach(buf, language)
1214+
end
12041215
end,
12051216
})
12061217
end,

0 commit comments

Comments
 (0)