refacto: deprecate used_by in parsers.lua

- remove print_warning function from utils.lua (unused)
- cleanup some functions in parsers.lua (parameters overloading
  and wrong bufnr used).
- log a deprecation notice when using used_by in a parser definition
- default the filetype_to_parsername table to the list of filetypes
  previously in the used_by keys
- update the README to indicate that change
This commit is contained in:
kiyan 2022-02-06 13:34:08 +01:00 committed by Kiyan
parent d7eab3a5a9
commit 58a4897e6d
3 changed files with 37 additions and 37 deletions

View file

@ -355,12 +355,19 @@ parser_config.zimbu = {
generate_requires_npm = false, -- if stand-alone parser without npm dependencies generate_requires_npm = false, -- if stand-alone parser without npm dependencies
requires_generate_from_grammar = false, -- if folder contains pre-generated src/parser.c requires_generate_from_grammar = false, -- if folder contains pre-generated src/parser.c
}, },
filetype = "zu", -- if filetype does not agrees with parser name filetype = "zu", -- if filetype does not match the parser name
used_by = {"bar", "baz"} -- additional filetypes that use this parser
} }
EOF EOF
``` ```
If you wish to set a specific parser for a filetype, you should extend the `filetype_to_parsername` table:
```vim
lua <<EOF
local ft_to_parser = require"nvim-treesitter.parser".filetype_to_parsername
filetype_to_parsername.someft = "python" -- the someft filetype will use the python parser and queries.
EOF
```
4. Start `nvim` and `:TSInstall zimbu`. 4. Start `nvim` and `:TSInstall zimbu`.
You can also skip step 2 and use `:TSInstallFromGrammar zimbu` to install directly from a `grammar.js` in the top-level directory specified by `url`. You can also skip step 2 and use `:TSInstallFromGrammar zimbu` to install directly from a `grammar.js` in the top-level directory specified by `url`.

View file

@ -1,16 +1,21 @@
local api = vim.api local api = vim.api
local ts = vim.treesitter local ts = vim.treesitter
local ft_to_parsername = {} local filetype_to_parsername = {
arduino = "cpp",
local function update_ft_to_parsername(name, parser) javascriptreact = "javascript",
if type(parser.used_by) == "table" then ecma = "javascript",
for _, ft in pairs(parser.used_by) do jsx = "javascript",
ft_to_parsername[ft] = name PKGBUILD = "bash",
end html_tags = "html",
end ["typescript.tsx"] = "tsx",
ft_to_parsername[parser.filetype or name] = name terraform = "hcl",
end ["html.handlebars"] = "glimmer",
systemverilog = "verilog",
cls = "latex",
sty = "latex",
OpenFOAM = "foam",
}
local list = setmetatable({}, { local list = setmetatable({}, {
__newindex = function(table, parsername, parserconfig) __newindex = function(table, parsername, parserconfig)
@ -20,7 +25,11 @@ local list = setmetatable({}, {
setmetatable(parserconfig, { setmetatable(parserconfig, {
__newindex = function(parserconfigtable, key, value) __newindex = function(parserconfigtable, key, value)
if key == "used_by" then if key == "used_by" then
ft_to_parsername[value] = parsername require("nvim-treesitter.utils").notify(
"used_by is deprecated, please use 'filetype_to_parsername'",
vim.log.levels.WARN
)
filetype_to_parsername[value] = parsername
else else
rawset(parserconfigtable, key, value) rawset(parserconfigtable, key, value)
end end
@ -28,7 +37,7 @@ local list = setmetatable({}, {
}) })
) )
update_ft_to_parsername(parsername, parserconfig) filetype_to_parsername[parserconfig.filetype or parsername] = parsername
end, end,
}) })
@ -37,7 +46,6 @@ list.javascript = {
url = "https://github.com/tree-sitter/tree-sitter-javascript", url = "https://github.com/tree-sitter/tree-sitter-javascript",
files = { "src/parser.c", "src/scanner.c" }, files = { "src/parser.c", "src/scanner.c" },
}, },
used_by = { "javascriptreact", "ecma", "jsx" },
maintainers = { "@steelsojka" }, maintainers = { "@steelsojka" },
} }
@ -73,7 +81,6 @@ list.cpp = {
files = { "src/parser.c", "src/scanner.cc" }, files = { "src/parser.c", "src/scanner.cc" },
generate_requires_npm = true, generate_requires_npm = true,
}, },
used_by = { "arduino" },
maintainers = { "@theHamsta" }, maintainers = { "@theHamsta" },
} }
@ -224,7 +231,6 @@ 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 = { "PKGBUILD" },
filetype = "sh", filetype = "sh",
maintainers = { "@TravonteD" }, maintainers = { "@TravonteD" },
} }
@ -267,7 +273,6 @@ list.html = {
url = "https://github.com/tree-sitter/tree-sitter-html", url = "https://github.com/tree-sitter/tree-sitter-html",
files = { "src/parser.c", "src/scanner.cc" }, files = { "src/parser.c", "src/scanner.cc" },
}, },
used_by = { "html_tags" },
maintainers = { "@TravonteD" }, maintainers = { "@TravonteD" },
} }
@ -414,7 +419,6 @@ list.tsx = {
location = "tree-sitter-tsx/tsx", location = "tree-sitter-tsx/tsx",
generate_requires_npm = true, generate_requires_npm = true,
}, },
used_by = { "typescript.tsx" },
filetype = "typescriptreact", filetype = "typescriptreact",
maintainers = { "@steelsojka" }, maintainers = { "@steelsojka" },
} }
@ -452,7 +456,6 @@ list.hcl = {
}, },
maintainers = { "@MichaHoffmann" }, maintainers = { "@MichaHoffmann" },
filetype = "hcl", filetype = "hcl",
used_by = { "terraform" },
} }
list.markdown = { list.markdown = {
@ -491,7 +494,6 @@ list.glimmer = {
readme_name = "Glimmer and Ember", readme_name = "Glimmer and Ember",
maintainers = { "@alexlafroscia" }, maintainers = { "@alexlafroscia" },
filetype = "handlebars", filetype = "handlebars",
used_by = { "html.handlebars" },
} }
list.pug = { list.pug = {
@ -609,7 +611,6 @@ list.verilog = {
files = { "src/parser.c" }, files = { "src/parser.c" },
generate_requires_npm = true, generate_requires_npm = true,
}, },
used_by = { "systemverilog" },
maintainers = { "@zegervdv" }, maintainers = { "@zegervdv" },
-- The parser still uses API version 12, because it does not compile with 13 -- The parser still uses API version 12, because it does not compile with 13
experimental = true, experimental = true,
@ -750,7 +751,6 @@ list.latex = {
files = { "src/parser.c", "src/scanner.c" }, files = { "src/parser.c", "src/scanner.c" },
}, },
filetype = "tex", filetype = "tex",
used_by = { "cls", "sty" },
maintainers = { "@theHamsta, @clason" }, maintainers = { "@theHamsta, @clason" },
} }
@ -887,7 +887,6 @@ list.foam = {
}, },
maintainers = { "@FoamScience" }, maintainers = { "@FoamScience" },
filetype = "foam", filetype = "foam",
used_by = { "OpenFOAM" },
-- Queries might change over time on the grammar's side -- Queries might change over time on the grammar's side
-- Otherwise everything runs fine -- Otherwise everything runs fine
experimental = true, experimental = true,
@ -923,15 +922,16 @@ list.vala = {
local M = { local M = {
list = list, list = list,
filetype_to_parsername = filetype_to_parsername,
} }
function M.ft_to_lang(ft) function M.ft_to_lang(ft)
local result = ft_to_parsername[ft] local result = filetype_to_parsername[ft]
if result then if result then
return result return result
else else
ft = vim.split(ft, ".", true)[1] ft = vim.split(ft, ".", true)[1]
return ft_to_parsername[ft] or ft return filetype_to_parsername[ft] or ft
end end
end end
@ -972,7 +972,7 @@ end
M.reset_cache() M.reset_cache()
function M.has_parser(lang) function M.has_parser(lang)
local lang = lang or M.get_buf_lang(api.nvim_get_current_buf()) lang = lang or M.get_buf_lang(api.nvim_get_current_buf())
if not lang or #lang == 0 then if not lang or #lang == 0 then
return false return false
@ -985,8 +985,8 @@ function M.has_parser(lang)
end end
function M.get_parser(bufnr, lang) function M.get_parser(bufnr, lang)
local buf = bufnr or api.nvim_get_current_buf() bufnr = bufnr or api.nvim_get_current_buf()
local lang = lang or M.get_buf_lang(buf) lang = lang or M.get_buf_lang(bufnr)
if M.has_parser(lang) then if M.has_parser(lang) then
return ts.get_parser(bufnr, lang) return ts.get_parser(bufnr, lang)
@ -996,8 +996,7 @@ end
-- @deprecated This is only kept for legacy purposes. -- @deprecated This is only kept for legacy purposes.
-- All root nodes should be accounted for. -- All root nodes should be accounted for.
function M.get_tree_root(bufnr) function M.get_tree_root(bufnr)
local bufnr = bufnr or api.nvim_get_current_buf() bufnr = bufnr or api.nvim_get_current_buf()
return M.get_parser(bufnr):parse()[1]:root() return M.get_parser(bufnr):parse()[1]:root()
end end

View file

@ -136,12 +136,6 @@ function M.get_at_path(tbl, path)
return result return result
end end
-- Prints a warning message
-- @param text the text message
function M.print_warning(text)
api.nvim_command(string.format([[echohl WarningMsg | echo "%s" | echohl None]], text))
end
function M.set_jump() function M.set_jump()
vim.cmd "normal! m'" vim.cmd "normal! m'"
end end