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

Code before:
      else
        vim.notify(("No configs for filetype '%s'"):format(filetype), vim.log.levels.ERROR)
      end
      return
    end
  end

  checked_enable(config_names)
end

--- @param config_names string[]
local function ex_lsp_disable(config_names)
  -- Default to disabling all clients attached to the current buffer.
  if #config_names == 0 then
    config_names = vim
      .iter(lsp.get_clients { bufnr = api.nvim_get_current_buf() })
      :map(function(client)
        return client.name
      end)
      :filter(function(name)
        return lsp.config[name] ~= nil
      end)
      :totable()
    if #config_names == 0 then
      vim.notify('No configs with clients attached to current buffer', vim.log.levels.ERROR)
      return
    end
  end

  checked_enable(config_names, false)
end

--- @param client_names string[]
--- @return vim.lsp.Client[]
local function get_clients_from_names(client_names)
  -- Default to stopping all active clients attached to the current buffer.
  if #client_names == 0 then
    local clients = lsp.get_clients { bufnr = api.nvim_get_current_buf() }
    if #clients == 0 then
      vim.notify('No clients attached to current buffer', vim.log.levels.ERROR)
    end
    return clients
  else
    return vim
      .iter(client_names)
      :map(function(name)
        local clients = lsp.get_clients { name = name }
        if #clients == 0 then
          vim.notify(("No active clients named '%s'"):format(name), vim.log.levels.ERROR)
        end
        return clients

Review comment (bug):
Just use `error()`. That avoids the need for `return`, and signals to any potential caller that it should fail. Otherwise the caller has no idea that something failed (although this is not intended for programmatic use, but still a good practice).

Better presentation of `error()` will be enabled by `:help vim._extui`, which will eventually be the default. So we should get in the habit of signaling errors as errors. No more workarounds with `vim.notify`.

Code after:
      end
    end
    if #config_names == 0 then
      if filetype == '' then
        echo_err('Current buffer has no filetype')
      else
        echo_err(("No configs for filetype '%s'"):format(filetype))
      end
      return
    end
  end

  checked_enable(config_names)
end

--- @param config_names string[]
local function ex_lsp_disable(config_names)
  -- Default to disabling all clients attached to the current buffer.
  if #config_names == 0 then
    config_names = vim
      .iter(lsp.get_clients { bufnr = api.nvim_get_current_buf() })
      :map(function(client)
        return client.name
      end)
      :filter(function(name)
        return lsp.config[name] ~= nil
      end)
      :totable()
    if #config_names == 0 then
      echo_err('No configs with clients attached to current buffer')
      return
    end
  end

  checked_enable(config_names, false)
end

--- @param client_names string[]
--- @return vim.lsp.Client[]
local function get_clients_from_names(client_names)
  -- Default to stopping all active clients attached to the current buffer.
  if #client_names == 0 then
    local clients = lsp.get_clients { bufnr = api.nvim_get_current_buf() }
    if #clients == 0 then
      echo_err('No clients attached to current buffer')
    end
    return clients
  else
    return vim
      .iter(client_names)
      :map(function(name)