From 74ffebf8ec725a25c2ae1dde81cf26b83fc7ae61 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Mon, 6 Mar 2023 15:08:22 +0100 Subject: fix(vim.version): incorrect version.cmp() Problem: If majorminor, cmp_version_core returns 1 Solution: - Fix logic in cmp_version_core - Delete most eq()/gt()/lt() tests, they are redundant. --- runtime/lua/vim/version.lua | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'runtime/lua/vim') diff --git a/runtime/lua/vim/version.lua b/runtime/lua/vim/version.lua index ddbe228244..35629c461f 100644 --- a/runtime/lua/vim/version.lua +++ b/runtime/lua/vim/version.lua @@ -3,8 +3,11 @@ local M = {} ---@private ---@param version string ---@return string -local function create_err_msg(version) - return string.format('invalid version: "%s"', version) +local function create_err_msg(v) + if type(v) == 'string' then + return string.format('invalid version: "%s"', tostring(v)) + end + return string.format('invalid version: %s (%s)', tostring(v), type(v)) end ---@private @@ -20,9 +23,6 @@ end ---@private --- Compares the prerelease component of the two versions. ----@param v1 table Parsed version. ----@param v2 table Parsed version. ----@return integer `-1` if `v1 < v2`, `0` if `v1 == v2`, `1` if `v1 > v2`. local function cmp_prerelease(v1, v2) if v1.prerelease and not v2.prerelease then return -1 @@ -88,19 +88,17 @@ local function cmp_prerelease(v1, v2) end ---@private ---- Compares the version core component of the two versions. ----@param v1 table Parsed version. ----@param v2 table Parsed version. ----@return integer `-1` if `v1 < v2`, `0` if `v1 == v2`, `1` if `v1 > v2`. local function cmp_version_core(v1, v2) if v1.major == v2.major and v1.minor == v2.minor and v1.patch == v2.patch then return 0 end - - if v1.major > v2.major or v1.minor > v2.minor or v1.patch > v2.patch then + if + v1.major > v2.major + or (v1.major == v2.major and v1.minor > v2.minor) + or (v1.major == v2.major and v1.minor == v2.minor and v1.patch > v2.patch) + then return 1 end - return -1 end -- cgit