Merge pull request #23 from vigoux/feature/textobjects

Node and scope text objects
This commit is contained in:
Kiyan Yazdani 2020-04-25 23:33:13 +02:00 committed by GitHub
commit f8b39e1d3a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 111 additions and 13 deletions

View file

@ -157,12 +157,15 @@ local config = {
return queries.get_query(ft, 'highlights') ~= nil
end
},
-- selection = {
-- enable = false,
-- disable = {},
-- keymaps = {},
-- is_supported = function() return false end
-- },
textobj = {
enable = false,
disable = {},
keymaps = {
node_incremental="grn",
scope_incremental="grc"
},
is_supported = function() return true end
},
-- folding = {
-- enable = false,
-- disable = {},

View file

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

View file

@ -0,0 +1,78 @@
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 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
return node:parent() or node
end
end)
M.scope_incremental = select_incremental(function(node)
if node then
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 config = require'nvim-treesitter.configs'.get_config().textobj
for funcname, mapping in pairs(config.keymaps) do
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
end
function M.detach(bufnr)
local buf = bufnr or api.nvim_get_current_buf()
local config = require'nvim-treesitter.configs'.get_config().textobj
for _, mapping in pairs(config.keymaps) do
api.nvim_buf_del_keymap(buf, 'v', mapping)
api.nvim_buf_del_keymap(buf, 'o', mapping)
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 = {}
@ -76,4 +77,20 @@ function M.setup_commands(mod, commands)
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()
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 25
if exists('g:loaded_nvim_treesitter')
finish