From ce584e951a4f13c534b2451c23c541af8f5fe5b5 Mon Sep 17 00:00:00 2001 From: Aaron Sullivan Date: Tue, 17 Dec 2024 20:11:29 -0800 Subject: [PATCH] Continuing iteration Currently it's looking at lines. It's not working fully, something wrong in the swap logic. But it's looking at lines. It might be nice to do this by nodes, so instead of highest_coincident starting on the same line, looking at the highest coincident on the same line and col. And swapping like that as well. --- lua/treewalker/swapping.lua | 42 +++++--- tests/treewalker/acceptance_spec.lua | 153 --------------------------- 2 files changed, 29 insertions(+), 166 deletions(-) delete mode 100644 tests/treewalker/acceptance_spec.lua diff --git a/lua/treewalker/swapping.lua b/lua/treewalker/swapping.lua index b5416d5..3480c00 100644 --- a/lua/treewalker/swapping.lua +++ b/lua/treewalker/swapping.lua @@ -26,20 +26,12 @@ function M.swap_in() end end ----@return nil -function M.swap_up() - local target, row, line = targets.up() - - if target and row and line then - --util.log("no up candidate") - ops.jump(row, target) - end -end - ----@return nil -function M.swap_down() +---@param target TSNode +---@param row integer +---@param line string +local function swap(target, row, line) local current = nodes.get_current() - local target, row, line = targets.down() + current = nodes.get_highest_coincident(current) if not target or not row or not line then --util.log("no down candidate") @@ -55,4 +47,28 @@ function M.swap_down() ) end +---@return nil +function M.swap_down() + local target, row, line = targets.down() + + if not target or not row or not line then + --util.log("no down candidate") + return + end + + swap(target, row, line) +end + +---@return nil +function M.swap_up() + local target, row, line = targets.up() + + if not target or not row or not line then + --util.log("no down candidate") + return + end + + swap(target, row, line) +end + return M diff --git a/tests/treewalker/acceptance_spec.lua b/tests/treewalker/acceptance_spec.lua deleted file mode 100644 index d357ac8..0000000 --- a/tests/treewalker/acceptance_spec.lua +++ /dev/null @@ -1,153 +0,0 @@ -local util = require "treewalker.util" -local load_fixture = require "tests.load_fixture" -local stub = require 'luassert.stub' -local assert = require "luassert" -local treewalker = require 'treewalker' -local ops = require 'treewalker.ops' -local lines = require 'treewalker.lines' - --- Assert the cursor is in the expected position ----@param line integer ----@param column integer ----@param msg string? -local function assert_cursor_at(line, column, msg) - local cursor_pos = vim.fn.getpos('.') - ---@type integer, integer - local current_line, current_column - current_line, current_column = cursor_pos[2], cursor_pos[3] - msg = string.format("expected to be at [%s] but wasn't", msg) - assert.are.same({ line, column }, { current_line, current_column }, msg) -end - -describe("Treewalker", function() - describe("regular lua file: ", function() - load_fixture("/lua.lua") - - it("moves up and down at the same pace", function() - vim.fn.cursor(1, 1) -- Reset cursor - treewalker.move_down() - assert_cursor_at(3, 1) - treewalker.move_down() - assert_cursor_at(5, 1) - treewalker.move_down() - assert_cursor_at(10, 1) - treewalker.move_down() - assert_cursor_at(21, 1) - treewalker.move_up() - assert_cursor_at(10, 1) - treewalker.move_up() - assert_cursor_at(5, 1) - treewalker.move_up() - assert_cursor_at(3, 1) - treewalker.move_up() - assert_cursor_at(1, 1) - end) - - it("doesn't consider empty lines to be outer scopes", function() - vim.fn.cursor(85, 1) - treewalker.move_down() - assert_cursor_at(88, 3, "local") - - vim.fn.cursor(85, 1) - treewalker.move_up() - assert_cursor_at(84, 3, "end") - end) - - it("goes into functions eagerly", function() - vim.fn.cursor(143, 1) -- In a bigger function - treewalker.move_in() - assert_cursor_at(144, 3) - treewalker.move_in() - assert_cursor_at(147, 5) - treewalker.move_in() - assert_cursor_at(149, 7) - end) - - it("doesn't jump into a comment", function() - vim.fn.cursor(177, 1) -- In a bigger function - treewalker.move_in() - assert_cursor_at(179, 3, "local") - end) - - it("goes out of functions", function() - vim.fn.cursor(149, 7) -- In a bigger function - treewalker.move_out() - assert_cursor_at(148, 5, "if") - treewalker.move_out() - assert_cursor_at(146, 3, "while") - treewalker.move_out() - assert_cursor_at(143, 1, "function") - end) - - it("respects highlight config option", function() - local highlight_stub = stub(ops, "highlight") - - treewalker.setup() - vim.fn.cursor(23, 5) - treewalker.move_out() - treewalker.move_down() - treewalker.move_up() - treewalker.move_in() - assert.equal(0, #highlight_stub.calls) - - treewalker.setup({ highlight = false }) - vim.fn.cursor(23, 5) - treewalker.move_out() - treewalker.move_down() - treewalker.move_up() - treewalker.move_in() - assert.equal(0, #highlight_stub.calls) - - treewalker.setup({ highlight = true }) - vim.fn.cursor(23, 5) - treewalker.move_out() - treewalker.move_down() - treewalker.move_up() - treewalker.move_in() - assert.equal(4, #highlight_stub.calls) - end) - - it("swaps down", function() - vim.fn.cursor(38, 1) - local lines1 = lines.get_lines(5, 8) - local lines2 = lines.get_lines(10, 17) - treewalker.swap_down() - assert.same(lines2, lines.get_lines(5, 12)) - assert.same(lines1, lines.get_lines(14, 17)) - end) - end) - - describe("lua spec file: ", function() - load_fixture("/lua-spec.lua") - - -- go to first describe - local function go_to_describe() - vim.fn.cursor(1, 1) - for _ = 1, 6 do - treewalker.move_down() - end - assert_cursor_at(17, 1, "describe") - end - - -- go to first load_buf - local function go_to_load_buf() - go_to_describe() - treewalker.move_in(); treewalker.move_in() - assert_cursor_at(19, 5, "load_buf") - end - - it("moves up and down at the same pace", function() - go_to_load_buf() - treewalker.move_down(); treewalker.move_down() - assert_cursor_at(41, 5, "it") - treewalker.move_up(); treewalker.move_up() - assert_cursor_at(19, 5, "load_buf") - end) - - it("down moves at least one line", function() - go_to_load_buf() - treewalker.move_down() - assert_cursor_at(21, 5, "it") - end) - end) -end)