Skip to content

Commit

Permalink
Fix swap up cursor placement bug
Browse files Browse the repository at this point in the history
In certain circumstances, which now have automated tests against,
   the cursor would not go into the right place when swapping up. Now
   it does, woo!
  • Loading branch information
aaronik committed Jan 16, 2025
1 parent 2964614 commit 18ccff5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
15 changes: 8 additions & 7 deletions lua/treewalker/swap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local operations = require "treewalker.operations"
local targets = require "treewalker.targets"
local augment = require "treewalker.augment"
local strategies = require "treewalker.strategies"
local util = require "treewalker.util"

local M = {}

Expand Down Expand Up @@ -72,28 +73,28 @@ function M.swap_up()
local current_augments = augment.get_node_augments(current)
local current_all = { current, unpack(current_augments) }
local current_all_rows = nodes.whole_range(current_all)
local current_srow = nodes.get_srow(current)

local target_range = nodes.range(target)
local target_srow = nodes.get_srow(target)
local target_scol = nodes.get_scol(target)
local target_augments = augment.get_node_augments(target)
local target_all = { target, unpack(target_augments) }
local target_all_rows = nodes.whole_range(target_all)

local target_augment_rows = nodes.whole_range(target_augments)
local target_augment_srow = target_augment_rows[1]
local target_augment_scol = target_augment_rows[2]
local target_augment_length = #target_augments > 0 and (target_augment_scol + 1 - target_augment_srow) or 0
local target_augment_length = #target_augments == 0 and 0 or (target_srow - target_augment_srow - 1)

local current_augment_rows = nodes.whole_range(current_augments)
local current_augment_srow = current_augment_rows[1]
local current_augment_scol = current_augment_rows[2]
local current_augment_length = #current_augments > 0 and (current_augment_scol + 1 - current_augment_srow) or 0
local current_augment_length = #current_augments == 0 and 0 or (current_srow - current_augment_srow - 1)

-- Do the swap
operations.swap_rows(target_all_rows, current_all_rows)

-- Place cursor
local x = target_range[1] + 1 + current_augment_length - target_augment_length
local y = target_range[2] + 1
local x = target_srow + current_augment_length - target_augment_length
local y = target_scol
vim.fn.cursor(x, y)
end

Expand Down
3 changes: 1 addition & 2 deletions tests/treewalker/swap_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,7 @@ describe("Swapping in a python file:", function()
assert.same('def handler_top(', lines.get_line(143))
end)

-- TODO
pending("swaps up from a decorated/commented node to a bare one", function()
it("swaps up from a decorated/commented node to a bare one", function()
vim.fn.cursor(131, 1) -- |def handler_top
tw.swap_up()
assert.same('# C1', lines.get_line(118))
Expand Down

0 comments on commit 18ccff5

Please sign in to comment.