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

Code before:
  local client_id = client.id

  -- Toggle for buffers already attached.
  for bufnr, _ in pairs(client.attached_buffers) do
    handler(client, bufnr)
  end

  -- If disabling, only clear the attachment autocmd. If enabling, create it.
  local group = api.nvim_create_augroup(get_client_augroup(client_id), { clear = true })
  if enable then
    api.nvim_create_autocmd('LspAttach', {
      group = group,
      desc = 'Enable on-type formatting for all buffers this client attaches to',
      callback = function(ev)
        if ev.data.client_id ~= client_id then
          return
        end

        attach_otf(client, ev.buf)
      end,
    })
  end
end

---@param enable boolean
local function toggle_otf_globally(enable)
  -- Toggle for clients that have already attached.
  local clients = lsp.get_clients({ method = method })
  for _, client in ipairs(clients) do
    toggle_otf_for_client(enable, client)
  end

  -- If disabling, only clear the attachment autocmd. If enabling, create it.
  local group = api.nvim_create_augroup('nvim.lsp.on_type_formatting', { clear = true })
  if enable then
    api.nvim_create_autocmd('LspAttach', {
      group = group,
      desc = 'Enable on-type formatting for all clients',
      callback = function(ev)
        local client = assert(lsp.get_client_by_id(ev.data.client_id))
        if client:supports_method(method, ev.buf) then
          attach_otf(client, ev.buf)
        end
      end,
    })
  end
end

--- Optional filters |kwargs|:
--- @inlinedoc
--- @class vim.lsp.on_type_formatting.enable.Filter

Review comment (suggestion):
As this is a local function, the context already implies this is `otf`.


```suggestion
local function toggle_globally(enable)
```

Same for attach and detach.

Code after:
    handler(client, bufnr)
  end

  client._otf_enabled = enable
end

---@param enable boolean
local function toggle_globally(enable)
  -- Toggle for clients that have already attached.
  local clients = lsp.get_clients({ method = method })
  for _, client in ipairs(clients) do
    toggle_for_client(enable, client)
  end

  -- If disabling, only clear the attachment autocmd. If enabling, create it as well.
  local group = api.nvim_create_augroup('nvim.lsp.on_type_formatting', { clear = true })
  if enable then
    api.nvim_create_autocmd('LspAttach', {
      group = group,
      desc = 'Enable on-type formatting for ALL clients by default.',
      callback = function(ev)
        local client = assert(lsp.get_client_by_id(ev.data.client_id))
        if client._otf_enabled ~= false then
          attach(client, ev.buf)
        end
      end,
    })
  end
end

--- Optional filters |kwargs|:
--- @inlinedoc
--- @class vim.lsp.on_type_formatting.enable.Filter
--- @field client_id integer? Client ID, or `nil` for all.

--- Enables/disables on-type formatting globally or for the {filter}ed scope. The following are some
--- practical usage examples:
---
--- ```lua
--- -- Enable for all clients
--- vim.lsp.on_type_formatting.enable()
---
--- -- Enable for a specific client
--- vim.api.nvim_create_autocmd('LspAttach', {
---   callback = function(args)
---     local client_id = args.data.client_id
---     local client = assert(vim.lsp.get_client_by_id(client_id))
---     if client.name == 'rust-analyzer' then
---       vim.lsp.on_type_formatting.enable(true, { client_id = client_id })
---     end
---   end,