dotfiles/nvim/lua/kanopo/autocmds.lua

41 lines
1.4 KiB
Lua

-- Highlight yanked text
local highlight_group = vim.api.nvim_create_augroup("YankHighlight", { clear = true })
vim.api.nvim_create_autocmd("TextYankPost", {
callback = function()
vim.highlight.on_yank()
end,
group = highlight_group,
pattern = "*",
})
-- Go to last cursor position when opening a file
vim.api.nvim_create_autocmd("BufReadPost", {
callback = function()
local ft = vim.bo.filetype
if ft ~= "commit" and ft ~= "rebase" then
local last_pos = vim.fn.line("'\"")
if last_pos > 1 and last_pos <= vim.fn.line("$") then
vim.cmd('normal! g`"')
end
end
end,
})
vim.api.nvim_create_autocmd('LspAttach', {
group = vim.api.nvim_create_augroup('my.lsp', {}),
callback = function(args)
local client = assert(vim.lsp.get_client_by_id(args.data.client_id))
-- Usually not needed if server supports "textDocument/willSaveWaitUntil".
if not client:supports_method('textDocument/willSaveWaitUntil')
and client:supports_method('textDocument/formatting') then
vim.api.nvim_create_autocmd('BufWritePre', {
group = vim.api.nvim_create_augroup('my.lsp', { clear = false }),
buffer = args.buf,
callback = function()
vim.lsp.buf.format({ bufnr = args.buf, id = client.id, timeout_ms = 1000 })
end,
})
end
end,
})