diff options
| author | Steven Sojka <Steven.Sojka@tdameritrade.com> | 2020-06-26 13:57:23 -0500 |
|---|---|---|
| committer | Kiyan Yazdani <yazdani.kiyan@protonmail.com> | 2020-06-30 08:21:01 +0200 |
| commit | 6f8e4c97a4f99b1a04cca5c41c333ffb5337d84a (patch) | |
| tree | 8b49bdf8651b54b0ff398af254c026cb303347fc /lua/nvim-treesitter/ts_utils.lua | |
| parent | 64838e51c0fcb9def4be912391a1544b4d9a9d27 (diff) | |
feat(refactor): add definition navigation module
Diffstat (limited to 'lua/nvim-treesitter/ts_utils.lua')
| -rw-r--r-- | lua/nvim-treesitter/ts_utils.lua | 34 |
1 files changed, 27 insertions, 7 deletions
diff --git a/lua/nvim-treesitter/ts_utils.lua b/lua/nvim-treesitter/ts_utils.lua index 90dc3d26..9720be7b 100644 --- a/lua/nvim-treesitter/ts_utils.lua +++ b/lua/nvim-treesitter/ts_utils.lua @@ -252,16 +252,36 @@ end -- @param local_def the local list result -- @returns a list of nodes function M.get_local_nodes(local_def) + local result = {} + + M.recurse_local_nodes(local_def, function(_, node) + table.insert(result, node) + end) + + return result +end + +-- Recurse locals results until a node is found. +-- The accumulator function is given +-- * The table of the node +-- * The node +-- * The full definition match `@definition.var.something` -> 'var.something' +-- * The last definition match `@definition.var.something` -> 'something' +-- @param The locals result +-- @param The accumulator function +-- @param The full match path to append to +-- @param The last match +function M.recurse_local_nodes(local_def, accumulator, full_match, last_match) if local_def.node then - return { local_def.node } + accumulator(local_def, local_def.node, full_match, last_match) else - local result = {} - - for _, def in pairs(local_def) do - vim.list_extend(result, M.get_local_nodes(def)) + for match_key, def in pairs(local_def) do + M.recurse_local_nodes( + def, + accumulator, + full_match and (full_match..'.'..match_key) or match_key, + match_key) end - - return result end end |
