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/textobj.lua | |
| parent | 78b40f895cb2bf789944f94abd47dd7f7ab3f8dc (diff) | |
textobj: add incremental scope selection
Diffstat (limited to 'lua/nvim-treesitter/textobj.lua')
| -rw-r--r-- | lua/nvim-treesitter/textobj.lua | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/lua/nvim-treesitter/textobj.lua b/lua/nvim-treesitter/textobj.lua index 9460e193..a28440e3 100644 --- a/lua/nvim-treesitter/textobj.lua +++ b/lua/nvim-treesitter/textobj.lua @@ -33,4 +33,26 @@ function M.node_incremental() end end +function M.scope_incremental() + local _, sel_start_line, sel_start_col, _ = unpack(vim.fn.getpos("'<")) + local _, sel_end_line, sel_end_col, _ = unpack(vim.fn.getpos("'>")) + + if parsers.has_parser() then + local root = parsers.get_parser():parse():root() + local node = utils.smallest_containing_scope( + root:named_descendant_for_range(sel_start_line-1, sel_start_col-1, sel_end_line-1, sel_end_col)) + + local node_start_row, node_start_col, node_end_row, node_end_col = node:range() + + if (sel_start_line-1) == node_start_row and (sel_start_col-1) == node_start_col + and (sel_end_line-1) == node_end_row and sel_end_col == node_end_col then + return node_range_to_vim(utils.smallest_containing_scope(node:parent() or node)) + else + return node_range_to_vim(node) + end + else + return node_range_to_vim() + end +end + return M |
