Skip to content

Commit 2442c57

Browse files
committed
Automatically install treesitter parsers
1 parent 1f57f13 commit 2442c57

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

init.lua

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -945,23 +945,54 @@ require('lazy').setup({
945945
branch = 'main',
946946
-- [[ Configure Treesitter ]] See `:help nvim-treesitter-intro`
947947
config = function()
948-
local parsers = { 'bash', 'c', 'diff', 'html', 'lua', 'luadoc', 'markdown', 'markdown_inline', 'query', 'vim', 'vimdoc' }
949-
require('nvim-treesitter').install(parsers)
948+
---@param buf integer
949+
---@param language string
950+
---@return boolean
951+
local function treesitter_attach(buf, language)
952+
-- check if parser exists before starting highlighter
953+
if not vim.treesitter.language.add(language) then
954+
return false
955+
end
956+
-- enables syntax highlighting and other treesitter features
957+
vim.treesitter.start(buf, language)
958+
959+
-- enables treesitter based folds
960+
-- for more info on folds see `:help folds`
961+
-- vim.wo.foldexpr = 'v:lua.vim.treesitter.foldexpr()'
950962

963+
-- enables treesitter based indentation
964+
vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
965+
return true
966+
end
967+
968+
local available_parsers = require('nvim-treesitter.config').get_available()
951969
vim.api.nvim_create_autocmd('FileType', {
952-
pattern = parsers,
953-
callback = function()
954-
-- enables syntax highlighting and other treesitter features
955-
vim.treesitter.start()
970+
callback = function(args)
971+
local buf, filetype = args.buf, args.match
972+
local language = vim.treesitter.language.get_lang(filetype)
973+
if not language then
974+
return
975+
end
956976

957-
-- enables treesitter based folds
958-
-- for more info on folds see `:help folds`
959-
-- vim.wo.foldexpr = 'v:lua.vim.treesitter.foldexpr()'
977+
if not (treesitter_attach(buf, language) or vim.tbl_contains(available_parsers, language)) then
978+
return
979+
end
960980

961-
-- enables treesitter based indentation
962-
vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
981+
-- automaically install parser for missing languages
982+
-- attempt to install even if it is available accoring to `vim.treesitter.langauge.add()`,
983+
-- to ensure the latest version is installed using `nvim-treesitter`, instead of the outdated vendored parser
984+
if not vim.tbl_contains(require('nvim-treesitter.config').get_installed 'parsers', language) then
985+
-- attempt to start highlighter after installing missing language
986+
require('nvim-treesitter.install').install(language):await(function()
987+
treesitter_attach(buf, language)
988+
end)
989+
end
963990
end,
964991
})
992+
993+
-- ensure basic parser are installed
994+
local parsers = { 'bash', 'c', 'diff', 'html', 'lua', 'luadoc', 'markdown', 'markdown_inline', 'query', 'vim', 'vimdoc' }
995+
require('nvim-treesitter').install(parsers)
965996
end,
966997
-- There are additional nvim-treesitter modules that you can use to interact
967998
-- with nvim-treesitter. You should go explore a few and see what interests you:

0 commit comments

Comments
 (0)