Key note categories and examples for tech issues

Toggle autoformat on nvim

This is an example note

I took this note when I wanted to toggle autoformat manually on nvim.

Lspconfig:

      -- options for vim.lsp.buf.format
      -- `bufnr` and `filter` is handled by the LazyVim formatter,
      -- but can be also overridden when specified
      format = {
        formatting_options = nil,
        timeout_ms = nil,
      },

...

      -- setup autoformat
      Util.format.register(Util.lsp.formatter())

      -- deprectaed options
      if opts.autoformat ~= nil then
        vim.g.autoformat = opts.autoformat
        Util.deprecate("nvim-lspconfig.opts.autoformat", "vim.g.autoformat")
      end

Util.lsp.formatter: https://github.com/LazyVim/LazyVim/blob/879e29504d43e9f178d967ecc34d482f902e5a91/lua/lazyvim/util/lsp.lua#L79

Found Util.formatter.toggle function!

Let's try:

local Util = require("lazyvim.util")
Util.format.toggle()

It works!

Let's make a command:

local M = {}
local LazyvimUtil = require("lazyvim.util")

function M.toggleAutoformat()
  LazyvimUtil.format.toggle()
end

Get help

Got any questions about this page? Feel free to ask them on the user forum here.