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'))