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

Code before:
          })
        end
      end
    end
  end

  return results
end

--- @class vim.lsp.WorkspaceDiagnosticsOpts
--- @inlinedoc
---
--- Only return diagnostics from the indicated client. If nil, diagnostics from all clients are returned.
--- @field client_id? integer

--- Returns workspace-wide diagnostics.
--- @param opts? vim.lsp.WorkspaceDiagnosticsOpts
--- @see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_dagnostics
function M.workspace_diagnostics(opts)
  vim.validate('opts', opts, 'table', true)

  opts = opts or {}

  local clients = lsp.get_clients({ method = ms.workspace_diagnostic, id = opts.client_id })

  local responses_received = 0

  local function process_reports() end

  --- @param error lsp.ResponseError?
  --- @param result lsp.WorkspaceDiagnosticReport
  --- @param ctx lsp.HandlerContext
  local function handler(error, result, ctx)
    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)
      end
      return
    end

    -- Count this result if there's any other kind of error or a successful response.
    responses_received = responses_received + 1

    if error == nil and result ~= nil then
      for _, report in ipairs(result.items) do
        if report.kind == 'full' then
        end
      end

      if responses_received == #clients then

Review comment (suggestion):
Before anyone starts screaming at me: Yes, I know that I said I would put this under `vim.lsp.buf` but I need to access a lot of local state in this module so I'm putting it here for now. Open to exposing the public entry point somewhere else later.

Code after:
        if buf_client_id == client_id then
          table.insert(results, {
            textDocument = util.make_text_document_params(bufnr),
            previousResultId = 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)
      end
      return
    end

    if error == nil and result ~= nil then
      for _, report in ipairs(result.items) do
        local bufnr = vim.uri_to_bufnr(report.uri)

        -- Start tracking the buffer (but don't send "textDocument/diagnostic" requests for it).
        if not bufstates[bufnr] then
          bufstates[bufnr] = { pull_kind = 'workspace', client_result_id = {} }
        end

        -- We favor document pull requests over workspace results, so only update the buffer
        -- state if we're not pulling document diagnostics for this buffer.
        if bufstates[bufnr].pull_kind == 'workspace' and report.kind == 'full' then
          handle_diagnostics(report.uri, ctx.client_id, report.items, true)
          bufstates[bufnr].client_result_id[ctx.client_id] = report.resultId
        end
      end
    end
  end