dotvim/lua/config/util.lua

100 lines
2.2 KiB
Lua
Raw Normal View History

2023-09-25 19:23:19 +00:00
local function invoke(args)
2024-04-20 02:21:42 +00:00
local output = vim.fn.system(table.concat(args, ' '))
2023-09-25 19:23:19 +00:00
return string.gsub(output, '%s+$', '')
end
2024-04-20 02:33:16 +00:00
local function git_parent_branch(parent_branch)
if parent_branch ~= '' then
return parent_branch
2024-04-20 02:21:42 +00:00
end
2023-09-25 19:23:19 +00:00
local author = invoke({'git', 'config', 'user.name'})
2024-03-27 19:00:52 +00:00
if #author == 0 then
print('User name is not set')
return
end
2023-09-25 19:23:19 +00:00
local hash = invoke{
'git',
'log',
'-n1',
'--author="^((?!' .. author .. ').)*$"',
'--perl-regexp',
'--pretty=format:"%H"'
}
if #hash == 0 then
print('All commits are by current author')
else
return hash
end
end
2024-04-20 02:21:42 +00:00
local function git_edit_paths(names)
2024-03-27 19:00:52 +00:00
local root_dir = invoke({'git', 'rev-parse', '--show-toplevel'})
for name in string.gmatch(names, '[^\r\n]+') do
vim.cmd(string.format('e %s/%s', root_dir, name))
end
end
2024-10-04 04:51:30 +00:00
local function git_delete_lock()
local root = invoke({'git', 'rev-parse', '--show-toplevel'})
local lock_path = root .. '/.git/index.lock'
vim.fn.delete(lock_path)
end
2024-04-20 02:21:42 +00:00
-- GitTopicEdit
2023-09-22 21:56:23 +00:00
vim.api.nvim_create_user_command(
2024-04-20 02:21:42 +00:00
'GitTopicEdit',
2023-09-22 21:56:23 +00:00
function(ctx)
2024-04-20 02:33:16 +00:00
local hash = git_parent_branch(ctx.args)
2023-09-25 19:23:19 +00:00
if hash then
2024-04-20 02:21:42 +00:00
git_edit_paths(invoke({'git', 'diff', '--name-only', hash}))
2023-09-22 21:56:23 +00:00
end
2023-09-25 19:23:19 +00:00
end,
2024-04-20 02:21:42 +00:00
{nargs = '?'}
2023-09-25 19:23:19 +00:00
)
2023-09-22 21:30:42 +00:00
2024-04-20 02:21:42 +00:00
-- GitTopicDiff
2023-09-25 19:23:19 +00:00
vim.api.nvim_create_user_command(
2024-04-20 02:21:42 +00:00
'GitTopicDiff',
2023-09-25 19:23:19 +00:00
function(ctx)
2024-04-20 02:33:16 +00:00
local hash = git_parent_branch(ctx.args)
2023-09-25 19:23:19 +00:00
if hash then
invoke({'git', 'difftool', '-d', hash})
2023-09-22 21:56:23 +00:00
end
end,
2024-04-20 02:21:42 +00:00
{nargs = '?'}
2023-09-22 21:56:23 +00:00
)
2024-10-04 04:51:30 +00:00
-- GitUnlock
vim.api.nvim_create_user_command(
'GitUnlock',
git_delete_lock,
{}
)
-- UnAlign
vim.api.nvim_create_user_command(
'UnAlign',
function(ctx)
vim.cmd(ctx.line1 .. ',' .. ctx.line2 .. 's/\\(\\S\\+\\)\\s\\{2,\\}/\\1 /g')
end,
{range = '%'}
)
-- BuffDeleteAllButCurrent
vim.api.nvim_create_user_command(
'BuffDeleteAllButCurrent',
function()
local bufs = vim.api.nvim_list_bufs()
local current_buf = vim.api.nvim_get_current_buf()
for _, i in ipairs(bufs) do
if i ~= current_buf then
vim.api.nvim_buf_delete(i, {})
end
end
end,
{}
)