From 5fddd7b5c3353167ecb755b8304d1565109e2f39 Mon Sep 17 00:00:00 2001 From: Yi Ming Date: Wed, 21 Aug 2024 13:57:06 +0800 Subject: [PATCH] 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 --- lua/nvim-treesitter-textobjects/move.lua | 48 ++++++++----------- .../repeatable_move.lua | 13 +++-- lua/nvim-treesitter-textobjects/swap.lua | 8 ++-- 3 files changed, 32 insertions(+), 37 deletions(-) diff --git a/lua/nvim-treesitter-textobjects/move.lua b/lua/nvim-treesitter-textobjects/move.lua index ff1d4789..fd991d00 100644 --- a/lua/nvim-treesitter-textobjects/move.lua +++ b/lua/nvim-treesitter-textobjects/move.lua @@ -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 @@ -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 diff --git a/lua/nvim-treesitter-textobjects/repeatable_move.lua b/lua/nvim-treesitter-textobjects/repeatable_move.lua index 14e3aa21..f4aa09b3 100644 --- a/lua/nvim-treesitter-textobjects/repeatable_move.lua +++ b/lua/nvim-treesitter-textobjects/repeatable_move.lua @@ -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 @@ -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 = { ... } } diff --git a/lua/nvim-treesitter-textobjects/swap.lua b/lua/nvim-treesitter-textobjects/swap.lua index ac540f38..9a0c3540 100644 --- a/lua/nvim-treesitter-textobjects/swap.lua +++ b/lua/nvim-treesitter-textobjects/swap.lua @@ -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()