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

Code before:
---@field bufnr? integer
---
--- Client ID, or nil for all
---@field client_id? integer

---@param feature string
---@param filter? vim.lsp.enable.Filter
function M._is_enabled(feature, filter)
  vim.validate('feature', feature, 'string')
  vim.validate('filter', filter, 'table', true)

  filter = filter or {}
  local bufnr = filter.bufnr
  local client_id = filter.client_id

  local var = make_enable_var(feature)
  local client_var = make_enable_var(feature, client_id)
  return vim.F.if_nil(client_id and vim.g[client_var], vim.g[var])
    and vim.F.if_nil(bufnr and vim.b[bufnr][var], vim.g[var])
end

---@param feature string
---@param enable? boolean
---@param filter? vim.lsp.enable.Filter
function M._enable(feature, enable, filter)
  vim.validate('feature', feature, 'string')
  vim.validate('enable', enable, 'boolean', true)
  vim.validate('filter', filter, 'table', true)

  enable = enable == nil or enable
  filter = filter or {}
  local bufnr = filter.bufnr
  local client_id = filter.client_id
  assert(
    not (bufnr and client_id),
    'Only one of `bufnr` or `client_id` filters can be specified at a time.'
  )

  local var = make_enable_var(feature)
  local client_var = make_enable_var(feature, client_id)

  if client_id then
    if enable == vim.g[var] then
      vim.g[client_var] = nil
    else
      vim.g[client_var] = enable
    end
  elseif bufnr then
    if enable == vim.g[var] then
      vim.b[bufnr][var] = nil
    else

Review comment (suggestion):
For better completion and type checking, it would be nice to make this an enum of string literals.

Code after:
---@param client_id? integer
local function make_enable_var(feature, client_id)
  return ('_lsp_enabled_%s%s'):format(feature, client_id and ('_client_%d'):format(client_id) or '')
end

---@class vim.lsp.enable.Filter
---@inlinedoc
---
--- Buffer number, or 0 for current buffer, or nil for all.
---@field bufnr? integer
---
--- Client ID, or nil for all
---@field client_id? integer

---@param feature string
---@param filter? vim.lsp.enable.Filter
function M._is_enabled(feature, filter)
  vim.validate('feature', feature, 'string')
  vim.validate('filter', filter, 'table', true)

  filter = filter or {}
  local bufnr = filter.bufnr
  local client_id = filter.client_id

  local var = make_enable_var(feature)
  local client_var = make_enable_var(feature, client_id)
  return vim.F.if_nil(client_id and vim.g[client_var], vim.g[var])
    and vim.F.if_nil(bufnr and vim.b[bufnr][var], vim.g[var])
end

---@param feature 'semantic_tokens'
---@param enable? boolean
---@param filter? vim.lsp.enable.Filter
function M._enable(feature, enable, filter)
  vim.validate('feature', feature, 'string')
  vim.validate('enable', enable, 'boolean', true)
  vim.validate('filter', filter, 'table', true)

  enable = enable == nil or enable
  filter = filter or {}
  local bufnr = filter.bufnr
  local client_id = filter.client_id
  assert(
    not (bufnr and client_id),
    'Only one of `bufnr` or `client_id` filters can be specified at a time.'
  )

  local var = make_enable_var(feature)
  local client_var = make_enable_var(feature, client_id)

  if client_id then