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

Code before:
---
--- Copy this sample code into `lua/foo/health.lua`, replacing "foo" in the path
--- with your plugin name: >lua
---
---         local M = {}
---
---         M.check = function()
---           vim.health.start("foo report")
---           -- make sure setup function parameters are ok
---           if check_setup() then
---             vim.health.ok("Setup is correct")
---           else
---             vim.health.error("Setup is incorrect")
---           end
---           -- do some more checking
---           -- ...
---         end
---
---         return M
---</pre>

local M = {}

local s_output = {} ---@type string[]
---@type { ['ok']: number?, ['warn']: number?, ['error']: number? }
local check_summary = {}

-- From a path return a list [{name}, {func}, {type}] representing a healthcheck
local function filepath_to_healthcheck(path)
  path = vim.fs.abspath(vim.fs.normalize(path))
  local name --- @type string
  local func --- @type string
  local filetype --- @type string
  if path:find('vim$') then
    name = vim.fs.basename(path):gsub('%.vim$', '')
    func = 'health#' .. name .. '#check'
    filetype = 'v'
  else
    local rtp_lua = vim
      .iter(vim.api.nvim_get_runtime_file('lua/', true))
      :map(function(rtp_lua)
        return vim.fs.abspath(vim.fs.normalize(rtp_lua))
      end)
      :find(function(rtp_lua)
        return vim.fs.relpath(rtp_lua, path)
      end)
    -- "/path/to/rtp/lua/foo/bar/health.lua" => "foo/bar/health.lua"
    -- "/another/rtp/lua/baz/health/init.lua" => "baz/health/init.lua"
    local subpath = path:gsub('^' .. vim.pesc(rtp_lua), ''):gsub('^/+', '')
    if vim.fs.basename(subpath) == 'health.lua' then
      -- */health.lua

Review comment (suggestion):
wouldn't need the `@type` if you define the table here. also eliminates the `or 0` in `M.ok()` etc.

```suggestion
local check_summary = {
  ok = 0,
  ...
}
```

Code after:
---
--- Copy this sample code into `lua/foo/health.lua`, replacing "foo" in the path
--- with your plugin name: >lua
---
---         local M = {}
---
---         M.check = function()
---           vim.health.start("foo report")
---           -- make sure setup function parameters are ok
---           if check_setup() then
---             vim.health.ok("Setup is correct")
---           else
---             vim.health.error("Setup is incorrect")
---           end
---           -- do some more checking
---           -- ...
---         end
---
---         return M
---</pre>

local M = {}

local s_output = {} ---@type string[]
local check_summary = { warn = 0, error = 0 }

-- From a path return a list [{name}, {func}, {type}] representing a healthcheck
local function filepath_to_healthcheck(path)
  path = vim.fs.abspath(vim.fs.normalize(path))
  local name --- @type string
  local func --- @type string
  local filetype --- @type string
  if path:find('vim$') then
    name = vim.fs.basename(path):gsub('%.vim$', '')
    func = 'health#' .. name .. '#check'
    filetype = 'v'
  else
    local rtp_lua = vim
      .iter(vim.api.nvim_get_runtime_file('lua/', true))
      :map(function(rtp_lua)
        return vim.fs.abspath(vim.fs.normalize(rtp_lua))
      end)
      :find(function(rtp_lua)
        return vim.fs.relpath(rtp_lua, path)
      end)
    -- "/path/to/rtp/lua/foo/bar/health.lua" => "foo/bar/health.lua"
    -- "/another/rtp/lua/baz/health/init.lua" => "baz/health/init.lua"
    local subpath = path:gsub('^' .. vim.pesc(rtp_lua), ''):gsub('^/+', '')
    if vim.fs.basename(subpath) == 'health.lua' then
      -- */health.lua
      name = vim.fs.dirname(subpath)