aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/treesitter/languagetree.lua
diff options
context:
space:
mode:
authorLewis Russell <lewis6991@gmail.com>2023-09-16 23:10:30 +0100
committerLewis Russell <me@lewisr.dev>2023-09-20 13:42:41 +0100
commit877d04d0fb83b5fc602dbab22b58f26a793ec236 (patch)
tree2f6ee1af59ae412256116d8abf28a2a6f7b6d848 /runtime/lua/vim/treesitter/languagetree.lua
parent11865dbe3917a538617b2b7df31bf7375e292bc8 (diff)
downloadrneovim-877d04d0fb83b5fc602dbab22b58f26a793ec236.tar.gz
rneovim-877d04d0fb83b5fc602dbab22b58f26a793ec236.tar.bz2
rneovim-877d04d0fb83b5fc602dbab22b58f26a793ec236.zip
feat(lua): add vim.func._memoize
Memoizes a function, using a custom function to hash the arguments. Private for now until: - There are other places in the codebase that could benefit from this (e.g. LSP), but might require other changes to accommodate. - Invalidation of the cache needs to be controllable. Using weak tables is an acceptable invalidation policy, but it shouldn't be the only one. - I don't think the story around `hash_fn` is completely thought out. We may be able to have a good default hash_fn by hashing each argument, so basically a better 'concat'.
Diffstat (limited to 'runtime/lua/vim/treesitter/languagetree.lua')
-rw-r--r--runtime/lua/vim/treesitter/languagetree.lua10
1 files changed, 6 insertions, 4 deletions
diff --git a/runtime/lua/vim/treesitter/languagetree.lua b/runtime/lua/vim/treesitter/languagetree.lua
index f931291ed7..b2c4e9167d 100644
--- a/runtime/lua/vim/treesitter/languagetree.lua
+++ b/runtime/lua/vim/treesitter/languagetree.lua
@@ -738,12 +738,14 @@ local function add_injection(t, tree_index, pattern, lang, combined, ranges)
end
-- TODO(clason): replace by refactored `ts.has_parser` API (without registering)
----@param lang string parser name
----@return boolean # true if parser for {lang} exists on rtp
-local has_parser = function(lang)
+--- The result of this function is cached to prevent nvim_get_runtime_file from being
+--- called too often
+--- @param lang string parser name
+--- @return boolean # true if parser for {lang} exists on rtp
+local has_parser = vim.func._memoize(1, function(lang)
return vim._ts_has_language(lang)
or #vim.api.nvim_get_runtime_file('parser/' .. lang .. '.*', false) > 0
-end
+end)
--- Return parser name for language (if exists) or filetype (if registered and exists).
--- Also attempts with the input lower-cased.