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

Code before:

    local extmarks =
      api.nvim_buf_get_extmarks(bufnr, ns.user_data.location_ns, extmarks_start, extmarks_end, {
        details = true,
        overlap = true,
      })

    local filtered_diagnostics =
      filter_by_severity(opts.severity, diagnostic_cache[bufnr][namespace])

    for _, diagnostic in ipairs(filtered_diagnostics) do
      for _, extmark in ipairs(extmarks) do
        if extmark[1] == diagnostic.extmark_id then
          table.insert(diagnostics, diagnostic)
        end
      end
    end
  end

  if vim.tbl_isempty(diagnostics) then
    return
  end

  local severity_sort = if_nil(opts.severity_sort, global_diagnostic_options.severity_sort)
  if severity_sort then
    if type(severity_sort) == 'table' and severity_sort.reverse then
      table.sort(diagnostics, function(a, b)
        return a.severity > b.severity
      end)
    else
      table.sort(diagnostics, function(a, b)
        return a.severity < b.severity
      end)
    end
  end

  local lines = {} --- @type string[]
  local highlights = {} --- @type table[]
  local header = if_nil(opts.header, 'Diagnostics:')
  if header then
    vim.validate('header', header, { 'string', 'table' }, "'string' or 'table'")
    if type(header) == 'table' then
      -- Don't insert any lines for an empty string
      if string.len(if_nil(header[1], '')) > 0 then
        table.insert(lines, header[1])
        table.insert(highlights, { hlname = header[2] or 'Bold' })
      end
    elseif #header > 0 then
      table.insert(lines, header)
      table.insert(highlights, { hlname = 'Bold' })
    end

Review comment (suggestion):
should `type(opts.namespace) == 'table'` case be supported?

Neither types nor docs seem to allow it, but implicitly it has been supported since
`get_diagnostics` does

Code after:
    error("Invalid value for option 'scope'")
  end

  local diagnostics = get_diagnostics(bufnr, opts --[[@as vim.diagnostic.GetOpts]], true)

  if scope == 'line' then
    --- @param d vim.Diagnostic
    diagnostics = vim.tbl_filter(function(d)
      local d_lnum, _, d_end_lnum, d_end_col = get_logical_pos(d)

      return lnum >= d_lnum
        and lnum <= d_end_lnum
        and (d_lnum == d_end_lnum or lnum ~= d_end_lnum or d_end_col ~= 0)
    end, diagnostics)
  elseif scope == 'cursor' then
    -- If `col` is past the end of the line, show if the cursor is on the last char in the line
    local line_length = #api.nvim_buf_get_lines(bufnr, lnum, lnum + 1, true)[1]
    --- @param d vim.Diagnostic
    diagnostics = vim.tbl_filter(function(d)
      local d_lnum, d_col, d_end_lnum, d_end_col = get_logical_pos(d)

      return lnum >= d_lnum
        and lnum <= d_end_lnum
        and (lnum ~= d_lnum or col >= math.min(d_col, line_length - 1))
        and ((d_lnum == d_end_lnum and d_col == d_end_col) or lnum ~= d_end_lnum or col < d_end_col)
    end, diagnostics)
  end

  if vim.tbl_isempty(diagnostics) then
    return
  end

  local severity_sort = if_nil(opts.severity_sort, global_diagnostic_options.severity_sort)
  if severity_sort then
    if type(severity_sort) == 'table' and severity_sort.reverse then
      table.sort(diagnostics, function(a, b)
        return a.severity > b.severity
      end)
    else
      table.sort(diagnostics, function(a, b)
        return a.severity < b.severity
      end)
    end
  end

  local lines = {} --- @type string[]
  local highlights = {} --- @type { hlname: string, prefix?: { length: integer, hlname: string? }, suffix?: { length: integer, hlname: string? } }[]
  local header = if_nil(opts.header, 'Diagnostics:')
  if header then
    vim.validate('header', header, { 'string', 'table' }, "'string' or 'table'")
    if type(header) == 'table' then