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

Code before:
  local rv = vim.tbl_keys(unique)
  table.sort(rv)
  return rv
end

--- Gets the results heading for the current report section.
---
---@return string
local function get_summary()
  local s = ''
  local errors = check_summary['error']
  local warns = check_summary['warn']

  s = s .. (warns > 0 and (' %2d ⚠️'):format(warns) or '')
  s = s .. (errors > 0 and (' %2d ❌'):format(errors) or '')
  if errors == 0 and warns == 0 then
    s = s .. '✅'
  end

  return s
end

--- Runs the specified healthchecks.
--- Runs all discovered healthchecks if plugin_names is empty.
---
--- @param mods string command modifiers that affect splitting a window.
--- @param plugin_names string glob of plugin names, split on whitespace. For example, using
---                            `:checkhealth vim.* nvim` will healthcheck `vim.lsp`, `vim.treesitter`
---                            and `nvim` modules.
function M._check(mods, plugin_names)
  local healthchecks = plugin_names == '' and get_healthcheck('*') or get_healthcheck(plugin_names)

  local emptybuf = vim.fn.bufnr('$') == 1 and vim.fn.getline(1) == '' and 1 == vim.fn.line('$')

  local bufnr ---@type integer
  if vim.tbl_get(vim.g, 'health', 'style') == 'float' then
    local available_lines = vim.o.lines - 12
    local max_height = math.min(math.floor(vim.o.lines * 0.8), available_lines)
    local max_width = 80
    local float_winid
    bufnr, float_winid = vim.lsp.util.open_floating_preview({}, '', {
      height = max_height,
      width = max_width,
      offset_x = math.floor((vim.o.columns - max_width) / 2),
      offset_y = math.floor((available_lines - max_height) / 2),
      relative = 'editor',
      close_events = {},
    })
    vim.api.nvim_set_current_win(float_winid)
    vim.bo[bufnr].modifiable = true
    vim.wo[float_winid].list = false

Review comment (suggestion):
We should probably put this in `_core/util.lua` , so we can start reusing it and getting a feel for what a public api might look like. And maybe it can be re-used from `pack.lua` which has a similar thing.

Code after:
  local rv = vim.tbl_keys(unique)
  table.sort(rv)
  return rv
end

--- Gets the results heading for the current report section.
---
---@return string
local function get_summary()
  local s = ''
  local errors = check_summary['error']
  local warns = check_summary['warn']

  s = s .. (warns > 0 and (' %2d ⚠️'):format(warns) or '')
  s = s .. (errors > 0 and (' %2d ❌'):format(errors) or '')
  if errors == 0 and warns == 0 then
    s = s .. '✅'
  end

  return s
end

---Emit progress messages
---@param len integer
---@return fun(status: 'success'|'running', idx: integer, fmt: string, ...: any): nil
local function progress_report(len)
  local progress = { kind = 'progress', title = 'checkhealth' }

  return function(status, idx, fmt, ...)
    progress.status = status
    progress.percent = status == 'success' and nil or math.floor(idx / len * 100)
    -- percent=0 omits the reporting of percentage, so use 1% instead
    -- progress.percent = progress.percent == 0 and 1 or progress.percent
    progress.id = vim.api.nvim_echo({ { fmt:format(...) } }, false, progress)
    -- extui/ui2 shows all messages at once after the healthchecks are finished.
    -- This 1ms wait ensures the messages are shown separately
    vim.wait(1)
  end
end

--- Runs the specified healthchecks.
--- Runs all discovered healthchecks if plugin_names is empty.
---
--- @param mods string command modifiers that affect splitting a window.
--- @param plugin_names string glob of plugin names, split on whitespace. For example, using
---                            `:checkhealth vim.* nvim` will healthcheck `vim.lsp`, `vim.treesitter`
---                            and `nvim` modules.
function M._check(mods, plugin_names)
  local healthchecks = plugin_names == '' and get_healthcheck('*') or get_healthcheck(plugin_names)

  local emptybuf = vim.fn.bufnr('$') == 1 and vim.fn.getline(1) == '' and 1 == vim.fn.line('$')