require('modules.theme') require('modules.terminal') local api = vim.api local au = api.nvim_create_autocmd local group = api.nvim_create_augroup('core.events', { clear = true }) vim.api.nvim_create_user_command('W', function() vim.cmd('w') end, { desc = 'Write current buffer' }) function _G.see(val) local lines = vim.split(vim.inspect(val), '\n') vim.cmd('new') vim.api.nvim_buf_set_lines(0, 0, -1, false, lines) vim.bo.buftype = 'nofile' vim.bo.bufhidden = 'wipe' end vim.api.nvim_create_user_command('MessagesToBuffer', function() local output = vim.api.nvim_exec2('silent messages', { output = true }).output or '' local lines = vim.split(output, '\n', { plain = true, trimempty = false }) vim.cmd('new') local bufnr = vim.api.nvim_get_current_buf() vim.bo[bufnr].buftype = 'nofile' vim.bo[bufnr].bufhidden = 'wipe' vim.bo[bufnr].swapfile = false vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) end, { desc = 'Open :messages output in a scratch buffer' }) vim.api.nvim_create_user_command( 'CmdToBuffer', function(opts) local cmd = table.concat(opts.fargs, ' ') vim.g.__cmd_to_buffer_cmd = cmd vim.g.__cmd_to_buffer_out = nil local exec_line = opts.bang and 'silent execute' or 'execute' local ok, err = pcall( vim.cmd, string.format( [[ redir => g:__cmd_to_buffer_out %s g:__cmd_to_buffer_cmd redir END ]], exec_line ) ) if not ok then pcall(vim.cmd, 'redir END') error(err) end local output = vim.g.__cmd_to_buffer_out or '' vim.g.__cmd_to_buffer_cmd = nil vim.g.__cmd_to_buffer_out = nil local lines = vim.split(output, '\n', { plain = true, trimempty = false }) vim.cmd('new') local bufnr = vim.api.nvim_get_current_buf() vim.bo[bufnr].buftype = 'nofile' vim.bo[bufnr].bufhidden = 'wipe' vim.bo[bufnr].swapfile = false vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) end, { nargs = '+', bang = true, complete = 'command', desc = 'Run an Ex command and open its output in a scratch buffer', } ) -- Automatically create a scratch buffer if Neovim starts with no files au('VimEnter', { group = group, callback = function() -- Only trigger if no file arguments are passed if vim.fn.argc() == 0 then vim.cmd('enew') vim.bo.buftype = 'nofile' vim.bo.bufhidden = 'wipe' vim.bo.swapfile = false end end, }) -- Highlight when yanking (copying) text au('TextYankPost', { group = group, callback = function() vim.hl.on_yank({ timeout = 200 }) end, }) -- Disable comment continuation only when using 'o'/'O', but keep it for au('FileType', { group = group, pattern = '*', callback = function() vim.opt_local.formatoptions:remove('o') end, }) -- Show cursor line only in active window au({ 'WinEnter', 'InsertLeave' }, { group = group, callback = function() if vim.w.auto_cursorline then vim.wo.cursorline = true vim.w.auto_cursorline = nil end end, }) au({ 'WinLeave', 'InsertEnter' }, { group = group, callback = function() -- Keep it on NvimTree to show current file if vim.bo.filetype == 'NvimTree' then return end if vim.wo.cursorline then vim.w.auto_cursorline = true vim.wo.cursorline = false end end, }) -- Autocompletion local function convert_completion_item(item) local converted = {} if item.kind then local kind = vim.lsp.protocol.CompletionItemKind[item.kind] or '' converted.menu = '[' .. kind .. ']' end local _, col = unpack(vim.api.nvim_win_get_cursor(0)) local next_char = vim.api.nvim_get_current_line():sub(col + 1, col + 1) if next_char ~= '"' and next_char ~= "'" then return converted end local completion_text if item.textEdit then completion_text = item.textEdit.newText elseif item.insertText and item.insertText ~= '' then completion_text = item.insertText else completion_text = item.label end if completion_text and completion_text:sub(-1) == next_char then local trimmed = completion_text:sub(1, -2) converted.word = trimmed if item.textEdit and item.textEdit.newText == completion_text then item.textEdit.newText = trimmed elseif item.insertText == completion_text then item.insertText = trimmed end end return converted end au('LspAttach', { group = group, callback = function(event) local client = vim.lsp.get_client_by_id(event.data.client_id) if not client then return end -- Enable native completion if client:supports_method('textDocument/completion') then vim.lsp.completion.enable(true, client.id, event.buf, { convert = convert_completion_item, }) end end, }) au('InsertEnter', { group = group, once = true, callback = function() require('nvim-ts-autotag').setup() require('nvim-autopairs').setup() end, }) au('UIEnter', { group = group, once = true, callback = function() vim.schedule(function() require('modules.navigation') require('plugins.language-manager').setup() require('modules.diagnostics') end) end, }) vim.api.nvim_create_autocmd('VimResized', { callback = function() vim.opt.scroll = 20 end, })