feat: Add auto save on InsertLeave event

This commit is contained in:
nedia 2023-01-28 15:04:21 +00:00
parent 32744c3f66
commit 4e8a5fa7cd
1 changed files with 19 additions and 0 deletions

View File

@ -329,6 +329,25 @@ local on_attach = function(_, bufnr)
vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_) vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_)
vim.lsp.buf.format() vim.lsp.buf.format()
end, { desc = 'Format current buffer with LSP' }) end, { desc = 'Format current buffer with LSP' })
-- Automatically save when leaving Insert mode
vim.api.nvim_create_autocmd({ 'InsertLeave' }, {
group = vim.api.nvim_create_augroup('AutoSave', { clear = true }),
buffer = bufnr,
callback = function()
-- We need to be able to save the buffer
if vim.fn.getbufvar(bufnr, '&modifiable') ~= 1 then
return
end
-- If the buffer hasn't been modified, don't bother doing anything
if not vim.api.nvim_buf_get_option(bufnr, 'modified') then
return
end
vim.cmd 'w'
end,
})
end end
-- Enable the following language servers -- Enable the following language servers