Repository: neovim/neovim
File: runtime/lua/vim/version.lua (Lua)

Code before:
      else
        to[1] = to[1] + 1
        to[2] = 0
        to[3] = 0
      end
    elseif mods == '^' then
      for i = 1, 3 do
        if to[i] ~= 0 then
          to[i] = to[i] + 1
          for j = i + 1, 3 do
            to[j] = 0
          end
          break
        end
      end
    end
    ---@diagnostic enable: need-check-nil
    return setmetatable({ from = from, to = to }, range_mt)
  end
end

--- Compute common range
---
--- @param r1 vim.VersionRange First range to intersect.
--- @param r2 vim.VersionRange Second range to intersect.
--- @return vim.VersionRange? Biggest range that is present inside both `r1` and `r2`.
---   `nil` if such range does not exist.
function M.intersect(r1, r2)
  assert(getmetatable(r1) == range_mt)
  assert(getmetatable(r2) == range_mt)

  local from = r1.from <= r2.from and r2.from or r1.from
  local to = (r1.to == nil or (r2.to ~= nil and r2.to <= r1.to)) and r2.to or r1.to
  if to == nil or from < to or (from == to and r1:has(from) and r2:has(from)) then
    return setmetatable({ from = from, to = to }, VersionRange)
  end
end

---@param v string|vim.Version
---@return string
local function create_err_msg(v)
  if type(v) == 'string' then
    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
  return string.format('invalid version: %s (%s)', tostring(v), type(v))
end

--- Parses and compares two version objects (the result of |vim.version.parse()|, or
--- specified literally as a `{major, minor, patch}` tuple, e.g. `{1, 0, 3}`).

Review comment (suggestion):
```suggestion
--- @return vim.VersionRange? Maximal range that is present inside both `r1` and `r2`.
```

Code after:
    elseif mods == '~' then
      if #parts >= 2 then
        to[2] = to[2] + 1
        to[3] = 0
      else
        to[1] = to[1] + 1
        to[2] = 0
        to[3] = 0
      end
    elseif mods == '^' then
      for i = 1, 3 do
        if to[i] ~= 0 then
          to[i] = to[i] + 1
          for j = i + 1, 3 do
            to[j] = 0
          end
          break
        end
      end
    end
    ---@diagnostic enable: need-check-nil
    return setmetatable({ from = from, to = to }, range_mt)
  end
end

--- Computes the common range shared by the given ranges.
---
--- @since 14
--- @param r1 vim.VersionRange First range to intersect.
--- @param r2 vim.VersionRange Second range to intersect.
--- @return vim.VersionRange? Maximal range that is present inside both `r1` and `r2`.
---   `nil` if such range does not exist.
function M.intersect(r1, r2)
  assert(getmetatable(r1) == range_mt)
  assert(getmetatable(r2) == range_mt)

  local from = r1.from <= r2.from and r2.from or r1.from
  local to = (r1.to == nil or (r2.to ~= nil and r2.to <= r1.to)) and r2.to or r1.to
  if to == nil or from < to or (from == to and r1:has(from) and r2:has(from)) then
    return setmetatable({ from = from, to = to }, VersionRange)
  end
end

---@param v string|vim.Version
---@return string
local function create_err_msg(v)
  if type(v) == 'string' then
    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