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

Code before:
  end,
  hide = function(namespace, bufnr)
    local ns = M.get_namespace(namespace)
    if ns.user_data.underline_ns then
      diagnostic_cache_extmarks[bufnr][ns.user_data.underline_ns] = {}
      if api.nvim_buf_is_valid(bufnr) then
        api.nvim_buf_clear_namespace(bufnr, ns.user_data.underline_ns, 0, -1)
      end
    end
  end,
}

--- @param namespace integer
--- @param bufnr integer
--- @param diagnostics table<integer, vim.Diagnostic[]>
--- @param opts vim.diagnostic.Opts.VirtualText
local function render_virtual_text(namespace, bufnr, diagnostics, opts)
  local lnum = api.nvim_win_get_cursor(0)[1] - 1
  local bufferlength = api.nvim_buf_line_count(bufnr)
  api.nvim_buf_clear_namespace(bufnr, namespace, 0, -1)

  for line, line_diagnostics in pairs(diagnostics) do
    local virt_texts = M._get_virt_text_chunks(line_diagnostics, opts)
    local skip = (opts.current_line == true and line ~= lnum)
      or (opts.current_line == false and line == lnum)
      or (opts.current_line == false and line >= bufferlength)

    if virt_texts and not skip then
      api.nvim_buf_set_extmark(bufnr, namespace, line, 0, {
        hl_mode = opts.hl_mode or 'combine',
        virt_text = virt_texts,
        virt_text_pos = opts.virt_text_pos,
        virt_text_hide = opts.virt_text_hide,
        virt_text_win_col = opts.virt_text_win_col,
      })
    end
  end
end

M.handlers.virtual_text = {
  show = function(namespace, bufnr, diagnostics, opts)
    vim.validate('namespace', namespace, 'number')
    vim.validate('bufnr', bufnr, 'number')
    vim.validate('diagnostics', diagnostics, vim.islist, 'a list of diagnostics')
    vim.validate('opts', opts, 'table', true)

    bufnr = vim._resolve_bufnr(bufnr)
    opts = opts or {}

    if not vim.api.nvim_buf_is_loaded(bufnr) then
      return

Review comment (suggestion):
I think that instead of skipping the diagnostic here we should clamp the line as `vim.diagnostic.get` does. That would make the API behavior more consistent (as suggested by https://github.com/neovim/neovim/issues/34013).

Code after:
  end,
  hide = function(namespace, bufnr)
    local ns = M.get_namespace(namespace)
    if ns.user_data.underline_ns then
      diagnostic_cache_extmarks[bufnr][ns.user_data.underline_ns] = {}
      if api.nvim_buf_is_valid(bufnr) then
        api.nvim_buf_clear_namespace(bufnr, ns.user_data.underline_ns, 0, -1)
      end
    end
  end,
}

--- @param namespace integer
--- @param bufnr integer
--- @param diagnostics table<integer, vim.Diagnostic[]>
--- @param opts vim.diagnostic.Opts.VirtualText
local function render_virtual_text(namespace, bufnr, diagnostics, opts)
  local lnum = api.nvim_win_get_cursor(0)[1] - 1
  local buf_len = api.nvim_buf_line_count(bufnr)
  api.nvim_buf_clear_namespace(bufnr, namespace, 0, -1)

  local function should_render(line)
    if
      (line >= buf_len)
      or (opts.current_line == true and line ~= lnum)
      or (opts.current_line == false and line == lnum)
    then
      return false
    end

    return true
  end

  for line, line_diagnostics in pairs(diagnostics) do
    if should_render(line) then
      local virt_texts = M._get_virt_text_chunks(line_diagnostics, opts)
      if virt_texts then
        api.nvim_buf_set_extmark(bufnr, namespace, line, 0, {
          hl_mode = opts.hl_mode or 'combine',
          virt_text = virt_texts,
          virt_text_pos = opts.virt_text_pos,
          virt_text_hide = opts.virt_text_hide,
          virt_text_win_col = opts.virt_text_win_col,
        })
      end
    end
  end
end

M.handlers.virtual_text = {
  show = function(namespace, bufnr, diagnostics, opts)