|
| 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 | + if client == nil then |
| 43 | + return |
| 44 | + end |
| 45 | + |
| 46 | + -- Only attach to clients that support document formatting |
| 47 | + if not client.server_capabilities.documentFormattingProvider then |
| 48 | + return |
| 49 | + end |
| 50 | + |
| 51 | + -- Tsserver usually works poorly. Sorry you work with bad languages |
| 52 | + -- You can remove this line if you know what you're doing :) |
| 53 | + if client.name == 'ts_ls' then |
| 54 | + return |
| 55 | + end |
| 56 | + |
| 57 | + -- i want prettier to format json files |
| 58 | + if client.name == 'jsonls' then |
| 59 | + return |
| 60 | + end |
| 61 | + |
| 62 | + -- Create an autocmd that will run *before* we save the buffer. |
| 63 | + -- Run the formatting command for the LSP that has just attached. |
| 64 | + vim.api.nvim_create_autocmd('BufWritePre', { |
| 65 | + group = get_augroup(client), |
| 66 | + buffer = bufnr, |
| 67 | + callback = function() |
| 68 | + if not format_is_enabled then |
| 69 | + return |
| 70 | + end |
| 71 | + |
| 72 | + vim.lsp.buf.format { |
| 73 | + async = false, |
| 74 | + filter = function(c) |
| 75 | + return c.id == client.id |
| 76 | + end, |
| 77 | + } |
| 78 | + end, |
| 79 | + }) |
| 80 | + end, |
| 81 | + }) |
| 82 | + end, |
| 83 | +} |
0 commit comments