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

Code before:
      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
    else
      return true
    end
  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)
    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)


Review comment (suggestion):
```suggestion
    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
```

Code after:
      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)
    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)