]> src.twobees.de Git - dotfiles.git/blob - stow/nvim/.config/nvim/lua/autoformat.lua
try kickstart nvim
[dotfiles.git] / stow / nvim / .config / nvim / lua / autoformat.lua
1 -- autoformat.lua
2 --
3 -- Use your language server to automatically format your code on save.
4 -- Adds additional commands as well to manage the behavior
5
6 return {
7   'neovim/nvim-lspconfig',
8   config = function()
9     -- Switch for controlling whether you want autoformatting.
10     --  Use :KickstartFormatToggle to toggle autoformatting on or off
11     local format_is_enabled = true
12     vim.api.nvim_create_user_command('KickstartFormatToggle', function()
13       format_is_enabled = not format_is_enabled
14       print('Setting autoformatting to: ' .. tostring(format_is_enabled))
15     end, {})
16
17     -- Create an augroup that is used for managing our formatting autocmds.
18     --      We need one augroup per client to make sure that multiple clients
19     --      can attach to the same buffer without interfering with each other.
20     local _augroups = {}
21     local get_augroup = function(client)
22       if not _augroups[client.id] then
23         local group_name = 'kickstart-lsp-format-' .. client.name
24         local id = vim.api.nvim_create_augroup(group_name, { clear = true })
25         _augroups[client.id] = id
26       end
27
28       return _augroups[client.id]
29     end
30
31     -- Whenever an LSP attaches to a buffer, we will run this function.
32     --
33     -- See `:help LspAttach` for more information about this autocmd event.
34     vim.api.nvim_create_autocmd('LspAttach', {
35       group = vim.api.nvim_create_augroup('kickstart-lsp-attach-format', { clear = true }),
36       -- This is where we attach the autoformatting for reasonable clients
37       callback = function(args)
38         local client_id = args.data.client_id
39         local client = vim.lsp.get_client_by_id(client_id)
40         local bufnr = args.buf
41
42         -- Only attach to clients that support document formatting
43         if not client.server_capabilities.documentFormattingProvider then
44           return
45         end
46
47         -- Tsserver usually works poorly. Sorry you work with bad languages
48         -- You can remove this line if you know what you're doing :)
49         --  if client.name == 'tsserver' then
50         --    return
51         --  end
52
53         -- Create an autocmd that will run *before* we save the buffer.
54         --  Run the formatting command for the LSP that has just attached.
55         vim.api.nvim_create_autocmd('BufWritePre', {
56           group = get_augroup(client),
57           buffer = bufnr,
58           callback = function()
59             if not format_is_enabled then
60               return
61             end
62
63             vim.lsp.buf.format {
64               async = false,
65               filter = function(c)
66                 return c.id == client.id
67               end,
68             }
69           end,
70         })
71       end,
72     })
73   end,
74 }