diff options
| author | Thomas Vigouroux <tomvig38@gmail.com> | 2020-04-23 12:15:03 +0200 |
|---|---|---|
| committer | Thomas Vigouroux <tomvig38@gmail.com> | 2020-04-25 21:46:22 +0200 |
| commit | 7c7d4e52703ba76a2aa96ddcc30c4e210addc8af (patch) | |
| tree | 6d740657756535a9f40659d4f64738b54f9ba559 | |
| parent | textobj: add incremental scope selection (diff) | |
| download | nvim-treesitter-7c7d4e52703ba76a2aa96ddcc30c4e210addc8af.tar nvim-treesitter-7c7d4e52703ba76a2aa96ddcc30c4e210addc8af.tar.gz nvim-treesitter-7c7d4e52703ba76a2aa96ddcc30c4e210addc8af.tar.bz2 nvim-treesitter-7c7d4e52703ba76a2aa96ddcc30c4e210addc8af.tar.lz nvim-treesitter-7c7d4e52703ba76a2aa96ddcc30c4e210addc8af.tar.xz nvim-treesitter-7c7d4e52703ba76a2aa96ddcc30c4e210addc8af.tar.zst nvim-treesitter-7c7d4e52703ba76a2aa96ddcc30c4e210addc8af.zip | |
textobj: little refactor
| -rw-r--r-- | lua/nvim-treesitter/textobj.lua | 25 | ||||
| -rw-r--r-- | lua/nvim-treesitter/utils.lua | 2 |
2 files changed, 14 insertions, 13 deletions
diff --git a/lua/nvim-treesitter/textobj.lua b/lua/nvim-treesitter/textobj.lua index a28440e32..8ba20d793 100644 --- a/lua/nvim-treesitter/textobj.lua +++ b/lua/nvim-treesitter/textobj.lua @@ -17,42 +17,41 @@ function M.node_incremental() local buf, sel_start_line, sel_start_col, _ = unpack(vim.fn.getpos("'<")) local buf, sel_end_line, sel_end_col, _ = unpack(vim.fn.getpos("'>")) + local node = nil if parsers.has_parser() then local root = parsers.get_parser():parse():root() - local node = root:named_descendant_for_range(sel_start_line-1, sel_start_col-1, sel_end_line-1, sel_end_col) + node = 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(node:parent() or node) - else - return node_range_to_vim(node) + -- TODO(vigoux): actually it is not really the parent here + -- it might be needed to climb more than only one parent, but a strictly growing node + node = node:parent() or node end - else - return node_range_to_vim() end + + return node_range_to_vim(node) 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("'>")) + local node = nil 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)) + 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) + node = utils.smallest_containing_scope(node:parent() or node) end - else - return node_range_to_vim() end + return node_range_to_vim(node) end return M diff --git a/lua/nvim-treesitter/utils.lua b/lua/nvim-treesitter/utils.lua index 2ef2746aa..391863f0d 100644 --- a/lua/nvim-treesitter/utils.lua +++ b/lua/nvim-treesitter/utils.lua @@ -75,6 +75,8 @@ function M.setup_commands(mod, commands) }) api.nvim_command(table.concat(parts, " ")) 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() |
