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

Code before:

--- Cancels a request with a given request id.
---
--- @param id integer id of request to cancel
--- @return boolean status indicating if the notification was successful.
--- @see |Client:notify()|
function Client:cancel_request(id)
  self:_process_request(id, 'cancel')
  return self.rpc.notify(ms.dollar_cancelRequest, { id = id })
end

--- Stops a client, optionally with force.
---
--- By default, it will just request the server to shutdown without force. If
--- you request to stop a client which has previously been requested to
--- shutdown, it will automatically escalate and force shutdown.
---
--- @param force? boolean
function Client:stop(force)
  if self.is_stopped() then
    return
  end
  self.is_stopping = true
  local rpc = self.rpc

  if rpc.is_closing() then
    return
  end

  vim.lsp._watchfiles.cancel(self.id)

  if force or not self.initialized or self._graceful_shutdown_failed then
    rpc.terminate()
    return
  end

  -- Sending a signal after a process has exited is acceptable.
  rpc.request(ms.shutdown, nil, function(err, _)
    if err == nil then
      rpc.notify(ms.exit)
    else
      -- If there was an error in the shutdown request, then term to be safe.
      rpc.terminate()
      self._graceful_shutdown_failed = true
    end
  end)
end

--- Get options for a method that is registered dynamically.
--- @param method vim.lsp.protocol.Method
function Client:_supports_registration(method)

Review comment (refactor):
since is_stopped() checks both, we can simplify this code.

```suggestion
  if self.is_stopped() then
    return
  end
  
  self.is_stopping = true
  local rpc = self.rpc
```

Code after:

--- Cancels a request with a given request id.
---
--- @param id integer id of request to cancel
--- @return boolean status indicating if the notification was successful.
--- @see |Client:notify()|
function Client:cancel_request(id)
  self:_process_request(id, 'cancel')
  return self.rpc.notify(ms.dollar_cancelRequest, { id = id })
end

--- Stops a client, optionally with force.
---
--- By default, it will just request the server to shutdown without force. If
--- you request to stop a client which has previously been requested to
--- shutdown, it will automatically escalate and force shutdown.
---
--- @param force? boolean
function Client:stop(force)
  if self:is_stopped() then
    return
  end

  self._is_stopping = true
  local rpc = self.rpc

  vim.lsp._watchfiles.cancel(self.id)

  if force or not self.initialized or self._graceful_shutdown_failed then
    rpc.terminate()
    return
  end

  -- Sending a signal after a process has exited is acceptable.
  rpc.request(ms.shutdown, nil, function(err, _)
    if err == nil then
      rpc.notify(ms.exit)
    else
      -- If there was an error in the shutdown request, then term to be safe.
      rpc.terminate()
      self._graceful_shutdown_failed = true
    end
  end)
end

--- Get options for a method that is registered dynamically.
--- @param method vim.lsp.protocol.Method
function Client:_supports_registration(method)
  local capability_path = lsp.protocol._request_name_to_client_capability[method] or {}
  local capability = vim.tbl_get(self.capabilities, unpack(capability_path))
  return type(capability) == 'table' and capability.dynamicRegistration