mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-18 11:20:07 -04:00
refactor(textobj): use configs and don't use VimL
This commit is contained in:
parent
7c7d4e5270
commit
cadb3ee1ff
4 changed files with 78 additions and 62 deletions
|
|
@ -1,16 +0,0 @@
|
||||||
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_scope_incr()
|
|
||||||
call s:visual_node(luaeval('require"nvim-treesitter.textobj".scope_incremental()'))
|
|
||||||
endfunction
|
|
||||||
|
|
@ -157,12 +157,15 @@ local config = {
|
||||||
return queries.get_query(ft, 'highlights') ~= nil
|
return queries.get_query(ft, 'highlights') ~= nil
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
-- selection = {
|
textobj = {
|
||||||
-- enable = false,
|
enable = false,
|
||||||
-- disable = {},
|
disable = {},
|
||||||
-- keymaps = {},
|
keymaps = {
|
||||||
-- is_supported = function() return false end
|
node_incremental="grn",
|
||||||
-- },
|
scope_incremental="grc"
|
||||||
|
},
|
||||||
|
is_supported = function() return true end
|
||||||
|
},
|
||||||
-- folding = {
|
-- folding = {
|
||||||
-- enable = false,
|
-- enable = false,
|
||||||
-- disable = {},
|
-- disable = {},
|
||||||
|
|
|
||||||
|
|
@ -4,54 +4,83 @@ local parsers = require'nvim-treesitter.parsers'
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
local function node_range_to_vim(node)
|
local function node_range_to_vim(node)
|
||||||
|
if not node then return end
|
||||||
|
|
||||||
|
local start_row, start_col, end_row, end_col = node:range()
|
||||||
|
|
||||||
|
local select_range = [[
|
||||||
|
call cursor(%d, %d)
|
||||||
|
normal v
|
||||||
|
call cursor(%d, %d)
|
||||||
|
]]
|
||||||
|
local exec_command = string.format(select_range,
|
||||||
|
start_row+1, start_col+1,
|
||||||
|
end_row+1, end_col+1)
|
||||||
|
|
||||||
|
api.nvim_exec(exec_command, false)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function select_incremental(increment_func)
|
||||||
|
return function()
|
||||||
|
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()
|
||||||
|
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
|
||||||
|
node = increment_func(node)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return node_range_to_vim(node)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
M.node_incremental = select_incremental(function(node)
|
||||||
if node then
|
if node then
|
||||||
local start_row, start_col, end_row, end_col = node:range()
|
return node:parent() or node
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
return {{start_row, start_col}, {end_row, end_col}}
|
M.scope_incremental = select_incremental(function(node)
|
||||||
else
|
if node then
|
||||||
return {{}, {}}
|
return utils.smallest_containing_scope(node:parent() or node)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
function M.attach(bufnr)
|
||||||
|
local buf = bufnr or api.nvim_get_current_buf()
|
||||||
|
|
||||||
|
local function textobj_create(mapping, funcname)
|
||||||
|
api.nvim_buf_set_keymap(buf, 'v', mapping,
|
||||||
|
string.format(":lua require'nvim-treesitter.textobj'.%s()<CR>", funcname), { silent = true })
|
||||||
|
api.nvim_buf_set_keymap(buf, 'o', mapping,
|
||||||
|
string.format(":normal v%s<CR>", mapping), { silent = true })
|
||||||
|
end
|
||||||
|
|
||||||
|
local config = require'nvim-treesitter.configs'.get_config().textobj
|
||||||
|
for funcname, mapping in pairs(config.keymaps) do
|
||||||
|
textobj_create(mapping, funcname)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.node_incremental()
|
function M.detach(bufnr)
|
||||||
local buf, sel_start_line, sel_start_col, _ = unpack(vim.fn.getpos("'<"))
|
local buf = bufnr or api.nvim_get_current_buf()
|
||||||
local buf, sel_end_line, sel_end_col, _ = unpack(vim.fn.getpos("'>"))
|
|
||||||
|
|
||||||
local node = nil
|
local function textobj_delete(mapping)
|
||||||
if parsers.has_parser() then
|
api.nvim_buf_del_keymap(buf, 'v', default_mapping)
|
||||||
local root = parsers.get_parser():parse():root()
|
api.nvim_buf_del_keymap(buf, 'o', default_mapping)
|
||||||
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
|
|
||||||
-- 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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return node_range_to_vim(node)
|
local config = require'nvim-treesitter.configs'.get_config().textobj
|
||||||
end
|
for _, mapping in pairs(config.keymaps) do
|
||||||
|
textobj_delete(mapping)
|
||||||
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()
|
|
||||||
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
|
|
||||||
node = utils.smallest_containing_scope(node:parent() or node)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
return node_range_to_vim(node)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
" Last Change: 2020 avril 23
|
" Last Change: 2020 avril 25
|
||||||
|
|
||||||
if exists('g:loaded_nvim_treesitter')
|
if exists('g:loaded_nvim_treesitter')
|
||||||
finish
|
finish
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue