aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim
diff options
context:
space:
mode:
authorSteven Sojka <steelsojka@gmail.com>2020-12-04 16:56:29 -0600
committerSteven Sojka <steelsojka@gmail.com>2020-12-04 16:56:29 -0600
commite15c5f58df52ee05d042c4696c72c3fbb4457eee (patch)
treea8d88a76ecec93a536850ff1b8a2772cb53e40ea /runtime/lua/vim
parent0c8d6ab536ec2e8e95c0de441e6ad1fdfccc909f (diff)
downloadrneovim-e15c5f58df52ee05d042c4696c72c3fbb4457eee.tar.gz
rneovim-e15c5f58df52ee05d042c4696c72c3fbb4457eee.tar.bz2
rneovim-e15c5f58df52ee05d042c4696c72c3fbb4457eee.zip
fix(treesitter): don't throw an error for missing injected langs
Diffstat (limited to 'runtime/lua/vim')
-rw-r--r--runtime/lua/vim/treesitter/language.lua16
-rw-r--r--runtime/lua/vim/treesitter/languagetree.lua31
2 files changed, 33 insertions, 14 deletions
diff --git a/runtime/lua/vim/treesitter/language.lua b/runtime/lua/vim/treesitter/language.lua
index a7e36a0b89..d60cd2d0c7 100644
--- a/runtime/lua/vim/treesitter/language.lua
+++ b/runtime/lua/vim/treesitter/language.lua
@@ -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.
diff --git a/runtime/lua/vim/treesitter/languagetree.lua b/runtime/lua/vim/treesitter/languagetree.lua
index a8b62e21b9..0a28891814 100644
--- a/runtime/lua/vim/treesitter/languagetree.lua
+++ b/runtime/lua/vim/treesitter/languagetree.lua
@@ -121,23 +121,30 @@ function LanguageTree:parse()
local seen_langs = {}
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 = self:add_child(lang)
- end
+ -- Child language trees should just be ignored if not found, since
+ -- 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]
- child:set_included_regions(injection_ranges)
+ if not child then
+ child = self:add_child(lang)
+ end
- local _, child_changes = child:parse()
+ child:set_included_regions(injection_ranges)
- -- 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
+ local _, child_changes = child:parse()
- seen_langs[lang] = true
+ -- 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