diff options
Diffstat (limited to 'runtime/lua/vim')
-rw-r--r-- | runtime/lua/vim/treesitter.lua | 33 | ||||
-rw-r--r-- | runtime/lua/vim/treesitter/language.lua | 57 | ||||
-rw-r--r-- | runtime/lua/vim/treesitter/languagetree.lua | 7 |
3 files changed, 51 insertions, 46 deletions
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, |