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

Code before:
      local clients = lsp.get_clients({ bufnr = bufnr, method = ms.textDocument_diagnostic })

      if
        not vim.iter(clients):any(function(c)
          return c.id ~= args.data.client_id
        end)
      then
        disable(bufnr)
      end
    end,
    group = augroup,
  })
end

--- Returns the result IDs from the reports provided by the given client.
--- @return lsp.PreviousResultId[]
local function previous_result_ids(client_id)
  local results = {}

  for bufnr, state in pairs(bufstates) do
    if state.pull_kind ~= 'disabled' then
      for buf_client_id, result_id in pairs(state.client_result_id) do
        if buf_client_id == client_id then
          table.insert(results, {
            uri = vim.uri_from_bufnr(bufnr),
            value = result_id,
          })
          break
        end
      end
    end
  end

  return results
end

--- Request workspace-wide diagnostics.
--- @param opts vim.lsp.WorkspaceDiagnosticsOpts
function M._workspace_diagnostics(opts)
  local clients = lsp.get_clients({ method = ms.workspace_diagnostic, id = opts.client_id })

  --- @param error lsp.ResponseError?
  --- @param result lsp.WorkspaceDiagnosticReport
  --- @param ctx lsp.HandlerContext
  local function handler(error, result, ctx)
    -- Check for retrigger requests on cancellation errors.
    -- Unless `retriggerRequest` is explicitly disabled, try again.
    if error ~= nil and error.code == lsp.protocol.ErrorCodes.ServerCancelled then
      if error.data == nil or error.data.retriggerRequest ~= false then
        local client = assert(lsp.get_client_by_id(ctx.client_id))
        client:request(ms.workspace_diagnostic, ctx.params, handler)

Review comment (suggestion):
Let's use a type annotation to validate this from the LSP:
```lua
local function previous_result_ids(client_id)
  local results = {} ---@type lsp.PreviousResultId[]

  for bufnr, state in pairs(bufstates) do
    if state.pull_kind ~= 'disabled' then
      for buf_client_id, result_id in pairs(state.client_result_id) do
        if buf_client_id == client_id then
          results[#results + 1] = {
            uri = vim.uri_from_bufnr(bufnr),
            value = result_id,
          }
          break
        end
      end
    end
  end

  return results
end
```

Code after:
      local clients = lsp.get_clients({ bufnr = bufnr, method = ms.textDocument_diagnostic })

      if
        not vim.iter(clients):any(function(c)
          return c.id ~= args.data.client_id
        end)
      then
        disable(bufnr)
      end
    end,
    group = augroup,
  })
end

--- Returns the result IDs from the reports provided by the given client.
--- @return lsp.PreviousResultId[]
local function previous_result_ids(client_id)
  local results = {} ---@type lsp.PreviousResultId[]

  for bufnr, state in pairs(bufstates) do
    if state.pull_kind ~= 'disabled' then
      for buf_client_id, result_id in pairs(state.client_result_id) do
        if buf_client_id == client_id then
          results[#results + 1] = {
            uri = vim.uri_from_bufnr(bufnr),
            value = result_id,
          }
          break
        end
      end
    end
  end

  return results
end

--- Request workspace-wide diagnostics.
--- @param opts vim.lsp.WorkspaceDiagnosticsOpts
function M._workspace_diagnostics(opts)
  local clients = lsp.get_clients({ method = ms.workspace_diagnostic, id = opts.client_id })

  --- @param error lsp.ResponseError?
  --- @param result lsp.WorkspaceDiagnosticReport
  --- @param ctx lsp.HandlerContext
  local function handler(error, result, ctx)
    -- Check for retrigger requests on cancellation errors.
    -- Unless `retriggerRequest` is explicitly disabled, try again.
    if error ~= nil and error.code == lsp.protocol.ErrorCodes.ServerCancelled then
      if error.data == nil or error.data.retriggerRequest ~= false then
        local client = assert(lsp.get_client_by_id(ctx.client_id))
        client:request(ms.workspace_diagnostic, ctx.params, handler)