From a165eee64fa628045241b200d150b0f1e7737301 Mon Sep 17 00:00:00 2001 From: ngicks <94618820+ngicks@users.noreply.github.com> Date: Thu, 9 Apr 2026 02:33:00 +0900 Subject: [PATCH] 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 --- runtime/doc/news.txt | 5 ++++- runtime/lua/vim/version.lua | 3 +++ test/functional/lua/version_spec.lua | 12 ++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt index e08515540e..f53c24c056 100644 --- a/runtime/doc/news.txt +++ b/runtime/doc/news.txt @@ -37,7 +37,7 @@ LSP LUA -• todo +• Added `__eq` metamethod to |vim.VersionRange|. OPTIONS @@ -98,6 +98,9 @@ LUA • |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 diff --git a/runtime/lua/vim/version.lua b/runtime/lua/vim/version.lua index e0e81c6861..cb78ee6760 100644 --- a/runtime/lua/vim/version.lua +++ b/runtime/lua/vim/version.lua @@ -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) diff --git a/test/functional/lua/version_spec.lua b/test/functional/lua/version_spec.lua index 6b119bb013..6033a715ef 100644 --- a/test/functional/lua/version_spec.lua +++ b/test/functional/lua/version_spec.lua @@ -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'))