Lspconfig: change server configuration after call to setup

Hi! Quick question. Say I have some lspconfig settings for a language server in my config. I start working on a project then realize I need to change some client settings. What’s the best way to do it?

At the moment, this is my approach, but I think there are cleaner ways using client.notify("workspace/didChangeConfiguration”)

local function updateconfig(name, config)
    local clients = vim.lsp.buf_get_clients(0)
    for _, client in pairs(clients) do
        if client.name == name then
            vim.lsp.stop_client(client.id)
             break
        end
    end
    require"lspconfig"[name].setup(config)
    vim.cmd("LspStart " .. name)
end

I was also looking into it yesterday. Came up with this after a bit of research: Neovim - Project Local Config with exrc.nvim • Munif Tanjim

--[[ ~/.config/nvim/lua/config/lsp/custom.lua ]]

local mod = {}

---@param server_name string
---@param settings_patcher fun(settings: table): table
function mod.patch_lsp_settings(server_name, settings_patcher)
  local function patch_settings(client)
    client.config.settings = settings_patcher(client.config.settings)
    client.notify("workspace/didChangeConfiguration", {
      settings = client.config.settings,
    })
  end

  local clients = vim.lsp.get_active_clients({ name = server_name })
  if #clients > 0 then
    patch_settings(clients[1])
    return
  end

  vim.api.nvim_create_autocmd("LspAttach", {
    callback = function(args)
      local client = vim.lsp.get_client_by_id(args.data.client_id)
      if client.name == server_name then
        patch_settings(client)
        return true
      end
    end,
  })
end

return mod

Then use it like this:

--[[ ~/.config/hammerspoon/.nvimrc.lua ]]

require("config.lsp.custom").patch_lsp_settings("sumneko_lua", function(settings)
  settings.Lua.diagnostics.globals = { "hs", "spoon" }

  settings.Lua.workspace.library = {}

  local hammerspoon_emmpylua_annotations = vim.fn.expand("~/.config/hammerspoon/Spoons/EmmyLua.spoon/annotations")
  if vim.fn.isdirectory(hammerspoon_emmpylua_annotations) == 1 then
    table.insert(settings.Lua.workspace.library, hammerspoon_emmpylua_annotations)
  end

  return settings
end)