diff options
| author | Evgeni Chasnovski <evgeni.chasnovski@gmail.com> | 2025-06-30 16:08:39 +0300 |
|---|---|---|
| committer | Evgeni Chasnovski <evgeni.chasnovski@gmail.com> | 2025-06-30 20:59:44 +0300 |
| commit | 773075b2bce8f2193343c0efa73f02b1f370dc67 (patch) | |
| tree | 935fb92f676ad64f7f31ccea3887316cb66a3250 /runtime/lua/vim/version.lua | |
| parent | 649ff924d990003e9d9338d964a206e7c7a5c144 (diff) | |
| download | rneovim-773075b2bce8f2193343c0efa73f02b1f370dc67.tar.gz rneovim-773075b2bce8f2193343c0efa73f02b1f370dc67.tar.bz2 rneovim-773075b2bce8f2193343c0efa73f02b1f370dc67.zip | |
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()`.
Diffstat (limited to 'runtime/lua/vim/version.lua')
| -rw-r--r-- | runtime/lua/vim/version.lua | 18 |
1 files changed, 18 insertions, 0 deletions
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) |