Skip to content

Commit 3c4b77d

Browse files
committed
added swaparound feature
1 parent fe814c2 commit 3c4b77d

File tree

8 files changed

+67
-4
lines changed

8 files changed

+67
-4
lines changed

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

tests/treewalker/c_sharp_spec.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,12 @@ describe("In a C Sharp file", function()
4141
assert.same(first_block, lines.get_lines(30, 48))
4242
h.assert_cursor_at(7, 5)
4343
end)
44+
45+
it("swaparound behavior works", function()
46+
vim.fn.cursor(52, 31) -- (|node1, node2)
47+
tw.swap_left()
48+
assert.same(" public static int Add(int b, int a) => a + b;", lines.get_line(52))
49+
tw.swap_right()
50+
assert.same(" public static int Add(int a, int b) => a + b;", lines.get_line(52))
51+
end)
4452
end)

tests/treewalker/c_spec.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,12 @@ describe("In a c file:", function()
5252
assert.same(first_block, lines.get_lines(68, 72))
5353
assert.same(second_block, lines.get_lines(63, 66))
5454
end)
55+
56+
it("swaparound behavior works", function()
57+
vim.fn.cursor(11, 24) -- (|node1, node2)
58+
tw.swap_left()
59+
assert.same("Account* createAccount(float initialBalance, int accountNumber) {", lines.get_line(11))
60+
tw.swap_right()
61+
assert.same("Account* createAccount(int accountNumber, float initialBalance) {", lines.get_line(11))
62+
end)
5563
end)

tests/treewalker/haskell_spec.lua

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local load_fixture = require "tests.load_fixture"
22
local tw = require 'treewalker'
33
local h = require 'tests.treewalker.helpers'
4+
local lines = require 'treewalker.lines'
45

56
describe("In a haskell file: ", function()
67
before_each(function()
@@ -27,6 +28,12 @@ describe("In a haskell file: ", function()
2728
tw.move_out()
2829
h.assert_cursor_at(19, 1)
2930
end)
30-
end)
31-
3231

32+
it("swaparound behavior works", function()
33+
vim.fn.cursor(40, 15) -- (|node1, node2)
34+
tw.swap_left()
35+
assert.same(" printEvens [9, 3, 5, 7, 1]", lines.get_line(40))
36+
tw.swap_right()
37+
assert.same(" printEvens [1, 3, 5, 7, 9]", lines.get_line(40))
38+
end)
39+
end)

tests/treewalker/lua_spec.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,14 @@ describe("Swapping in a regular lua file:", function()
321321
apply_text_edits_stub:revert()
322322
end)
323323

324+
it("swaparound behavior works", function()
325+
vim.fn.cursor(38, 32) -- (|node1, node2)
326+
tw.swap_left()
327+
assert.same("local function have_same_range(node2, node1)", lines.get_line(38))
328+
tw.swap_right()
329+
assert.same("local function have_same_range(node1, node2)", lines.get_line(38))
330+
end)
331+
324332
-- Actually I don't think this is supposed to work. It's ambiguous what
325333
-- node we're on. We'd need to do the lowest coincident that is the highest string
326334
-- or something.

tests/treewalker/python_spec.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,13 @@ describe("In a python file: ", function()
7676
assert.same(bottom_before, lines.get_lines(118, 131))
7777
assert.same(top_before, lines.get_lines(135, 136))
7878
end)
79+
80+
it("swaparound feature works", function()
81+
vim.fn.cursor(14, 18) -- (|node1, node2)
82+
tw.swap_left()
83+
assert.same(" def __init__(name, self):", lines.get_line(14))
84+
tw.swap_right()
85+
assert.same(" def __init__(self, name):", lines.get_line(14))
86+
end)
7987
end)
8088

tests/treewalker/ruby_spec.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,13 @@ describe("In a ruby file: ", function()
3939
assert.same(first_block, lines.get_lines(15, 19))
4040
h.assert_cursor_at(16, 3)
4141
end)
42+
43+
it("swaparound feature works", function()
44+
vim.fn.cursor(40, 27) -- (|node1, node2)
45+
tw.swap_left()
46+
assert.same(" def process_transaction(callback = nil, amount)", lines.get_line(40))
47+
tw.swap_right()
48+
assert.same(" def process_transaction(amount, callback = nil)", lines.get_line(40))
49+
end)
4250
end)
4351

0 commit comments

Comments
 (0)