From 175b18211b2fdcad70b01c046afce4dcc6161775 Mon Sep 17 00:00:00 2001 From: n3wborn Date: Wed, 10 Jun 2026 02:25:14 +0200 Subject: [PATCH 1/2] refactor: utils functions --- lua/utils.lua | 168 ++++++++++++++++++++++---------------------------- 1 file changed, 74 insertions(+), 94 deletions(-) diff --git a/lua/utils.lua b/lua/utils.lua index ee7ca694..4ed1d09e 100644 --- a/lua/utils.lua +++ b/lua/utils.lua @@ -1,133 +1,113 @@ local api = vim.api --- global inspect fn -_G.inspect = function(...) - print(vim.inspect(...)) +local M = {} + +local default_map_opts = { + silent = true, +} + +---@param mode string|string[] +---@param lhs string +---@param rhs string|function +---@param opts? vim.keymap.set.Opts +function M.map(mode, lhs, rhs, opts) + vim.keymap.set(mode, lhs, rhs, vim.tbl_extend('force', default_map_opts, opts or {})) end -local get_map_options = function(custom_options) - local options = { noremap = true, silent = true } - if custom_options then - options = vim.tbl_extend('force', options, custom_options) - end - return options +---@param bufnr integer +---@param mode string|string[] +---@param lhs string +---@param rhs string|function +---@param opts? vim.keymap.set.Opts +function M.buf_map(bufnr, mode, lhs, rhs, opts) + vim.keymap.set(mode, lhs, rhs, { buffer = bufnr, unpack(opts or {}) }) end -local M = {} +---@param name string name given to the command +---@param fn function to be execute when calling the command +---@param opts table options to set for the user command +function M.command(name, fn, opts) + api.nvim_create_user_command(name, fn, opts or {}) +end -M.map = function(mode, target, source, opts) - vim.keymap.set(mode, target, source, get_map_options(opts)) +function M.buf_command(bufnr, name, fn, opts) + api.nvim_buf_create_user_command(bufnr, name, fn, opts or {}) end -for _, mode in ipairs({ 'n', 'o', 'i', 'x', 't' }) do - M[mode .. 'map'] = function(...) - M.map(mode, ...) - end +function M.t(keys) + return vim.keycode(keys) end -M.buf_map = function(bufnr, mode, target, source, opts) - opts = opts or {} - opts.buffer = bufnr +function M.input(keys) + vim.api.nvim_input(M.t(keys)) +end - M.map(mode, target, source, get_map_options(opts)) +function M.warn(msg) + vim.notify(msg, vim.log.levels.WARN) end -M.for_each = function(tbl, cb) - for _, v in ipairs(tbl) do - cb(v) - end +function M.info(msg) + vim.notify(msg, vim.log.levels.INFO) end -M.buf_command = function(bufnr, name, fn, opts) - api.nvim_buf_create_user_command(bufnr, name, fn, opts or {}) +function M.print(...) + vim.print(...) end -M.table = { - some = function(tbl, cb) - for k, v in pairs(tbl) do - if cb(k, v) then - return true - end - end - return false - end, -} +function M.cwd() + return vim.uv.cwd() +end -M.timer = { - start_time = nil, - start = function() - M.timer.start_time = vim.loop.now() - end, - stop = function() - print(vim.loop.now() - M.timer.start_time .. ' ms') - M.timer.start_time = nil - end, - - start_nano = function() - M.timer.start_time = vim.loop.hrtime() - end, - stop_nano = function() - print(vim.loop.hrtime() - M.timer.start_time .. ' ns') - M.timer.start_time = nil - end, -} +function M.is_file(path) + local stat = vim.uv.fs_stat(path) -M.command = function(name, fn, opts) - api.nvim_create_user_command(name, fn, opts or {}) + return stat ~= nil and stat.type == 'file' end -M.t = function(str) - return vim.keycode(str) -end +---@param markers? string[] +---@param file string +---@return string|nil +function M.root(markers, file) + local found = vim.fs.find(markers or { '.git' }, { upward = true, path = vim.fs.dirname(file) })[1] -M.input = function(keys, mode) - vim.api.nvim_feedkeys(M.t(keys), mode or 'i', true) + return found and vim.fs.dirname(found) or nil end -M.warn = function(msg) - api.nvim_echo({ { msg, 'WarningMsg' } }, true, {}) -end +function M.yank_file_path() + local path = api.nvim_buf_get_name(0) -M.is_file = function(path) - if path == '' then - return false - end + vim.fn.setreg('+', path) - local stat = vim.loop.fs_stat(path) - return stat and stat.type == 'file' + vim.notify(('Copied: %s'):format(path), vim.log.levels.INFO) end -M.get_cwd = function() - return assert(vim.uv.cwd) -end +M.table = {} ----@param files table ----@param file_name string -M.get_root_dir = function(files, file_name) - vim.fs.dirname(vim.fs.find(files or { '.git' }, { - upward = true, - path = vim.fs.dirname(file_name), - })[1]) +function M.table.some(tbl, predicate) + return vim.iter(pairs(tbl)):any(function(k, v) + return predicate(v, k) + end) end -M.yank_file_path = function() - local file_path = vim.api.nvim_buf_get_name(0) - vim.fn.setreg('+', file_path) - vim.api.nvim_echo({ { 'File path copied to clipboard: ' .. file_path } }, true, {}) +function M.table.find(tbl, predicate) + for k, v in pairs(tbl) do + if predicate(v, k) then + return v, k + end + end end ----@param msg string -M.notif = function(msg) - vim.schedule(function() - vim.notify(msg) - end) +function M.timer() + local start = vim.uv.hrtime() + + return function() + return (vim.uv.hrtime() - start) / 1e6 + end end -M.undotree = function() - local close = require('undotree').open({ - title = 'Undotree', - command = 'topleft 30vnew', - }) +function M.undotree() + local close = require('undotree').open({ title = 'Undotree', command = 'topleft 30vnew' }) + if not close then vim.bo.filetype = 'undotree' end From 996da1d3b2f16a7798a35bf52f3fe8e01e03ccdb Mon Sep 17 00:00:00 2001 From: n3wborn Date: Wed, 10 Jun 2026 02:25:55 +0200 Subject: [PATCH 2/2] refactor(keymaps): replace u.map with native function --- lua/config/keymaps.lua | 122 ++++++++++++++++------------------------- 1 file changed, 48 insertions(+), 74 deletions(-) diff --git a/lua/config/keymaps.lua b/lua/config/keymaps.lua index f6820b86..ccd087f4 100644 --- a/lua/config/keymaps.lua +++ b/lua/config/keymaps.lua @@ -2,63 +2,64 @@ local u = require('utils') -- source config -u.map('n', 'R', 'source $MYVIMRC') +vim.keymap.set('n', 'R', 'source $MYVIMRC') -- fix indentation -u.map('n', 'i', 'mmgg=G`m') +vim.keymap.set('n', 'i', 'mmgg=G`m') -- easier windows jump -u.map('n', '', 'h') -u.map('n', '', 'l') -u.map('n', '', 'j') -u.map('n', '', 'k') +vim.keymap.set('n', '', 'h') +vim.keymap.set('n', '', 'l') +vim.keymap.set('n', '', 'j') +vim.keymap.set('n', '', 'k') --- Resize windows -u.map('n', '+', 'vertical resize +10') -u.map('n', '-', 'vertical resize -10') +vim.keymap.set('n', '+', 'vertical resize +10') +vim.keymap.set('n', '-', 'vertical resize -10') -u.map('n', '+', 'resize +5') -u.map('n', '-', 'resize -5') +vim.keymap.set('n', '+', 'resize +5') +vim.keymap.set('n', '-', 'resize -5') -- save in insert mode vim.keymap.set('n', '', ':w') --- Phpcbf - Php-cs-fixer -u.map('n', 'FB', '!phpcbf %') -- *B*eautify -u.map('n', 'FS', '!php-cs-fixer --rules=@Symfony --using-cache=no fix %') -- *F*ix (Symfony) -u.map('n', 'FP', '!php-cs-fixer --rules=@PSR12 --using-cache=no fix %') -- *F*ix (PSR12) --- u.map('n', 'FF', '!php-cs-fixer --rules=@PSR12,@Symfony --using-cache=no fix %') -u.map('n', 'FF', '!docker compose exec php php-cs-fixer fix %') +--- @TODO: use a task runner +vim.keymap.set('n', 'FB', '!phpcbf %') -- *B*eautify +vim.keymap.set('n', 'FS', '!php-cs-fixer --rules=@Symfony --using-cache=no fix %') -- *F*ix (Symfony) +vim.keymap.set('n', 'FP', '!php-cs-fixer --rules=@PSR12 --using-cache=no fix %') -- *F*ix (PSR12) +-- vim.keymap.set('n', 'FF', '!php-cs-fixer --rules=@PSR12,@Symfony --using-cache=no fix %') +vim.keymap.set('n', 'FF', '!docker compose exec php php-cs-fixer fix %') --- Git -u.map('n', 'gh', ':diffget //3') -u.map('n', 'gu', ':diffget //2') +vim.keymap.set('n', 'gh', ':diffget //3') +vim.keymap.set('n', 'gu', ':diffget //2') -- Lazy UI -u.map('n', 'L', 'Lazy') +vim.keymap.set('n', 'L', 'Lazy') --- keep text selected after indentation -u.map('v', '<', '', '>gv') +vim.keymap.set('v', '<', '', '>gv') --- move current line up/down -u.map('v', 'J', ":m '>+1gv=gv") -u.map('v', 'K', ":m '<-2gv=gv") +vim.keymap.set('v', 'J', ":m '>+1gv=gv") +vim.keymap.set('v', 'K', ":m '<-2gv=gv") --- u.map Ctrl-c to Escape -u.map('i', '', '') -u.map('i', '', '') +-- vim.keymap.set Ctrl-c to Escape +vim.keymap.set('i', '', '') +vim.keymap.set('i', '', '') -- close current window -u.map('n', '', 'close') +vim.keymap.set('n', '', 'close') --- Copy-paste -u.map('n', 'Y', 'gg"+yG', { desc = 'Copy whole file' }) -u.map('n', 'D', '"_dd', { noremap = true, silent = true, desc = 'Delete line without yanking' }) --- u.map({'n', 'v'}, 'p', 'p`[=`]', { desc = 'Paste and indent' }) --- u.map({ 'n', 'v' }, 'P', 'P`[=`]', { desc = 'Paste before and indent' }) --- u.map('v', 'p', 'p`[=`]', { desc = 'Paste and indent' }) --- u.map('v', 'P', 'P`[=`]', { desc = 'Paste before and indent' }) +vim.keymap.set('n', 'Y', 'gg"+yG', { desc = 'Copy whole file' }) +vim.keymap.set('n', 'D', '"_dd', { noremap = true, silent = true, desc = 'Delete line without yanking' }) +-- vim.keymap.set({'n', 'v'}, 'p', 'p`[=`]', { desc = 'Paste and indent' }) +-- vim.keymap.set({ 'n', 'v' }, 'P', 'P`[=`]', { desc = 'Paste before and indent' }) +-- vim.keymap.set('v', 'p', 'p`[=`]', { desc = 'Paste and indent' }) +-- vim.keymap.set('v', 'P', 'P`[=`]', { desc = 'Paste before and indent' }) vim.keymap.set('n', 'P', '"+P') vim.keymap.set('n', 'p', '"+p') -- always center search results @@ -68,18 +69,6 @@ vim.keymap.set('n', '*', '*zz', { silent = true }) vim.keymap.set('n', '#', '#zz', { silent = true }) vim.keymap.set('n', 'g*', 'g*zz', { silent = true }) ---- Switch to previous buffer -u.map('n', '', 'e #', { desc = 'Switch to previous buffer' }) - --- search within visual selection -vim.keymap.set('x', '/', '/\\%V') - -u.map('n', 'B', function() - u.yank_file_path() -end) - -u.map('n', 'qq', 'qa', { desc = 'Quit All' }) - -- diagnostics local diagnostic_goto = function(next, severity) return function() @@ -91,8 +80,8 @@ local diagnostic_goto = function(next, severity) end end -u.map('n', ']e', diagnostic_goto(true, 'ERROR'), { desc = 'Next Error' }) -u.map('n', '[e', diagnostic_goto(false, 'ERROR'), { desc = 'Prev Error' }) +vim.keymap.set('n', ']e', diagnostic_goto(true, 'ERROR'), { desc = 'Next Error' }) +vim.keymap.set('n', '[e', diagnostic_goto(false, 'ERROR'), { desc = 'Prev Error' }) -- Autosave u.command('ToggleAutoWrite', function() @@ -100,33 +89,18 @@ u.command('ToggleAutoWrite', function() vim.o.autowrite = value vim.o.autowriteall = value - vim.notify('autowrite: ' .. (value and 'enabled' or 'disabled'), vim.log.levels.INFO) -end, { - desc = 'Toogle Autowrite', -}) + u.info('autowrite: ' .. (value and 'enabled' or 'disabled')) +end, { desc = 'Toogle Autowrite' }) --- autosave +-- stylua: ignore start +vim.keymap.set('n', '', 'e #zz', { desc = 'Switch to previous buffer' }) +vim.keymap.set('x', '/', '/\\%V', { desc = 'Search within visual selection' }) +vim.keymap.set('n', 'B', function() u.yank_file_path() end, { desc = 'Yank file path' }) vim.keymap.set('n', 'as', ':ToggleAutoWrite', { desc = 'ToggleAutowrite' }) - --- undotree -u.map('n', 'U', u.undotree) - --- restart -u.map('n', 'R', ':restart', { desc = 'Restart Nvim' }) - -u.map('n', 'ff', function() - require('fff').find_files() -end) - -u.map('n', 'sp', function() - require('fff').live_grep() -end) - -u.map('n', 'sd', function() - local word = vim.fn.expand('') - if word == '' then - word = vim.fn.expand('') - end - - require('fff').live_grep({ query = word }) -end) +vim.keymap.set('n', 'U', u.undotree, { desc = 'Show/Hide Undotree' }) +vim.keymap.set('n', 'R', ':restart', { desc = 'Restart Nvim' }) +-- FFF +vim.keymap.set('n', 'ff', function() require('fff').find_files() end) +vim.keymap.set('n', 'sp', function() require('fff').live_grep() end) +vim.keymap.set('n', 'sd', function() require('fff').live_grep({ query = vim.fn.expand('') }) end) +-- stylua: ignore end