diff options
author | Gianmaria Bajo <mg1979.git@gmail.com> | 2023-06-06 15:38:45 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-06 06:38:45 -0700 |
commit | ca887b80a911df4db4ab5f5496075b9415b9990a (patch) | |
tree | 6dbffbfc49528a9980b78ef0cd787ed359acdbb2 | |
parent | f5d12889e80d3369359b8248806694cf6d21f820 (diff) | |
download | rneovim-ca887b80a911df4db4ab5f5496075b9415b9990a.tar.gz rneovim-ca887b80a911df4db4ab5f5496075b9415b9990a.tar.bz2 rneovim-ca887b80a911df4db4ab5f5496075b9415b9990a.zip |
fix: version-range < and <= #23539
vim.version.range() couldn't parse them correctly.
For example, vim.version.range('<0.9.0'):has('0.9.0') returned `true`.
fix: range:has() accepts vim.version()
So that it's possible to compare a range with:
vim.version.range(spec):has(vim.version())
-rw-r--r-- | runtime/doc/lua.txt | 10 | ||||
-rw-r--r-- | runtime/lua/vim/version.lua | 21 | ||||
-rw-r--r-- | test/functional/lua/version_spec.lua | 2 |
3 files changed, 26 insertions, 7 deletions
diff --git a/runtime/doc/lua.txt b/runtime/doc/lua.txt index 54527c5a50..38289dc5d0 100644 --- a/runtime/doc/lua.txt +++ b/runtime/doc/lua.txt @@ -2832,11 +2832,15 @@ range({spec}) *vim.version.range()* } < - `:has()` checks if a version is in the range (inclusive `from` , exclusive `to` ). Example: >lua + `:has()` checks if a version is in the range (inclusive `from`, exclusive + `to`). + + Example: >lua local r = vim.version.range('1.0.0 - 2.0.0') - print(r:has('1.9.9')) -- true - print(r:has('2.0.0')) -- false + print(r:has('1.9.9')) -- true + print(r:has('2.0.0')) -- false + print(r:has(vim.version())) -- check against current Nvim version < Or use cmp(), eq(), lt(), and gt() to compare `.to` and `.from` directly: >lua diff --git a/runtime/lua/vim/version.lua b/runtime/lua/vim/version.lua index ebe8f4e053..d4a3752e37 100644 --- a/runtime/lua/vim/version.lua +++ b/runtime/lua/vim/version.lua @@ -226,8 +226,13 @@ function Range:has(version) if type(version) == 'string' then ---@diagnostic disable-next-line: cast-local-type version = M.parse(version) + else + -- Need metatable to compare versions. + version = setmetatable(vim.deepcopy(version), Version) end if version then + -- Workaround: vim.version() reports "prerelease" as a boolean. + version.prerelease = version.prerelease or nil if version.prerelease ~= self.from.prerelease then return false end @@ -244,11 +249,14 @@ end --- } --- </pre> --- ---- `:has()` checks if a version is in the range (inclusive `from`, exclusive `to`). Example: +--- `:has()` checks if a version is in the range (inclusive `from`, exclusive `to`). +--- +--- Example: --- <pre>lua --- local r = vim.version.range('1.0.0 - 2.0.0') ---- print(r:has('1.9.9')) -- true ---- print(r:has('2.0.0')) -- false +--- print(r:has('1.9.9')) -- true +--- print(r:has('2.0.0')) -- false +--- print(r:has(vim.version())) -- check against current Nvim version --- </pre> --- --- Or use cmp(), eq(), lt(), and gt() to compare `.to` and `.from` directly: @@ -279,7 +287,7 @@ function M.range(spec) -- Adapted from https://github.com/folke/lazy.nvim }, { __index = Range }) end ---@type string, string - local mods, version = spec:lower():match('^([%^=>~]*)(.*)$') + local mods, version = spec:lower():match('^([%^=<>~]*)(.*)$') version = version:gsub('%.[%*x]', '') local parts = vim.split(version:gsub('%-.*', ''), '.', { plain = true }) if #parts < 3 and mods == '' then @@ -292,6 +300,11 @@ function M.range(spec) -- Adapted from https://github.com/folke/lazy.nvim local to = vim.deepcopy(semver) if mods == '' or mods == '=' then to.patch = to.patch + 1 + elseif mods == '<' then + from = M._version({}) + elseif mods == '<=' then + from = M._version({}) + to.patch = to.patch + 1 elseif mods == '>' then from.patch = from.patch + 1 to = nil ---@diagnostic disable-line: cast-local-type diff --git a/test/functional/lua/version_spec.lua b/test/functional/lua/version_spec.lua index 75b62e8318..64dcbec983 100644 --- a/test/functional/lua/version_spec.lua +++ b/test/functional/lua/version_spec.lua @@ -40,6 +40,8 @@ describe('version', function() ['=1.2.3'] = { from = { 1, 2, 3 }, to = { 1, 2, 4 } }, ['>1.2.3'] = { from = { 1, 2, 4 } }, ['>=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 = { 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 } }, |