Skip to content

Commit 8d74e05

Browse files
committed
added swaparound feature
1 parent dbe096c commit 8d74e05

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

lua/treewalker/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
local movement = require('treewalker.movement')
1+
\local movement = require('treewalker.movement')
22
local swap = require('treewalker.swap')
33
local options = require('treewalker.options')
44

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)