Automatically install treesitter parsers
This commit is contained in:
parent
1f57f13bc9
commit
2442c57b0b
45
init.lua
45
init.lua
|
@ -945,14 +945,16 @@ require('lazy').setup({
|
||||||
branch = 'main',
|
branch = 'main',
|
||||||
-- [[ Configure Treesitter ]] See `:help nvim-treesitter-intro`
|
-- [[ Configure Treesitter ]] See `:help nvim-treesitter-intro`
|
||||||
config = function()
|
config = function()
|
||||||
local parsers = { 'bash', 'c', 'diff', 'html', 'lua', 'luadoc', 'markdown', 'markdown_inline', 'query', 'vim', 'vimdoc' }
|
---@param buf integer
|
||||||
require('nvim-treesitter').install(parsers)
|
---@param language string
|
||||||
|
---@return boolean
|
||||||
vim.api.nvim_create_autocmd('FileType', {
|
local function treesitter_attach(buf, language)
|
||||||
pattern = parsers,
|
-- check if parser exists before starting highlighter
|
||||||
callback = function()
|
if not vim.treesitter.language.add(language) then
|
||||||
|
return false
|
||||||
|
end
|
||||||
-- enables syntax highlighting and other treesitter features
|
-- enables syntax highlighting and other treesitter features
|
||||||
vim.treesitter.start()
|
vim.treesitter.start(buf, language)
|
||||||
|
|
||||||
-- enables treesitter based folds
|
-- enables treesitter based folds
|
||||||
-- for more info on folds see `:help folds`
|
-- for more info on folds see `:help folds`
|
||||||
|
@ -960,8 +962,37 @@ require('lazy').setup({
|
||||||
|
|
||||||
-- enables treesitter based indentation
|
-- enables treesitter based indentation
|
||||||
vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
|
vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
local available_parsers = require('nvim-treesitter.config').get_available()
|
||||||
|
vim.api.nvim_create_autocmd('FileType', {
|
||||||
|
callback = function(args)
|
||||||
|
local buf, filetype = args.buf, args.match
|
||||||
|
local language = vim.treesitter.language.get_lang(filetype)
|
||||||
|
if not language then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if not (treesitter_attach(buf, language) or vim.tbl_contains(available_parsers, language)) then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
-- automaically install parser for missing languages
|
||||||
|
-- attempt to install even if it is available accoring to `vim.treesitter.langauge.add()`,
|
||||||
|
-- to ensure the latest version is installed using `nvim-treesitter`, instead of the outdated vendored parser
|
||||||
|
if not vim.tbl_contains(require('nvim-treesitter.config').get_installed 'parsers', language) then
|
||||||
|
-- attempt to start highlighter after installing missing language
|
||||||
|
require('nvim-treesitter.install').install(language):await(function()
|
||||||
|
treesitter_attach(buf, language)
|
||||||
|
end)
|
||||||
|
end
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
-- ensure basic parser are installed
|
||||||
|
local parsers = { 'bash', 'c', 'diff', 'html', 'lua', 'luadoc', 'markdown', 'markdown_inline', 'query', 'vim', 'vimdoc' }
|
||||||
|
require('nvim-treesitter').install(parsers)
|
||||||
end,
|
end,
|
||||||
-- There are additional nvim-treesitter modules that you can use to interact
|
-- There are additional nvim-treesitter modules that you can use to interact
|
||||||
-- with nvim-treesitter. You should go explore a few and see what interests you:
|
-- with nvim-treesitter. You should go explore a few and see what interests you:
|
||||||
|
|
Loading…
Reference in New Issue