mirror of
https://github.com/neovim/neovim.git
synced 2025-09-05 19:08:15 +00:00
docs(vim.version): document vim.VersionRange
as a dedicated class
This commit is contained in:
@@ -3927,6 +3927,43 @@ versions (1.2.3-rc1) are not matched. >
|
||||
<
|
||||
|
||||
|
||||
*vim.VersionRange*
|
||||
|
||||
Fields: ~
|
||||
• {from} (`vim.Version`)
|
||||
• {to}? (`vim.Version`)
|
||||
• {has} (`fun(self: vim.VersionRange, version: string|vim.Version): boolean`)
|
||||
See |VersionRange:has()|.
|
||||
|
||||
|
||||
VersionRange:has({version}) *VersionRange:has()*
|
||||
Check 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(vim.version())) -- check against current Nvim version
|
||||
<
|
||||
|
||||
Or use cmp(), le(), lt(), ge(), gt(), and/or eq() to compare a version
|
||||
against `.to` and `.from` directly: >lua
|
||||
local r = vim.version.range('1.0.0 - 2.0.0') -- >=1.0, <2.0
|
||||
print(vim.version.ge({1,0,3}, r.from) and vim.version.lt({1,0,3}, r.to))
|
||||
<
|
||||
|
||||
Attributes: ~
|
||||
Since: 0.9.0
|
||||
|
||||
Parameters: ~
|
||||
• {version} (`string|vim.Version`)
|
||||
|
||||
Return: ~
|
||||
(`boolean`)
|
||||
|
||||
See also: ~
|
||||
• https://github.com/npm/node-semver#ranges
|
||||
|
||||
vim.version.cmp({v1}, {v2}) *vim.version.cmp()*
|
||||
Parses and compares two version objects (the result of
|
||||
|vim.version.parse()|, or specified literally as a `{major, minor, patch}`
|
||||
@@ -4058,29 +4095,8 @@ vim.version.parse({version}, {opts}) *vim.version.parse()*
|
||||
• https://semver.org/spec/v2.0.0.html
|
||||
|
||||
vim.version.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
|
||||
print(r:has(vim.version())) -- check against current Nvim version
|
||||
<
|
||||
|
||||
Or use cmp(), le(), lt(), ge(), gt(), and/or eq() to compare a version
|
||||
against `.to` and `.from` directly: >lua
|
||||
local r = vim.version.range('1.0.0 - 2.0.0') -- >=1.0, <2.0
|
||||
print(vim.version.ge({1,0,3}, r.from) and vim.version.lt({1,0,3}, r.to))
|
||||
<
|
||||
Parses a semver |version-range| "spec" and returns |vim.VersionRange|
|
||||
object:
|
||||
|
||||
Attributes: ~
|
||||
Since: 0.9.0
|
||||
@@ -4089,13 +4105,7 @@ vim.version.range({spec}) *vim.version.range()*
|
||||
• {spec} (`string`) Version range "spec"
|
||||
|
||||
Return: ~
|
||||
(`table?`) A table with the following fields:
|
||||
• {from} (`vim.Version`)
|
||||
• {to}? (`vim.Version`)
|
||||
• {has} (`fun(self: vim.VersionRange, version: string|vim.Version)`)
|
||||
|
||||
See also: ~
|
||||
• https://github.com/npm/node-semver#ranges
|
||||
(`vim.VersionRange?`) See |vim.VersionRange|.
|
||||
|
||||
|
||||
==============================================================================
|
||||
|
@@ -222,40 +222,11 @@ function M.last(versions)
|
||||
end
|
||||
|
||||
---@class vim.VersionRange
|
||||
---@inlinedoc
|
||||
---@field from vim.Version
|
||||
---@field to? vim.Version
|
||||
local VersionRange = {}
|
||||
|
||||
---@nodoc
|
||||
---@param version string|vim.Version
|
||||
function VersionRange:has(version)
|
||||
if type(version) == 'string' then
|
||||
---@diagnostic disable-next-line: cast-local-type
|
||||
version = M.parse(version)
|
||||
elseif getmetatable(version) ~= Version then
|
||||
-- Need metatable to compare versions.
|
||||
version = setmetatable(vim.deepcopy(version, true), Version)
|
||||
end
|
||||
if version then
|
||||
if self.from == self.to then
|
||||
return version == self.from
|
||||
end
|
||||
return version >= self.from and (self.to == nil or version < self.to)
|
||||
end
|
||||
end
|
||||
|
||||
--- 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`).
|
||||
--- Check if a version is in the range (inclusive `from`, exclusive `to`).
|
||||
---
|
||||
--- Example:
|
||||
---
|
||||
@@ -276,7 +247,27 @@ end
|
||||
---
|
||||
--- @see # https://github.com/npm/node-semver#ranges
|
||||
--- @since 11
|
||||
---
|
||||
--- @param version string|vim.Version
|
||||
--- @return boolean
|
||||
function VersionRange:has(version)
|
||||
if type(version) == 'string' then
|
||||
---@diagnostic disable-next-line: cast-local-type
|
||||
version = M.parse(version)
|
||||
elseif getmetatable(version) ~= Version then
|
||||
-- Need metatable to compare versions.
|
||||
version = setmetatable(vim.deepcopy(version, true), Version)
|
||||
end
|
||||
if not version then
|
||||
return false
|
||||
end
|
||||
if self.from == self.to then
|
||||
return version == self.from
|
||||
end
|
||||
return version >= self.from and (self.to == nil or version < self.to)
|
||||
end
|
||||
|
||||
--- Parses a semver |version-range| "spec" and returns |vim.VersionRange| object:
|
||||
--- @since 11
|
||||
--- @param spec string Version range "spec"
|
||||
--- @return vim.VersionRange?
|
||||
function M.range(spec) -- Adapted from https://github.com/folke/lazy.nvim
|
||||
|
Reference in New Issue
Block a user