diff options
| author | Steven Sojka <Steven.Sojka@tdameritrade.com> | 2020-06-25 13:26:31 -0500 |
|---|---|---|
| committer | Kiyan Yazdani <yazdani.kiyan@protonmail.com> | 2020-06-30 08:21:01 +0200 |
| commit | 058e8d2296515041be982c6f23c119ec6b6d1ba9 (patch) | |
| tree | d4d7b1d05b3b1d420d41a468460336d3d3ec3971 /lua/nvim-treesitter/ts_utils.lua | |
| parent | 180ad9a1a8c1212d9839bdbe97c11137b48a7064 (diff) | |
feat(refactor): highlight usages module
Diffstat (limited to 'lua/nvim-treesitter/ts_utils.lua')
| -rw-r--r-- | lua/nvim-treesitter/ts_utils.lua | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/lua/nvim-treesitter/ts_utils.lua b/lua/nvim-treesitter/ts_utils.lua index 687a3c53..363d2637 100644 --- a/lua/nvim-treesitter/ts_utils.lua +++ b/lua/nvim-treesitter/ts_utils.lua @@ -212,4 +212,55 @@ function M.get_node_at_cursor(winnr) return root:named_descendant_for_range(cursor[1]-1,cursor[2],cursor[1]-1,cursor[2]) end +-- Finds the definition node and it's scope node of a node +-- @param node starting node +-- @param bufnr buffer +-- @returns the definition node and the definition nodes scope node +function M.find_definition(node, bufnr) + local bufnr = bufnr or api.nvim_get_current_buf() + local node_text = M.get_node_text(node)[1] + local current_scope = M.containing_scope(node) + local _, _, node_start = node:start() + + -- If a scope wasn't found then use the root node + if current_scope == node then + current_scope = parsers.get_parser(bufnr).tree:root() + end + + while current_scope ~= nil and current_scope ~= node do + for _, def in ipairs(locals.collect_locals(bufnr, current_scope)) do + if def.definition then + for _, def_node in ipairs(M.get_local_nodes(def.definition)) do + local _, _, def_start = def_node:start() + + if M.get_node_text(def_node)[1] == node_text and def_start < node_start then + return def_node, current_scope + end + end + end + end + + current_scope = M.containing_scope(current_scope:parent()) + end + + return nil, nil +end + +-- Gets all nodes from a local list result. +-- @param local_def the local list result +-- @returns a list of nodes +function M.get_local_nodes(local_def) + if local_def.node then + return { local_def.node } + else + local result = {} + + for _, def in pairs(local_def) do + vim.list_extend(result, M.get_local_nodes(def)) + end + + return result + end +end + return M |
