mirror of
https://github.com/neovim/neovim.git
synced 2025-09-07 11:58:17 +00:00
feat: add vim.treesitter.language.get_filetypes()
(#22643)
This commit is contained in:
@@ -592,8 +592,7 @@ get_node_text({node}, {source}, {opts})
|
|||||||
(string)
|
(string)
|
||||||
|
|
||||||
get_parser({bufnr}, {lang}, {opts}) *vim.treesitter.get_parser()*
|
get_parser({bufnr}, {lang}, {opts}) *vim.treesitter.get_parser()*
|
||||||
Returns the parser for a specific buffer and filetype and attaches it to
|
Returns the parser for a specific buffer and attaches it to the buffer
|
||||||
the buffer
|
|
||||||
|
|
||||||
If needed, this will create the parser.
|
If needed, this will create the parser.
|
||||||
|
|
||||||
@@ -728,29 +727,35 @@ stop({bufnr}) *vim.treesitter.stop()*
|
|||||||
Lua module: vim.treesitter.language *lua-treesitter-language*
|
Lua module: vim.treesitter.language *lua-treesitter-language*
|
||||||
|
|
||||||
add({lang}, {opts}) *vim.treesitter.language.add()*
|
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
|
Parsers are searched in the `parser` runtime directory, or the provided
|
||||||
{path}
|
{path}
|
||||||
|
|
||||||
Parameters: ~
|
Parameters: ~
|
||||||
• {lang} (string) Language the parser should parse (alphanumerical and
|
• {lang} (string) Name of the parser (alphanumerical and `_` only)
|
||||||
`_` only)
|
|
||||||
• {opts} (table|nil) Options:
|
• {opts} (table|nil) Options:
|
||||||
• filetype (string|string[]) Filetype(s) that lang can be
|
• filetype (string|string[]) Default filetype the parser
|
||||||
parsed with. Note this is not strictly the same as lang
|
should be associated with. Defaults to {lang}.
|
||||||
since a single lang can parse multiple filetypes. Defaults
|
|
||||||
to lang.
|
|
||||||
• path (string|nil) Optional path the parser is located at
|
• path (string|nil) Optional path the parser is located at
|
||||||
• symbol_name (string|nil) Internal symbol name for the
|
• symbol_name (string|nil) Internal symbol name for the
|
||||||
language to load
|
language to load
|
||||||
|
|
||||||
get_lang({filetype}) *vim.treesitter.language.get_lang()*
|
get_filetypes({lang}) *vim.treesitter.language.get_filetypes()*
|
||||||
|
Get the filetypes associated with the parser named {lang}.
|
||||||
|
|
||||||
Parameters: ~
|
Parameters: ~
|
||||||
• {filetype} (string)
|
• {lang} string Name of parser
|
||||||
|
|
||||||
Return: ~
|
Return: ~
|
||||||
(string|nil)
|
string[] filetypes
|
||||||
|
|
||||||
|
get_lang({filetype}) *vim.treesitter.language.get_lang()*
|
||||||
|
Parameters: ~
|
||||||
|
• {filetype} string
|
||||||
|
|
||||||
|
Return: ~
|
||||||
|
string|nil
|
||||||
|
|
||||||
inspect({lang}) *vim.treesitter.language.inspect()*
|
inspect({lang}) *vim.treesitter.language.inspect()*
|
||||||
Inspects the provided language.
|
Inspects the provided language.
|
||||||
@@ -765,10 +770,10 @@ inspect({lang}) *vim.treesitter.language.inspect()*
|
|||||||
(table)
|
(table)
|
||||||
|
|
||||||
register({lang}, {filetype}) *vim.treesitter.language.register()*
|
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: ~
|
Parameters: ~
|
||||||
• {lang} (string) Language to register
|
• {lang} string Name of parser
|
||||||
• {filetype} string|string[] Filetype(s) to associate with lang
|
• {filetype} string|string[] Filetype(s) to associate with lang
|
||||||
|
|
||||||
|
|
||||||
|
@@ -58,9 +58,6 @@ function M._create_parser(bufnr, lang, opts)
|
|||||||
|
|
||||||
vim.fn.bufload(bufnr)
|
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)
|
local self = LanguageTree.new(bufnr, lang, opts)
|
||||||
|
|
||||||
---@private
|
---@private
|
||||||
@@ -94,7 +91,12 @@ function M._create_parser(bufnr, lang, opts)
|
|||||||
return self
|
return self
|
||||||
end
|
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.
|
--- If needed, this will create the parser.
|
||||||
---
|
---
|
||||||
@@ -110,18 +112,12 @@ function M.get_parser(bufnr, lang, opts)
|
|||||||
bufnr = a.nvim_get_current_buf()
|
bufnr = a.nvim_get_current_buf()
|
||||||
end
|
end
|
||||||
|
|
||||||
if lang == nil then
|
if not valid_lang(lang) then
|
||||||
local ft = vim.bo[bufnr].filetype
|
lang = M.language.get_lang(vim.bo[bufnr].filetype) or vim.bo[bufnr].filetype
|
||||||
if ft ~= '' then
|
end
|
||||||
lang = M.language.get_lang(ft) or ft
|
|
||||||
-- TODO(lewis6991): we should error here and not default to ft
|
if not valid_lang(lang) then
|
||||||
-- if not lang then
|
if not parsers[bufnr] 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
|
|
||||||
error(
|
error(
|
||||||
string.format(
|
string.format(
|
||||||
'There is no parser available for buffer %d and one could not be'
|
'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
|
||||||
end
|
elseif parsers[bufnr] == nil or parsers[bufnr]:lang() ~= lang then
|
||||||
|
|
||||||
if parsers[bufnr] == nil or parsers[bufnr]:lang() ~= lang then
|
|
||||||
parsers[bufnr] = M._create_parser(bufnr, lang, opts)
|
parsers[bufnr] = M._create_parser(bufnr, lang, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -164,7 +158,6 @@ function M.get_string_parser(str, lang, opts)
|
|||||||
str = { str, 'string' },
|
str = { str, 'string' },
|
||||||
lang = { lang, 'string' },
|
lang = { lang, 'string' },
|
||||||
})
|
})
|
||||||
M.language.add(lang)
|
|
||||||
|
|
||||||
return LanguageTree.new(str, lang, opts)
|
return LanguageTree.new(str, lang, opts)
|
||||||
end
|
end
|
||||||
|
@@ -6,9 +6,25 @@ local M = {}
|
|||||||
---@type table<string,string>
|
---@type table<string,string>
|
||||||
local ft_to_lang = {}
|
local ft_to_lang = {}
|
||||||
|
|
||||||
---@param filetype string
|
--- Get the filetypes associated with the parser named {lang}.
|
||||||
---@return string|nil
|
--- @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)
|
function M.get_lang(filetype)
|
||||||
|
if filetype == '' then
|
||||||
|
return
|
||||||
|
end
|
||||||
return ft_to_lang[filetype]
|
return ft_to_lang[filetype]
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -35,16 +51,14 @@ end
|
|||||||
---@field filetype? string|string[]
|
---@field filetype? string|string[]
|
||||||
---@field symbol_name? 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}
|
--- 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:
|
---@param opts (table|nil) Options:
|
||||||
--- - filetype (string|string[]) Filetype(s) that lang can be parsed with.
|
--- - filetype (string|string[]) Default filetype the parser should be associated with.
|
||||||
--- Note this is not strictly the same as lang since a single lang can
|
--- Defaults to {lang}.
|
||||||
--- parse multiple filetypes.
|
|
||||||
--- Defaults to lang.
|
|
||||||
--- - path (string|nil) Optional path the parser is located at
|
--- - path (string|nil) Optional path the parser is located at
|
||||||
--- - symbol_name (string|nil) Internal symbol name for the language to load
|
--- - symbol_name (string|nil) Internal symbol name for the language to load
|
||||||
function M.add(lang, opts)
|
function M.add(lang, opts)
|
||||||
@@ -61,7 +75,7 @@ function M.add(lang, opts)
|
|||||||
filetype = { filetype, { 'string', 'table' }, true },
|
filetype = { filetype, { 'string', 'table' }, true },
|
||||||
})
|
})
|
||||||
|
|
||||||
M.register(lang, filetype or lang)
|
M.register(lang, filetype)
|
||||||
|
|
||||||
if vim._ts_has_language(lang) then
|
if vim._ts_has_language(lang) then
|
||||||
return
|
return
|
||||||
@@ -83,23 +97,26 @@ function M.add(lang, opts)
|
|||||||
vim._ts_add_language(path, lang, symbol_name)
|
vim._ts_add_language(path, lang, symbol_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Register a lang to be used for a filetype (or list of filetypes).
|
--- @private
|
||||||
---@param lang string Language to register
|
--- @param x string|string[]
|
||||||
---@param filetype string|string[] Filetype(s) to associate with lang
|
--- @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)
|
function M.register(lang, filetype)
|
||||||
vim.validate({
|
vim.validate({
|
||||||
lang = { lang, 'string' },
|
lang = { lang, 'string' },
|
||||||
filetype = { filetype, { 'string', 'table' } },
|
filetype = { filetype, { 'string', 'table' } },
|
||||||
})
|
})
|
||||||
|
|
||||||
local filetypes ---@type string[]
|
for _, f in ipairs(ensure_list(filetype)) do
|
||||||
if type(filetype) == 'string' then
|
|
||||||
filetypes = { filetype }
|
|
||||||
else
|
|
||||||
filetypes = filetype
|
|
||||||
end
|
|
||||||
|
|
||||||
for _, f in ipairs(filetypes) do
|
|
||||||
if f ~= '' then
|
if f ~= '' then
|
||||||
ft_to_lang[f] = lang
|
ft_to_lang[f] = lang
|
||||||
end
|
end
|
||||||
|
@@ -76,7 +76,7 @@ LanguageTree.__index = LanguageTree
|
|||||||
--- "injected" language parsers, which themselves may inject other languages, recursively.
|
--- "injected" language parsers, which themselves may inject other languages, recursively.
|
||||||
---
|
---
|
||||||
---@param source (integer|string) Buffer or text string to parse
|
---@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:
|
---@param opts (table|nil) Optional arguments:
|
||||||
--- - injections table Map of language to injection query strings. Overrides the
|
--- - injections table Map of language to injection query strings. Overrides the
|
||||||
--- built-in runtime file searching for language injections.
|
--- built-in runtime file searching for language injections.
|
||||||
@@ -86,11 +86,6 @@ function LanguageTree.new(source, lang, opts)
|
|||||||
---@type LanguageTreeOpts
|
---@type LanguageTreeOpts
|
||||||
opts = opts or {}
|
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 injections = opts.injections or {}
|
||||||
local self = setmetatable({
|
local self = setmetatable({
|
||||||
_source = source,
|
_source = source,
|
||||||
|
Reference in New Issue
Block a user