Skip to content

Commit

Permalink
fix(repeatable_move)!: make d; work like the builtin behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
phanen committed Oct 18, 2024
1 parent aad2c8e commit 402ab3f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 32 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ local ts_repeat_move = require "nvim-treesitter-textobjects.repeatable_move"

-- Repeat movement with ; and ,
-- ensure ; goes forward and , goes backward regardless of the last direction
vim.keymap.set({ "n", "x", "o" }, ";", ts_repeat_move.repeat_last_move_next)
vim.keymap.set({ "n", "x", "o" }, ",", ts_repeat_move.repeat_last_move_previous)
vim.keymap.set({ "n", "x", "o" }, ";", ts_repeat_move.repeat_last_move_next, { expr = true })
vim.keymap.set({ "n", "x", "o" }, ",", ts_repeat_move.repeat_last_move_previous, { expr = true })

-- vim way: ; goes to the direction you were moving.
-- vim.keymap.set({ "n", "x", "o" }, ";", ts_repeat_move.repeat_last_move)
Expand Down
37 changes: 11 additions & 26 deletions lua/nvim-treesitter-textobjects/repeatable_move.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,19 @@ M.make_repeatable_move = function(move_fn)
end

---@param opts_extend TSTextObjects.MoveOpts?
---@return boolean
---@return string?
M.repeat_last_move = function(opts_extend)
if M.last_move then
local opts ---@type table
if opts_extend ~= nil then
opts = vim.tbl_deep_extend("force", {}, M.last_move.opts, opts_extend)
else
opts = M.last_move.opts
end

if M.last_move.func == "f" or M.last_move.func == "t" then
if opts.forward then
vim.cmd.normal { vim.v.count1 .. ";", bang = true }
else
vim.cmd.normal { vim.v.count1 .. ",", bang = true }
end
elseif M.last_move.func == "F" or M.last_move.func == "T" then
if opts.forward then
vim.cmd.normal { vim.v.count1 .. ",", bang = true }
else
vim.cmd.normal { vim.v.count1 .. ";", bang = true }
end
else
M.last_move.func(opts, unpack(M.last_move.additional_args))
end
return true
if not M.last_move then
return
end
local opts = vim.tbl_deep_extend("force", M.last_move.opts, opts_extend or {})
if M.last_move.func == "f" or M.last_move.func == "t" then
return opts.forward and ";" or ","
elseif M.last_move.func == "F" or M.last_move.func == "T" then
return opts.forward and "," or ";"
else
return M.last_move.func(opts, unpack(M.last_move.additional_args))
end
return false
end

M.repeat_last_move_opposite = function()
Expand Down
8 changes: 4 additions & 4 deletions scripts/minimal_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -394,14 +394,14 @@ end)

-- Repeat movement with ; and ,
-- ensure ; goes forward and , goes backward regardless of the last direction
-- vim.keymap.set({ "n", "x", "o" }, ";", ts_repeat_move.repeat_last_move_next)
-- vim.keymap.set({ "n", "x", "o" }, ",", ts_repeat_move.repeat_last_move_previous)
-- vim.keymap.set({ "n", "x", "o" }, ";", ts_repeat_move.repeat_last_move_next, { expr = true })
-- vim.keymap.set({ "n", "x", "o" }, ",", ts_repeat_move.repeat_last_move_previous, { expr = true })

local repeat_move = require "nvim-treesitter-textobjects.repeatable_move"

-- vim way: ; goes to the direction you were moving.
vim.keymap.set({ "n", "x", "o" }, ";", repeat_move.repeat_last_move)
vim.keymap.set({ "n", "x", "o" }, ",", repeat_move.repeat_last_move_opposite)
vim.keymap.set({ "n", "x", "o" }, ";", repeat_move.repeat_last_move, { expr = true })
vim.keymap.set({ "n", "x", "o" }, ",", repeat_move.repeat_last_move_opposite, { expr = true })

-- Optionally, make builtin f, F, t, T also repeatable with ; and ,
vim.keymap.set({ "n", "x", "o" }, "f", repeat_move.builtin_f_expr, { expr = true })
Expand Down
5 changes: 5 additions & 0 deletions tests/repeatable_move/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ function M.run_builtin_find_test(file, spec)
gt_cols[#gt_cols + 1] = vim.fn.col "."
vim.cmd([[normal! 2]] .. cmd .. spec.char)
gt_cols[#gt_cols + 1] = vim.fn.col "."
vim.cmd([[normal! ^d]] .. repeat_cmd)
gt_cols[#gt_cols + 1] = vim.fn.col "."

-- test using tstextobj repeatable_move.lua
vim.api.nvim_win_set_cursor(0, { spec.row, col })
Expand All @@ -57,6 +59,9 @@ function M.run_builtin_find_test(file, spec)
vim.cmd([[normal 2]] .. cmd .. spec.char)
assert.are.same(spec.row, vim.fn.line ".", "Command shouldn't move cursor over rows")
ts_cols[#ts_cols + 1] = vim.fn.col "."
vim.cmd([[normal ^d]] .. repeat_cmd)
assert.are.same(spec.row, vim.fn.line ".", "Command shouldn't move cursor over rows")
gt_cols[#gt_cols + 1] = vim.fn.col "."

assert.are.same(
gt_cols,
Expand Down

0 comments on commit 402ab3f

Please sign in to comment.