feat(vim.version): add vim.version.intersect()

Problem: No way to compute intersection of two version ranges, which is
useful when computing version range that fits inside several reference
ranges.

Solution: Add `vim.version.intersect()`.
This commit is contained in:
Evgeni Chasnovski
2025-06-30 16:08:39 +03:00
parent 649ff924d9
commit 773075b2bc
4 changed files with 159 additions and 0 deletions

View File

@@ -361,6 +361,24 @@ function M.range(spec) -- Adapted from https://github.com/folke/lazy.nvim
end
end
--- Computes the common range shared by the given ranges.
---
--- @since 14
--- @param r1 vim.VersionRange First range to intersect.
--- @param r2 vim.VersionRange Second range to intersect.
--- @return vim.VersionRange? Maximal range that is present inside both `r1` and `r2`.
--- `nil` if such range does not exist.
function M.intersect(r1, r2)
assert(getmetatable(r1) == range_mt)
assert(getmetatable(r2) == range_mt)
local from = r1.from <= r2.from and r2.from or r1.from
local to = (r1.to == nil or (r2.to ~= nil and r2.to <= r1.to)) and r2.to or r1.to
if to == nil or from < to or (from == to and r1:has(from) and r2:has(from)) then
return setmetatable({ from = from, to = to }, VersionRange)
end
end
---@param v string|vim.Version
---@return string
local function create_err_msg(v)