mirror of
https://github.com/neovim/neovim.git
synced 2025-10-26 12:27:24 +00:00
fix(treesitter): don't throw an error for missing injected langs
This commit is contained in:
@@ -8,7 +8,8 @@ local M = {}
|
|||||||
--
|
--
|
||||||
-- @param lang The language the parser should parse
|
-- @param lang The language the parser should parse
|
||||||
-- @param path Optionnal path the parser is located at
|
-- @param path Optionnal path the parser is located at
|
||||||
function M.require_language(lang, path)
|
-- @param silent Don't throw an error if language not found
|
||||||
|
function M.require_language(lang, path, silent)
|
||||||
if vim._ts_has_language(lang) then
|
if vim._ts_has_language(lang) then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
@@ -16,12 +17,23 @@ function M.require_language(lang, path)
|
|||||||
local fname = 'parser/' .. lang .. '.*'
|
local fname = 'parser/' .. lang .. '.*'
|
||||||
local paths = a.nvim_get_runtime_file(fname, false)
|
local paths = a.nvim_get_runtime_file(fname, false)
|
||||||
if #paths == 0 then
|
if #paths == 0 then
|
||||||
|
if silent then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
-- TODO(bfredl): help tag?
|
-- TODO(bfredl): help tag?
|
||||||
error("no parser for '"..lang.."' language, see :help treesitter-parsers")
|
error("no parser for '"..lang.."' language, see :help treesitter-parsers")
|
||||||
end
|
end
|
||||||
path = paths[1]
|
path = paths[1]
|
||||||
end
|
end
|
||||||
vim._ts_add_language(path, lang)
|
|
||||||
|
if silent then
|
||||||
|
return pcall(function() vim._ts_add_language(path, lang) end)
|
||||||
|
else
|
||||||
|
vim._ts_add_language(path, lang)
|
||||||
|
end
|
||||||
|
|
||||||
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Inspects the provided language.
|
--- Inspects the provided language.
|
||||||
|
|||||||
@@ -121,23 +121,30 @@ function LanguageTree:parse()
|
|||||||
local seen_langs = {}
|
local seen_langs = {}
|
||||||
|
|
||||||
for lang, injection_ranges in pairs(injections_by_lang) do
|
for lang, injection_ranges in pairs(injections_by_lang) do
|
||||||
local child = self._children[lang]
|
local has_lang = language.require_language(lang, nil, true)
|
||||||
|
|
||||||
if not child then
|
-- Child language trees should just be ignored if not found, since
|
||||||
child = self:add_child(lang)
|
-- they can depend on the text of a node. Intermediate strings
|
||||||
|
-- would cause errors for unknown parsers.
|
||||||
|
if has_lang then
|
||||||
|
local child = self._children[lang]
|
||||||
|
|
||||||
|
if not child then
|
||||||
|
child = self:add_child(lang)
|
||||||
|
end
|
||||||
|
|
||||||
|
child:set_included_regions(injection_ranges)
|
||||||
|
|
||||||
|
local _, child_changes = child:parse()
|
||||||
|
|
||||||
|
-- Propagate any child changes so they are included in the
|
||||||
|
-- the change list for the callback.
|
||||||
|
if child_changes then
|
||||||
|
vim.list_extend(changes, child_changes)
|
||||||
|
end
|
||||||
|
|
||||||
|
seen_langs[lang] = true
|
||||||
end
|
end
|
||||||
|
|
||||||
child:set_included_regions(injection_ranges)
|
|
||||||
|
|
||||||
local _, child_changes = child:parse()
|
|
||||||
|
|
||||||
-- Propagate any child changes so they are included in the
|
|
||||||
-- the change list for the callback.
|
|
||||||
if child_changes then
|
|
||||||
vim.list_extend(changes, child_changes)
|
|
||||||
end
|
|
||||||
|
|
||||||
seen_langs[lang] = true
|
|
||||||
end
|
end
|
||||||
|
|
||||||
for lang, _ in pairs(self._children) do
|
for lang, _ in pairs(self._children) do
|
||||||
|
|||||||
@@ -22,6 +22,10 @@ describe('treesitter API', function()
|
|||||||
matches("Error executing lua: Failed to load parser: uv_dlopen: .+",
|
matches("Error executing lua: Failed to load parser: uv_dlopen: .+",
|
||||||
pcall_err(exec_lua, "parser = vim.treesitter.require_language('borklang', 'borkbork.so')"))
|
pcall_err(exec_lua, "parser = vim.treesitter.require_language('borklang', 'borkbork.so')"))
|
||||||
|
|
||||||
|
-- Should not throw an error when silent
|
||||||
|
eq(false, exec_lua("return vim.treesitter.require_language('borklang', nil, true)"))
|
||||||
|
eq(false, exec_lua("return vim.treesitter.require_language('borklang', 'borkbork.so', true)"))
|
||||||
|
|
||||||
eq("Error executing lua: .../language.lua:0: no parser for 'borklang' language, see :help treesitter-parsers",
|
eq("Error executing lua: .../language.lua:0: no parser for 'borklang' language, see :help treesitter-parsers",
|
||||||
pcall_err(exec_lua, "parser = vim.treesitter.inspect_language('borklang')"))
|
pcall_err(exec_lua, "parser = vim.treesitter.inspect_language('borklang')"))
|
||||||
end)
|
end)
|
||||||
|
|||||||
Reference in New Issue
Block a user