Refactor treesitter attach code

This commit is contained in:
Ori Perry 2026-03-20 21:59:19 +02:00
parent 0619d89884
commit 8ac4b12632

View File

@ -874,15 +874,13 @@ 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
local parsers = { 'bash', 'c', 'diff', 'html', 'lua', 'luadoc', 'markdown', 'markdown_inline', 'query', 'vim', 'vimdoc' } local parsers = { 'bash', 'c', 'diff', 'html', 'lua', 'luadoc', 'markdown', 'markdown_inline', 'query', 'vim', 'vimdoc' }
require('nvim-treesitter').install(parsers) require('nvim-treesitter').install(parsers)
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
---@param buf integer
---@param language string
local function treesitter_try_attach(buf, language)
-- check if parser exists and load it -- check if parser exists and load it
if not vim.treesitter.language.add(language) then return end if not vim.treesitter.language.add(language) then return end
-- enables syntax highlighting and other treesitter features -- enables syntax highlighting and other treesitter features
@ -895,6 +893,17 @@ 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()"
end
local available_parsers = require('nvim-treesitter').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
treesitter_try_attach(buf, language)
end, end,
}) })
end, end,