mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-03 12:06:55 -04:00
Allow user configure parsers using metatables
This commit is contained in:
parent
a47c83d12d
commit
76486942e7
2 changed files with 40 additions and 12 deletions
|
|
@ -1,7 +1,33 @@
|
|||
local api = vim.api
|
||||
local ts = vim.treesitter
|
||||
|
||||
local list = {}
|
||||
local ft_to_parsername = {}
|
||||
|
||||
local function update_ft_to_parsername(name, parser)
|
||||
if type(parser.used_by) == 'table' then
|
||||
for _, ft in pairs(parser.used_by) do
|
||||
ft_to_parsername[ft] = name
|
||||
end
|
||||
end
|
||||
ft_to_parsername[parser.filetype or name] = name
|
||||
end
|
||||
|
||||
local list = setmetatable({}, {
|
||||
__newindex = function(table, parsername, parserconfig)
|
||||
|
||||
rawset(table, parsername, setmetatable(parserconfig, {
|
||||
__newindex = function(parserconfigtable, key, value)
|
||||
if key == "used_by" then
|
||||
ft_to_parsername[value] = parsername
|
||||
else
|
||||
rawset(parserconfigtable, key, value)
|
||||
end
|
||||
end
|
||||
}))
|
||||
|
||||
update_ft_to_parsername(parsername, parserconfig)
|
||||
end
|
||||
})
|
||||
|
||||
list.javascript = {
|
||||
install_info = {
|
||||
|
|
@ -349,17 +375,6 @@ local M = {
|
|||
list = list
|
||||
}
|
||||
|
||||
local ft_to_parsername = {}
|
||||
|
||||
for name, obj in pairs(M.list) do
|
||||
if type(obj.used_by) == 'table' then
|
||||
for _, ft in pairs(obj.used_by) do
|
||||
ft_to_parsername[ft] = name
|
||||
end
|
||||
end
|
||||
ft_to_parsername[obj.filetype or name] = name
|
||||
end
|
||||
|
||||
function M.ft_to_lang(ft)
|
||||
return ft_to_parsername[ft] or ft
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue