feat(vim.version): add __eq to vim.VersionRange #38881

Problem: vim.VersionRange had no __eq metamethod, so comparing 2 distinct
but same value instances always returned false. In vim.pack.add this caused
redundant lockfile rewrites, even when the resulting lockfile content was
unchanged.

Solution: Add __eq metamethod on vim.VersionRange
This commit is contained in:
ngicks
2026-04-09 02:33:00 +09:00
committed by Justin M. Keyes
parent df726644b8
commit 78234f2d54
3 changed files with 19 additions and 0 deletions

View File

@@ -65,6 +65,7 @@ LSP
LUA
• Renamed `vim.diff` to `vim.text.diff`.
• Added `__eq` metamethod to |vim.VersionRange|.
OPTIONS
@@ -301,6 +302,9 @@ LUA
• EXPERIMENTAL: |vim.pos|, |vim.range| provide Position/Range abstraction.
• |vim.filetype.inspect()| returns a copy of the internal tables used for
filetype detection.
• Added `__eq` metamethod to |vim.VersionRange|. 2 distinct but representing
the same range instances now compare equal.
OPTIONS

View File

@@ -273,6 +273,9 @@ end
local range_mt = {
__index = VersionRange,
__eq = function(self, other)
return self.to == other.to and self.from == other.from
end,
__tostring = function(self)
if not self.to then
return '>=' .. tostring(self.from)

View File

@@ -118,6 +118,18 @@ describe('version', function()
end)
end
it('__eq', function()
local range1 = vim.version.range('1.2.3 - 2.3.4')
local range2 = vim.version.range('1.2.3 - 2.3.4')
local range3 = vim.version.range('<=1.2.3')
local range4 = vim.version.range('1.2.3')
assert(range1 == range1)
assert(range1 == range2)
assert(range1 ~= range3)
assert(range1 ~= range4)
assert(range3 ~= range4)
end)
it('handles prerelease', function()
assert(not vim.version.range('1.2.3'):has('1.2.3-alpha'))
assert(vim.version.range('1.2.3-alpha'):has('1.2.3-alpha'))