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

Code before:

--- Make a map from vim.diagnostic.Severity -> Highlight Name
--- @param base_name string
--- @return table<vim.diagnostic.Severity,string>
local function make_highlight_map(base_name)
  local result = {} --- @type table<vim.diagnostic.Severity,string>
  for k in pairs(diagnostic_severities) do
    local name = severity_invert[k]
    result[k] = ('Diagnostic%s%s%s'):format(base_name, name:sub(1, 1), name:sub(2):lower())
  end

  return result
end

local virtual_text_highlight_map = make_highlight_map('VirtualText')
local virtual_lines_highlight_map = make_highlight_map('VirtualLines')
local underline_highlight_map = make_highlight_map('Underline')
local floating_highlight_map = make_highlight_map('Floating')
local sign_highlight_map = make_highlight_map('Sign')

--- @param diagnostic vim.Diagnostic
--- @return integer lnum
--- @return integer col
--- @return integer end_lnum
--- @return integer end_col
local function get_logical_location(diagnostic)
  local ns = M.get_namespace(diagnostic.namespace)
  local extmark = api.nvim_buf_get_extmark_by_id(
    diagnostic.bufnr,
    ns.user_data.location_ns,
    diagnostic._extmark_id,
    { details = true }
  )

  return extmark[1], extmark[2], extmark[3].end_row, extmark[3].end_col
end

--- @param diagnostics vim.Diagnostic[]
--- @return table<integer,vim.Diagnostic[]>
local function diagnostic_lines(diagnostics)
  if not diagnostics then
    return {}
  end

  local diagnostics_by_line = {} --- @type table<integer,vim.Diagnostic[]>
  for _, diagnostic in ipairs(diagnostics) do
    local lnum = get_logical_location(diagnostic)
    local line_diagnostics = diagnostics_by_line[lnum]
    if not line_diagnostics then
      line_diagnostics = {}
      diagnostics_by_line[lnum] = line_diagnostics

Review comment (suggestion):
Let's stick to "pos" as the nonce for position/location. Jargon creep adds friction.

```suggestion
local function get_logical_pos(diagnostic)
```

Code after:

--- Make a map from vim.diagnostic.Severity -> Highlight Name
--- @param base_name string
--- @return table<vim.diagnostic.Severity,string>
local function make_highlight_map(base_name)
  local result = {} --- @type table<vim.diagnostic.Severity,string>
  for k in pairs(diagnostic_severities) do
    local name = severity_invert[k]
    result[k] = ('Diagnostic%s%s%s'):format(base_name, name:sub(1, 1), name:sub(2):lower())
  end

  return result
end

local virtual_text_highlight_map = make_highlight_map('VirtualText')
local virtual_lines_highlight_map = make_highlight_map('VirtualLines')
local underline_highlight_map = make_highlight_map('Underline')
local floating_highlight_map = make_highlight_map('Floating')
local sign_highlight_map = make_highlight_map('Sign')

--- @param diagnostic vim.Diagnostic
--- @return integer lnum
--- @return integer col
--- @return integer end_lnum
--- @return integer end_col
local function get_logical_pos(diagnostic)
  local ns = M.get_namespace(diagnostic.namespace)
  local extmark = api.nvim_buf_get_extmark_by_id(
    diagnostic.bufnr,
    ns.user_data.location_ns,
    diagnostic._extmark_id,
    { details = true }
  )

  return extmark[1], extmark[2], extmark[3].end_row, extmark[3].end_col
end

--- @param diagnostics vim.Diagnostic[]
--- @return table<integer,vim.Diagnostic[]>
local function diagnostic_lines(diagnostics)
  if not diagnostics then
    return {}
  end

  local diagnostics_by_line = {} --- @type table<integer,vim.Diagnostic[]>
  for _, diagnostic in ipairs(diagnostics) do
    local lnum = get_logical_pos(diagnostic)
    local line_diagnostics = diagnostics_by_line[lnum]
    if not line_diagnostics then
      line_diagnostics = {}
      diagnostics_by_line[lnum] = line_diagnostics