aboutsummaryrefslogtreecommitdiffstats
path: root/nvim/.config/nvim/lua/eugenetriguba/plugins/conform.lua
blob: d1777590e41741e283c4a3b334ac69026f5a73cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
vim.pack.add({
  {
    name = 'conform',
    src = 'https://github.com/stevearc/conform.nvim',
  },
}, { load = false })

vim.api.nvim_create_autocmd('VimEnter', {
  once = true,
  callback = function()
    local prettier_fmt = { 'prettierd', 'prettier', stop_after_first = true }
    require('conform').setup {
      notify_on_error = true,
      format_on_save = function(bufnr)
        if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
          return
        end
        return {
          timeout_ms = 500,
          lsp_format = 'fallback',
        }
      end,
      formatters_by_ft = {
        lua = { 'stylua' },
        go = { 'goimports', 'gofmt' },
        python = { 'isort', 'black' },
        rust = { 'rustfmt' },
        javascript = prettier_fmt,
        typescript = prettier_fmt,
        javascriptreact = prettier_fmt,
        typescriptreact = prettier_fmt,
        css = prettier_fmt,
        htmldjango = prettier_fmt,
        html = prettier_fmt,
        sh = { 'shfmt' },
        cpp = { 'clang-format' },
        c = { 'clang-format' },
        tf = { 'terraformfmt' },
      },
      formatters = {
        isort = {
          prepend_args = { '--profile', 'black' },
        },
      },
    }
  end,
})

-- Format current buffer
vim.keymap.set({ 'n', 'v' }, '<leader>f', function()
  require('conform').format { async = true, lsp_format = 'fallback' }
end, { desc = '[F]ormat buffer' })

-- Toggle autoformat for current buffer
vim.keymap.set('n', '<leader>tf', function()
  if vim.b.disable_autoformat then
    vim.cmd 'FormatEnable'
    vim.notify 'Enabled autoformat for current buffer'
  else
    vim.cmd 'FormatDisable!'
    vim.notify 'Disabled autoformat for current buffer'
  end
end, { desc = 'Toggle autoformat for current buffer' })

-- Toggle autoformat globally
vim.keymap.set('n', '<leader>tF', function()
  if vim.g.disable_autoformat then
    vim.cmd 'FormatEnable'
    vim.notify 'Enabled autoformat globally'
  else
    vim.cmd 'FormatDisable'
    vim.notify 'Disabled autoformat globally'
  end
end, { desc = 'Toggle autoformat globally' })

-- :FormatDisable! disables autoformat for this buffer only
-- :FormatDisable disables autoformat globally
vim.api.nvim_create_user_command('FormatDisable', function(args)
  if args.bang then
    vim.b.disable_autoformat = true
  else
    vim.g.disable_autoformat = true
  end
end, {
  desc = 'Disable autoformat-on-save',
  bang = true,
})

-- :FormatEnable enables autoformat globally and for this buffer
vim.api.nvim_create_user_command('FormatEnable', function()
  vim.b.disable_autoformat = false
  vim.g.disable_autoformat = false
end, {
  desc = 'Re-enable autoformat-on-save',
})