mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-01 19:17:02 -04:00
textobj: add incremental node selection
This commit is contained in:
parent
2ca0c348f7
commit
78b40f895c
3 changed files with 58 additions and 6 deletions
16
autoload/nvim_treesitter.vim
Normal file
16
autoload/nvim_treesitter.vim
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
function! s:visual_node(node_range)
|
||||
let [l:cursor_start, l:cursor_end] = a:node_range
|
||||
if !empty(l:cursor_start) && !empty(l:cursor_end)
|
||||
call cursor(l:cursor_start[0]+1, l:cursor_start[1]+1)
|
||||
normal v
|
||||
call cursor(l:cursor_end[0]+1, l:cursor_end[1])
|
||||
endif
|
||||
endfunction
|
||||
|
||||
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()'))
|
||||
endfunction
|
||||
|
|
@ -68,8 +68,8 @@ function M.get_definitions(bufnr)
|
|||
local defs = {}
|
||||
|
||||
for _, loc in ipairs(locals) do
|
||||
if loc.definition then
|
||||
table.insert(defs, {definition=loc.definition, kind=loc.kind})
|
||||
if loc.definition and loc.definition.node then
|
||||
table.insert(defs, {node=loc.definition.node, kind=loc.kind})
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -82,8 +82,8 @@ function M.get_scopes(bufnr)
|
|||
local scopes = {}
|
||||
|
||||
for _, loc in ipairs(locals) do
|
||||
if loc.scope then
|
||||
table.insert(scopes, loc.scope)
|
||||
if loc.scope and loc.scope.node then
|
||||
table.insert(scopes, loc.scope.node)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -96,8 +96,8 @@ function M.get_references(bufnr)
|
|||
local refs = {}
|
||||
|
||||
for _, loc in ipairs(locals) do
|
||||
if loc.reference then
|
||||
table.insert(refs, loc.reference)
|
||||
if loc.reference and loc.reference.node then
|
||||
table.insert(refs, loc.reference.node)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
36
lua/nvim-treesitter/textobj.lua
Normal file
36
lua/nvim-treesitter/textobj.lua
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
local api = vim.api
|
||||
local utils = require'nvim-treesitter.utils'
|
||||
local parsers = require'nvim-treesitter.parsers'
|
||||
local M = {}
|
||||
|
||||
local function node_range_to_vim(node)
|
||||
if node then
|
||||
local start_row, start_col, end_row, end_col = node:range()
|
||||
|
||||
return {{start_row, start_col}, {end_row, end_col}}
|
||||
else
|
||||
return {{}, {}}
|
||||
end
|
||||
end
|
||||
|
||||
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("'>"))
|
||||
|
||||
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)
|
||||
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)
|
||||
end
|
||||
else
|
||||
return node_range_to_vim()
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
Loading…
Add table
Add a link
Reference in a new issue