diff --git a/lua/nvim-treesitter/locals.lua b/lua/nvim-treesitter/locals.lua index b6d9c25b2..aeb3f4cb1 100644 --- a/lua/nvim-treesitter/locals.lua +++ b/lua/nvim-treesitter/locals.lua @@ -18,15 +18,7 @@ end ---@param node TSNode ---@return TSNode result local function get_root_for_node(node) - local parent = node ---@type TSNode? - local result = node - - while parent ~= nil do - result = parent - parent = result:parent() - end - - return result + return node:tree():root() end -- Creates unique id for a node based on text and range @@ -103,7 +95,9 @@ function M.recurse_local_nodes(local_def, accumulator, full_match, last_match) if local_def.node then accumulator(local_def, local_def.node, full_match, last_match) else - for match_key, def in pairs(local_def) do + for match_key, def in + pairs(local_def --[[@as {[string]: TSLocal}]]) + do M.recurse_local_nodes( def, accumulator, @@ -120,16 +114,16 @@ end ---@param hash_fn fun(...): any ---@return F local function memoize(fn, hash_fn) - local cache = setmetatable({}, { __mode = 'kv' }) ---@type table + local cache = setmetatable({}, { __mode = 'kv' }) ---@type table return function(...) local key = hash_fn(...) if cache[key] == nil then - local v = { fn(...) } ---@type any + local v = { fn(...) } ---@type any[] for k, value in pairs(v) do if value == nil then - value[k] = vim.NIL + value[k] = vim.NIL ---@type table end end @@ -138,9 +132,9 @@ local function memoize(fn, hash_fn) local v = cache[key] - for k, value in pairs(v) do + for k, value in ipairs(v) do if value == vim.NIL then - value[k] = nil + value[k] = nil ---@type table end end @@ -200,7 +194,9 @@ M.get = memoize(function(bufnr) local kind = query.captures[id] local scope = 'local' ---@type string - for k, v in pairs(metadata) do + for k, v in + pairs(metadata --[[@as {[integer|string]: string}]]) + do if type(k) == 'string' and vim.endswith(k, 'local.scope') then scope = v end @@ -379,7 +375,8 @@ function M.containing_scope(node, bufnr, allow_scope) local iter_node = node ---@type TSNode? while iter_node ~= nil and not vim.tbl_contains(scopes, iter_node) do - iter_node = iter_node:parent() + local parent = iter_node:parent() + iter_node = parent end return iter_node or (allow_scope and node or nil)