/ nvim / lua / plugins / conform.lua
conform.lua
 1  --- Conform: format on save.
 2  return {
 3    specs = {
 4      { src = 'https://github.com/stevearc/conform.nvim', name = 'conform.nvim' },
 5    },
 6    config = function()
 7      local conform = require('conform')
 8      if type(conform) ~= 'table' or not conform.setup or not conform.format then
 9        return
10      end
11      conform.setup({
12        formatters_by_ft = {
13          bash = { 'prettierd' },
14          go = { 'goimports', 'gofmt' },
15          lua = { 'stylua' },
16          markdown = { 'prettierd' },
17          python = { 'ruff_format' },
18          yaml = { 'yamlfmt' },
19          zig = { 'zigfmt' },
20          toml = { 'taplo' },
21          sh = { 'shfmt' },
22          ['_'] = { 'trim_whitespace' },
23        },
24      })
25      vim.api.nvim_create_autocmd('BufWritePre', {
26        callback = function(args)
27          if vim.g.autoformat == false then return end
28          if vim.b[args.buf].autoformat == false then return end
29          pcall(conform.format, { bufnr = args.buf, async = false })
30        end,
31      })
32    end,
33  }