mirror of
https://github.com/neovim/neovim.git
synced 2025-12-08 15:42:52 +00:00
feat(vim.version): more coercion with strict=false
Problem: "tmux 3.2a" (output from "tmux -V") is not parsed easily. Solution: With `strict=false`, discard everything before the first digit. - rename Semver => Version - rename vim.version.version() => vim.version._version() - rename matches() => has() - remove `opts` from cmp()
This commit is contained in:
@@ -145,8 +145,8 @@ function! provider#clipboard#Executable() abort
|
|||||||
let s:paste['*'] = s:paste['+']
|
let s:paste['*'] = s:paste['+']
|
||||||
return 'termux-clipboard'
|
return 'termux-clipboard'
|
||||||
elseif !empty($TMUX) && executable('tmux')
|
elseif !empty($TMUX) && executable('tmux')
|
||||||
let ver = matchlist(systemlist(['tmux', '-V'])[0], '\vtmux %(next-)?(\d+)\.(\d+)')
|
let tmux_v = v:lua.vim.version.parse(system(['tmux', '-V']))
|
||||||
if len(ver) >= 3 && (ver[1] > 3 || (ver[1] == 3 && ver[2] >= 2))
|
if !empty(tmux_v) && !v:lua.vim.version.lt(tmux_v, [3,2,0])
|
||||||
let s:copy['+'] = ['tmux', 'load-buffer', '-w', '-']
|
let s:copy['+'] = ['tmux', 'load-buffer', '-w', '-']
|
||||||
else
|
else
|
||||||
let s:copy['+'] = ['tmux', 'load-buffer', '-']
|
let s:copy['+'] = ['tmux', 'load-buffer', '-']
|
||||||
|
|||||||
@@ -782,9 +782,6 @@ vim.api.{func}({...}) *vim.api*
|
|||||||
Example: call the "nvim_get_current_line()" API function: >lua
|
Example: call the "nvim_get_current_line()" API function: >lua
|
||||||
print(tostring(vim.api.nvim_get_current_line()))
|
print(tostring(vim.api.nvim_get_current_line()))
|
||||||
|
|
||||||
vim.version() *vim.version*
|
|
||||||
Gets the version of the current Nvim build.
|
|
||||||
|
|
||||||
vim.in_fast_event() *vim.in_fast_event()*
|
vim.in_fast_event() *vim.in_fast_event()*
|
||||||
Returns true if the code is executing as part of a "fast" event handler,
|
Returns true if the code is executing as part of a "fast" event handler,
|
||||||
where most of the API is disabled. These are low-level events (e.g.
|
where most of the API is disabled. These are low-level events (e.g.
|
||||||
@@ -1391,8 +1388,7 @@ deprecate({name}, {alternative}, {version}, {plugin}, {backtrace})
|
|||||||
Parameters: ~
|
Parameters: ~
|
||||||
• {name} string Deprecated feature (function, API, etc.).
|
• {name} string Deprecated feature (function, API, etc.).
|
||||||
• {alternative} (string|nil) Suggested alternative feature.
|
• {alternative} (string|nil) Suggested alternative feature.
|
||||||
• {version} string Version when the deprecated function will be
|
• {version} string Version when the deprecated function will be removed.
|
||||||
removed.
|
|
||||||
• {plugin} string|nil Name of the plugin that owns the deprecated
|
• {plugin} string|nil Name of the plugin that owns the deprecated
|
||||||
feature. Defaults to "Nvim".
|
feature. Defaults to "Nvim".
|
||||||
• {backtrace} boolean|nil Prints backtrace. Defaults to true.
|
• {backtrace} boolean|nil Prints backtrace. Defaults to true.
|
||||||
@@ -2533,67 +2529,179 @@ trust({opts}) *vim.secure.trust()*
|
|||||||
==============================================================================
|
==============================================================================
|
||||||
Lua module: version *lua-version*
|
Lua module: version *lua-version*
|
||||||
|
|
||||||
cmp({v1}, {v2}, {opts}) *vim.version.cmp()*
|
|
||||||
Compares two strings ( `v1` and `v2` ) in semver format.
|
The `vim.version` module provides functions for comparing versions and
|
||||||
|
ranges conforming to the
|
||||||
|
|
||||||
|
https://semver.org
|
||||||
|
|
||||||
|
spec. Plugins, and plugin managers, can use this to check available tools
|
||||||
|
and dependencies on the current system.
|
||||||
|
|
||||||
|
Example: >lua
|
||||||
|
|
||||||
|
local v = vim.version.parse(vim.fn.system({'tmux', '-V'}), {strict=false})
|
||||||
|
if vim.version.gt(v, {3, 2, 0}) then
|
||||||
|
-- ...
|
||||||
|
end
|
||||||
|
|
||||||
|
<
|
||||||
|
|
||||||
|
*vim.version()* returns the version of the current Nvim process.
|
||||||
|
|
||||||
|
VERSION RANGE SPEC *version-range*
|
||||||
|
|
||||||
|
A version "range spec" defines a semantic version range which can be
|
||||||
|
tested against a version, using |vim.version.range()|.
|
||||||
|
|
||||||
|
Supported range specs are shown in the following table. Note: suffixed
|
||||||
|
versions (1.2.3-rc1) are not matched. >
|
||||||
|
|
||||||
|
1.2.3 is 1.2.3
|
||||||
|
=1.2.3 is 1.2.3
|
||||||
|
>1.2.3 greater than 1.2.3
|
||||||
|
<1.2.3 before 1.2.3
|
||||||
|
>=1.2.3 at least 1.2.3
|
||||||
|
~1.2.3 is >=1.2.3 <1.3.0 "reasonably close to 1.2.3"
|
||||||
|
^1.2.3 is >=1.2.3 <2.0.0 "compatible with 1.2.3"
|
||||||
|
^0.2.3 is >=0.2.3 <0.3.0 (0.x.x is special)
|
||||||
|
^0.0.1 is =0.0.1 (0.0.x is special)
|
||||||
|
^1.2 is >=1.2.0 <2.0.0 (like ^1.2.0)
|
||||||
|
~1.2 is >=1.2.0 <1.3.0 (like ~1.2.0)
|
||||||
|
^1 is >=1.0.0 <2.0.0 "compatible with 1"
|
||||||
|
~1 same "reasonably close to 1"
|
||||||
|
1.x same
|
||||||
|
1.* same
|
||||||
|
1 same
|
||||||
|
* any version
|
||||||
|
x same
|
||||||
|
|
||||||
|
1.2.3 - 2.3.4 is >=1.2.3 <=2.3.4
|
||||||
|
|
||||||
|
Partial right: missing pieces treated as x (2.3 => 2.3.x).
|
||||||
|
1.2.3 - 2.3 is >=1.2.3 <2.4.0
|
||||||
|
1.2.3 - 2 is >=1.2.3 <3.0.0
|
||||||
|
|
||||||
|
Partial left: missing pieces treated as 0 (1.2 => 1.2.0).
|
||||||
|
1.2 - 2.3.0 is 1.2.0 - 2.3.0
|
||||||
|
|
||||||
|
<
|
||||||
|
|
||||||
|
cmp({v1}, {v2}) *vim.version.cmp()*
|
||||||
|
Parses and compares two version version objects (the result of
|
||||||
|
|vim.version.parse()|, or specified literally as a `{major, minor, patch}`
|
||||||
|
tuple, e.g. `{1, 0, 3}`).
|
||||||
|
|
||||||
|
Example: >lua
|
||||||
|
|
||||||
|
if vim.version.cmp({1,0,3}, {0,2,1}) == 0 then
|
||||||
|
-- ...
|
||||||
|
end
|
||||||
|
local v1 = vim.version.parse('1.0.3-pre')
|
||||||
|
local v2 = vim.version.parse('0.2.1')
|
||||||
|
if vim.version.cmp(v1, v2) == 0 then
|
||||||
|
-- ...
|
||||||
|
end
|
||||||
|
<
|
||||||
|
|
||||||
|
Note:
|
||||||
|
Per semver, build metadata is ignored when comparing two
|
||||||
|
otherwise-equivalent versions.
|
||||||
|
|
||||||
Parameters: ~
|
Parameters: ~
|
||||||
• {v1} (string) Version.
|
• {v1} Version|number[] Version object.
|
||||||
• {v2} (string) Version to compare with v1.
|
• {v2} Version|number[] Version to compare with `v1` .
|
||||||
• {opts} (table|nil) Optional keyword arguments:
|
|
||||||
• strict (boolean): see `semver.parse` for details. Defaults
|
|
||||||
to false.
|
|
||||||
|
|
||||||
Return: ~
|
Return: ~
|
||||||
(integer) `-1` if `v1 < v2`, `0` if `v1 == v2`, `1` if `v1 > v2`.
|
(integer) -1 if `v1 < v2`, 0 if `v1 == v2`, 1 if `v1 > v2`.
|
||||||
|
|
||||||
eq({v1}, {v2}) *vim.version.eq()*
|
eq({v1}, {v2}) *vim.version.eq()*
|
||||||
Returns `true` if `v1` are `v2` are equal versions.
|
Returns `true` if the given versions are equal.
|
||||||
|
|
||||||
Parameters: ~
|
Parameters: ~
|
||||||
• {v1} (string)
|
• {v1} Version|number[]
|
||||||
• {v2} (string)
|
• {v2} Version|number[]
|
||||||
|
|
||||||
Return: ~
|
Return: ~
|
||||||
(boolean)
|
(boolean)
|
||||||
|
|
||||||
gt({v1}, {v2}) *vim.version.gt()*
|
gt({v1}, {v2}) *vim.version.gt()*
|
||||||
Returns `true` if `v1` is greater than `v2` .
|
Returns `true` if `v1 > v2` .
|
||||||
|
|
||||||
Parameters: ~
|
Parameters: ~
|
||||||
• {v1} (string)
|
• {v1} Version|number[]
|
||||||
• {v2} (string)
|
• {v2} Version|number[]
|
||||||
|
|
||||||
Return: ~
|
Return: ~
|
||||||
(boolean)
|
(boolean)
|
||||||
|
|
||||||
lt({v1}, {v2}) *vim.version.lt()*
|
last({versions}) *vim.version.last()*
|
||||||
Returns `true` if `v1` is less than `v2` .
|
TODO: generalize this, move to func.lua
|
||||||
|
|
||||||
Parameters: ~
|
Parameters: ~
|
||||||
• {v1} (string)
|
• {versions} Version []
|
||||||
• {v2} (string)
|
|
||||||
|
Return: ~
|
||||||
|
Version ?|ni
|
||||||
|
|
||||||
|
lt({v1}, {v2}) *vim.version.lt()*
|
||||||
|
Returns `true` if `v1 < v2` .
|
||||||
|
|
||||||
|
Parameters: ~
|
||||||
|
• {v1} Version|number[]
|
||||||
|
• {v2} Version|number[]
|
||||||
|
|
||||||
Return: ~
|
Return: ~
|
||||||
(boolean)
|
(boolean)
|
||||||
|
|
||||||
parse({version}, {opts}) *vim.version.parse()*
|
parse({version}, {opts}) *vim.version.parse()*
|
||||||
Parses a semantic version string.
|
Parses a semantic version string and returns a version object which can be
|
||||||
|
used with other `vim.version` functions. For example "1.0.1-rc1+build.2" returns: >
|
||||||
Ignores leading "v" and surrounding whitespace, e.g. "
|
|
||||||
v1.0.1-rc1+build.2", "1.0.1-rc1+build.2", "v1.0.1-rc1+build.2" and
|
|
||||||
"v1.0.1-rc1+build.2 " are all parsed as: >
|
|
||||||
|
|
||||||
{ major = 1, minor = 0, patch = 1, prerelease = "rc1", build = "build.2" }
|
{ major = 1, minor = 0, patch = 1, prerelease = "rc1", build = "build.2" }
|
||||||
<
|
<
|
||||||
|
|
||||||
Parameters: ~
|
Parameters: ~
|
||||||
• {version} (string) Version string to be parsed.
|
• {version} (string) Version string to parse.
|
||||||
• {opts} (table|nil) Optional keyword arguments:
|
• {opts} (table|nil) Optional keyword arguments:
|
||||||
• strict (boolean): Default false. If `true` , no coercion is attempted on input not strictly
|
• strict (boolean): Default false. If `true`, no coercion
|
||||||
conforming to semver v2.0.0 ( https://semver.org/spec/v2.0.0.html ). E.g. `parse("v1.2")` returns nil.
|
is attempted on input not conforming to semver v2.0.0. If
|
||||||
|
`false`, `parse()` attempts to coerce input such as
|
||||||
|
"1.0", "0-x", "tmux 3.2a" into valid versions.
|
||||||
|
|
||||||
Return: ~
|
Return: ~
|
||||||
(table|nil) parsed_version Parsed version table or `nil` if `version`
|
(table|nil) parsed_version Version object or `nil` if input is invalid.
|
||||||
is invalid.
|
|
||||||
|
See also: ~
|
||||||
|
• # https://semver.org/spec/v2.0.0.html
|
||||||
|
|
||||||
|
range({spec}) *vim.version.range()*
|
||||||
|
Parses a semver |version-range| "spec" and returns a range object: >
|
||||||
|
|
||||||
|
{
|
||||||
|
from: Version
|
||||||
|
to: Version
|
||||||
|
has(v: string|Version)
|
||||||
|
}
|
||||||
|
<
|
||||||
|
|
||||||
|
`: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
|
||||||
|
<
|
||||||
|
|
||||||
|
Or use cmp(), eq(), lt(), and gt() to compare `.to` and `.from` directly: >lua
|
||||||
|
|
||||||
|
local r = vim.version.range('1.0.0 - 2.0.0')
|
||||||
|
print(vim.version.gt({1,0,3}, r.from) and vim.version.lt({1,0,3}, r.to))
|
||||||
|
<
|
||||||
|
|
||||||
|
Parameters: ~
|
||||||
|
• {spec} string Version range "spec"
|
||||||
|
|
||||||
|
See also: ~
|
||||||
|
• # https://github.com/npm/node-semver#ranges
|
||||||
|
|
||||||
vim:tw=78:ts=8:sw=4:sts=4:et:ft=help:norl:
|
vim:tw=78:ts=8:sw=4:sts=4:et:ft=help:norl:
|
||||||
|
|||||||
@@ -65,8 +65,8 @@ NEW FEATURES *news-features*
|
|||||||
|
|
||||||
The following new APIs or features were added.
|
The following new APIs or features were added.
|
||||||
|
|
||||||
• Added |vim.version| for parsing and comparing version strings conforming to
|
• Added |lua-version| for parsing and comparing version strings conforming to
|
||||||
the semver specification, see |lua-version|.
|
the semver specification.
|
||||||
|
|
||||||
• A new environment variable named NVIM_APPNAME enables configuring the
|
• A new environment variable named NVIM_APPNAME enables configuring the
|
||||||
directories where Neovim should find its configuration and state files. See
|
directories where Neovim should find its configuration and state files. See
|
||||||
|
|||||||
@@ -1,9 +1,59 @@
|
|||||||
|
--- @defgroup lua-version
|
||||||
|
---
|
||||||
|
--- @brief The \`vim.version\` module provides functions for comparing versions and ranges
|
||||||
|
--- conforming to the https://semver.org spec. Plugins, and plugin managers, can use this to check
|
||||||
|
--- available tools and dependencies on the current system.
|
||||||
|
---
|
||||||
|
--- Example:
|
||||||
|
--- <pre>lua
|
||||||
|
--- local v = vim.version.parse(vim.fn.system({'tmux', '-V'}), {strict=false})
|
||||||
|
--- if vim.version.gt(v, {3, 2, 0}) then
|
||||||
|
--- -- ...
|
||||||
|
--- end
|
||||||
|
--- </pre>
|
||||||
|
---
|
||||||
|
--- \*vim.version()\* returns the version of the current Nvim process.
|
||||||
|
---
|
||||||
|
--- VERSION RANGE SPEC \*version-range\*
|
||||||
|
---
|
||||||
|
--- A version "range spec" defines a semantic version range which can be tested against a version,
|
||||||
|
--- using |vim.version.range()|.
|
||||||
|
---
|
||||||
|
--- Supported range specs are shown in the following table.
|
||||||
|
--- Note: suffixed versions (1.2.3-rc1) are not matched.
|
||||||
|
--- <pre>
|
||||||
|
--- 1.2.3 is 1.2.3
|
||||||
|
--- =1.2.3 is 1.2.3
|
||||||
|
--- >1.2.3 greater than 1.2.3
|
||||||
|
--- <1.2.3 before 1.2.3
|
||||||
|
--- >=1.2.3 at least 1.2.3
|
||||||
|
--- ~1.2.3 is >=1.2.3 <1.3.0 "reasonably close to 1.2.3"
|
||||||
|
--- ^1.2.3 is >=1.2.3 <2.0.0 "compatible with 1.2.3"
|
||||||
|
--- ^0.2.3 is >=0.2.3 <0.3.0 (0.x.x is special)
|
||||||
|
--- ^0.0.1 is =0.0.1 (0.0.x is special)
|
||||||
|
--- ^1.2 is >=1.2.0 <2.0.0 (like ^1.2.0)
|
||||||
|
--- ~1.2 is >=1.2.0 <1.3.0 (like ~1.2.0)
|
||||||
|
--- ^1 is >=1.0.0 <2.0.0 "compatible with 1"
|
||||||
|
--- ~1 same "reasonably close to 1"
|
||||||
|
--- 1.x same
|
||||||
|
--- 1.* same
|
||||||
|
--- 1 same
|
||||||
|
--- * any version
|
||||||
|
--- x same
|
||||||
|
---
|
||||||
|
--- 1.2.3 - 2.3.4 is >=1.2.3 <=2.3.4
|
||||||
|
---
|
||||||
|
--- Partial right: missing pieces treated as x (2.3 => 2.3.x).
|
||||||
|
--- 1.2.3 - 2.3 is >=1.2.3 <2.4.0
|
||||||
|
--- 1.2.3 - 2 is >=1.2.3 <3.0.0
|
||||||
|
---
|
||||||
|
--- Partial left: missing pieces treated as 0 (1.2 => 1.2.0).
|
||||||
|
--- 1.2 - 2.3.0 is 1.2.0 - 2.3.0
|
||||||
|
--- </pre>
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
local LazyM = {}
|
---@class Version
|
||||||
M.LazyM = LazyM
|
|
||||||
|
|
||||||
---@class Semver
|
|
||||||
---@field [1] number
|
---@field [1] number
|
||||||
---@field [2] number
|
---@field [2] number
|
||||||
---@field [3] number
|
---@field [3] number
|
||||||
@@ -12,14 +62,14 @@ M.LazyM = LazyM
|
|||||||
---@field patch number
|
---@field patch number
|
||||||
---@field prerelease? string
|
---@field prerelease? string
|
||||||
---@field build? string
|
---@field build? string
|
||||||
local Semver = {}
|
local Version = {}
|
||||||
Semver.__index = Semver
|
Version.__index = Version
|
||||||
|
|
||||||
function Semver:__index(key)
|
function Version:__index(key)
|
||||||
return type(key) == 'number' and ({ self.major, self.minor, self.patch })[key] or Semver[key]
|
return type(key) == 'number' and ({ self.major, self.minor, self.patch })[key] or Version[key]
|
||||||
end
|
end
|
||||||
|
|
||||||
function Semver:__newindex(key, value)
|
function Version:__newindex(key, value)
|
||||||
if key == 1 then
|
if key == 1 then
|
||||||
self.major = value
|
self.major = value
|
||||||
elseif key == 2 then
|
elseif key == 2 then
|
||||||
@@ -31,8 +81,8 @@ function Semver:__newindex(key, value)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param other Semver
|
---@param other Version
|
||||||
function Semver:__eq(other)
|
function Version:__eq(other)
|
||||||
for i = 1, 3 do
|
for i = 1, 3 do
|
||||||
if self[i] ~= other[i] then
|
if self[i] ~= other[i] then
|
||||||
return false
|
return false
|
||||||
@@ -41,7 +91,7 @@ function Semver:__eq(other)
|
|||||||
return self.prerelease == other.prerelease
|
return self.prerelease == other.prerelease
|
||||||
end
|
end
|
||||||
|
|
||||||
function Semver:__tostring()
|
function Version:__tostring()
|
||||||
local ret = table.concat({ self.major, self.minor, self.patch }, '.')
|
local ret = table.concat({ self.major, self.minor, self.patch }, '.')
|
||||||
if self.prerelease then
|
if self.prerelease then
|
||||||
ret = ret .. '-' .. self.prerelease
|
ret = ret .. '-' .. self.prerelease
|
||||||
@@ -52,8 +102,8 @@ function Semver:__tostring()
|
|||||||
return ret
|
return ret
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param other Semver
|
---@param other Version
|
||||||
function Semver:__lt(other)
|
function Version:__lt(other)
|
||||||
for i = 1, 3 do
|
for i = 1, 3 do
|
||||||
if self[i] > other[i] then
|
if self[i] > other[i] then
|
||||||
return false
|
return false
|
||||||
@@ -70,21 +120,32 @@ function Semver:__lt(other)
|
|||||||
return (self.prerelease or '') < (other.prerelease or '')
|
return (self.prerelease or '') < (other.prerelease or '')
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param other Semver
|
---@param other Version
|
||||||
function Semver:__le(other)
|
function Version:__le(other)
|
||||||
return self < other or self == other
|
return self < other or self == other
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param version string|number[]
|
--- @private
|
||||||
---@param strict? boolean Reject "1.0", "0-x" or other non-conforming version strings
|
---
|
||||||
---@return Semver?
|
--- Creates a new Version object. Not public currently.
|
||||||
function LazyM.version(version, strict)
|
---
|
||||||
|
--- @param version string|number[]|Version
|
||||||
|
--- @param strict? boolean Reject "1.0", "0-x", "3.2a" or other non-conforming version strings
|
||||||
|
--- @return Version?
|
||||||
|
function M._version(version, strict) -- Adapted from https://github.com/folke/lazy.nvim
|
||||||
if type(version) == 'table' then
|
if type(version) == 'table' then
|
||||||
|
if version.major then
|
||||||
|
return setmetatable(vim.deepcopy(version), Version)
|
||||||
|
end
|
||||||
return setmetatable({
|
return setmetatable({
|
||||||
major = version[1] or 0,
|
major = version[1] or 0,
|
||||||
minor = version[2] or 0,
|
minor = version[2] or 0,
|
||||||
patch = version[3] or 0,
|
patch = version[3] or 0,
|
||||||
}, Semver)
|
}, Version)
|
||||||
|
end
|
||||||
|
|
||||||
|
if not strict then -- TODO: add more "scrubbing".
|
||||||
|
version = version:match('%d[^ ]*')
|
||||||
end
|
end
|
||||||
|
|
||||||
local prerel = version:match('%-([^+]*)')
|
local prerel = version:match('%-([^+]*)')
|
||||||
@@ -110,11 +171,13 @@ function LazyM.version(version, strict)
|
|||||||
patch = patch == '' and 0 or tonumber(patch),
|
patch = patch == '' and 0 or tonumber(patch),
|
||||||
prerelease = prerel ~= '' and prerel or nil,
|
prerelease = prerel ~= '' and prerel or nil,
|
||||||
build = build ~= '' and build or nil,
|
build = build ~= '' and build or nil,
|
||||||
}, Semver)
|
}, Version)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---@generic T: Semver
|
---TODO: generalize this, move to func.lua
|
||||||
|
---
|
||||||
|
---@generic T: Version
|
||||||
---@param versions T[]
|
---@param versions T[]
|
||||||
---@return T?
|
---@return T?
|
||||||
function M.last(versions)
|
function M.last(versions)
|
||||||
@@ -127,13 +190,15 @@ function M.last(versions)
|
|||||||
return last
|
return last
|
||||||
end
|
end
|
||||||
|
|
||||||
---@class SemverRange
|
---@class Range
|
||||||
---@field from Semver
|
---@field from Version
|
||||||
---@field to? Semver
|
---@field to? Version
|
||||||
local Range = {}
|
local Range = {}
|
||||||
|
|
||||||
---@param version string|Semver
|
--- @private
|
||||||
function Range:matches(version)
|
---
|
||||||
|
---@param version string|Version
|
||||||
|
function Range:has(version)
|
||||||
if type(version) == 'string' then
|
if type(version) == 'string' then
|
||||||
---@diagnostic disable-next-line: cast-local-type
|
---@diagnostic disable-next-line: cast-local-type
|
||||||
version = M.parse(version)
|
version = M.parse(version)
|
||||||
@@ -146,8 +211,32 @@ function Range:matches(version)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param spec string
|
--- Parses a semver |version-range| "spec" and returns a range object:
|
||||||
function LazyM.range(spec)
|
--- <pre>
|
||||||
|
--- {
|
||||||
|
--- from: Version
|
||||||
|
--- to: Version
|
||||||
|
--- has(v: string|Version)
|
||||||
|
--- }
|
||||||
|
--- </pre>
|
||||||
|
---
|
||||||
|
--- `: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
|
||||||
|
--- </pre>
|
||||||
|
---
|
||||||
|
--- Or use cmp(), eq(), lt(), and gt() to compare `.to` and `.from` directly:
|
||||||
|
--- <pre>lua
|
||||||
|
--- local r = vim.version.range('1.0.0 - 2.0.0')
|
||||||
|
--- print(vim.version.gt({1,0,3}, r.from) and vim.version.lt({1,0,3}, r.to))
|
||||||
|
--- </pre>
|
||||||
|
---
|
||||||
|
--- @see # https://github.com/npm/node-semver#ranges
|
||||||
|
---
|
||||||
|
--- @param spec string Version range "spec"
|
||||||
|
function M.range(spec) -- Adapted from https://github.com/folke/lazy.nvim
|
||||||
if spec == '*' or spec == '' then
|
if spec == '*' or spec == '' then
|
||||||
return setmetatable({ from = M.parse('0.0.0') }, { __index = Range })
|
return setmetatable({ from = M.parse('0.0.0') }, { __index = Range })
|
||||||
end
|
end
|
||||||
@@ -158,8 +247,8 @@ function LazyM.range(spec)
|
|||||||
local a = spec:sub(1, hyphen - 1)
|
local a = spec:sub(1, hyphen - 1)
|
||||||
local b = spec:sub(hyphen + 3)
|
local b = spec:sub(hyphen + 3)
|
||||||
local parts = vim.split(b, '.', { plain = true })
|
local parts = vim.split(b, '.', { plain = true })
|
||||||
local ra = LazyM.range(a)
|
local ra = M.range(a)
|
||||||
local rb = LazyM.range(b)
|
local rb = M.range(b)
|
||||||
return setmetatable({
|
return setmetatable({
|
||||||
from = ra and ra.from,
|
from = ra and ra.from,
|
||||||
to = rb and (#parts == 3 and rb.from or rb.to),
|
to = rb and (#parts == 3 and rb.from or rb.to),
|
||||||
@@ -209,40 +298,40 @@ function LazyM.range(spec)
|
|||||||
end
|
end
|
||||||
|
|
||||||
---@private
|
---@private
|
||||||
---@param v string
|
---@param v string|Version
|
||||||
---@return string
|
---@return string
|
||||||
local function create_err_msg(v)
|
local function create_err_msg(v)
|
||||||
if type(v) == 'string' then
|
if type(v) == 'string' then
|
||||||
return string.format('invalid version: "%s"', tostring(v))
|
return string.format('invalid version: "%s"', tostring(v))
|
||||||
|
elseif type(v) == 'table' and v.major then
|
||||||
|
return string.format('invalid version: %s', vim.inspect(v))
|
||||||
end
|
end
|
||||||
return string.format('invalid version: %s (%s)', tostring(v), type(v))
|
return string.format('invalid version: %s (%s)', tostring(v), type(v))
|
||||||
end
|
end
|
||||||
|
|
||||||
---@private
|
--- Parses and compares two version version objects (the result of |vim.version.parse()|, or
|
||||||
--- Throws an error if `version` cannot be parsed.
|
--- specified literally as a `{major, minor, patch}` tuple, e.g. `{1, 0, 3}`).
|
||||||
---@param v string
|
|
||||||
local function assert_version(v, opt)
|
|
||||||
local rv = M.parse(v, opt)
|
|
||||||
if rv == nil then
|
|
||||||
error(create_err_msg(v))
|
|
||||||
end
|
|
||||||
return rv
|
|
||||||
end
|
|
||||||
|
|
||||||
--- Parses and compares two version strings.
|
|
||||||
---
|
---
|
||||||
--- semver notes:
|
--- Example:
|
||||||
--- - Build metadata MUST be ignored when comparing versions.
|
--- <pre>lua
|
||||||
|
--- if vim.version.cmp({1,0,3}, {0,2,1}) == 0 then
|
||||||
|
--- -- ...
|
||||||
|
--- end
|
||||||
|
--- local v1 = vim.version.parse('1.0.3-pre')
|
||||||
|
--- local v2 = vim.version.parse('0.2.1')
|
||||||
|
--- if vim.version.cmp(v1, v2) == 0 then
|
||||||
|
--- -- ...
|
||||||
|
--- end
|
||||||
|
--- </pre>
|
||||||
---
|
---
|
||||||
---@param v1 string Version.
|
--- @note Per semver, build metadata is ignored when comparing two otherwise-equivalent versions.
|
||||||
---@param v2 string Version to compare with v1.
|
---
|
||||||
---@param opts table|nil Optional keyword arguments:
|
---@param v1 Version|number[] Version object.
|
||||||
--- - strict (boolean): see `version.parse` for details. Defaults to false.
|
---@param v2 Version|number[] Version to compare with `v1`.
|
||||||
---@return integer `-1` if `v1 < v2`, `0` if `v1 == v2`, `1` if `v1 > v2`.
|
---@return integer -1 if `v1 < v2`, 0 if `v1 == v2`, 1 if `v1 > v2`.
|
||||||
function M.cmp(v1, v2, opts)
|
function M.cmp(v1, v2)
|
||||||
opts = opts or { strict = false }
|
local v1_parsed = assert(M._version(v1), create_err_msg(v1))
|
||||||
local v1_parsed = assert_version(v1, opts)
|
local v2_parsed = assert(M._version(v2), create_err_msg(v1))
|
||||||
local v2_parsed = assert_version(v2, opts)
|
|
||||||
if v1_parsed == v2_parsed then
|
if v1_parsed == v2_parsed then
|
||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
@@ -252,58 +341,50 @@ function M.cmp(v1, v2, opts)
|
|||||||
return -1
|
return -1
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Parses a semantic version string.
|
---Returns `true` if the given versions are equal.
|
||||||
---
|
---@param v1 Version|number[]
|
||||||
--- Ignores leading "v" and surrounding whitespace, e.g. " v1.0.1-rc1+build.2",
|
---@param v2 Version|number[]
|
||||||
--- "1.0.1-rc1+build.2", "v1.0.1-rc1+build.2" and "v1.0.1-rc1+build.2 " are all parsed as:
|
|
||||||
--- <pre>
|
|
||||||
--- { major = 1, minor = 0, patch = 1, prerelease = "rc1", build = "build.2" }
|
|
||||||
--- </pre>
|
|
||||||
---
|
|
||||||
---@param version string Version string to be parsed.
|
|
||||||
---@param opts table|nil Optional keyword arguments:
|
|
||||||
--- - strict (boolean): Default false. If `true`, no coercion is attempted on
|
|
||||||
--- input not strictly conforming to semver v2.0.0
|
|
||||||
--- (https://semver.org/spec/v2.0.0.html). E.g. `parse("v1.2")` returns nil.
|
|
||||||
---@return table|nil parsed_version Parsed version table or `nil` if `version` is invalid.
|
|
||||||
function M.parse(version, opts)
|
|
||||||
if type(version) ~= 'string' then
|
|
||||||
error(create_err_msg(version))
|
|
||||||
end
|
|
||||||
opts = opts or { strict = false }
|
|
||||||
|
|
||||||
if opts.strict then
|
|
||||||
return LazyM.version(version, true)
|
|
||||||
end
|
|
||||||
|
|
||||||
version = vim.trim(version) -- TODO: add more "scrubbing".
|
|
||||||
return LazyM.version(version, false)
|
|
||||||
end
|
|
||||||
|
|
||||||
---Returns `true` if `v1` are `v2` are equal versions.
|
|
||||||
---@param v1 string
|
|
||||||
---@param v2 string
|
|
||||||
---@return boolean
|
---@return boolean
|
||||||
function M.eq(v1, v2)
|
function M.eq(v1, v2)
|
||||||
return M.cmp(v1, v2) == 0
|
return M.cmp(v1, v2) == 0
|
||||||
end
|
end
|
||||||
|
|
||||||
---Returns `true` if `v1` is less than `v2`.
|
---Returns `true` if `v1 < v2`.
|
||||||
---@param v1 string
|
---@param v1 Version|number[]
|
||||||
---@param v2 string
|
---@param v2 Version|number[]
|
||||||
---@return boolean
|
---@return boolean
|
||||||
function M.lt(v1, v2)
|
function M.lt(v1, v2)
|
||||||
return M.cmp(v1, v2) == -1
|
return M.cmp(v1, v2) == -1
|
||||||
end
|
end
|
||||||
|
|
||||||
---Returns `true` if `v1` is greater than `v2`.
|
---Returns `true` if `v1 > v2`.
|
||||||
---@param v1 string
|
---@param v1 Version|number[]
|
||||||
---@param v2 string
|
---@param v2 Version|number[]
|
||||||
---@return boolean
|
---@return boolean
|
||||||
function M.gt(v1, v2)
|
function M.gt(v1, v2)
|
||||||
return M.cmp(v1, v2) == 1
|
return M.cmp(v1, v2) == 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Parses a semantic version string and returns a version object which can be used with other
|
||||||
|
--- `vim.version` functions. For example "1.0.1-rc1+build.2" returns:
|
||||||
|
--- <pre>
|
||||||
|
--- { major = 1, minor = 0, patch = 1, prerelease = "rc1", build = "build.2" }
|
||||||
|
--- </pre>
|
||||||
|
---
|
||||||
|
--- @see # https://semver.org/spec/v2.0.0.html
|
||||||
|
---
|
||||||
|
---@param version string Version string to parse.
|
||||||
|
---@param opts table|nil Optional keyword arguments:
|
||||||
|
--- - strict (boolean): Default false. If `true`, no coercion is attempted on
|
||||||
|
--- input not conforming to semver v2.0.0. If `false`, `parse()` attempts to
|
||||||
|
--- coerce input such as "1.0", "0-x", "tmux 3.2a" into valid versions.
|
||||||
|
---@return table|nil parsed_version Version object or `nil` if input is invalid.
|
||||||
|
function M.parse(version, opts)
|
||||||
|
assert(type(version) == 'string', create_err_msg(version))
|
||||||
|
opts = opts or { strict = false }
|
||||||
|
return M._version(version, opts.strict)
|
||||||
|
end
|
||||||
|
|
||||||
setmetatable(M, {
|
setmetatable(M, {
|
||||||
__call = function()
|
__call = function()
|
||||||
return vim.fn.api_info().version
|
return vim.fn.api_info().version
|
||||||
|
|||||||
@@ -6,11 +6,8 @@ local exec_lua = helpers.exec_lua
|
|||||||
local matches = helpers.matches
|
local matches = helpers.matches
|
||||||
local pcall_err = helpers.pcall_err
|
local pcall_err = helpers.pcall_err
|
||||||
|
|
||||||
local version = require('vim.version')
|
|
||||||
local Semver = version.LazyM
|
|
||||||
|
|
||||||
local function v(ver)
|
local function v(ver)
|
||||||
return Semver.version(ver)
|
return vim.version._version(ver)
|
||||||
end
|
end
|
||||||
|
|
||||||
describe('version', function()
|
describe('version', function()
|
||||||
@@ -20,13 +17,13 @@ describe('version', function()
|
|||||||
eq({ major = 42, minor = 3, patch = 99 }, exec_lua("return vim.version.parse('v42.3.99')"))
|
eq({ major = 42, minor = 3, patch = 99 }, exec_lua("return vim.version.parse('v42.3.99')"))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('lazy semver version', function()
|
describe('_version()', function()
|
||||||
local tests = {
|
local tests = {
|
||||||
['v1.2.3'] = { major = 1, minor = 2, patch = 3 },
|
['v1.2.3'] = { major = 1, minor = 2, patch = 3 },
|
||||||
['v1.2'] = { major = 1, minor = 2, patch = 0 },
|
['v1.2'] = { major = 1, minor = 2, patch = 0 },
|
||||||
['v1.2.3-prerelease'] = { major = 1, minor = 2, patch = 3, prerelease = 'prerelease' },
|
['v1.2.3-prerelease'] = { major = 1, minor = 2, patch = 3, prerelease = 'prerelease' },
|
||||||
['v1.2-prerelease'] = { major = 1, minor = 2, patch = 0, prerelease = 'prerelease' },
|
['v1.2-prerelease'] = { major = 1, minor = 2, patch = 0, prerelease = 'prerelease' },
|
||||||
['v1.2.3-prerelease+build'] = { major = 1, minor = 2, patch = 3, prerelease = 'prerelease', build = "build" },
|
['v1.2.3-prerelease+build'] = { major = 1, minor = 2, patch = 3, prerelease = 'prerelease', build = 'build' },
|
||||||
['1.2.3+build'] = { major = 1, minor = 2, patch = 3, build = 'build' },
|
['1.2.3+build'] = { major = 1, minor = 2, patch = 3, build = 'build' },
|
||||||
}
|
}
|
||||||
for input, output in pairs(tests) do
|
for input, output in pairs(tests) do
|
||||||
@@ -36,7 +33,7 @@ describe('version', function()
|
|||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('lazy semver range', function()
|
describe('range', function()
|
||||||
local tests = {
|
local tests = {
|
||||||
['1.2.3'] = { from = { 1, 2, 3 }, to = { 1, 2, 4 } },
|
['1.2.3'] = { from = { 1, 2, 3 }, to = { 1, 2, 4 } },
|
||||||
['1.2'] = { from = { 1, 2, 0 }, to = { 1, 3, 0 } },
|
['1.2'] = { from = { 1, 2, 0 }, to = { 1, 3, 0 } },
|
||||||
@@ -64,51 +61,34 @@ describe('version', function()
|
|||||||
for input, output in pairs(tests) do
|
for input, output in pairs(tests) do
|
||||||
output.from = v(output.from)
|
output.from = v(output.from)
|
||||||
output.to = output.to and v(output.to)
|
output.to = output.to and v(output.to)
|
||||||
|
local range = vim.version.range(input)
|
||||||
|
|
||||||
local range = Semver.range(input)
|
|
||||||
it('parses ' .. input, function()
|
it('parses ' .. input, function()
|
||||||
eq(output, range)
|
eq(output, range)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('[from] in range ' .. input, function()
|
it('[from] in range ' .. input, function()
|
||||||
assert(range:matches(output.from))
|
assert(range:has(output.from))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('[from-1] not in range ' .. input, function()
|
it('[from-1] not in range ' .. input, function()
|
||||||
local lower = vim.deepcopy(range.from)
|
local lower = vim.deepcopy(range.from)
|
||||||
lower.major = lower.major - 1
|
lower.major = lower.major - 1
|
||||||
assert(not range:matches(lower))
|
assert(not range:has(lower))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('[to] not in range ' .. input .. ' to:' .. tostring(range and range.to), function()
|
it('[to] not in range ' .. input .. ' to:' .. tostring(range.to), function()
|
||||||
if range.to then
|
if range.to then
|
||||||
assert(not (range.to < range.to))
|
assert(not (range.to < range.to))
|
||||||
assert(not range:matches(range.to))
|
assert(not range:has(range.to))
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
it("handles prerelease", function()
|
it('handles prerelease', function()
|
||||||
assert(not Semver.range('1.2.3'):matches('1.2.3-alpha'))
|
assert(not vim.version.range('1.2.3'):has('1.2.3-alpha'))
|
||||||
assert(Semver.range('1.2.3-alpha'):matches('1.2.3-alpha'))
|
assert(vim.version.range('1.2.3-alpha'):has('1.2.3-alpha'))
|
||||||
assert(not Semver.range('1.2.3-alpha'):matches('1.2.3-beta'))
|
assert(not vim.version.range('1.2.3-alpha'):has('1.2.3-beta'))
|
||||||
end)
|
|
||||||
end)
|
|
||||||
|
|
||||||
describe('lazy semver order', function()
|
|
||||||
it('is correct', function()
|
|
||||||
assert(v('v1.2.3') == v('1.2.3'))
|
|
||||||
assert(not (v('v1.2.3') < v('1.2.3')))
|
|
||||||
assert(v('v1.2.3') > v('1.2.3-prerelease'))
|
|
||||||
assert(v('v1.2.3-alpha') < v('1.2.3-beta'))
|
|
||||||
assert(v('v1.2.3-prerelease') < v('1.2.3'))
|
|
||||||
assert(v('v1.2.3') >= v('1.2.3'))
|
|
||||||
assert(v('v1.2.3') >= v('1.0.3'))
|
|
||||||
assert(v('v1.2.3') >= v('1.2.2'))
|
|
||||||
assert(v('v1.2.3') > v('1.2.2'))
|
|
||||||
assert(v('v1.2.3') > v('1.0.3'))
|
|
||||||
eq(version.last({ v('1.2.3'), v('2.0.0') }), v('2.0.0'))
|
|
||||||
eq(version.last({ v('2.0.0'), v('1.2.3') }), v('2.0.0'))
|
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
@@ -143,12 +123,11 @@ describe('version', function()
|
|||||||
{ v1 = '1.0.0-beta.11', v2 = '1.0.0-rc.1', want = -1, },
|
{ v1 = '1.0.0-beta.11', v2 = '1.0.0-rc.1', want = -1, },
|
||||||
}
|
}
|
||||||
for _, tc in ipairs(testcases) do
|
for _, tc in ipairs(testcases) do
|
||||||
local want = ('v1 %s v2'):format(tc.want == 0 and '==' or (tc.want == 1 and '>' or '<'))
|
local msg = function(s) return ('v1 %s v2'):format(s == 0 and '==' or (s == 1 and '>' or '<')) end
|
||||||
it(string.format('(v1 = %s, v2 = %s)', tc.v1, tc.v2),
|
it(string.format('(v1 = %s, v2 = %s)', tc.v1, tc.v2),
|
||||||
function()
|
function()
|
||||||
local rv = version.cmp(tc.v1, tc.v2, { strict = true })
|
local rv = vim.version.cmp(tc.v1, tc.v2, { strict = true })
|
||||||
local got = ('v1 %s v2'):format(rv == 0 and '==' or (rv == 1 and '>' or '<'))
|
ok(tc.want == rv, msg(tc.want), msg(rv))
|
||||||
ok(tc.want == rv, want, got)
|
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
@@ -157,47 +136,19 @@ describe('version', function()
|
|||||||
describe('parse()', function()
|
describe('parse()', function()
|
||||||
describe('strict=true', function()
|
describe('strict=true', function()
|
||||||
local testcases = {
|
local testcases = {
|
||||||
{
|
{ desc = 'Nvim version', version = 'v0.9.0-dev-1233+g210120dde81e', want = { major = 0, minor = 9, patch = 0, prerelease = 'dev-1233', build = 'g210120dde81e', }, },
|
||||||
desc = 'Nvim version',
|
{ desc = 'no v', version = '10.20.123', want = { major = 10, minor = 20, patch = 123, prerelease = nil, build = nil, }, },
|
||||||
version = 'v0.9.0-dev-1233+g210120dde81e',
|
{ desc = 'with v', version = 'v1.2.3', want = { major = 1, minor = 2, patch = 3 }, },
|
||||||
want = { major = 0, minor = 9, patch = 0, prerelease = 'dev-1233', build = 'g210120dde81e', },
|
{ desc = 'prerelease', version = '1.2.3-alpha', want = { major = 1, minor = 2, patch = 3, prerelease = 'alpha' }, },
|
||||||
},
|
{ desc = 'prerelease.x', version = '1.2.3-alpha.1', want = { major = 1, minor = 2, patch = 3, prerelease = 'alpha.1' }, },
|
||||||
{
|
{ desc = 'build.x', version = '1.2.3+build.15', want = { major = 1, minor = 2, patch = 3, build = 'build.15' }, },
|
||||||
desc = 'no leading v',
|
{ desc = 'prerelease and build', version = '1.2.3-rc1+build.15', want = { major = 1, minor = 2, patch = 3, prerelease = 'rc1', build = 'build.15', }, },
|
||||||
version = '10.20.123',
|
|
||||||
want = { major = 10, minor = 20, patch = 123, prerelease = nil, build = nil, },
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc = 'leading v',
|
|
||||||
version = 'v1.2.3',
|
|
||||||
want = { major = 1, minor = 2, patch = 3 },
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc = 'prerelease',
|
|
||||||
version = '1.2.3-alpha',
|
|
||||||
want = { major = 1, minor = 2, patch = 3, prerelease = 'alpha' },
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc = 'prerelease and other identifiers',
|
|
||||||
version = '1.2.3-alpha.1',
|
|
||||||
want = { major = 1, minor = 2, patch = 3, prerelease = 'alpha.1' },
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc = 'build',
|
|
||||||
version = '1.2.3+build.15',
|
|
||||||
want = { major = 1, minor = 2, patch = 3, build = 'build.15' },
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc = 'prerelease and build',
|
|
||||||
version = '1.2.3-rc1+build.15',
|
|
||||||
want = { major = 1, minor = 2, patch = 3, prerelease = 'rc1', build = 'build.15', },
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
for _, tc in ipairs(testcases) do
|
for _, tc in ipairs(testcases) do
|
||||||
it(
|
it(
|
||||||
string.format('%q: version = %q', tc.desc, tc.version),
|
string.format('%q: version = %q', tc.desc, tc.version),
|
||||||
function()
|
function()
|
||||||
eq(tc.want, version.parse(tc.version))
|
eq(tc.want, vim.version.parse(tc.version))
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
@@ -211,12 +162,13 @@ describe('version', function()
|
|||||||
{ version = '1-1.0', want = { major = 1, minor = 0, patch = 0, prerelease = '1.0' }, },
|
{ version = '1-1.0', want = { major = 1, minor = 0, patch = 0, prerelease = '1.0' }, },
|
||||||
{ version = 'v1.2.3 ', want = { major = 1, minor = 2, patch = 3 }, },
|
{ version = 'v1.2.3 ', want = { major = 1, minor = 2, patch = 3 }, },
|
||||||
{ version = ' v1.2.3', want = { major = 1, minor = 2, patch = 3 }, },
|
{ version = ' v1.2.3', want = { major = 1, minor = 2, patch = 3 }, },
|
||||||
|
{ version = 'tmux 3.2a', want = { major = 3, minor = 2, patch = 0, }, },
|
||||||
}
|
}
|
||||||
for _, tc in ipairs(testcases) do
|
for _, tc in ipairs(testcases) do
|
||||||
it(
|
it(
|
||||||
string.format('version = %q', tc.version),
|
string.format('version = %q', tc.version),
|
||||||
function()
|
function()
|
||||||
eq(tc.want, version.parse(tc.version, { strict = false }))
|
eq(tc.want, vim.version.parse(tc.version, { strict = false }))
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
@@ -236,6 +188,8 @@ describe('version', function()
|
|||||||
{ version = '1.2.3-%?' },
|
{ version = '1.2.3-%?' },
|
||||||
{ version = '1.2.3+%?' },
|
{ version = '1.2.3+%?' },
|
||||||
{ version = '1.2.3+build.0-rc1' },
|
{ version = '1.2.3+build.0-rc1' },
|
||||||
|
{ version = '3.2a', },
|
||||||
|
{ version = 'tmux 3.2a', },
|
||||||
}
|
}
|
||||||
|
|
||||||
local function quote_empty(s)
|
local function quote_empty(s)
|
||||||
@@ -244,7 +198,7 @@ describe('version', function()
|
|||||||
|
|
||||||
for _, tc in ipairs(testcases) do
|
for _, tc in ipairs(testcases) do
|
||||||
it(quote_empty(tc.version), function()
|
it(quote_empty(tc.version), function()
|
||||||
eq(nil, version.parse(tc.version, { strict = true }))
|
eq(nil, vim.version.parse(tc.version, { strict = true }))
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
@@ -261,21 +215,42 @@ describe('version', function()
|
|||||||
it(string.format('(%s): %s', tc.desc, tostring(tc.version)), function()
|
it(string.format('(%s): %s', tc.desc, tostring(tc.version)), function()
|
||||||
local expected = string.format(type(tc.version) == 'string'
|
local expected = string.format(type(tc.version) == 'string'
|
||||||
and 'invalid version: "%s"' or 'invalid version: %s', tostring(tc.version))
|
and 'invalid version: "%s"' or 'invalid version: %s', tostring(tc.version))
|
||||||
matches(expected, pcall_err(version.parse, tc.version, { strict = true }))
|
matches(expected, pcall_err(vim.version.parse, tc.version, { strict = true }))
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('relational metamethods (== < >)', function()
|
||||||
|
assert(v('v1.2.3') == v('1.2.3'))
|
||||||
|
assert(not (v('v1.2.3') < v('1.2.3')))
|
||||||
|
assert(v('v1.2.3') > v('1.2.3-prerelease'))
|
||||||
|
assert(v('v1.2.3-alpha') < v('1.2.3-beta'))
|
||||||
|
assert(v('v1.2.3-prerelease') < v('1.2.3'))
|
||||||
|
assert(v('v1.2.3') >= v('1.2.3'))
|
||||||
|
assert(v('v1.2.3') >= v('1.0.3'))
|
||||||
|
assert(v('v1.2.3') >= v('1.2.2'))
|
||||||
|
assert(v('v1.2.3') > v('1.2.2'))
|
||||||
|
assert(v('v1.2.3') > v('1.0.3'))
|
||||||
|
eq(vim.version.last({ v('1.2.3'), v('2.0.0') }), v('2.0.0'))
|
||||||
|
eq(vim.version.last({ v('2.0.0'), v('1.2.3') }), v('2.0.0'))
|
||||||
|
end)
|
||||||
|
|
||||||
it('lt()', function()
|
it('lt()', function()
|
||||||
eq(true, version.lt('1', '2'))
|
eq(true, vim.version.lt('1', '2'))
|
||||||
|
eq(false, vim.version.lt({3}, {0, 7, 4}))
|
||||||
|
eq(false, vim.version.lt({major=3, minor=3, patch=0}, {3, 2, 0}))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('gt()', function()
|
it('gt()', function()
|
||||||
eq(true, version.gt('2', '1'))
|
eq(true, vim.version.gt('2', '1'))
|
||||||
|
eq(true, vim.version.gt({3}, {0, 7, 4}))
|
||||||
|
eq(true, vim.version.gt({major=3, minor=3, patch=0}, {3, 2, 0}))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('eq()', function()
|
it('eq()', function()
|
||||||
eq(true, version.eq('2', '2'))
|
eq(true, vim.version.eq('2', '2'))
|
||||||
|
eq(true, vim.version.eq({3, 1, 0}, '3.1.0'))
|
||||||
|
eq(true, vim.version.eq({major=3, minor=3, patch=0}, {3, 3, 0}))
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|||||||
@@ -202,7 +202,7 @@ describe(':terminal buffer', function()
|
|||||||
|
|
||||||
-- Save the buffer number of the terminal for later testing.
|
-- Save the buffer number of the terminal for later testing.
|
||||||
local tbuf = eval('bufnr("%")')
|
local tbuf = eval('bufnr("%")')
|
||||||
local exitcmd = helpers.is_os('win')
|
local exitcmd = is_os('win')
|
||||||
and "['cmd', '/c', 'exit']"
|
and "['cmd', '/c', 'exit']"
|
||||||
or "['sh', '-c', 'exit']"
|
or "['sh', '-c', 'exit']"
|
||||||
source([[
|
source([[
|
||||||
|
|||||||
Reference in New Issue
Block a user