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

Code before:
    extmarks_index[namespace] = {}

    for _, extmark in ipairs(extmarks) do
      extmarks_index[namespace][extmark[1]] = extmark
    end

    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 opts._highest then
    filter_highest(diagnostics)
  end

  local line_diagnostics = {} --- @type table<integer,vim.Diagnostic[]>
  for _, diagnostic in ipairs(diagnostics) do
    local namespace = diagnostic.namespace
    assert(namespace, 'namespace must be set')

    local extmark_id = diagnostic.extmark_id
    assert(extmark_id, 'extmark_id must be set')

    local lnum = extmarks_index[namespace][extmark_id][2]

    if not line_diagnostics[lnum] then
      line_diagnostics[lnum] = {}
    end

    table.insert(line_diagnostics[lnum], diagnostic)
  end

  local line_count = api.nvim_buf_line_count(bufnr)
  for i = 0, line_count do
    local offset = i * (search_forward and 1 or -1)
    local lnum = position[1] + offset
    if lnum < 0 or lnum >= line_count then
      if not wrap then
        return
      end
      lnum = (lnum + line_count) % line_count
    end
    if line_diagnostics[lnum] and not vim.tbl_isempty(line_diagnostics[lnum]) then
      local line_length = #api.nvim_buf_get_lines(bufnr, lnum, lnum + 1, true)[1]

Review comment (suggestion):
You can combine the assignment and the `assert`:
```suggestion
    local namespace = diagnostic.namespace, 'namespace must be set')
```

Code after:
    vim.deprecate('opts.win_id', 'opts.winid', '0.13')
    opts.winid = opts.win_id
    opts.win_id = nil --- @diagnostic disable-line
  end

  -- Support deprecated cursor_position alias
  if opts.cursor_position then
    vim.deprecate('opts.cursor_position', 'opts.pos', '0.13')
    opts.pos = opts.cursor_position
    opts.cursor_position = nil --- @diagnostic disable-line
  end

  local winid = opts.winid or api.nvim_get_current_win()
  local bufnr = api.nvim_win_get_buf(winid)
  local position = opts.pos or api.nvim_win_get_cursor(winid)

  -- Adjust row to be 0-indexed
  position[1] = position[1] - 1

  local wrap = if_nil(opts.wrap, true)

  local diagnostics = get_diagnostics(bufnr, opts, true)

  if opts._highest then
    filter_highest(diagnostics)
  end

  local line_diagnostics = diagnostic_lines(diagnostics)

  --- @param diagnostic vim.Diagnostic
  --- @return integer
  local function col(diagnostic)
    return select(2, get_logical_pos(diagnostic))
  end

  local line_count = api.nvim_buf_line_count(bufnr)
  for i = 0, line_count do
    local offset = i * (search_forward and 1 or -1)
    local lnum = position[1] + offset
    if lnum < 0 or lnum >= line_count then
      if not wrap then
        return
      end
      lnum = (lnum + line_count) % line_count
    end
    if line_diagnostics[lnum] and not vim.tbl_isempty(line_diagnostics[lnum]) then
      local line_length = #api.nvim_buf_get_lines(bufnr, lnum, lnum + 1, true)[1]
      --- @type function, function
      local sort_diagnostics, is_next
      if search_forward then
        sort_diagnostics = function(a, b)