Skip to content

Commit

Permalink
refactor(repeatable_move): extract the MoveFunction interface (#671)
Browse files Browse the repository at this point in the history
* refactor(move): extract parameters from opts fields

refactor(move): drop the unused field

* refactor(move): convert ternary to if-else for type assertion

refactor(swap): more consistent parameter naming

* refactor(repeatable_move): extract the `MoveFunction` interface

docs(repeatable_move): annotate function parameters
  • Loading branch information
ofseed authored and clason committed Aug 30, 2024
1 parent bd713b2 commit 35ef33c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 37 deletions.
48 changes: 21 additions & 27 deletions lua/nvim-treesitter-textobjects/move.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@ end
local M = {}

---@param opts TSTextObjects.MoveOpts
local function move(opts)
local query_group = opts.query_group or "textobjects"
local query_strings = type(opts.query_strings) == "string" and { opts.query_strings } or opts.query_strings
---@param query_strings string[]|string
---@param query_group? string
local function move(opts, query_strings, query_group)
query_group = query_group or "textobjects"
if type(query_strings) == "string" then
query_strings = { query_strings }
end

local winid = opts.winid or api.nvim_get_current_win()
local winid = api.nvim_get_current_win()
local bufnr = api.nvim_win_get_buf(winid)

local forward = opts.forward
Expand Down Expand Up @@ -132,67 +136,57 @@ local function move(opts)
end
end

---@type fun(opts: TSTextObjects.MoveOpts)
---@type fun(opts: TSTextObjects.MoveOpts, query_strings: string[]|string, query_group?: string)
local move_repeatable = repeatable_move.make_repeatable_move(move)

---@param query_strings string|string[]
---@param query_group? string
M.goto_next_start = function(query_strings, query_group)
move_repeatable {
move_repeatable({
forward = true,
start = true,
query_strings = query_strings,
query_group = query_group,
}
}, query_strings, query_group)
end
---@param query_strings string|string[]
---@param query_group? string
M.goto_next_end = function(query_strings, query_group)
move_repeatable {
move_repeatable({
forward = true,
start = false,
query_strings = query_strings,
query_group = query_group,
}
}, query_strings, query_group)
end
---@param query_strings string|string[]
---@param query_group? string
M.goto_previous_start = function(query_strings, query_group)
move_repeatable {
move_repeatable({
forward = false,
start = true,
query_strings = query_strings,
query_group = query_group,
}
}, query_strings, query_group)
end
---@param query_strings string|string[]
---@param query_group? string
M.goto_previous_end = function(query_strings, query_group)
move_repeatable {
move_repeatable({
forward = false,
start = false,
query_strings = query_strings,
query_group = query_group,
}
}, query_strings, query_group)
end

---@param query_strings string|string[]
---@param query_group? string
M.goto_next = function(query_strings, query_group)
move_repeatable {
move_repeatable({
forward = true,
query_strings = query_strings,
query_group = query_group,
}
}, query_strings, query_group)
end
---@param query_strings string|string[]
---@param query_group? string
M.goto_previous = function(query_strings, query_group)
move_repeatable {
move_repeatable({
forward = false,
query_strings = query_strings,
query_group = query_group,
}
}, query_strings, query_group)
end

return M
13 changes: 6 additions & 7 deletions lua/nvim-treesitter-textobjects/repeatable_move.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
local M = {}

---@class TSTextObjects.MoveOpts
---@field query_strings? string[]|string
---@field query_group? string
---@field forward boolean
---@field forward boolean If true, move forward, and false is for backward.
---@field start? boolean If true, choose the start of the node, and false is for the end.
---@field winid? integer

---@alias TSTextObjects.MoveFunction fun(opts: TSTextObjects.MoveOpts, ...: any)

---@class TSTextObjects.RepeatableMove
---@field func string | function
---@field func string | TSTextObjects.MoveFunction
---@field opts TSTextObjects.MoveOpts
---@field additional_args table

Expand All @@ -18,8 +17,8 @@ M.last_move = nil
--- Make move function repeatable. Creates a wrapper that takes a TSTextObjects.MoveOpts table,
--- stores them, and executes the move.
---
---@param move_fn function
---@return fun(opts: TSTextObjects.MoveOpts, ...: any)
---@param move_fn TSTextObjects.MoveFunction
---@return TSTextObjects.MoveFunction
M.make_repeatable_move = function(move_fn)
return function(opts, ...)
M.last_move = { func = move_fn, opts = vim.deepcopy(opts), additional_args = { ... } }
Expand Down
8 changes: 5 additions & 3 deletions lua/nvim-treesitter-textobjects/swap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,13 @@ end

local M = {}

---@param captures string|string[]
---@param query_strings string|string[]
---@param query_group? string
---@param direction integer
local function swap_textobject(captures, query_group, direction)
local query_strings = type(captures) == "string" and { captures } or captures
local function swap_textobject(query_strings, query_group, direction)
if type(query_strings) == "string" then
query_strings = { query_strings }
end
query_group = query_group or "textobjects"
local bufnr = vim.api.nvim_get_current_buf()

Expand Down

0 comments on commit 35ef33c

Please sign in to comment.