From 773075b2bce8f2193343c0efa73f02b1f370dc67 Mon Sep 17 00:00:00 2001 From: Evgeni Chasnovski Date: Mon, 30 Jun 2025 16:08:39 +0300 Subject: 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()`. --- runtime/lua/vim/version.lua | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'runtime/lua/vim/version.lua') diff --git a/runtime/lua/vim/version.lua b/runtime/lua/vim/version.lua index dee99a22cc..7c49e5d38f 100644 --- a/runtime/lua/vim/version.lua +++ b/runtime/lua/vim/version.lua @@ -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) -- cgit