mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-01 19:17:02 -04:00
add used_by key to parsers
Enables the use of multiple filetypes for one parser.
This commit is contained in:
parent
a2f0931254
commit
7f6f7596da
3 changed files with 35 additions and 17 deletions
|
|
@ -17,9 +17,10 @@ function M.setup()
|
|||
for _, lang in pairs(parsers.available_parsers()) do
|
||||
for _, mod in pairs(configs.available_modules()) do
|
||||
if configs.is_enabled(mod, lang) then
|
||||
local ft = parsers.lang_to_ft(lang)
|
||||
local cmd = string.format("lua require'nvim-treesitter.%s'.attach()", mod)
|
||||
api.nvim_command(string.format("autocmd NvimTreesitter FileType %s %s", ft, cmd))
|
||||
for _, ft in pairs(parsers.lang_to_ft(lang)) do
|
||||
api.nvim_command(string.format("autocmd NvimTreesitter FileType %s %s", ft, cmd))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -49,9 +49,10 @@ end
|
|||
local function enable_mod_conf_autocmd(mod, lang)
|
||||
if not config.modules[mod] or M.is_enabled(mod, lang) then return end
|
||||
|
||||
local ft = parsers.lang_to_ft(lang)
|
||||
local cmd = string.format("lua require'nvim-treesitter.%s'.attach()", mod)
|
||||
api.nvim_command(string.format("autocmd FileType %s %s", ft, cmd))
|
||||
for _, ft in pairs(parsers.lang_to_ft(lang)) do
|
||||
api.nvim_command(string.format("autocmd NvimTreesitter FileType %s %s", ft, cmd))
|
||||
end
|
||||
for i, parser in pairs(config.modules[mod].disable) do
|
||||
if parser == lang then
|
||||
table.remove(config.modules[mod].disable, i)
|
||||
|
|
@ -65,7 +66,7 @@ local function enable_all(mod, lang)
|
|||
|
||||
for _, bufnr in pairs(api.nvim_list_bufs()) do
|
||||
local ft = api.nvim_buf_get_option(bufnr, 'ft')
|
||||
if not lang or ft == parsers.lang_to_ft(lang) then
|
||||
if not lang or parsers.lang_match_ft(lang, ft) then
|
||||
enable_module(mod, bufnr, lang)
|
||||
end
|
||||
end
|
||||
|
|
@ -101,15 +102,18 @@ end
|
|||
local function disable_mod_conf_autocmd(mod, lang)
|
||||
if not config.modules[mod] or not M.is_enabled(mod, lang) then return end
|
||||
|
||||
local ft = parsers.lang_to_ft(lang)
|
||||
api.nvim_command(string.format("autocmd! FileType %s", ft))
|
||||
local cmd = string.format("lua require'nvim-treesitter.%s'.attach()", mod)
|
||||
-- TODO(kyazdani): detach the correct autocmd... doesn't work when using %s, cmd
|
||||
for _, ft in pairs(parsers.lang_to_ft(lang)) do
|
||||
api.nvim_command(string.format("autocmd! NvimTreesitter FileType %s", ft))
|
||||
end
|
||||
table.insert(config.modules[mod].disable, lang)
|
||||
end
|
||||
|
||||
local function disable_all(mod, lang)
|
||||
for _, bufnr in pairs(api.nvim_list_bufs()) do
|
||||
local ft = api.nvim_buf_get_option(bufnr, 'ft')
|
||||
if not lang or ft == parsers.lang_to_ft(lang) then
|
||||
if not lang or parsers.lang_match_ft(lang, ft) then
|
||||
disable_module(mod, bufnr, lang)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -64,6 +64,7 @@ list.bash = {
|
|||
url = "https://github.com/tree-sitter/tree-sitter-bash",
|
||||
files = { "src/parser.c", "src/scanner.cc" },
|
||||
},
|
||||
used_by = { "zsh" },
|
||||
filetype = 'sh'
|
||||
}
|
||||
|
||||
|
|
@ -205,10 +206,10 @@ list.nix = {
|
|||
}
|
||||
|
||||
list.regex = {
|
||||
install_info = {
|
||||
url = "https://github.com/tree-sitter/tree-sitter-regex",
|
||||
files = { "src/parser.c" }
|
||||
}
|
||||
install_info = {
|
||||
url = "https://github.com/tree-sitter/tree-sitter-regex",
|
||||
files = { "src/parser.c" }
|
||||
}
|
||||
}
|
||||
|
||||
local M = {
|
||||
|
|
@ -216,12 +217,14 @@ local M = {
|
|||
}
|
||||
|
||||
local ft_to_parsername = {}
|
||||
|
||||
for name, obj in pairs(M.list) do
|
||||
if obj.filetype then
|
||||
ft_to_parsername[obj.filetype] = name
|
||||
else
|
||||
ft_to_parsername[name] = name
|
||||
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)
|
||||
|
|
@ -229,7 +232,17 @@ function M.ft_to_lang(ft)
|
|||
end
|
||||
|
||||
function M.lang_to_ft(lang)
|
||||
return M.list[lang].filetype or lang
|
||||
local obj = M.list[lang]
|
||||
return vim.tbl_flatten({{obj.filetype or lang}, obj.used_by or {}})
|
||||
end
|
||||
|
||||
function M.lang_match_ft(lang, ft)
|
||||
for _, f in pairs(M.lang_to_ft(lang)) do
|
||||
if ft == f then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
function M.available_parsers()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue