diff options
| author | Thomas Vigouroux <tomvig38@gmail.com> | 2020-04-23 08:43:35 +0200 |
|---|---|---|
| committer | Thomas Vigouroux <tomvig38@gmail.com> | 2020-04-25 21:46:22 +0200 |
| commit | c62685841e52dacb4659f49d16336765e7e10f10 (patch) | |
| tree | 4a453cb7249de1d910e5cc96fe6d274efab4b08e /lua/nvim-treesitter/utils.lua | |
| parent | 78b40f895cb2bf789944f94abd47dd7f7ab3f8dc (diff) | |
textobj: add incremental scope selection
Diffstat (limited to 'lua/nvim-treesitter/utils.lua')
| -rw-r--r-- | lua/nvim-treesitter/utils.lua | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/lua/nvim-treesitter/utils.lua b/lua/nvim-treesitter/utils.lua index 59de5dd5..2ef2746a 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 = {} @@ -74,6 +75,20 @@ function M.setup_commands(mod, commands) }) api.nvim_command(table.concat(parts, " ")) 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 |
