Skip to content

Commit

Permalink
refactor: add types to subset
Browse files Browse the repository at this point in the history
table.pack can't be used here because neovim functions will error if a `n` key is present.
  • Loading branch information
aarondill committed Jan 16, 2024
1 parent e5452ac commit 1c329f6
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lua/tabnine/utils.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local fn = vim.fn
local api = vim.api
local unpack = table.unpack or unpack

local M = {}

Expand Down Expand Up @@ -45,7 +46,15 @@ function M.remove_matching_prefix(str, prefix)
return str:sub(#prefix)
end

---@generic T
---@param tbl T[] | {n?: integer} The table to get a subset from
---@param from integer? defaults to 1
---@param to integer? defaults to tbl.n or #tbl.
---@return T[]
function M.subset(tbl, from, to)
to = to or tbl.n or #tbl -- support table.pack values if no end given
-- We can't use table.pack here because nvim_buf_set_extmark will error if non-integer keys are present
-- Ideally, implementations would ignore string keys in an 'array'.
return { unpack(tbl, from, to) }
end

Expand Down

0 comments on commit 1c329f6

Please sign in to comment.