fix(treesitter): don't throw an error for missing injected langs

This commit is contained in:
Steven Sojka
2020-12-04 16:56:29 -06:00
parent 0c8d6ab536
commit e15c5f58df
3 changed files with 40 additions and 17 deletions

View File

@ -8,7 +8,8 @@ local M = {}
--
-- @param lang The language the parser should parse
-- @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
return true
end
@ -16,12 +17,23 @@ function M.require_language(lang, path)
local fname = 'parser/' .. lang .. '.*'
local paths = a.nvim_get_runtime_file(fname, false)
if #paths == 0 then
if silent then
return false
end
-- TODO(bfredl): help tag?
error("no parser for '"..lang.."' language, see :help treesitter-parsers")
end
path = paths[1]
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
--- Inspects the provided language.