summaryrefslogtreecommitdiff
path: root/lua/nvim-treesitter/textobjects/swap.lua
diff options
context:
space:
mode:
authorStephan Seitz <stephan.seitz@fau.de>2020-08-16 14:50:54 +0200
committerStephan Seitz <stephan.seitz@fau.de>2020-08-17 17:46:40 +0200
commit52168114594d791a3ae6092ab2489758da7b3ae8 (patch)
tree1a1a6a2415e6b4dee44dd322fb89655900f4ba3e /lua/nvim-treesitter/textobjects/swap.lua
parent32271b26ef64433b2d9cc41392594db614449a4c (diff)
chore(textobjects): split up into submodules
Diffstat (limited to 'lua/nvim-treesitter/textobjects/swap.lua')
-rw-r--r--lua/nvim-treesitter/textobjects/swap.lua35
1 files changed, 35 insertions, 0 deletions
diff --git a/lua/nvim-treesitter/textobjects/swap.lua b/lua/nvim-treesitter/textobjects/swap.lua
new file mode 100644
index 00000000..bb681fdd
--- /dev/null
+++ b/lua/nvim-treesitter/textobjects/swap.lua
@@ -0,0 +1,35 @@
+local ts_utils = require'nvim-treesitter.ts_utils'
+local shared = require'nvim-treesitter.textobjects.shared'
+local attach = require'nvim-treesitter.textobjects.attach'
+
+local M = {}
+
+local function swap_textobject(query_string, direction)
+ local bufnr, textobject_range, node = shared.textobject_at_point(query_string)
+ if not node then return end
+
+ local step = direction > 0 and 1 or -1
+ local overlapping_range_ok = false
+ local same_parent = true
+ for _ = 1, math.abs(direction), step do
+ local forward = direction > 0
+ local adjacent = shared.get_adjacent(forward, node, query_string, same_parent, overlapping_range_ok, bufnr)
+ ts_utils.swap_nodes(textobject_range, adjacent, bufnr, "yes, set cursor!")
+ end
+end
+
+function M.swap_next(query_string)
+ swap_textobject(query_string, 1)
+end
+
+function M.swap_previous(query_string)
+ swap_textobject(query_string, -1)
+end
+
+local normal_mode_functions = {"swap_next",
+ "swap_previous"}
+
+M.attach = attach.make_attach(normal_mode_functions, "swap")
+M.deattach = attach.make_detach(normal_mode_functions, "swap")
+
+return M