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

Code before:
  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._otf_enabled ~= false and client:supports_method(method, ev.buf) 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 given language client. The following is
--- a practical usage example:
---
--- ```lua
--- vim.lsp.start({
---   name = 'ts_ls',
---   cmd = '…',
---   on_attach = function(client)
---     vim.lsp.on_type_formatting.enable(true, { client_id = client.id })
---   end,
--- })
--- ```
---
--- @param enable? boolean true/nil to enable, false to disable.
--- @param filter vim.lsp.on_type_formatting.enable.Filter?
function M.enable(enable, filter)
  vim.validate('enable', enable, 'boolean', true)
  vim.validate('filter', filter, 'table', true)

  enable = enable ~= false
  filter = filter or {}

  if filter.client_id then
    local client =
      assert(lsp.get_client_by_id(filter.client_id), 'Client not found for id ' .. filter.client_id)
    toggle_for_client(enable, client)
  else
    toggle_globally(enable)
  end
end


Review comment (bug):
I might be wrong but I don't think `ts_ls` supports OTF. Pretty sure `rust_analyzer` does though (would be nice to make the example accurate). 

Code after:
  -- 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,
--- })
--- ```
---
--- @param enable? boolean true/nil to enable, false to disable.
--- @param filter vim.lsp.on_type_formatting.enable.Filter?
function M.enable(enable, filter)
  vim.validate('enable', enable, 'boolean', true)
  vim.validate('filter', filter, 'table', true)

  enable = enable ~= false
  filter = filter or {}

  if filter.client_id then
    local client =