local function augroup(name) return vim.api.nvim_create_augroup('kyren-' .. name, { clear = true }) end -- Highlight when yanking text vim.api.nvim_create_autocmd('TextYankPost', { group = augroup('highlight-yank'), callback = function() vim.highlight.on_yank() end, }) vim.api.nvim_create_autocmd('BufEnter', { desc = 'Disable newlines on commented lines from continuing the comment', group = augroup('disable-comments-continuation'), callback = function() -- vim.opt_local.formatoptions:remove('r') -- no comments on enter vim.opt_local.formatoptions:remove('o') -- no comments on `o` or `O` end, }) -- highlight %v, %s etc in go string literals vim.api.nvim_create_autocmd({ 'BufEnter', 'TextChanged', 'InsertLeave' }, { pattern = '*.go', callback = function() local query = vim.treesitter.query.parse('go', '(interpreted_string_literal) @string') local parser = vim.treesitter.get_parser(0, 'go') if not parser then return end local tree = parser:parse()[1] local root = tree:root() local bufnr = vim.api.nvim_get_current_buf() for id, node in query:iter_captures(root, bufnr, 0, -1) do if query.captures[id] == 'string' then local start_row, start_col, end_row, end_col = node:range() -- Get the text of the string literal local text = vim.api.nvim_buf_get_text(bufnr, start_row, start_col, end_row, end_col, {})[1] -- Highlight only the parts matching `%[a-z]` for match_start, match_end in text:gmatch('()%%[a-z]()') do vim.api.nvim_buf_add_highlight( bufnr, -1, '@lsp.type.formatSpecifier.go', -- Higlight group start_row, start_col + match_start - 1, start_col + match_end - 1 ) end end end end, }) -- Snacks snippet to notify LSP servers when renaming files in oil.nvim vim.api.nvim_create_autocmd('User', { pattern = 'OilActionsPost', callback = function(event) if event.data.actions.type == 'move' then Snacks.rename.on_rename_file(event.data.actions.src_url, event.data.actions.dest_url) end end, }) -- Check if we need to reload the file when it changed vim.api.nvim_create_autocmd({ 'FocusGained', 'TermClose', 'TermLeave' }, { group = augroup('checktime'), callback = function() if vim.o.buftype ~= 'nofile' then vim.cmd('checktime') end end, }) -- close some filetypes with vim.api.nvim_create_autocmd('FileType', { group = augroup('close_with_q'), pattern = { 'PlenaryTestPopup', 'checkhealth', 'dbout', 'gitsigns-blame', 'grug-far', 'help', 'lspinfo', 'neotest-output', 'neotest-output-panel', 'neotest-summary', 'notify', 'qf', 'startuptime', 'tsplayground', }, callback = function(event) vim.bo[event.buf].buflisted = false vim.schedule(function() vim.keymap.set('n', 'q', function() vim.cmd('close') pcall(vim.api.nvim_buf_delete, event.buf, { force = true }) end, { buffer = event.buf, silent = true, desc = 'Quit buffer', }) end) end, }) -- Format and organize imports on file save vim.api.nvim_create_autocmd('BufWritePre', { pattern = '*.java', callback = function() if not vim.g.disable_autoformat then require('jdtls').organize_imports() vim.lsp.buf.format({ async = false }) end end, })