treesitter: add filetype mappings

This commit is contained in:
Snoweuph 2026-04-23 22:51:01 +02:00 committed by Ching Pei Yang
commit 9ff0dcb60c
3 changed files with 64 additions and 40 deletions

View file

@ -24,49 +24,56 @@ in {
treesitter.grammars = optionals cfg.addDefaultGrammars cfg.defaultGrammars;
pluginRC.treesitter-autocommands = entryAfter ["basic"] ''
vim.api.nvim_create_augroup("nvf_treesitter", { clear = true })
pluginRC = {
treesitter-autocommands = entryAfter ["basic"] ''
vim.api.nvim_create_augroup("nvf_treesitter", { clear = true })
${lib.optionalString cfg.highlight.enable ''
-- Enable treesitter highlighting for all filetypes
vim.api.nvim_create_autocmd("FileType", {
group = "nvf_treesitter",
pattern = "*",
callback = function()
pcall(vim.treesitter.start)
end,
})
''}
${lib.optionalString cfg.indent.enable ''
-- Enable treesitter highlighting for all filetypes
vim.api.nvim_create_autocmd("FileType", {
group = "nvf_treesitter",
pattern = ${toLuaObject cfg.indent.pattern},
callback = function(args)
${optionalString (builtins.length cfg.indent.excludes > 0) ''
local ft = vim.bo[args.buf].filetype
if vim.tbl_contains(${toLuaObject cfg.indent.excludes}, ft) then
return
end
${lib.optionalString cfg.highlight.enable ''
-- Enable treesitter highlighting for all filetypes
vim.api.nvim_create_autocmd("FileType", {
group = "nvf_treesitter",
pattern = "*",
callback = function()
pcall(vim.treesitter.start)
end,
})
''}
vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
end,
})
''}
${lib.optionalString cfg.fold ''
-- Enable treesitter folding for all filetypes
vim.api.nvim_create_autocmd("FileType", {
group = "nvf_treesitter",
pattern = "*",
callback = function()
vim.wo[0][0].foldmethod = "expr"
vim.wo[0][0].foldexpr = "v:lua.vim.treesitter.foldexpr()"
end,
})
''}
'';
${lib.optionalString cfg.indent.enable ''
-- Enable treesitter highlighting for all filetypes
vim.api.nvim_create_autocmd("FileType", {
group = "nvf_treesitter",
pattern = ${toLuaObject cfg.indent.pattern},
callback = function(args)
${optionalString (builtins.length cfg.indent.excludes > 0) ''
local ft = vim.bo[args.buf].filetype
if vim.tbl_contains(${toLuaObject cfg.indent.excludes}, ft) then
return
end
''}
vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
end,
})
''}
${lib.optionalString cfg.fold ''
-- Enable treesitter folding for all filetypes
vim.api.nvim_create_autocmd("FileType", {
group = "nvf_treesitter",
pattern = "*",
callback = function()
vim.wo[0][0].foldmethod = "expr"
vim.wo[0][0].foldexpr = "v:lua.vim.treesitter.foldexpr()"
end,
})
''}
'';
treesitter-filetype-mappings = entryAfter ["basic"] ''
for lang, ft in pairs(${toLuaObject cfg.filetypeMappings}) do
vim.treesitter.language.register(lang, ft)
end
'';
};
additionalRuntimePaths = mkIf (cfg.queries != []) [
(let