Skip to content

Commit e6eeff7

Browse files
committed
Enhancement: allow for up/down swapping from beginning of indented lines
At the time of writing, it seems possible (although not certain) that #22 is from the lack of this feature. Now up/down swaps will work if you're on a line that can be up/down swapped, even if your cursor is at a column number less than where the code on the line starts.
1 parent ca740c5 commit e6eeff7

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

lua/treewalker/swap.lua

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ local function get_highest_string_node_pipe(node)
4747
end
4848

4949
function M.swap_down()
50+
vim.cmd("normal! ^")
5051
if not is_on_target_node() then return end
5152
if not is_supported_ft() then return end
5253

@@ -78,6 +79,7 @@ function M.swap_down()
7879
end
7980

8081
function M.swap_up()
82+
vim.cmd("normal! ^")
8183
if not is_on_target_node() then return end
8284
if not is_supported_ft() then return end
8385

tests/fixtures/typescript.ts

+12
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,15 @@ function sleep(ms: number): Promise<void> {
9898
return new Promise((resolve) => setTimeout(resolve, ms));
9999
}
100100

101+
// Courtesy of pkenway-figma in #22
102+
const x = 1
103+
const y = 2
104+
105+
function doThings(b: number ) {
106+
const i = 1
107+
const j = 2
108+
109+
for (let a = i;a < j;a++) {
110+
console.log(a)
111+
}
112+
}

tests/treewalker/swap_spec.lua

+22
Original file line numberDiff line numberDiff line change
@@ -464,3 +464,25 @@ describe("Swapping in a python file:", function()
464464
helpers.assert_cursor_at(135, 1, "|def other")
465465
end)
466466
end)
467+
468+
describe("Swapping in a typescript file:", function()
469+
before_each(function()
470+
load_fixture("/typescript.ts")
471+
end)
472+
473+
it("swaps up annotated functions of different length", function()
474+
vim.fn.cursor(106, 1) -- | const i = 1
475+
tw.swap_down()
476+
helpers.assert_cursor_at(107, 3)
477+
assert.same(' const j = 2', lines.get_line(106))
478+
assert.same(' const i = 1', lines.get_line(107))
479+
end)
480+
481+
it("swaps down annotated functions of different length", function()
482+
vim.fn.cursor(107, 1) -- | const i = 1
483+
tw.swap_up()
484+
helpers.assert_cursor_at(106, 3)
485+
assert.same(' const j = 2', lines.get_line(106))
486+
assert.same(' const i = 1', lines.get_line(107))
487+
end)
488+
end)

0 commit comments

Comments
 (0)