nvim-config/plugin/events.lua
2025-10-24 17:13:50 +03:00

181 lines
4.1 KiB
Lua

local api = vim.api
local au = api.nvim_create_autocmd
local group = api.nvim_create_augroup('triimd.events', { clear = true })
-- 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.highlight.on_yank({ timeout = 400 })
end,
})
-- Disable comment continuation only when using 'o'/'O', but keep it for <Enter>
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()
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
au('LspAttach', {
group = group,
callback = function(ev)
local client = vim.lsp.get_client_by_id(ev.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, ev.buf, { autotrigger = true })
end
end,
})
vim.lsp.handlers['textDocument/completion'] = function(err, result, ctx, config)
if err or not result then
return
end
for _, item in ipairs(result.items or result) do
if item.kind then
local kind = vim.lsp.protocol.CompletionItemKind[item.kind] or ''
item.menu = '[' .. kind .. ']'
end
end
return vim.lsp.completion._on_completion_result(err, result, ctx, config)
end
-- Custom terminal
au('TermOpen', {
group = group,
callback = function()
vim.opt_local.number = false
vim.opt_local.relativenumber = false
vim.opt_local.scrolloff = 0
vim.bo.filetype = 'terminal'
vim.cmd.startinsert()
end,
})
-- Insert when re-entering a terminal window (after switching back)
au('BufEnter', {
group = group,
pattern = 'term://*',
callback = function()
if vim.bo.buftype == 'terminal' and vim.fn.mode() ~= 'i' then
vim.cmd.startinsert()
end
end,
})
-- Close all terminal buffers before quitting
au('QuitPre', {
group = group,
callback = function()
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_buf_is_loaded(buf) and vim.bo[buf].buftype == 'terminal' then
vim.api.nvim_buf_delete(buf, { force = true })
end
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()
-- start -----
local commands = {
TermDefault = function()
vim.cmd('terminal')
end,
TermRelative = function()
local shell = vim.o.shell or 'zsh'
local dir = vim.fn.expand('%:p:h')
vim.cmd(string.format('edit term://%s//%s', dir, shell))
end,
TermSplit = function()
vim.cmd('new')
vim.cmd('wincmd J')
vim.api.nvim_win_set_height(0, 12)
vim.wo.winfixheight = true
vim.cmd('term')
end,
TermVSplit = function()
vim.cmd('vsplit')
vim.cmd('term')
end,
}
for name, fn in pairs(commands) do
vim.api.nvim_create_user_command(name, fn, {})
end
require('triimd.keymaps')
require('triimd.filetree')
require('triimd.syntax')
require('triimd.diagnostics')
require('plugins.navigation')
require('plugins.tabline').setup()
-- end -----
end)
end,
})