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

Code before:

  local rmethod = lsp._resolve_to_request[method]
  if rmethod then
    if self:_supports_registration(rmethod) then
      local reg = self:_get_registration(rmethod, bufnr)
      return vim.tbl_get(reg or {}, 'registerOptions', 'resolveProvider') or false
    end
  else
    if self:_supports_registration(method) then
      return self:_get_registration(method, bufnr) ~= nil
    end
  end
  return false
end

--- Checks if a client can facilitate a `Provider` for a specific RegistrationMethod
---@param method vim.lsp.protocol.RegistrationMethods
function Client:supports_registrationMethod(method)
  local required_capability = lsp.protocol._request_name_to_server_capability[method]
  if not required_capability then
    return false
  end
  if vim.tbl_get(self.server_capabilities, unpack(required_capability)) then
    return true
  end
  return false
end

--- @private
--- Handles a notification sent by an LSP server by invoking the
--- corresponding handler.
---
--- @param method vim.lsp.protocol.Method.ServerToClient.Notification LSP method name
--- @param params table The parameters for that method.
function Client:_notification(method, params)
  log.trace('notification', method, params)
  local handler = self:_resolve_handler(method)
  if handler then
    -- Method name is provided here for convenience.
    handler(nil, params, { method = method, client_id = self.id })
  end
end

--- @private
--- Handles a request from an LSP server by invoking the corresponding handler.
---
--- @param method (vim.lsp.protocol.Method.ServerToClient) LSP method name
--- @param params (table) The parameters for that method
--- @return any result
--- @return lsp.ResponseError? error code and message set in case an exception happens during the request.
function Client:_server_request(method, params)

Review comment (suggestion):
```suggestion
  return vim.tbl_get(self.server_capabilities, unpack(required_capability)) ~= nil
```

Code after:

  local rmethod = lsp._resolve_to_request[method]
  if rmethod then
    if self:_supports_registration(rmethod) then
      local reg = self:_get_registration(rmethod, bufnr)
      return vim.tbl_get(reg or {}, 'registerOptions', 'resolveProvider') or false
    end
  else
    if self:_supports_registration(method) then
      return self:_get_registration(method, bufnr) ~= nil
    end
  end
  return false
end

--- @private
--- Handles a notification sent by an LSP server by invoking the
--- corresponding handler.
---
--- @param method vim.lsp.protocol.Method.ServerToClient.Notification LSP method name
--- @param params table The parameters for that method.
function Client:_notification(method, params)
  log.trace('notification', method, params)
  local handler = self:_resolve_handler(method)
  if handler then
    -- Method name is provided here for convenience.
    handler(nil, params, { method = method, client_id = self.id })
  end
end

--- @private
--- Handles a request from an LSP server by invoking the corresponding handler.
---
--- @param method (vim.lsp.protocol.Method.ServerToClient) LSP method name
--- @param params (table) The parameters for that method
--- @return any result
--- @return lsp.ResponseError? error code and message set in case an exception happens during the request.
function Client:_server_request(method, params)
  log.trace('server_request', method, params)
  local handler = self:_resolve_handler(method)
  if handler then
    log.trace('server_request: found handler for', method)
    return handler(nil, params, { method = method, client_id = self.id })
  end
  log.warn('server_request: no handler found for', method)
  return nil, lsp.rpc_response_error(lsp.protocol.ErrorCodes.MethodNotFound)
end

--- @private
--- Invoked when the client operation throws an error.
---