Skip to content

Commit 35ef33c

Browse files
ofseedclason
authored andcommitted
refactor(repeatable_move): extract the MoveFunction interface (#671)
* 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
1 parent bd713b2 commit 35ef33c

File tree

3 files changed

+32
-37
lines changed

3 files changed

+32
-37
lines changed

lua/nvim-treesitter-textobjects/move.lua

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,15 @@ end
3636
local M = {}
3737

3838
---@param opts TSTextObjects.MoveOpts
39-
local function move(opts)
40-
local query_group = opts.query_group or "textobjects"
41-
local query_strings = type(opts.query_strings) == "string" and { opts.query_strings } or opts.query_strings
39+
---@param query_strings string[]|string
40+
---@param query_group? string
41+
local function move(opts, query_strings, query_group)
42+
query_group = query_group or "textobjects"
43+
if type(query_strings) == "string" then
44+
query_strings = { query_strings }
45+
end
4246

43-
local winid = opts.winid or api.nvim_get_current_win()
47+
local winid = api.nvim_get_current_win()
4448
local bufnr = api.nvim_win_get_buf(winid)
4549

4650
local forward = opts.forward
@@ -132,67 +136,57 @@ local function move(opts)
132136
end
133137
end
134138

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

138142
---@param query_strings string|string[]
139143
---@param query_group? string
140144
M.goto_next_start = function(query_strings, query_group)
141-
move_repeatable {
145+
move_repeatable({
142146
forward = true,
143147
start = true,
144-
query_strings = query_strings,
145-
query_group = query_group,
146-
}
148+
}, query_strings, query_group)
147149
end
148150
---@param query_strings string|string[]
149151
---@param query_group? string
150152
M.goto_next_end = function(query_strings, query_group)
151-
move_repeatable {
153+
move_repeatable({
152154
forward = true,
153155
start = false,
154-
query_strings = query_strings,
155-
query_group = query_group,
156-
}
156+
}, query_strings, query_group)
157157
end
158158
---@param query_strings string|string[]
159159
---@param query_group? string
160160
M.goto_previous_start = function(query_strings, query_group)
161-
move_repeatable {
161+
move_repeatable({
162162
forward = false,
163163
start = true,
164-
query_strings = query_strings,
165-
query_group = query_group,
166-
}
164+
}, query_strings, query_group)
167165
end
168166
---@param query_strings string|string[]
169167
---@param query_group? string
170168
M.goto_previous_end = function(query_strings, query_group)
171-
move_repeatable {
169+
move_repeatable({
172170
forward = false,
173171
start = false,
174-
query_strings = query_strings,
175-
query_group = query_group,
176-
}
172+
}, query_strings, query_group)
177173
end
178174

179175
---@param query_strings string|string[]
180176
---@param query_group? string
181177
M.goto_next = function(query_strings, query_group)
182-
move_repeatable {
178+
move_repeatable({
183179
forward = true,
184-
query_strings = query_strings,
185-
query_group = query_group,
186-
}
180+
}, query_strings, query_group)
187181
end
188182
---@param query_strings string|string[]
189183
---@param query_group? string
190184
M.goto_previous = function(query_strings, query_group)
191-
move_repeatable {
185+
move_repeatable({
192186
forward = false,
193187
query_strings = query_strings,
194188
query_group = query_group,
195-
}
189+
}, query_strings, query_group)
196190
end
197191

198192
return M

lua/nvim-treesitter-textobjects/repeatable_move.lua

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
local M = {}
22

33
---@class TSTextObjects.MoveOpts
4-
---@field query_strings? string[]|string
5-
---@field query_group? string
6-
---@field forward boolean
4+
---@field forward boolean If true, move forward, and false is for backward.
75
---@field start? boolean If true, choose the start of the node, and false is for the end.
8-
---@field winid? integer
6+
7+
---@alias TSTextObjects.MoveFunction fun(opts: TSTextObjects.MoveOpts, ...: any)
98

109
---@class TSTextObjects.RepeatableMove
11-
---@field func string | function
10+
---@field func string | TSTextObjects.MoveFunction
1211
---@field opts TSTextObjects.MoveOpts
1312
---@field additional_args table
1413

@@ -18,8 +17,8 @@ M.last_move = nil
1817
--- Make move function repeatable. Creates a wrapper that takes a TSTextObjects.MoveOpts table,
1918
--- stores them, and executes the move.
2019
---
21-
---@param move_fn function
22-
---@return fun(opts: TSTextObjects.MoveOpts, ...: any)
20+
---@param move_fn TSTextObjects.MoveFunction
21+
---@return TSTextObjects.MoveFunction
2322
M.make_repeatable_move = function(move_fn)
2423
return function(opts, ...)
2524
M.last_move = { func = move_fn, opts = vim.deepcopy(opts), additional_args = { ... } }

lua/nvim-treesitter-textobjects/swap.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,13 @@ end
154154

155155
local M = {}
156156

157-
---@param captures string|string[]
157+
---@param query_strings string|string[]
158158
---@param query_group? string
159159
---@param direction integer
160-
local function swap_textobject(captures, query_group, direction)
161-
local query_strings = type(captures) == "string" and { captures } or captures
160+
local function swap_textobject(query_strings, query_group, direction)
161+
if type(query_strings) == "string" then
162+
query_strings = { query_strings }
163+
end
162164
query_group = query_group or "textobjects"
163165
local bufnr = vim.api.nvim_get_current_buf()
164166

0 commit comments

Comments
 (0)