commit 649ff924d990003e9d9338d964a206e7c7a5c144
parent aeb8bca12ddfe741569df622ef0cbc90211a6064
Author: Evgeni Chasnovski <evgeni.chasnovski@gmail.com>
Date: Mon, 30 Jun 2025 16:08:37 +0300
fix(vim.version): improve construction of '<=a.b.c' and '>a.b.c' ranges
Problem: `vim.version.range('<=a.b.c')` is not precise when it comes to
its right hand side. This is due to version ranges using exclusive right
hand side. While `vim.version.range('>a.b.c')` is not precise when it
comes to its left hand side because left hand sides are inclusive.
Solution: For '>=a.b.c' increase `to` from 'a.b.c' to the smallest
reasonable version that is bigger than 'a.b.c'. For '<a.b.c' do the same
for `from`.
More proper solution is an explicit control over inclusivity of version
range sides, but it has more side effects and requires design decisions.
Diffstat:
3 files changed, 33 insertions(+), 6 deletions(-)
diff --git a/runtime/doc/lua.txt b/runtime/doc/lua.txt
@@ -3902,6 +3902,7 @@ versions (1.2.3-rc1) are not matched. >
>1.2.3 greater than 1.2.3
<1.2.3 before 1.2.3
>=1.2.3 at least 1.2.3
+ <=1.2.3 at most 1.2.3
~1.2.3 is >=1.2.3 <1.3.0 "reasonably close to 1.2.3"
^1.2.3 is >=1.2.3 <2.0.0 "compatible with 1.2.3"
^0.2.3 is >=0.2.3 <0.3.0 (0.x.x is special)
@@ -3916,7 +3917,7 @@ versions (1.2.3-rc1) are not matched. >
* any version
x same
- 1.2.3 - 2.3.4 is >=1.2.3 <=2.3.4
+ 1.2.3 - 2.3.4 is >=1.2.3 <2.3.4
Partial right: missing pieces treated as x (2.3 => 2.3.x).
1.2.3 - 2.3 is >=1.2.3 <2.4.0
diff --git a/runtime/lua/vim/version.lua b/runtime/lua/vim/version.lua
@@ -28,6 +28,7 @@
--- >1.2.3 greater than 1.2.3
--- <1.2.3 before 1.2.3
--- >=1.2.3 at least 1.2.3
+--- <=1.2.3 at most 1.2.3
--- ~1.2.3 is >=1.2.3 <1.3.0 "reasonably close to 1.2.3"
--- ^1.2.3 is >=1.2.3 <2.0.0 "compatible with 1.2.3"
--- ^0.2.3 is >=0.2.3 <0.3.0 (0.x.x is special)
@@ -42,7 +43,7 @@
--- * any version
--- x same
---
---- 1.2.3 - 2.3.4 is >=1.2.3 <=2.3.4
+--- 1.2.3 - 2.3.4 is >=1.2.3 <2.3.4
---
--- Partial right: missing pieces treated as x (2.3 => 2.3.x).
--- 1.2.3 - 2.3 is >=1.2.3 <2.4.0
@@ -315,9 +316,23 @@ function M.range(spec) -- Adapted from https://github.com/folke/lazy.nvim
from = M._version({})
elseif mods == '<=' then
from = M._version({})
- to.patch = to.patch + 1
+ -- HACK: construct the smallest reasonable version bigger than `to`
+ -- to simulate `<=` while using exclusive right hand side
+ if to.prerelease then
+ to.prerelease = to.prerelease .. '.0'
+ else
+ to.patch = to.patch + 1
+ to.prerelease = '0'
+ end
elseif mods == '>' then
- from.patch = from.patch + 1
+ -- HACK: construct the smallest reasonable version bigger than `from`
+ -- to simulate `>` while using inclusive left hand side
+ if from.prerelease then
+ from.prerelease = from.prerelease .. '.0'
+ else
+ from.patch = from.patch + 1
+ from.prerelease = '0'
+ end
to = nil
elseif mods == '>=' then
to = nil
diff --git a/test/functional/lua/version_spec.lua b/test/functional/lua/version_spec.lua
@@ -58,10 +58,10 @@ describe('version', function()
['1.2.3'] = { from = { 1, 2, 3 }, to = { 1, 2, 3 } },
['1.2'] = { from = { 1, 2, 0 }, to = { 1, 3, 0 } },
['=1.2.3'] = { from = { 1, 2, 3 }, to = { 1, 2, 3 } },
- ['>1.2.3'] = { from = { 1, 2, 4 } },
+ ['>1.2.3'] = { from = '1.2.4-0' },
['>=1.2.3'] = { from = { 1, 2, 3 } },
['<1.2.3'] = { from = { 0, 0, 0 }, to = { 1, 2, 3 } },
- ['<=1.2.3'] = { from = { 0, 0, 0 }, to = { 1, 2, 4 } },
+ ['<=1.2.3'] = { from = { 0, 0, 0 }, to = '1.2.4-0' },
['~1.2.3'] = { from = { 1, 2, 3 }, to = { 1, 3, 0 } },
['^1.2.3'] = { from = { 1, 2, 3 }, to = { 2, 0, 0 } },
['^0.2.3'] = { from = { 0, 2, 3 }, to = { 0, 3, 0 } },
@@ -125,6 +125,17 @@ describe('version', function()
assert(vim.version.range('>0.10'):has('0.12.0-dev'))
assert(not vim.version.range('>=0.12'):has('0.12.0-dev'))
+ assert(not vim.version.range('<=1.2.3'):has('1.2.4-alpha'))
+ assert(not vim.version.range('<=1.2.3-0'):has('1.2.3'))
+ assert(not vim.version.range('<=1.2.3-alpha'):has('1.2.3'))
+ assert(not vim.version.range('<=1.2.3-1'):has('1.2.4-0'))
+ assert(vim.version.range('<=1.2.3-0'):has('1.2.3-0'))
+ assert(vim.version.range('<=1.2.3-alpha'):has('1.2.3-alpha'))
+
+ assert(vim.version.range('>1.2.3'):has('1.2.4-0'))
+ assert(vim.version.range('>1.2.3'):has('1.2.4-alpha'))
+ assert(vim.version.range('>1.2.3-0'):has('1.2.3-1'))
+
local range_alpha = vim.version.range('1.2.3-alpha')
eq(vim.version.range(tostring(range_alpha)), range_alpha)
end)