aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLewis Russell <lewis6991@gmail.com>2023-03-30 10:26:28 +0100
committerGitHub <noreply@github.com>2023-03-30 10:26:28 +0100
commit61e54f26361b2e7d08eabde9a4cbf42aaa41683b (patch)
treedeb9d00e4a01a771fde08f777af2fa6054479d17
parent8b7fb668e440f7793564b764bc9a691e3f45382a (diff)
downloadrneovim-61e54f26361b2e7d08eabde9a4cbf42aaa41683b.tar.gz
rneovim-61e54f26361b2e7d08eabde9a4cbf42aaa41683b.tar.bz2
rneovim-61e54f26361b2e7d08eabde9a4cbf42aaa41683b.zip
feat: add `vim.treesitter.language.get_filetypes()` (#22643)
-rw-r--r--runtime/doc/treesitter.txt31
-rw-r--r--runtime/lua/vim/treesitter.lua33
-rw-r--r--runtime/lua/vim/treesitter/language.lua57
-rw-r--r--runtime/lua/vim/treesitter/languagetree.lua7
4 files changed, 69 insertions, 59 deletions
diff --git a/runtime/doc/treesitter.txt b/runtime/doc/treesitter.txt
index d7e005ae51..96021d6272 100644
--- a/runtime/doc/treesitter.txt
+++ b/runtime/doc/treesitter.txt
@@ -592,8 +592,7 @@ get_node_text({node}, {source}, {opts})
(string)
get_parser({bufnr}, {lang}, {opts}) *vim.treesitter.get_parser()*
- Returns the parser for a specific buffer and filetype and attaches it to
- the buffer
+ Returns the parser for a specific buffer and attaches it to the buffer
If needed, this will create the parser.
@@ -728,29 +727,35 @@ stop({bufnr}) *vim.treesitter.stop()*
Lua module: vim.treesitter.language *lua-treesitter-language*
add({lang}, {opts}) *vim.treesitter.language.add()*
- Asserts that a parser for the language {lang} is installed.
+ Load parser with name {lang}
Parsers are searched in the `parser` runtime directory, or the provided
{path}
Parameters: ~
- • {lang} (string) Language the parser should parse (alphanumerical and
- `_` only)
+ • {lang} (string) Name of the parser (alphanumerical and `_` only)
• {opts} (table|nil) Options:
- • filetype (string|string[]) Filetype(s) that lang can be
- parsed with. Note this is not strictly the same as lang
- since a single lang can parse multiple filetypes. Defaults
- to lang.
+ • filetype (string|string[]) Default filetype the parser
+ should be associated with. Defaults to {lang}.
• path (string|nil) Optional path the parser is located at
• symbol_name (string|nil) Internal symbol name for the
language to load
+get_filetypes({lang}) *vim.treesitter.language.get_filetypes()*
+ Get the filetypes associated with the parser named {lang}.
+
+ Parameters: ~
+ • {lang} string Name of parser
+
+ Return: ~
+ string[] filetypes
+
get_lang({filetype}) *vim.treesitter.language.get_lang()*
Parameters: ~
- • {filetype} (string)
+ • {filetype} string
Return: ~
- (string|nil)
+ string|nil
inspect({lang}) *vim.treesitter.language.inspect()*
Inspects the provided language.
@@ -765,10 +770,10 @@ inspect({lang}) *vim.treesitter.language.inspect()*
(table)
register({lang}, {filetype}) *vim.treesitter.language.register()*
- Register a lang to be used for a filetype (or list of filetypes).
+ Register a parser named {lang} to be used for {filetype}(s).
Parameters: ~
- • {lang} (string) Language to register
+ • {lang} string Name of parser
• {filetype} string|string[] Filetype(s) to associate with lang
diff --git a/runtime/lua/vim/treesitter.lua b/runtime/lua/vim/treesitter.lua
index 4c3b17daa4..2594c1672d 100644
--- a/runtime/lua/vim/treesitter.lua
+++ b/runtime/lua/vim/treesitter.lua
@@ -58,9 +58,6 @@ function M._create_parser(bufnr, lang, opts)
vim.fn.bufload(bufnr)
- local ft = vim.bo[bufnr].filetype
- M.language.add(lang, { filetype = ft ~= '' and ft or nil })
-
local self = LanguageTree.new(bufnr, lang, opts)
---@private
@@ -94,7 +91,12 @@ function M._create_parser(bufnr, lang, opts)
return self
end
---- Returns the parser for a specific buffer and filetype and attaches it to the buffer
+--- @private
+local function valid_lang(lang)
+ return lang and lang ~= ''
+end
+
+--- Returns the parser for a specific buffer and attaches it to the buffer
---
--- If needed, this will create the parser.
---
@@ -110,18 +112,12 @@ function M.get_parser(bufnr, lang, opts)
bufnr = a.nvim_get_current_buf()
end
- if lang == nil then
- local ft = vim.bo[bufnr].filetype
- if ft ~= '' then
- lang = M.language.get_lang(ft) or ft
- -- TODO(lewis6991): we should error here and not default to ft
- -- if not lang then
- -- error(string.format('filetype %s of buffer %d is not associated with any lang', ft, bufnr))
- -- end
- else
- if parsers[bufnr] then
- return parsers[bufnr]
- end
+ if not valid_lang(lang) then
+ lang = M.language.get_lang(vim.bo[bufnr].filetype) or vim.bo[bufnr].filetype
+ end
+
+ if not valid_lang(lang) then
+ if not parsers[bufnr] then
error(
string.format(
'There is no parser available for buffer %d and one could not be'
@@ -131,9 +127,7 @@ function M.get_parser(bufnr, lang, opts)
)
)
end
- end
-
- if parsers[bufnr] == nil or parsers[bufnr]:lang() ~= lang then
+ elseif parsers[bufnr] == nil or parsers[bufnr]:lang() ~= lang then
parsers[bufnr] = M._create_parser(bufnr, lang, opts)
end
@@ -164,7 +158,6 @@ function M.get_string_parser(str, lang, opts)
str = { str, 'string' },
lang = { lang, 'string' },
})
- M.language.add(lang)
return LanguageTree.new(str, lang, opts)
end
diff --git a/runtime/lua/vim/treesitter/language.lua b/runtime/lua/vim/treesitter/language.lua
index 974d66ec05..b1c788e6ba 100644
--- a/runtime/lua/vim/treesitter/language.lua
+++ b/runtime/lua/vim/treesitter/language.lua
@@ -6,9 +6,25 @@ local M = {}
---@type table<string,string>
local ft_to_lang = {}
----@param filetype string
----@return string|nil
+--- Get the filetypes associated with the parser named {lang}.
+--- @param lang string Name of parser
+--- @return string[] filetypes
+function M.get_filetypes(lang)
+ local r = {} ---@type string[]
+ for ft, p in pairs(ft_to_lang) do
+ if p == lang then
+ r[#r + 1] = ft
+ end
+ end
+ return r
+end
+
+--- @param filetype string
+--- @return string|nil
function M.get_lang(filetype)
+ if filetype == '' then
+ return
+ end
return ft_to_lang[filetype]
end
@@ -35,16 +51,14 @@ end
---@field filetype? string|string[]
---@field symbol_name? string
---- Asserts that a parser for the language {lang} is installed.
+--- Load parser with name {lang}
---
--- Parsers are searched in the `parser` runtime directory, or the provided {path}
---
----@param lang string Language the parser should parse (alphanumerical and `_` only)
+---@param lang string Name of the parser (alphanumerical and `_` only)
---@param opts (table|nil) Options:
---- - filetype (string|string[]) Filetype(s) that lang can be parsed with.
---- Note this is not strictly the same as lang since a single lang can
---- parse multiple filetypes.
---- Defaults to lang.
+--- - filetype (string|string[]) Default filetype the parser should be associated with.
+--- Defaults to {lang}.
--- - path (string|nil) Optional path the parser is located at
--- - symbol_name (string|nil) Internal symbol name for the language to load
function M.add(lang, opts)
@@ -61,7 +75,7 @@ function M.add(lang, opts)
filetype = { filetype, { 'string', 'table' }, true },
})
- M.register(lang, filetype or lang)
+ M.register(lang, filetype)
if vim._ts_has_language(lang) then
return
@@ -83,23 +97,26 @@ function M.add(lang, opts)
vim._ts_add_language(path, lang, symbol_name)
end
---- Register a lang to be used for a filetype (or list of filetypes).
----@param lang string Language to register
----@param filetype string|string[] Filetype(s) to associate with lang
+--- @private
+--- @param x string|string[]
+--- @return string[]
+local function ensure_list(x)
+ if type(x) == 'table' then
+ return x
+ end
+ return { x }
+end
+
+--- Register a parser named {lang} to be used for {filetype}(s).
+--- @param lang string Name of parser
+--- @param filetype string|string[] Filetype(s) to associate with lang
function M.register(lang, filetype)
vim.validate({
lang = { lang, 'string' },
filetype = { filetype, { 'string', 'table' } },
})
- local filetypes ---@type string[]
- if type(filetype) == 'string' then
- filetypes = { filetype }
- else
- filetypes = filetype
- end
-
- for _, f in ipairs(filetypes) do
+ for _, f in ipairs(ensure_list(filetype)) do
if f ~= '' then
ft_to_lang[f] = lang
end
diff --git a/runtime/lua/vim/treesitter/languagetree.lua b/runtime/lua/vim/treesitter/languagetree.lua
index 82e507551d..922e4881ca 100644
--- a/runtime/lua/vim/treesitter/languagetree.lua
+++ b/runtime/lua/vim/treesitter/languagetree.lua
@@ -76,7 +76,7 @@ LanguageTree.__index = LanguageTree
--- "injected" language parsers, which themselves may inject other languages, recursively.
---
---@param source (integer|string) Buffer or text string to parse
----@param lang string Root language of this tree
+---@param lang string|nil Root language of this tree
---@param opts (table|nil) Optional arguments:
--- - injections table Map of language to injection query strings. Overrides the
--- built-in runtime file searching for language injections.
@@ -86,11 +86,6 @@ function LanguageTree.new(source, lang, opts)
---@type LanguageTreeOpts
opts = opts or {}
- if opts.queries then
- a.nvim_err_writeln("'queries' is no longer supported. Use 'injections' now")
- opts.injections = opts.queries
- end
-
local injections = opts.injections or {}
local self = setmetatable({
_source = source,