robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1--- @brief 2--- The `vim.version` module provides functions for comparing versions and ranges 3--- conforming to the https://semver.org spec. Plugins, and plugin managers, can use this to check 4--- available tools and dependencies on the current system. 5--- 6--- Example: 7--- 8--- ```lua 9--- local v = vim.version.parse(vim.system({'tmux', '-V'}):wait().stdout, {strict=false}) 10--- if vim.version.gt(v, {3, 2, 0}) then 11--- -- ... 12--- end 13--- ``` 14--- 15--- [vim.version()] returns the version of the current Nvim process. 16--- 17--- VERSION RANGE SPEC [version-range]() 18--- 19--- A version "range spec" defines a semantic version range which can be tested against a version, 20--- using |vim.version.range()|. 21--- 22--- Supported range specs are shown in the following table. 23--- Note: suffixed versions (1.2.3-rc1) are not matched. 24--- 25--- ``` 26--- 1.2.3 is 1.2.3 27--- =1.2.3 is 1.2.3 28--- >1.2.3 greater than 1.2.3 29--- <1.2.3 before 1.2.3 30--- >=1.2.3 at least 1.2.3 31--- <=1.2.3 at most 1.2.3 32--- ~1.2.3 is >=1.2.3 <1.3.0 "reasonably close to 1.2.3" 33--- ^1.2.3 is >=1.2.3 <2.0.0 "compatible with 1.2.3" 34--- ^0.2.3 is >=0.2.3 <0.3.0 (0.x.x is special) 35--- ^0.0.1 is =0.0.1 (0.0.x is special) 36--- ^1.2 is >=1.2.0 <2.0.0 (like ^1.2.0) 37--- ~1.2 is >=1.2.0 <1.3.0 (like ~1.2.0) 38--- ^1 is >=1.0.0 <2.0.0 "compatible with 1" 39--- ~1 same "reasonably close to 1" 40--- 1.x same 41--- 1.* same 42--- 1 same 43--- * any version 44--- x same 45--- 46--- 1.2.3 - 2.3.4 is >=1.2.3 <2.3.4 47--- 48--- Partial right: missing pieces treated as x (2.3 => 2.3.x). 49--- 1.2.3 - 2.3 is >=1.2.3 <2.4.0 50--- 1.2.3 - 2 is >=1.2.3 <3.0.0 51--- 52--- Partial left: missing pieces treated as 0 (1.2 => 1.2.0). 53--- 1.2 - 2.3.0 is 1.2.0 - 2.3.0 54--- ``` 55 56---@class (internal) vim.VersionModule 57---@operator call: vim.Version 58local M ={} 59 60---@nodoc 61---@class vim.Version 62---@field [1] number 63---@field [2] number 64---@field [3] number 65---@field major number 66---@field minor number 67---@field patch number 68---@field prerelease? string 69---@field build? string 70local Version ={} 71 72--- Compares prerelease strings: per semver, number parts must be must be treated as numbers: 73--- "pre1.10" is greater than "pre1.2". https://semver.org/#spec-item-11 74---@param prerel1 string? 75---@param prerel2 string? 76local function cmp_prerel ( prerel1 , prerel2 ) 77if not prerel1 or not prerel2 then 78return prerel1 and - 1 or ( prerel2 and 1 or 0 ) 79end 80-- TODO(justinmk): not fully spec-compliant; this treats non-dot-delimited digit sequences as 81-- numbers. Maybe better: "(.-)(%.%d*)". 82local iter1 =prerel1 : gmatch ( '([^0-9]*)(%d*)' ) 83local iter2 =prerel2 : gmatch ( '([^0-9]*)(%d*)' ) 84while true do 85local word1 , n1 =iter1 () --- @type string?, string|number|nil 86local word2 , n2 =iter2 () --- @type string?, string|number|nil 87if word1 == nil and word2 == nil then -- Done iterating. 88return 0 89end 90 91word1 =word1 or '' 92n1 =vim . _tointeger ( n1 ) or 0 93word2 =word2 or '' 94n2 =vim . _tointeger ( n2 ) or 0 95 96if word1 ~= word2 then 97return word1 < word2 and - 1 or 1 98end 99if n1 ~= n2 then 100return n1 < n2 and - 1 or 1 101end 102end 103end 104 105---@param key string|integer 106function Version : __index ( key ) 107return type ( key ) == 'number' and ( { self . major , self . minor , self . patch } )[ key ] or Version [ key ] 108end 109 110---@param key string|integer 111---@param value integer|string|nil 112function Version : __newindex ( key , value ) 113if key == 1 then 114self . major =value 115elseif key == 2 then 116self . minor =value 117elseif key == 3 then 118self . patch =value 119else 120rawset ( self , key , value ) 121end 122end 123 124---@param other vim.Version 125function Version : __eq ( other ) 126for i =1 , 3 do 127if self [ i ] ~= other [ i ] then 128return false 129end 130end 131return 0 == cmp_prerel ( self . prerelease , other . prerelease ) 132end 133 134function Version : __tostring () 135local ret =table . concat ( { self . major , self . minor , self . patch } , '.' ) 136if self . prerelease then 137ret =ret .. '-' .. self . prerelease 138end 139if self . build and self . build ~= vim . NIL then 140ret =ret .. '+' .. self . build 141end 142return ret 143end 144 145---@param other vim.Version 146function Version : __lt ( other ) 147for i =1 , 3 do 148if self [ i ] > other [ i ] then 149return false 150elseif self [ i ] < other [ i ] then 151return true 152end 153end 154return - 1 == cmp_prerel ( self . prerelease , other . prerelease ) 155end 156 157---@param other vim.Version 158function Version : __le ( other ) 159return self < other or self == other 160end 161 162--- @private 163--- 164--- Creates a new Version object, or returns `nil` if `version` is invalid. 165--- 166--- @param version string|number[]|vim.Version 167--- @param strict? boolean Reject "1.0", "0-x", "3.2a" or other non-conforming version strings 168--- @return vim.Version? 169function M . _version ( version , strict ) -- Adapted from https://github.com/folke/lazy.nvim 170if type ( version ) == 'table' then 171if version . major then 172return setmetatable ( vim . deepcopy ( version , true ), Version ) 173end 174return setmetatable ( { 175major =version [ 1 ] or 0 , 176minor =version [ 2 ] or 0 , 177patch =version [ 3 ] or 0 , 178} , Version ) 179end 180 181if not strict then -- TODO: add more "scrubbing". 182local scrubbed =version : match ( '%d[^ ]*' ) 183if not scrubbed then 184return nil 185end 186version =scrubbed 187end 188 189local prerel =version : match ( '%-([^+]*)' ) 190local prerel_strict =version : match ( '%-([0-9A-Za-z-]*)' ) 191if 192strict 193and prerel 194and ( prerel_strict == nil or prerel_strict == '' or not vim . startswith ( prerel , prerel_strict )) 195then 196return nil -- Invalid prerelease. 197end 198local build =prerel and version : match ( '%-[^+]*%+(.*)$' ) or version : match ( '%+(.*)$' ) 199local major , minor , patch = 200version : match ( '^v?(%d+)%.?(%d*)%.?(%d*)' .. ( strict and ( prerel and '%-' or '$' ) or '' )) 201 202if 203( not strict and major ) 204or ( major and minor and patch and major ~= '' and minor ~= '' and patch ~= '' ) 205then 206return setmetatable ( { 207major =vim . _assert_integer ( major ), 208minor =minor == '' and 0 or vim . _assert_integer ( minor ), 209patch =patch == '' and 0 or vim . _assert_integer ( patch ), 210prerelease =prerel ~= '' and prerel or nil , 211build =build ~= '' and build or nil , 212} , Version ) 213end 214return nil -- Invalid version string. 215end 216 217---TODO: generalize this, move to func.lua 218--- 219---@generic T: vim.Version 220---@param versions T[] 221---@return T? 222function M . last ( versions ) 223local last =versions [ 1 ] 224for i =2 , # versions do 225if versions [ i ] > last then 226last =versions [ i ] 227end 228end 229return last 230end 231 232---@class vim.VersionRange 233---@field from vim.Version 234---@field to? vim.Version 235local VersionRange ={} 236 237--- Check if a version is in the range (inclusive `from`, exclusive `to`). 238--- 239--- Example: 240--- 241--- ```lua 242--- local r = vim.version.range('1.0.0 - 2.0.0') 243--- print(r:has('1.9.9')) -- true 244--- print(r:has('2.0.0')) -- false 245--- print(r:has(vim.version())) -- check against current Nvim version 246--- ``` 247--- 248--- Or use cmp(), le(), lt(), ge(), gt(), and/or eq() to compare a version 249--- against `.to` and `.from` directly: 250--- 251--- ```lua 252--- local r = vim.version.range('1.0.0 - 2.0.0') -- >=1.0, <2.0 253--- print(vim.version.ge({1,0,3}, r.from) and vim.version.lt({1,0,3}, r.to)) 254--- ``` 255--- 256--- @see # https://github.com/npm/node-semver#ranges 257--- @since 11 258--- @param version string|vim.Version 259--- @return boolean 260function VersionRange : has ( version ) 261if type ( version ) == 'string' then 262local parsed =M . parse ( version ) 263if not parsed then 264return false 265end 266version =parsed 267elseif getmetatable ( version ) ~= Version then 268-- Need metatable to compare versions. 269version =setmetatable ( vim . deepcopy ( version , true ), Version ) 270end 271if self . from == self . to then 272return version == self . from 273end 274return version >= self . from and ( self . to == nil or version < self . to ) 275end 276 277local range_mt ={ 278__index =VersionRange , 279__eq =function ( self , other ) 280return self . to == other . to and self . from == other . from 281end , 282__tostring =function ( self ) 283if not self . to then 284return '>=' .. tostring ( self . from ) 285end 286return ( '%s - %s' ): format ( tostring ( self . from ), tostring ( self . to )) 287end , 288} 289 290--- Parses a semver |version-range| "spec" and returns |vim.VersionRange| object: 291--- @since 11 292--- @param spec string Version range "spec" 293--- @return vim.VersionRange? 294function M . range ( spec ) -- Adapted from https://github.com/folke/lazy.nvim 295if spec == '*' or spec == '' then 296return setmetatable ( { from =M . parse ( '0.0.0' ) } , range_mt ) 297end 298 299local hyphen =spec : find ( ' - ' , 1 , true ) 300if hyphen then 301local a =spec : sub ( 1 , hyphen - 1 ) 302local b =spec : sub ( hyphen + 3 ) 303local parts =vim . split ( b , '.' , { plain =true } ) 304local ra =M . range ( a ) 305local rb =M . range ( b ) 306return setmetatable ( { 307from =ra and ra . from , 308to =rb and ( # parts == 3 and rb . from or rb . to ), 309} , range_mt ) 310end 311local mods , version =spec : lower (): match ( '^([%^=<>~]*)(.*)$' ) 312assert ( mods and version ) -- The pattern matches every string. 313version =version : gsub ( '%.[%*x]' , '' ) 314local parts =vim . split ( version : gsub ( '%-.*' , '' ), '.' , { plain =true } ) 315if # parts < 3 and mods == '' then 316mods ='~' 317end 318 319local semver =M . parse ( version ) 320if semver then 321local from =semver --- @type vim.Version? 322local to =vim . deepcopy ( semver , true ) --- @type vim.Version? 323---@diagnostic disable: need-check-nil 324if mods == '<' then 325from =M . _version ( {} ) 326elseif mods == '<=' then 327from =M . _version ( {} ) 328-- HACK: construct the smallest reasonable version bigger than `to` 329-- to simulate `<=` while using exclusive right hand side 330if to . prerelease then 331to . prerelease =to . prerelease .. '.0' 332else 333to . patch =to . patch + 1 334to . prerelease ='0' 335end 336elseif mods == '>' then 337-- HACK: construct the smallest reasonable version bigger than `from` 338-- to simulate `>` while using inclusive left hand side 339if from . prerelease then 340from . prerelease =from . prerelease .. '.0' 341else 342from . patch =from . patch + 1 343from . prerelease ='0' 344end 345to =nil 346elseif mods == '>=' then 347to =nil 348elseif mods == '~' then 349if # parts >= 2 then 350to [ 2 ] =to [ 2 ] + 1 351to [ 3 ] =0 352else 353to [ 1 ] =to [ 1 ] + 1 354to [ 2 ] =0 355to [ 3 ] =0 356end 357elseif mods == '^' then 358for i =1 , 3 do 359if to [ i ] ~= 0 then 360to [ i ] =to [ i ] + 1 361for j =i + 1 , 3 do 362to [ j ] =0 363end 364break 365end 366end 367end 368---@diagnostic enable: need-check-nil 369return setmetatable ( { from =from , to =to } , range_mt ) 370end 371end 372 373--- Computes the common range shared by the given ranges. 374--- 375--- @since 14 376--- @param r1 vim.VersionRange First range to intersect. 377--- @param r2 vim.VersionRange Second range to intersect. 378--- @return vim.VersionRange? Maximal range that is present inside both `r1` and `r2`. 379--- `nil` if such range does not exist. 380function M . intersect ( r1 , r2 ) 381assert ( getmetatable ( r1 ) == range_mt ) 382assert ( getmetatable ( r2 ) == range_mt ) 383 384local from =r1 . from <= r2 . from and r2 . from or r1 . from 385local to =( r1 . to == nil or ( r2 . to ~= nil and r2 . to <= r1 . to )) and r2 . to or r1 . to 386if to == nil or from < to or ( from == to and r1 : has ( from ) and r2 : has ( from )) then 387return setmetatable ( { from =from , to =to } , range_mt ) 388end 389end 390 391---@param v any 392---@return string 393local function err_msg ( v ) 394if type ( v ) == 'string' then 395return ( 'invalid version: "%s"' ): format ( v ) 396elseif type ( v ) == 'table' and v . major then 397return ( 'invalid version: %s' ): format ( vim . inspect ( v )) 398end 399return ( 'invalid version: %s (%s)' ): format ( tostring ( v ), type ( v )) 400end 401 402--- Parses and compares two version objects (the result of |vim.version.parse()|, or 403--- specified literally as a `{major, minor, patch}` tuple, e.g. `{1, 0, 3}`). 404--- 405--- Example: 406--- 407--- ```lua 408--- if vim.version.cmp({1,0,3}, {0,2,1}) == 0 then 409--- -- ... 410--- end 411--- local v1 = vim.version.parse('1.0.3-pre') 412--- local v2 = vim.version.parse('0.2.1') 413--- if vim.version.cmp(v1, v2) == 0 then 414--- -- ... 415--- end 416--- ``` 417--- 418--- @note Per semver, build metadata is ignored when comparing two otherwise-equivalent versions. 419--- @since 11 420--- 421---@param v1 vim.Version|number[]|string Version object. 422---@param v2 vim.Version|number[]|string Version to compare with `v1`. 423---@return integer -1 if `v1 < v2`, 0 if `v1 == v2`, 1 if `v1 > v2`. 424function M . cmp ( v1 , v2 ) 425local v1_parsed =M . _version ( v1 ) or error ( err_msg ( v1 )) 426local v2_parsed =M . _version ( v2 ) or error ( err_msg ( v2 )) 427if v1_parsed == v2_parsed then 428return 0 429end 430if v1_parsed > v2_parsed then 431return 1 432end 433return - 1 434end 435 436---Returns `true` if the given versions are equal. See |vim.version.cmp()| for usage. 437---@since 11 438---@param v1 vim.Version|number[]|string 439---@param v2 vim.Version|number[]|string 440---@return boolean 441function M . eq ( v1 , v2 ) 442return M . cmp ( v1 , v2 ) == 0 443end 444 445---Returns `true` if `v1 <= v2`. See |vim.version.cmp()| for usage. 446---@since 12 447---@param v1 vim.Version|number[]|string 448---@param v2 vim.Version|number[]|string 449---@return boolean 450function M . le ( v1 , v2 ) 451return M . cmp ( v1 , v2 ) <= 0 452end 453 454---Returns `true` if `v1 < v2`. See |vim.version.cmp()| for usage. 455---@since 11 456---@param v1 vim.Version|number[]|string 457---@param v2 vim.Version|number[]|string 458---@return boolean 459function M . lt ( v1 , v2 ) 460return M . cmp ( v1 , v2 ) == - 1 461end 462 463---Returns `true` if `v1 >= v2`. See |vim.version.cmp()| for usage. 464---@since 12 465---@param v1 vim.Version|number[]|string 466---@param v2 vim.Version|number[]|string 467---@return boolean 468function M . ge ( v1 , v2 ) 469return M . cmp ( v1 , v2 ) >= 0 470end 471 472---Returns `true` if `v1 > v2`. See |vim.version.cmp()| for usage. 473---@since 11 474---@param v1 vim.Version|number[]|string 475---@param v2 vim.Version|number[]|string 476---@return boolean 477function M . gt ( v1 , v2 ) 478return M . cmp ( v1 , v2 ) == 1 479end 480 481---@class vim.version.parse.Opts 482---@inlinedoc 483--- 484--- If `true`, no coercion is attempted on input not conforming to semver v2.0.0. 485--- If `false`, `parse()` attempts to coerce input such as "1.0", "0-x", "tmux 3.2a" into valid 486--- versions. 487--- (default: `false`) 488---@field strict? boolean 489 490--- Parses a semantic version string and returns a version object which can be used with other 491--- `vim.version` functions. For example "1.0.1-rc1+build.2" returns: 492--- 493--- ``` 494--- { major = 1, minor = 0, patch = 1, prerelease = "rc1", build = "build.2" } 495--- ``` 496--- 497---@see # https://semver.org/spec/v2.0.0.html 498---@since 11 499--- 500---@param version string Version string to parse. 501---@param opts vim.version.parse.Opts? Options for parsing. 502---@return vim.Version? # `Version` object or `nil` if input is invalid. 503function M . parse ( version , opts ) 504if type ( version ) ~= 'string' then 505error ( err_msg ( version )) 506end 507opts =opts or { strict =false } 508return M . _version ( version , opts . strict ) 509end 510 511setmetatable ( M , { 512--- Returns the current Nvim version. 513---@return vim.Version 514__call =function () 515local version =vim . fn . api_info (). version ---@type vim.Version 516-- Workaround: vim.fn.api_info().version reports "prerelease" as a boolean. 517version . prerelease =version . prerelease and 'dev' or nil 518return setmetatable ( version , Version ) 519end , 520} ) 521 522return M