diff options
author | Thomas Vigouroux <thomas.vigouroux@protonmail.com> | 2022-09-07 08:39:56 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-07 08:39:56 +0200 |
commit | fd1595514b747d8b083f78007579d869ccfbe89c (patch) | |
tree | 41e52c9e43db9351de3aa33a8628b76db07906c3 /runtime/lua | |
parent | f32fd19f1eedbd75e6a37b73f28cf8761e0e875c (diff) | |
download | rneovim-fd1595514b747d8b083f78007579d869ccfbe89c.tar.gz rneovim-fd1595514b747d8b083f78007579d869ccfbe89c.tar.bz2 rneovim-fd1595514b747d8b083f78007579d869ccfbe89c.zip |
Use weak tables in tree-sitter code (#17117)
feat(treesitter): use weak tables when possible
Also add the defaulttable function to create a table whose values are created when a key is missing.
Diffstat (limited to 'runtime/lua')
-rw-r--r-- | runtime/lua/vim/shared.lua | 25 | ||||
-rw-r--r-- | runtime/lua/vim/treesitter.lua | 5 | ||||
-rw-r--r-- | runtime/lua/vim/treesitter/query.lua | 9 |
3 files changed, 29 insertions, 10 deletions
diff --git a/runtime/lua/vim/shared.lua b/runtime/lua/vim/shared.lua index e1b4ed4ea9..59cb669609 100644 --- a/runtime/lua/vim/shared.lua +++ b/runtime/lua/vim/shared.lua @@ -715,5 +715,30 @@ function vim.is_callable(f) return type(m.__call) == 'function' end +--- Creates a table whose members are automatically created when accessed, if they don't already +--- exist. +--- +--- They mimic defaultdict in python. +--- +--- If @p create is @c nil, this will create a defaulttable whose constructor function is +--- this function, effectively allowing to create nested tables on the fly: +--- +--- <pre> +--- local a = vim.defaulttable() +--- a.b.c = 1 +--- </pre> +--- +---@param create function|nil The function called to create a missing value. +---@return table Empty table with metamethod +function vim.defaulttable(create) + create = create or vim.defaulttable + return setmetatable({}, { + __index = function(tbl, key) + rawset(tbl, key, create()) + return rawget(tbl, key) + end, + }) +end + return vim -- vim:sw=2 ts=2 et diff --git a/runtime/lua/vim/treesitter.lua b/runtime/lua/vim/treesitter.lua index 5ebccff8f7..cde0491b12 100644 --- a/runtime/lua/vim/treesitter.lua +++ b/runtime/lua/vim/treesitter.lua @@ -3,10 +3,7 @@ local query = require('vim.treesitter.query') local language = require('vim.treesitter.language') local LanguageTree = require('vim.treesitter.languagetree') --- TODO(bfredl): currently we retain parsers for the lifetime of the buffer. --- Consider use weak references to release parser if all plugins are done with --- it. -local parsers = {} +local parsers = setmetatable({}, { __mode = 'v' }) local M = vim.tbl_extend('error', query, language) diff --git a/runtime/lua/vim/treesitter/query.lua b/runtime/lua/vim/treesitter/query.lua index 697e2e7691..78042a74bf 100644 --- a/runtime/lua/vim/treesitter/query.lua +++ b/runtime/lua/vim/treesitter/query.lua @@ -140,12 +140,9 @@ function M.get_query(lang, query_name) end end -local query_cache = setmetatable({}, { - __index = function(tbl, key) - rawset(tbl, key, {}) - return rawget(tbl, key) - end, -}) +local query_cache = vim.defaulttable(function() + return setmetatable({}, { __mode = 'v' }) +end) --- Parse {query} as a string. (If the query is in a file, the caller --- should read the contents into a string before calling). |