Skip to content

Commit 370290a

Browse files
committed
added swaparound feature
1 parent dbe096c commit 370290a

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

lua/treewalker/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Treewalker.opts = {
1111
highlight_duration = 250,
1212
highlight_group = "CursorLine",
1313
jumplist = true,
14+
swaparound = true
1415
}
1516

1617
-- This does not need to be called for Treewalker to work. The defaults are preinitialized and aim to be sane.

lua/treewalker/nodes.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,22 @@ function M.next_sib(node)
176176
return node:next_named_sibling()
177177
end
178178

179+
---@param node TSNode
180+
---@param fn function
181+
---@return TSNode | nil
182+
function M.farthest_sibling(node, fn)
183+
if not node then return nil end
184+
185+
---@type TSNode | nil
186+
local iter = fn(node)
187+
188+
while iter do
189+
node = iter
190+
iter = fn(iter)
191+
end
192+
return node
193+
end
194+
179195
-- Convenience for give me back prev sibling of a potentially nil node
180196
---@param node TSNode | nil
181197
function M.prev_sib(node)

lua/treewalker/swap.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ function M.swap_right()
121121
current = strategies.get_highest_string_node(current) or current
122122
current = nodes.get_highest_coincident(current)
123123

124-
local target = nodes.next_sib(current)
124+
local target = nodes.next_sib(current) or nodes.farthest_sibling(current, nodes.prev_sib)
125125

126126
if not current or not target then return end
127127

@@ -158,7 +158,7 @@ function M.swap_left()
158158
current = strategies.get_highest_string_node(current) or current
159159
current = nodes.get_highest_coincident(current)
160160

161-
local target = nodes.prev_sib(current)
161+
local target = nodes.prev_sib(current) or nodes.farthest_sibling(current, nodes.next_sib)
162162

163163
if not current or not target then return end
164164

0 commit comments

Comments
 (0)