Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 48 additions & 74 deletions lua/config/keymaps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,64 @@
local u = require('utils')

-- source config
u.map('n', '<leader>R', '<cmd>source $MYVIMRC<cr>')
vim.keymap.set('n', '<leader>R', '<cmd>source $MYVIMRC<cr>')

-- fix indentation
u.map('n', '<leader>i', 'mmgg=G`m<cr>')
vim.keymap.set('n', '<leader>i', 'mmgg=G`m<cr>')

-- easier windows jump
u.map('n', '<C-Left>', '<C-w>h')
u.map('n', '<C-Right>', '<C-w>l')
u.map('n', '<C-Down>', '<C-w>j')
u.map('n', '<C-Up>', '<C-w>k')
vim.keymap.set('n', '<C-Left>', '<C-w>h')
vim.keymap.set('n', '<C-Right>', '<C-w>l')
vim.keymap.set('n', '<C-Down>', '<C-w>j')
vim.keymap.set('n', '<C-Up>', '<C-w>k')

--- Resize windows
u.map('n', '<leader>+', '<cmd>vertical resize +10<cr>')
u.map('n', '<leader>-', '<cmd>vertical resize -10<cr>')
vim.keymap.set('n', '<leader>+', '<cmd>vertical resize +10<cr>')
vim.keymap.set('n', '<leader>-', '<cmd>vertical resize -10<cr>')

u.map('n', '<space>+', '<cmd>resize +5<cr>')
u.map('n', '<space>-', '<cmd>resize -5<cr>')
vim.keymap.set('n', '<space>+', '<cmd>resize +5<cr>')
vim.keymap.set('n', '<space>-', '<cmd>resize -5<cr>')

-- save in insert mode
vim.keymap.set('n', '<C-s>', '<cmd>:w<cr><esc>')

--- Phpcbf - Php-cs-fixer
u.map('n', '<leader>FB', '<cmd>!phpcbf %<cr>') -- *B*eautify
u.map('n', '<leader>FS', '<cmd>!php-cs-fixer --rules=@Symfony --using-cache=no fix %<cr>') -- *F*ix (Symfony)
u.map('n', '<leader>FP', '<cmd>!php-cs-fixer --rules=@PSR12 --using-cache=no fix %<cr>') -- *F*ix (PSR12)
-- u.map('n', '<leader>FF', '<cmd>!php-cs-fixer --rules=@PSR12,@Symfony --using-cache=no fix %<cr>')
u.map('n', '<leader>FF', '<cmd>!docker compose exec php php-cs-fixer fix %<cr>')
--- @TODO: use a task runner
vim.keymap.set('n', '<leader>FB', '<cmd>!phpcbf %<cr>') -- *B*eautify
vim.keymap.set('n', '<leader>FS', '<cmd>!php-cs-fixer --rules=@Symfony --using-cache=no fix %<cr>') -- *F*ix (Symfony)
vim.keymap.set('n', '<leader>FP', '<cmd>!php-cs-fixer --rules=@PSR12 --using-cache=no fix %<cr>') -- *F*ix (PSR12)
-- vim.keymap.set('n', '<leader>FF', '<cmd>!php-cs-fixer --rules=@PSR12,@Symfony --using-cache=no fix %<cr>')
vim.keymap.set('n', '<leader>FF', '<cmd>!docker compose exec php php-cs-fixer fix %<cr>')

--- Git
u.map('n', '<leader>gh', ':diffget //3<cr>')
u.map('n', '<leader>gu', ':diffget //2<cr>')
vim.keymap.set('n', '<leader>gh', ':diffget //3<cr>')
vim.keymap.set('n', '<leader>gu', ':diffget //2<cr>')

-- Lazy UI
u.map('n', '<leader>L', '<cmd>Lazy<cr>')
vim.keymap.set('n', '<leader>L', '<cmd>Lazy<cr>')

--- keep text selected after indentation
u.map('v', '<', '<gv')
u.map('v', '>', '>gv')
vim.keymap.set('v', '<', '<gv')
vim.keymap.set('v', '>', '>gv')

--- move current line up/down
u.map('v', 'J', ":m '>+1<CR>gv=gv")
u.map('v', 'K', ":m '<-2<CR>gv=gv")
vim.keymap.set('v', 'J', ":m '>+1<CR>gv=gv")
vim.keymap.set('v', 'K', ":m '<-2<CR>gv=gv")

-- u.map Ctrl-c to Escape
u.map('i', '<C-c>', '')
u.map('i', '<C-c>', '<Esc>')
-- vim.keymap.set Ctrl-c to Escape
vim.keymap.set('i', '<C-c>', '')
vim.keymap.set('i', '<C-c>', '<Esc>')

-- close current window
u.map('n', '<C-c><C-c>', '<cmd>close<cr>')
vim.keymap.set('n', '<C-c><C-c>', '<cmd>close<cr>')

--- Copy-paste
u.map('n', '<leader>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', '<leader>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
Expand All @@ -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', '<space><space>', '<cmd>e #<cr>', { desc = 'Switch to previous buffer' })

-- search within visual selection
vim.keymap.set('x', '/', '<Esc>/\\%V')

u.map('n', '<leader>B', function()
u.yank_file_path()
end)

u.map('n', '<leader>qq', '<cmd>qa<cr>', { desc = 'Quit All' })

-- diagnostics
local diagnostic_goto = function(next, severity)
return function()
Expand All @@ -91,42 +80,27 @@ 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()
local value = not vim.o.autowrite
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', '<space><space>', '<cmd>e #<cr>zz', { desc = 'Switch to previous buffer' })
vim.keymap.set('x', '/', '<Esc>/\\%V', { desc = 'Search within visual selection' })
vim.keymap.set('n', '<leader>B', function() u.yank_file_path() end, { desc = 'Yank file path' })
vim.keymap.set('n', '<leader>as', ':ToggleAutoWrite<CR>', { desc = 'ToggleAutowrite' })

-- undotree
u.map('n', '<leader>U', u.undotree)

-- restart
u.map('n', '<space>R', ':restart<CR>', { desc = 'Restart Nvim' })

u.map('n', 'ff', function()
require('fff').find_files()
end)

u.map('n', '<space>sp', function()
require('fff').live_grep()
end)

u.map('n', '<space>sd', function()
local word = vim.fn.expand('<cword>')
if word == '' then
word = vim.fn.expand('<cWORD>')
end

require('fff').live_grep({ query = word })
end)
vim.keymap.set('n', '<leader>U', u.undotree, { desc = 'Show/Hide Undotree' })
vim.keymap.set('n', '<space>R', ':restart<CR>', { desc = 'Restart Nvim' })
-- FFF
vim.keymap.set('n', 'ff', function() require('fff').find_files() end)
vim.keymap.set('n', '<space>sp', function() require('fff').live_grep() end)
vim.keymap.set('n', '<space>sd', function() require('fff').live_grep({ query = vim.fn.expand('<cWORD>') }) end)
-- stylua: ignore end
168 changes: 74 additions & 94 deletions lua/utils.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Loading