summaryrefslogtreecommitdiff
path: root/lua/nvim-treesitter/utils.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua/nvim-treesitter/utils.lua')
-rw-r--r--lua/nvim-treesitter/utils.lua15
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