summaryrefslogtreecommitdiff
path: root/lua/nvim-treesitter/textobjects.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua/nvim-treesitter/textobjects.lua')
-rw-r--r--lua/nvim-treesitter/textobjects.lua120
1 files changed, 117 insertions, 3 deletions
diff --git a/lua/nvim-treesitter/textobjects.lua b/lua/nvim-treesitter/textobjects.lua
index 7227b9e0..9c312f44 100644
--- a/lua/nvim-treesitter/textobjects.lua
+++ b/lua/nvim-treesitter/textobjects.lua
@@ -8,7 +8,7 @@ local ts_utils = require'nvim-treesitter.ts_utils'
local M = {}
-function M.select_textobject(query_string)
+local function get_textobject_at_point(query_string)
local bufnr = vim.api.nvim_get_current_buf()
local lang = parsers.get_buf_lang(bufnr)
if not lang then return end
@@ -66,11 +66,93 @@ function M.select_textobject(query_string)
if smallest_range.start then
local start_range = {smallest_range.start.node:range()}
local node_range = {smallest_range.node:range()}
- ts_utils.update_selection(bufnr, {start_range[1], start_range[2], node_range[3], node_range[4]})
+ return bufnr, {start_range[1], start_range[2], node_range[3], node_range[4]}, smallest_range.node
else
- ts_utils.update_selection(bufnr, smallest_range.node)
+ return bufnr, {smallest_range.node:range()}, smallest_range.node
+ end
+ end
+end
+
+function M.select_textobject(query_string)
+ local bufnr, textobject = get_textobject_at_point(query_string)
+ if textobject then
+ ts_utils.update_selection(bufnr, textobject)
+ end
+end
+
+local function swap_textobject(query_string, direction)
+ local bufnr, textobject_range, node = get_textobject_at_point(query_string)
+ local step = direction > 0 and 1 or -1
+ if not node then return end
+ for _ = 1, math.abs(direction), step do
+ if direction > 0 then
+ ts_utils.swap_nodes(textobject_range, M.next_textobject(node, query_string, true, bufnr), bufnr, "yes, set cursor!")
+ else
+ ts_utils.swap_nodes(textobject_range, M.previous_textobject(node, query_string, true, bufnr), bufnr, "yes, set cursor!")
+ end
+ end
+end
+
+function M.swap_textobject_next(query_string)
+ swap_textobject(query_string, 1)
+end
+
+function M.swap_textobject_previous(query_string)
+ swap_textobject(query_string, -1)
+end
+
+function M.next_textobject(node, query_string, same_parent, bufnr)
+ local bufnr = bufnr or api.nvim_get_current_buf()
+
+ local matches = queries.get_capture_matches(bufnr, query_string, 'textobjects')
+ local _, _ , node_end = node:end_()
+ local next_node
+ local next_node_start
+
+ for _, m in pairs(matches) do
+ local _, _, other_end = m.node:start()
+ if other_end > node_end then
+ if not same_parent or node:parent() == m.node:parent() then
+ if not next_node then
+ next_node = m
+ _, _, next_node_start = next_node.node:start()
+ end
+ if other_end < next_node_start then
+ next_node = m
+ _, _, next_node_start = next_node.node:start()
+ end
+ end
end
end
+
+ return next_node and next_node.node
+end
+
+function M.previous_textobject(node, query_string, same_parent, bufnr)
+ local bufnr = bufnr or api.nvim_get_current_buf()
+
+ local matches = queries.get_capture_matches(bufnr, query_string, 'textobjects')
+ local _, _ , node_start = node:start()
+ local previous_node
+ local previous_node_end
+
+ for _, m in pairs(matches) do
+ local _, _, other_end = m.node:end_()
+ if other_end < node_start then
+ if not same_parent or node:parent() == m.node:parent() then
+ if not previous_node then
+ previous_node = m
+ _, _, previous_node_end = previous_node.node:end_()
+ end
+ if other_end > previous_node_end then
+ previous_node = m
+ _, _, previous_node_end = previous_node.node:end_()
+ end
+ end
+ end
+ end
+
+ return previous_node and previous_node.node
end
function M.attach(bufnr, lang)
@@ -90,6 +172,28 @@ function M.attach(bufnr, lang)
api.nvim_buf_set_keymap(buf, "v", mapping, cmd, {silent = true, noremap = true })
end
end
+ for mapping, query in pairs(config.swap_next_keymaps) do
+ if type(query) == 'table' then
+ query = query[lang]
+ elseif not queries.get_query(lang, 'textobjects') then
+ query = nil
+ end
+ if query then
+ local cmd = ":lua require'nvim-treesitter.textobjects'.swap_textobject_next('"..query.."')<CR>"
+ api.nvim_buf_set_keymap(buf, "n", mapping, cmd, {silent = true})
+ end
+ end
+ for mapping, query in pairs(config.swap_previous_keymaps) do
+ if type(query) == 'table' then
+ query = query[lang]
+ elseif not queries.get_query(lang, 'textobjects') then
+ query = nil
+ end
+ if query then
+ local cmd = ":lua require'nvim-treesitter.textobjects'.swap_textobject_previous('"..query.."')<CR>"
+ api.nvim_buf_set_keymap(buf, "n", mapping, cmd, {silent = true})
+ end
+ end
end
function M.detach(bufnr)
@@ -108,6 +212,16 @@ function M.detach(bufnr)
api.nvim_buf_del_keymap(buf, "v", mapping)
end
end
+ for mapping, query in pairs(config.swap_next_keymaps) or pairs(config.swap_previous_keymaps) do
+ if type(query) == 'table' then
+ query = query[lang]
+ elseif not queries.get_query(lang, 'textobjects') then
+ query = nil
+ end
+ if query then
+ api.nvim_buf_del_keymap(buf, "n", mapping)
+ end
+ end
end
return M