fix: change config for treesitter
Both `install()` and `get_available()` are nil which causes the error while spawning nvim and make the treesitter not working. Replacing the custom `FileType` autocmd and manual parser attachment logic with the standard `nvim-treesitter.configs` setup function seems to work fine.
This commit is contained in:
parent
4b065ad2f7
commit
ce8cc5a718
78
init.lua
78
init.lua
|
|
@ -888,53 +888,49 @@ 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()
|
||||||
-- ensure basic parser are installed
|
require('nvim-treesitter.configs').setup {
|
||||||
local parsers = { 'bash', 'c', 'diff', 'html', 'lua', 'luadoc', 'markdown', 'markdown_inline', 'query', 'vim', 'vimdoc' }
|
-- Added to appease the Lua Language Server strict typing
|
||||||
require('nvim-treesitter').install(parsers)
|
modules = {},
|
||||||
|
ignore_install = {},
|
||||||
|
|
||||||
---@param buf integer
|
-- Ensure these basic parsers are installed
|
||||||
---@param language string
|
ensure_installed = {
|
||||||
local function treesitter_try_attach(buf, language)
|
'bash',
|
||||||
-- check if parser exists and load it
|
'c',
|
||||||
if not vim.treesitter.language.add(language) then return end
|
'diff',
|
||||||
-- enables syntax highlighting and other treesitter features
|
'html',
|
||||||
vim.treesitter.start(buf, language)
|
'lua',
|
||||||
|
'luadoc',
|
||||||
|
'markdown',
|
||||||
|
'markdown_inline',
|
||||||
|
'query',
|
||||||
|
'vim',
|
||||||
|
'vimdoc',
|
||||||
|
},
|
||||||
|
|
||||||
-- enables treesitter based folds
|
-- Automatically install missing parsers when entering a buffer
|
||||||
-- for more info on folds see `:help folds`
|
auto_install = true,
|
||||||
-- vim.wo.foldexpr = 'v:lua.vim.treesitter.foldexpr()'
|
|
||||||
-- vim.wo.foldmethod = 'expr'
|
|
||||||
|
|
||||||
-- check if treesitter indentation is available for this language, and if so enable it
|
-- Install parsers synchronously (only applied to `ensure_installed`)
|
||||||
-- in case there is no indent query, the indentexpr will fallback to the vim's built in one
|
sync_install = false,
|
||||||
local has_indent_query = vim.treesitter.query.get(language, 'indents') ~= nil
|
|
||||||
|
|
||||||
-- enables treesitter based indentation
|
-- Enable syntax highlighting
|
||||||
if has_indent_query then vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()" end
|
highlight = {
|
||||||
end
|
enable = true,
|
||||||
|
-- Some languages depend on vim's regex highlighting system (such as Ruby) for indent rules.
|
||||||
|
-- If you are experiencing weird indenting issues, add the language to the list of additional_vim_regex_highlighting and disabled languages for indent.
|
||||||
|
additional_vim_regex_highlighting = false,
|
||||||
|
},
|
||||||
|
|
||||||
local available_parsers = require('nvim-treesitter').get_available()
|
-- Enable treesitter-based indentation
|
||||||
vim.api.nvim_create_autocmd('FileType', {
|
indent = {
|
||||||
callback = function(args)
|
enable = true,
|
||||||
local buf, filetype = args.buf, args.match
|
},
|
||||||
|
}
|
||||||
|
|
||||||
local language = vim.treesitter.language.get_lang(filetype)
|
-- If you want to enable your treesitter-based folds, you can keep this here:
|
||||||
if not language then return end
|
-- vim.wo.foldexpr = 'v:lua.vim.treesitter.foldexpr()'
|
||||||
|
-- vim.wo.foldmethod = 'expr'
|
||||||
local installed_parsers = require('nvim-treesitter').get_installed 'parsers'
|
|
||||||
|
|
||||||
if vim.tbl_contains(installed_parsers, language) then
|
|
||||||
-- enable the parser if it is installed
|
|
||||||
treesitter_try_attach(buf, language)
|
|
||||||
elseif vim.tbl_contains(available_parsers, language) then
|
|
||||||
-- if a parser is available in `nvim-treesitter` auto install it, and enable it after the installation is done
|
|
||||||
require('nvim-treesitter').install(language):await(function() treesitter_try_attach(buf, language) end)
|
|
||||||
else
|
|
||||||
-- try to enable treesitter features in case the parser exists but is not available from `nvim-treesitter`
|
|
||||||
treesitter_try_attach(buf, language)
|
|
||||||
end
|
|
||||||
end,
|
|
||||||
})
|
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue