mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-19 03:40:04 -04:00
add used_by key to parsers
Enables the use of multiple filetypes for one parser.
This commit is contained in:
parent
b184f1cafb
commit
92162b54cf
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 _, lang in pairs(parsers.available_parsers()) do
|
||||||
for _, mod in pairs(configs.available_modules()) do
|
for _, mod in pairs(configs.available_modules()) do
|
||||||
if configs.is_enabled(mod, lang) then
|
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)
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -49,9 +49,10 @@ end
|
||||||
local function enable_mod_conf_autocmd(mod, lang)
|
local function enable_mod_conf_autocmd(mod, lang)
|
||||||
if not config.modules[mod] or M.is_enabled(mod, lang) then return end
|
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)
|
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
|
for i, parser in pairs(config.modules[mod].disable) do
|
||||||
if parser == lang then
|
if parser == lang then
|
||||||
table.remove(config.modules[mod].disable, i)
|
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
|
for _, bufnr in pairs(api.nvim_list_bufs()) do
|
||||||
local ft = api.nvim_buf_get_option(bufnr, 'ft')
|
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)
|
enable_module(mod, bufnr, lang)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -101,15 +102,18 @@ end
|
||||||
local function disable_mod_conf_autocmd(mod, lang)
|
local function disable_mod_conf_autocmd(mod, lang)
|
||||||
if not config.modules[mod] or not M.is_enabled(mod, lang) then return end
|
if not config.modules[mod] or not 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", ft))
|
-- 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)
|
table.insert(config.modules[mod].disable, lang)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function disable_all(mod, lang)
|
local function disable_all(mod, lang)
|
||||||
for _, bufnr in pairs(api.nvim_list_bufs()) do
|
for _, bufnr in pairs(api.nvim_list_bufs()) do
|
||||||
local ft = api.nvim_buf_get_option(bufnr, 'ft')
|
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)
|
disable_module(mod, bufnr, lang)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -64,6 +64,7 @@ list.bash = {
|
||||||
url = "https://github.com/tree-sitter/tree-sitter-bash",
|
url = "https://github.com/tree-sitter/tree-sitter-bash",
|
||||||
files = { "src/parser.c", "src/scanner.cc" },
|
files = { "src/parser.c", "src/scanner.cc" },
|
||||||
},
|
},
|
||||||
|
used_by = { "zsh" },
|
||||||
filetype = 'sh'
|
filetype = 'sh'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -205,10 +206,10 @@ list.nix = {
|
||||||
}
|
}
|
||||||
|
|
||||||
list.regex = {
|
list.regex = {
|
||||||
install_info = {
|
install_info = {
|
||||||
url = "https://github.com/tree-sitter/tree-sitter-regex",
|
url = "https://github.com/tree-sitter/tree-sitter-regex",
|
||||||
files = { "src/parser.c" }
|
files = { "src/parser.c" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
local M = {
|
local M = {
|
||||||
|
|
@ -216,12 +217,14 @@ local M = {
|
||||||
}
|
}
|
||||||
|
|
||||||
local ft_to_parsername = {}
|
local ft_to_parsername = {}
|
||||||
|
|
||||||
for name, obj in pairs(M.list) do
|
for name, obj in pairs(M.list) do
|
||||||
if obj.filetype then
|
if type(obj.used_by) == 'table' then
|
||||||
ft_to_parsername[obj.filetype] = name
|
for _, ft in pairs(obj.used_by) do
|
||||||
else
|
ft_to_parsername[ft] = name
|
||||||
ft_to_parsername[name] = name
|
end
|
||||||
end
|
end
|
||||||
|
ft_to_parsername[obj.filetype or name] = name
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.ft_to_lang(ft)
|
function M.ft_to_lang(ft)
|
||||||
|
|
@ -229,7 +232,17 @@ function M.ft_to_lang(ft)
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.lang_to_ft(lang)
|
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
|
end
|
||||||
|
|
||||||
function M.available_parsers()
|
function M.available_parsers()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue