New way to config parser per filetype under main branch #7894
Replies: 5 comments 6 replies
-
It cannot. Simply move the install to the |
Beta Was this translation helpful? Give feedback.
-
|
I would like to share the autocmd that I use to enable highlighting: vim.api.nvim_create_autocmd("FileType", {
group = init,
callback = function(args)
local filetype = args.match
local lang = vim.treesitter.language.get_lang(filetype)
if vim.treesitter.language.add(lang) then
vim.treesitter.start()
end
end,
})It uses |
Beta Was this translation helpful? Give feedback.
-
|
Just to have this in one place: if you're missing the autoinstall feature (and don't want to migrate to https://github.com/lewis6991/ts-install.nvim), you can use {
'nvim-treesitter/nvim-treesitter',
lazy = false,
branch = "main",
build = { ":TSUpdate" },
init = function ()
vim.api.nvim_create_autocmd("FileType", {
callback = function(args)
local filetype = args.match
local lang = vim.treesitter.language.get_lang(filetype)
require("nvim-treesitter").install(lang):await(function()
vim.wo.foldexpr = 'v:lua.vim.treesitter.foldexpr()'
vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
vim.treesitter.start()
end)
end
})
end,
}(again, not tested) |
Beta Was this translation helpful? Give feedback.
-
|
Another snippet with auto install feature and mini.deps: local add, now, later = MiniDeps.add, MiniDeps.now, MiniDeps.later
local parser_installed = { ... }
local install_ts_langs = function() require('nvim-treesitter').install(parser_installed) end
add({
source = 'nvim-treesitter/nvim-treesitter',
checkout = 'main',
hooks = {
post_install = function() later(install_ts_langs) end,
post_checkout = function() vim.cmd('TSUpdate') end,
},
})
local start_ts = function(lang)
if not vim.tbl_contains(require('nvim-treesitter.config').get_available(), lang) then return end
vim.wo.foldexpr = 'v:lua.vim.treesitter.foldexpr()'
vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
vim.treesitter.start()
end
vim.api.nvim_create_autocmd('FileType', {
pattern = '*',
callback = function(event)
local filetype = event.match
local lang = vim.treesitter.language.get_lang(filetype)
if not vim.tbl_contains(parser_installed, lang) then
require('nvim-treesitter').install(lang):await(function() start_ts(lang) end)
else
start_ts(lang)
end
end,
})
|
Beta Was this translation helpful? Give feedback.
-
|
This is what I'm using: https://github.com/luisdavim/dotfiles/blob/master/files%2Fconfig%2Fnvim%2Finit.lua#L141 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Here is my lazy config:
{ 'nvim-treesitter/nvim-treesitter', lazy = false, branch = "main", build = ":TSUpdate", opts = function () local parser_installed = { "c", "cpp", "diff", "java", "javadoc", "kotlin", "groovy", "dockerfile", "go", "gomod", "gosum", "html", "css", "javascript", "svelte", "lua", "markdown", "markdown_inline", "comment", "python", "htmldjango", "rust", "sql", "typescript", "tsx", "yaml", "toml", "elixir", "bash", "http", "tmux", "xml", "fish", "awk", "jq", "json", "jsonc", "json5", "printf", "vim", "vimdoc", "query", "cmake", "csv", "dot", "func", "gotmpl", "graphql", "ini", "jsdoc", "luadoc", "make", "nginx", "regex", "requirements", "ssh_config", "strace", "styled", "templ", "todotxt", "vue", "xresources", "asm", "mermaid", } require("nvim-treesitter").install(parser_installed) local ft_list = {} local ft_set = {} for _, parser in ipairs(parser_installed) do local fts = vim.treesitter.language.get_filetypes(parser) for _, ft in ipairs(fts) do ft_set[ft] = true end end for filetype in pairs(ft_set) do table.insert(ft_list, filetype) end vim.api.nvim_create_autocmd("FileType", { pattern = ft_list, callback = function() vim.treesitter.start() vim.wo.foldexpr = 'v:lua.vim.treesitter.foldexpr()' vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()" end }) end, },there is a nvim built-in function:
vim.treesitter.language.get_filetypeswhich get filetype names from parser name,nvim-treesitterreport an error when filetype not supported if set autocmd pattern to*I just share my config, new elegant solution is very much welcomed
btw, the startup message:
[nvim-treesitter]: Installed 79/79 languagesis annoying, Would be very appreciated if the install message can be turned off when nothing goes wrong.Thanks for speed up my nvim startuptime!
Beta Was this translation helpful? Give feedback.
All reactions