diff options
| author | Kiyan Yazdani <yazdani.kiyan@protonmail.com> | 2020-04-25 23:33:13 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-04-25 23:33:13 +0200 |
| commit | f8b39e1d3ad36c2e5d6eb6f51983275ecc9145f0 (patch) | |
| tree | 6ff340a7c1e243e111bd0e593444eaeca7236741 /lua/nvim-treesitter/utils.lua | |
| parent | 2ca0c348f73655b395ded2e60e13ba0c2249dc74 (diff) | |
| parent | bb709aa8d0e241232b31ca1ae9fc042b16413ffd (diff) | |
Merge pull request #23 from vigoux/feature/textobjects
Node and scope text objects
Diffstat (limited to 'lua/nvim-treesitter/utils.lua')
| -rw-r--r-- | lua/nvim-treesitter/utils.lua | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/lua/nvim-treesitter/utils.lua b/lua/nvim-treesitter/utils.lua index 59de5dd5..391863f0 100644 --- a/lua/nvim-treesitter/utils.lua +++ b/lua/nvim-treesitter/utils.lua @@ -1,6 +1,7 @@ -- Utils collection for nvim-treesitter local api = vim.api local parsers = require'nvim-treesitter.parsers' +local locals = require'nvim-treesitter.locals' local M = {} @@ -76,4 +77,20 @@ function M.setup_commands(mod, commands) end end +--- Gets the smallest scope which contains @param node +function M.smallest_containing_scope(node, bufnr) + local bufnr = bufnr or api.nvim_get_current_buf() + + local root = parsers.get_parser(bufnr):parse():root() + if not node then return root end + + local scopes = locals.get_scopes(bufnr) + local current = node + while current ~= nil and not vim.tbl_contains(scopes, current) do + current = current:parent() + end + + return current or root +end + return M |
