version.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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.fn.system({'tmux', '-V'}), {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 is >=1.2.3 <1.3.0 "reasonably close to 1.2.3"
  32. --- ^1.2.3 is >=1.2.3 <2.0.0 "compatible with 1.2.3"
  33. --- ^0.2.3 is >=0.2.3 <0.3.0 (0.x.x is special)
  34. --- ^0.0.1 is =0.0.1 (0.0.x is special)
  35. --- ^1.2 is >=1.2.0 <2.0.0 (like ^1.2.0)
  36. --- ~1.2 is >=1.2.0 <1.3.0 (like ~1.2.0)
  37. --- ^1 is >=1.0.0 <2.0.0 "compatible with 1"
  38. --- ~1 same "reasonably close to 1"
  39. --- 1.x same
  40. --- 1.* same
  41. --- 1 same
  42. --- * any version
  43. --- x same
  44. ---
  45. --- 1.2.3 - 2.3.4 is >=1.2.3 <=2.3.4
  46. ---
  47. --- Partial right: missing pieces treated as x (2.3 => 2.3.x).
  48. --- 1.2.3 - 2.3 is >=1.2.3 <2.4.0
  49. --- 1.2.3 - 2 is >=1.2.3 <3.0.0
  50. ---
  51. --- Partial left: missing pieces treated as 0 (1.2 => 1.2.0).
  52. --- 1.2 - 2.3.0 is 1.2.0 - 2.3.0
  53. --- ```
  54. local M = {}
  55. ---@nodoc
  56. ---@class vim.Version
  57. ---@field [1] number
  58. ---@field [2] number
  59. ---@field [3] number
  60. ---@field major number
  61. ---@field minor number
  62. ---@field patch number
  63. ---@field prerelease? string
  64. ---@field build? string
  65. local Version = {}
  66. Version.__index = Version
  67. --- Compares prerelease strings: per semver, number parts must be must be treated as numbers:
  68. --- "pre1.10" is greater than "pre1.2". https://semver.org/#spec-item-11
  69. ---@param prerel1 string?
  70. ---@param prerel2 string?
  71. local function cmp_prerel(prerel1, prerel2)
  72. if not prerel1 or not prerel2 then
  73. return prerel1 and -1 or (prerel2 and 1 or 0)
  74. end
  75. -- TODO(justinmk): not fully spec-compliant; this treats non-dot-delimited digit sequences as
  76. -- numbers. Maybe better: "(.-)(%.%d*)".
  77. local iter1 = prerel1:gmatch('([^0-9]*)(%d*)')
  78. local iter2 = prerel2:gmatch('([^0-9]*)(%d*)')
  79. while true do
  80. local word1, n1 = iter1() --- @type string?, string|number|nil
  81. local word2, n2 = iter2() --- @type string?, string|number|nil
  82. if word1 == nil and word2 == nil then -- Done iterating.
  83. return 0
  84. end
  85. word1, n1, word2, n2 =
  86. word1 or '', n1 and tonumber(n1) or 0, word2 or '', n2 and tonumber(n2) or 0
  87. if word1 ~= word2 then
  88. return word1 < word2 and -1 or 1
  89. end
  90. if n1 ~= n2 then
  91. return n1 < n2 and -1 or 1
  92. end
  93. end
  94. end
  95. function Version:__index(key)
  96. return type(key) == 'number' and ({ self.major, self.minor, self.patch })[key] or Version[key]
  97. end
  98. function Version:__newindex(key, value)
  99. if key == 1 then
  100. self.major = value
  101. elseif key == 2 then
  102. self.minor = value
  103. elseif key == 3 then
  104. self.patch = value
  105. else
  106. rawset(self, key, value)
  107. end
  108. end
  109. ---@param other vim.Version
  110. function Version:__eq(other)
  111. for i = 1, 3 do
  112. if self[i] ~= other[i] then
  113. return false
  114. end
  115. end
  116. return 0 == cmp_prerel(self.prerelease, other.prerelease)
  117. end
  118. function Version:__tostring()
  119. local ret = table.concat({ self.major, self.minor, self.patch }, '.')
  120. if self.prerelease then
  121. ret = ret .. '-' .. self.prerelease
  122. end
  123. if self.build and self.build ~= vim.NIL then
  124. ret = ret .. '+' .. self.build
  125. end
  126. return ret
  127. end
  128. ---@param other vim.Version
  129. function Version:__lt(other)
  130. for i = 1, 3 do
  131. if self[i] > other[i] then
  132. return false
  133. elseif self[i] < other[i] then
  134. return true
  135. end
  136. end
  137. return -1 == cmp_prerel(self.prerelease, other.prerelease)
  138. end
  139. ---@param other vim.Version
  140. function Version:__le(other)
  141. return self < other or self == other
  142. end
  143. --- @private
  144. ---
  145. --- Creates a new Version object, or returns `nil` if `version` is invalid.
  146. ---
  147. --- @param version string|number[]|vim.Version
  148. --- @param strict? boolean Reject "1.0", "0-x", "3.2a" or other non-conforming version strings
  149. --- @return vim.Version?
  150. function M._version(version, strict) -- Adapted from https://github.com/folke/lazy.nvim
  151. if type(version) == 'table' then
  152. if version.major then
  153. return setmetatable(vim.deepcopy(version, true), Version)
  154. end
  155. return setmetatable({
  156. major = version[1] or 0,
  157. minor = version[2] or 0,
  158. patch = version[3] or 0,
  159. }, Version)
  160. end
  161. if not strict then -- TODO: add more "scrubbing".
  162. --- @cast version string
  163. version = version:match('%d[^ ]*')
  164. end
  165. if version == nil then
  166. return nil
  167. end
  168. local prerel = version:match('%-([^+]*)')
  169. local prerel_strict = version:match('%-([0-9A-Za-z-]*)')
  170. if
  171. strict
  172. and prerel
  173. and (prerel_strict == nil or prerel_strict == '' or not vim.startswith(prerel, prerel_strict))
  174. then
  175. return nil -- Invalid prerelease.
  176. end
  177. local build = prerel and version:match('%-[^+]*%+(.*)$') or version:match('%+(.*)$')
  178. local major, minor, patch =
  179. version:match('^v?(%d+)%.?(%d*)%.?(%d*)' .. (strict and (prerel and '%-' or '$') or ''))
  180. if
  181. (not strict and major)
  182. or (major and minor and patch and major ~= '' and minor ~= '' and patch ~= '')
  183. then
  184. return setmetatable({
  185. major = tonumber(major),
  186. minor = minor == '' and 0 or tonumber(minor),
  187. patch = patch == '' and 0 or tonumber(patch),
  188. prerelease = prerel ~= '' and prerel or nil,
  189. build = build ~= '' and build or nil,
  190. }, Version)
  191. end
  192. return nil -- Invalid version string.
  193. end
  194. ---TODO: generalize this, move to func.lua
  195. ---
  196. ---@generic T: vim.Version
  197. ---@param versions T[]
  198. ---@return T?
  199. function M.last(versions)
  200. local last = versions[1]
  201. for i = 2, #versions do
  202. if versions[i] > last then
  203. last = versions[i]
  204. end
  205. end
  206. return last
  207. end
  208. ---@class vim.VersionRange
  209. ---@inlinedoc
  210. ---@field from vim.Version
  211. ---@field to? vim.Version
  212. local VersionRange = {}
  213. --- @private
  214. ---
  215. ---@param version string|vim.Version
  216. function VersionRange:has(version)
  217. if type(version) == 'string' then
  218. ---@diagnostic disable-next-line: cast-local-type
  219. version = M.parse(version)
  220. elseif getmetatable(version) ~= Version then
  221. -- Need metatable to compare versions.
  222. version = setmetatable(vim.deepcopy(version, true), Version)
  223. end
  224. if version then
  225. if version.prerelease ~= self.from.prerelease then
  226. return false
  227. end
  228. return version >= self.from and (self.to == nil or version < self.to)
  229. end
  230. end
  231. --- Parses a semver |version-range| "spec" and returns a range object:
  232. ---
  233. --- ```
  234. --- {
  235. --- from: Version
  236. --- to: Version
  237. --- has(v: string|Version)
  238. --- }
  239. --- ```
  240. ---
  241. --- `:has()` checks if a version is in the range (inclusive `from`, exclusive `to`).
  242. ---
  243. --- Example:
  244. ---
  245. --- ```lua
  246. --- local r = vim.version.range('1.0.0 - 2.0.0')
  247. --- print(r:has('1.9.9')) -- true
  248. --- print(r:has('2.0.0')) -- false
  249. --- print(r:has(vim.version())) -- check against current Nvim version
  250. --- ```
  251. ---
  252. --- Or use cmp(), le(), lt(), ge(), gt(), and/or eq() to compare a version
  253. --- against `.to` and `.from` directly:
  254. ---
  255. --- ```lua
  256. --- local r = vim.version.range('1.0.0 - 2.0.0') -- >=1.0, <2.0
  257. --- print(vim.version.ge({1,0,3}, r.from) and vim.version.lt({1,0,3}, r.to))
  258. --- ```
  259. ---
  260. --- @see # https://github.com/npm/node-semver#ranges
  261. --- @since 11
  262. ---
  263. --- @param spec string Version range "spec"
  264. --- @return vim.VersionRange?
  265. function M.range(spec) -- Adapted from https://github.com/folke/lazy.nvim
  266. if spec == '*' or spec == '' then
  267. return setmetatable({ from = M.parse('0.0.0') }, { __index = VersionRange })
  268. end
  269. ---@type number?
  270. local hyphen = spec:find(' - ', 1, true)
  271. if hyphen then
  272. local a = spec:sub(1, hyphen - 1)
  273. local b = spec:sub(hyphen + 3)
  274. local parts = vim.split(b, '.', { plain = true })
  275. local ra = M.range(a)
  276. local rb = M.range(b)
  277. return setmetatable({
  278. from = ra and ra.from,
  279. to = rb and (#parts == 3 and rb.from or rb.to),
  280. }, { __index = VersionRange })
  281. end
  282. ---@type string, string
  283. local mods, version = spec:lower():match('^([%^=<>~]*)(.*)$')
  284. version = version:gsub('%.[%*x]', '')
  285. local parts = vim.split(version:gsub('%-.*', ''), '.', { plain = true })
  286. if #parts < 3 and mods == '' then
  287. mods = '~'
  288. end
  289. local semver = M.parse(version)
  290. if semver then
  291. local from = semver --- @type vim.Version?
  292. local to = vim.deepcopy(semver, true) --- @type vim.Version?
  293. ---@diagnostic disable: need-check-nil
  294. if mods == '' or mods == '=' then
  295. to.patch = to.patch + 1
  296. elseif mods == '<' then
  297. from = M._version({})
  298. elseif mods == '<=' then
  299. from = M._version({})
  300. to.patch = to.patch + 1
  301. elseif mods == '>' then
  302. from.patch = from.patch + 1
  303. to = nil
  304. elseif mods == '>=' then
  305. to = nil
  306. elseif mods == '~' then
  307. if #parts >= 2 then
  308. to[2] = to[2] + 1
  309. to[3] = 0
  310. else
  311. to[1] = to[1] + 1
  312. to[2] = 0
  313. to[3] = 0
  314. end
  315. elseif mods == '^' then
  316. for i = 1, 3 do
  317. if to[i] ~= 0 then
  318. to[i] = to[i] + 1
  319. for j = i + 1, 3 do
  320. to[j] = 0
  321. end
  322. break
  323. end
  324. end
  325. end
  326. ---@diagnostic enable: need-check-nil
  327. return setmetatable({ from = from, to = to }, { __index = VersionRange })
  328. end
  329. end
  330. ---@param v string|vim.Version
  331. ---@return string
  332. local function create_err_msg(v)
  333. if type(v) == 'string' then
  334. return string.format('invalid version: "%s"', tostring(v))
  335. elseif type(v) == 'table' and v.major then
  336. return string.format('invalid version: %s', vim.inspect(v))
  337. end
  338. return string.format('invalid version: %s (%s)', tostring(v), type(v))
  339. end
  340. --- Parses and compares two version objects (the result of |vim.version.parse()|, or
  341. --- specified literally as a `{major, minor, patch}` tuple, e.g. `{1, 0, 3}`).
  342. ---
  343. --- Example:
  344. ---
  345. --- ```lua
  346. --- if vim.version.cmp({1,0,3}, {0,2,1}) == 0 then
  347. --- -- ...
  348. --- end
  349. --- local v1 = vim.version.parse('1.0.3-pre')
  350. --- local v2 = vim.version.parse('0.2.1')
  351. --- if vim.version.cmp(v1, v2) == 0 then
  352. --- -- ...
  353. --- end
  354. --- ```
  355. ---
  356. --- @note Per semver, build metadata is ignored when comparing two otherwise-equivalent versions.
  357. --- @since 11
  358. ---
  359. ---@param v1 vim.Version|number[]|string Version object.
  360. ---@param v2 vim.Version|number[]|string Version to compare with `v1`.
  361. ---@return integer -1 if `v1 < v2`, 0 if `v1 == v2`, 1 if `v1 > v2`.
  362. function M.cmp(v1, v2)
  363. local v1_parsed = assert(M._version(v1), create_err_msg(v1))
  364. local v2_parsed = assert(M._version(v2), create_err_msg(v1))
  365. if v1_parsed == v2_parsed then
  366. return 0
  367. end
  368. if v1_parsed > v2_parsed then
  369. return 1
  370. end
  371. return -1
  372. end
  373. ---Returns `true` if the given versions are equal. See |vim.version.cmp()| for usage.
  374. ---@since 11
  375. ---@param v1 vim.Version|number[]|string
  376. ---@param v2 vim.Version|number[]|string
  377. ---@return boolean
  378. function M.eq(v1, v2)
  379. return M.cmp(v1, v2) == 0
  380. end
  381. ---Returns `true` if `v1 <= v2`. See |vim.version.cmp()| for usage.
  382. ---@since 12
  383. ---@param v1 vim.Version|number[]|string
  384. ---@param v2 vim.Version|number[]|string
  385. ---@return boolean
  386. function M.le(v1, v2)
  387. return M.cmp(v1, v2) <= 0
  388. end
  389. ---Returns `true` if `v1 < v2`. See |vim.version.cmp()| for usage.
  390. ---@since 11
  391. ---@param v1 vim.Version|number[]|string
  392. ---@param v2 vim.Version|number[]|string
  393. ---@return boolean
  394. function M.lt(v1, v2)
  395. return M.cmp(v1, v2) == -1
  396. end
  397. ---Returns `true` if `v1 >= v2`. See |vim.version.cmp()| for usage.
  398. ---@since 12
  399. ---@param v1 vim.Version|number[]|string
  400. ---@param v2 vim.Version|number[]|string
  401. ---@return boolean
  402. function M.ge(v1, v2)
  403. return M.cmp(v1, v2) >= 0
  404. end
  405. ---Returns `true` if `v1 > v2`. See |vim.version.cmp()| for usage.
  406. ---@since 11
  407. ---@param v1 vim.Version|number[]|string
  408. ---@param v2 vim.Version|number[]|string
  409. ---@return boolean
  410. function M.gt(v1, v2)
  411. return M.cmp(v1, v2) == 1
  412. end
  413. --- Parses a semantic version string and returns a version object which can be used with other
  414. --- `vim.version` functions. For example "1.0.1-rc1+build.2" returns:
  415. ---
  416. --- ```
  417. --- { major = 1, minor = 0, patch = 1, prerelease = "rc1", build = "build.2" }
  418. --- ```
  419. ---
  420. ---@see # https://semver.org/spec/v2.0.0.html
  421. ---@since 11
  422. ---
  423. ---@param version string Version string to parse.
  424. ---@param opts table|nil Optional keyword arguments:
  425. --- - strict (boolean): Default false. If `true`, no coercion is attempted on
  426. --- input not conforming to semver v2.0.0. If `false`, `parse()` attempts to
  427. --- coerce input such as "1.0", "0-x", "tmux 3.2a" into valid versions.
  428. ---@return vim.Version? parsed_version Version object or `nil` if input is invalid.
  429. function M.parse(version, opts)
  430. assert(type(version) == 'string', create_err_msg(version))
  431. opts = opts or { strict = false }
  432. return M._version(version, opts.strict)
  433. end
  434. setmetatable(M, {
  435. --- Returns the current Nvim version.
  436. ---@return vim.Version
  437. __call = function()
  438. local version = vim.fn.api_info().version ---@type vim.Version
  439. -- Workaround: vim.fn.api_info().version reports "prerelease" as a boolean.
  440. version.prerelease = version.prerelease and 'dev' or nil
  441. return setmetatable(version, Version)
  442. end,
  443. })
  444. return M