aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/version.lua
diff options
context:
space:
mode:
authorEvgeni Chasnovski <evgeni.chasnovski@gmail.com>2025-06-30 16:08:35 +0300
committerEvgeni Chasnovski <evgeni.chasnovski@gmail.com>2025-06-30 16:08:56 +0300
commitaeb8bca12ddfe741569df622ef0cbc90211a6064 (patch)
treea893f6dc7e1ce067eea56b1c106a1024f685a273 /runtime/lua/vim/version.lua
parent561021bacd8e9f4acc01efe99b03c70e5dd8accb (diff)
downloadrneovim-aeb8bca12ddfe741569df622ef0cbc90211a6064.tar.gz
rneovim-aeb8bca12ddfe741569df622ef0cbc90211a6064.tar.bz2
rneovim-aeb8bca12ddfe741569df622ef0cbc90211a6064.zip
feat(vim.version): make `tostring()` return human-readable version range
Problem: `tostring()` applied to version range doesn't return human-readable text with information about the range. Solution: Add `__tostring()` method.
Diffstat (limited to 'runtime/lua/vim/version.lua')
-rw-r--r--runtime/lua/vim/version.lua16
1 files changed, 13 insertions, 3 deletions
diff --git a/runtime/lua/vim/version.lua b/runtime/lua/vim/version.lua
index e036cf946d..1aa7d4144d 100644
--- a/runtime/lua/vim/version.lua
+++ b/runtime/lua/vim/version.lua
@@ -266,13 +266,23 @@ function VersionRange:has(version)
return version >= self.from and (self.to == nil or version < self.to)
end
+local range_mt = {
+ __index = VersionRange,
+ __tostring = function(self)
+ if not self.to then
+ return '>=' .. tostring(self.from)
+ end
+ return ('%s - %s'):format(tostring(self.from), tostring(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
if spec == '*' or spec == '' then
- return setmetatable({ from = M.parse('0.0.0') }, { __index = VersionRange })
+ return setmetatable({ from = M.parse('0.0.0') }, range_mt)
end
---@type number?
@@ -286,7 +296,7 @@ function M.range(spec) -- Adapted from https://github.com/folke/lazy.nvim
return setmetatable({
from = ra and ra.from,
to = rb and (#parts == 3 and rb.from or rb.to),
- }, { __index = VersionRange })
+ }, range_mt)
end
---@type string, string
local mods, version = spec:lower():match('^([%^=<>~]*)(.*)$')
@@ -332,7 +342,7 @@ function M.range(spec) -- Adapted from https://github.com/folke/lazy.nvim
end
end
---@diagnostic enable: need-check-nil
- return setmetatable({ from = from, to = to }, { __index = VersionRange })
+ return setmetatable({ from = from, to = to }, range_mt)
end
end