refactor(autocmd): update augroup names and add arb filetype handling

This commit is contained in:
sankalp committed 2025-06-02 15:42:42 +05:45
1 parent 3263b7383f
commit 203cf76e6f
1 file changed
+90 -110
+90 -110
View File
@@ -1,149 +1,129 @@
local function augroup(name) local function augroup(name)
return vim.api.nvim_create_augroup('nvim' .. name, { clear = true }) return vim.api.nvim_create_augroup("keycodex" .. name, { clear = true })
end end
-- Strip trailing spaces before write -- Strip trailing spaces before write
vim.api.nvim_create_autocmd({ 'BufWritePre' }, { vim.api.nvim_create_autocmd({ "BufWritePre" }, {
group = augroup('strip_space'), group = augroup("strip_space"),
pattern = { '*' }, pattern = { "*" },
callback = function() callback = function()
vim.cmd([[ %s/\s\+$//e ]]) vim.cmd([[ %s/\s\+$//e ]])
end, end,
}) })
-- Check if we need to reload the file when it changed -- Check if we need to reload the file when it changed
vim.api.nvim_create_autocmd({ 'FocusGained', 'TermClose', 'TermLeave' }, { vim.api.nvim_create_autocmd({ "FocusGained", "TermClose", "TermLeave" }, {
group = augroup('checktime'), group = augroup("checktime"),
command = 'checktime', command = "checktime",
}) })
-- Highlight on yank -- Highlight on yank
vim.api.nvim_create_autocmd('TextYankPost', { vim.api.nvim_create_autocmd("TextYankPost", {
group = augroup('highlight_yank'), group = augroup("highlight_yank"),
callback = function() callback = function()
vim.highlight.on_yank() vim.highlight.on_yank()
end, end,
}) })
-- resize splits if window got resized -- resize splits if window got resized
vim.api.nvim_create_autocmd({ 'VimResized' }, { vim.api.nvim_create_autocmd({ "VimResized" }, {
group = augroup('resize_splits'), group = augroup("resize_splits"),
callback = function() callback = function()
vim.cmd('tabdo wincmd =') vim.cmd("tabdo wincmd =")
end, end,
}) })
-- go to last loc when opening a buffer -- go to last loc when opening a buffer
vim.api.nvim_create_autocmd('BufReadPost', { vim.api.nvim_create_autocmd("BufReadPost", {
group = augroup('last_loc'), group = augroup("last_loc"),
callback = function() callback = function()
local mark = vim.api.nvim_buf_get_mark(0, '"') local mark = vim.api.nvim_buf_get_mark(0, '"')
local lcount = vim.api.nvim_buf_line_count(0) local lcount = vim.api.nvim_buf_line_count(0)
if mark[1] > 0 and mark[1] <= lcount then if mark[1] > 0 and mark[1] <= lcount then
pcall(vim.api.nvim_win_set_cursor, 0, mark) pcall(vim.api.nvim_win_set_cursor, 0, mark)
end end
end, end,
}) })
-- close some filetypes with <q> -- close some filetypes with <q>
vim.api.nvim_create_autocmd('FileType', { vim.api.nvim_create_autocmd("FileType", {
group = augroup('close_with_q'), group = augroup("close_with_q"),
pattern = { pattern = {
'DressingSelect', "Jaq",
'Jaq', "PlenaryTestPopup",
'PlenaryTestPopup', "Avante",
'fugitive', "AvanteInput",
'git', "AvanteSelectedFiles",
'help', "fugitive",
'lir', "git",
'lspinfo', "help",
'man', "lir",
'notify', "lspinfo",
'qf', "man",
'spectre_panel', "netrw",
'startuptime', "notify",
'tsplayground', "qf",
}, "spectre_panel",
callback = function(event) "startuptime",
vim.bo[event.buf].buflisted = false "tsplayground",
vim.keymap.set('n', 'q', '<cmd>close<cr>', { buffer = event.buf, silent = true }) },
end, callback = function(event)
vim.bo[event.buf].buflisted = false
vim.keymap.set("n", "q", "<cmd>close<cr>", { buffer = event.buf, silent = true })
end,
}) })
-- wrap and check for spell in text filetypes -- wrap and check for spell in text filetypes
vim.api.nvim_create_autocmd('FileType', { vim.api.nvim_create_autocmd("FileType", {
group = augroup('wrap_spell'), group = augroup("wrap_spell"),
pattern = { 'gitcommit', 'markdown' }, pattern = { "gitcommit", "markdown" },
callback = function() callback = function()
vim.opt_local.wrap = true vim.opt_local.wrap = true
vim.opt_local.spell = true vim.opt_local.spell = true
end, end,
}) })
-- Auto create dir when saving a file, in case some intermediate directory does not exist -- Auto create dir when saving a file, in case some intermediate directory does not exist
vim.api.nvim_create_autocmd({ 'BufWritePre' }, { vim.api.nvim_create_autocmd({ "BufWritePre" }, {
group = augroup('auto_create_dir'), group = augroup("auto_create_dir"),
callback = function(event) callback = function(event)
local file = vim.loop.fs_realpath(event.match) or event.match local file = vim.loop.fs_realpath(event.match) or event.match
vim.fn.mkdir(vim.fn.fnamemodify(file, ':p:h'), 'p') vim.fn.mkdir(vim.fn.fnamemodify(file, ":p:h"), "p")
end, end,
}) })
-- Set arb filetype -- Set arb filetype
vim.api.nvim_create_autocmd({ 'BufEnter' }, { vim.api.nvim_create_autocmd({ "BufEnter" }, {
group = augroup('set_file_type'), group = augroup("set_file_type"),
pattern = { '*.arb' }, pattern = { "*.arb" },
command = require('lib.util').get_file_type_cmd('arb'), command = require("lib.util").get_file_type_cmd("arb"),
}) })
-- Disable format options -- Disable format options
vim.api.nvim_create_autocmd('FileType', { vim.api.nvim_create_autocmd("FileType", {
group = augroup('disable_formatoptions'), group = augroup("disable_formatoptions"),
pattern = '*', pattern = "*",
callback = function() callback = function()
vim.opt_local.formatoptions:remove({ 'c', 'r', 'o' }) vim.opt_local.formatoptions:remove({ "c", "r", "o" })
end, end,
}) })
local function enable_autoformat() local function enable_autoformat()
vim.api.nvim_create_autocmd({ 'BufWritePre' }, { vim.api.nvim_create_autocmd({ "BufWritePre" }, {
group = augroup('autoformat'), group = augroup("autoformat"),
pattern = { '*' }, pattern = { "*" },
callback = function() callback = function()
vim.lsp.buf.format() vim.lsp.buf.format()
end, end,
}) })
end end
enable_autoformat() enable_autoformat()
vim.api.nvim_create_user_command('WriteNoFormat', function() vim.api.nvim_create_user_command("WriteNoFormat", function()
-- Temporarily disable the autoformat autocmd -- Temporarily disable the autoformat autocmd
vim.api.nvim_del_augroup_by_name('nvim_autoformat') vim.api.nvim_del_augroup_by_name("keycodex_autoformat")
vim.cmd('write') vim.cmd("write")
-- Re-enable the autoformat autocmd -- Re-enable the autoformat autocmd
enable_autoformat() enable_autoformat()
end, {}) end, {})
-- Toggle between relative/absolute line numbers
local numbertoggle = augroup('numbertoggle')
vim.api.nvim_create_autocmd({ 'BufEnter', 'FocusGained', 'InsertLeave', 'CmdlineLeave', 'WinEnter' }, {
pattern = '*',
group = numbertoggle,
callback = function()
if vim.o.nu and vim.api.nvim_get_mode().mode ~= 'i' then
vim.opt.relativenumber = true
end
end,
})
vim.api.nvim_create_autocmd({ 'BufLeave', 'FocusLost', 'InsertEnter', 'CmdlineEnter', 'WinLeave' }, {
pattern = '*',
group = numbertoggle,
callback = function()
if vim.o.nu then
vim.opt.relativenumber = false
vim.cmd.redraw()
end
end,
})