Skip to content

Commit

Permalink
fix: vendor tbl_flatten
Browse files Browse the repository at this point in the history
  • Loading branch information
lewis6991 committed May 17, 2024
1 parent 6d07641 commit 865424e
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion lua/treesitter-context/context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,30 @@ local function iter_context_parents(bufnr, line_range)
end
end

--- Creates a copy of a list-like table such that any nested tables are
--- "unrolled" and appended to the result.
---
---@see From https://github.com/premake/premake-core/blob/master/src/base/table.lua
---
---@param t table List-like table
---@return table Flattened copy of the given list-like table
local function tbl_flatten(t)
local result = {}
--- @param _t table<any,any>
local function _tbl_flatten(_t)
for i = 1, #_t do
local v = _t[i]
if type(v) == 'table' then
_tbl_flatten(v)
elseif v then
table.insert(result, v)
end
end
end
_tbl_flatten(t)
return result
end

--- @param bufnr integer
--- @param winid integer
--- @return Range4[]?, string[]?
Expand Down Expand Up @@ -301,7 +325,7 @@ function M.get(bufnr, winid)
trim_contexts(context_ranges, context_lines, trim, config.trim_scope == 'outer')
end

return context_ranges, vim.tbl_flatten(context_lines)
return context_ranges, tbl_flatten(context_lines)
end

return M

0 comments on commit 865424e

Please sign in to comment.