Skip to content

Commit

Permalink
refactor: improve naming + add luadocs
Browse files Browse the repository at this point in the history
  • Loading branch information
cbochs committed Mar 27, 2024
1 parent b460174 commit 3adcb2a
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lua/grapple.lua
Original file line number Diff line number Diff line change
Expand Up @@ -196,21 +196,26 @@ function Grapple.quickfix(opts)
end
end

---@param current_index? integer
---@param direction "next" | "prev"
---@param length integer
---@return integer
local function next_index(current_index, direction, length)
-- Fancy maths to get the next index for a given direction
-- 1. Change to 0-based indexing
-- 2. Perform index % container length, being careful of negative values
-- 3. Change back to 1-based indexing
-- stylua: ignore
local index = (
current_index = (
current_index
or direction == "next" and length
or direction == "prev" and 1
) - 1
local next_direction = direction == "next" and 1 or -1
local next_index = math.fmod(index + next_direction + length, length) + 1

return next_index
local next_inc = direction == "next" and 1 or -1
local next_idx = math.fmod(current_index + next_inc + length, length) + 1

return next_idx
end

---Select the next available tag for a given scope
Expand Down

0 comments on commit 3adcb2a

Please sign in to comment.