textobj: add incremental scope selection

This commit is contained in:
Thomas Vigouroux 2020-04-23 08:43:35 +02:00
parent 78b40f895c
commit c62685841e
4 changed files with 40 additions and 3 deletions

View file

@ -11,6 +11,6 @@ function! nvim_treesitter#select_node_incr()
call s:visual_node(luaeval('require"nvim-treesitter.textobj".node_incremental()'))
endfunction
function! nvim_treesitter#select_context_incr()
call s:visual_node(luaeval('require"nvim-treesitter.textobj".context_incremental()'))
function! nvim_treesitter#select_scope_incr()
call s:visual_node(luaeval('require"nvim-treesitter.textobj".scope_incremental()'))
endfunction

View file

@ -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

View file

@ -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

View file

@ -1,4 +1,4 @@
" Last Change: 2020 avril 19
" Last Change: 2020 avril 23
if exists('g:loaded_nvim_treesitter')
finish