dotvim/lua/config/util.lua

65 lines
1.5 KiB
Lua
Raw Normal View History

2023-09-25 19:23:19 +00:00
local function invoke(args)
output = vim.fn.system(table.concat(args, ' '))
return string.gsub(output, '%s+$', '')
end
local function get_branch_parent()
local author = invoke({'git', 'config', 'user.name'})
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
2022-10-22 05:45:38 +00:00
-- UnAlign
vim.api.nvim_create_user_command(
'UnAlign',
2023-09-22 21:56:23 +00:00
function(ctx)
vim.cmd(ctx.line1 .. ',' .. ctx.line2 .. 's/\\(\\S\\+\\)\\s\\{2,\\}/\\1 /g')
end,
2022-10-22 16:53:29 +00:00
{range = '%'}
2022-10-22 05:45:38 +00:00
)
2023-09-22 21:30:42 +00:00
2023-09-25 19:23:19 +00:00
-- TopicEdit
2023-09-22 21:56:23 +00:00
vim.api.nvim_create_user_command(
2023-09-25 19:23:19 +00:00
'TopicEdit',
2023-09-22 21:56:23 +00:00
function(ctx)
2023-09-25 19:23:19 +00:00
local hash = get_branch_parent()
if hash then
local names = invoke({'git', 'diff', '--name-only', hash})
if #names == 0 then
print('No files changed since previous author')
else
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
2023-09-22 21:56:23 +00:00
end
2023-09-25 19:23:19 +00:00
end,
{}
)
2023-09-22 21:30:42 +00:00
2023-09-25 19:23:19 +00:00
-- TopicDiff
vim.api.nvim_create_user_command(
'TopicDiff',
function(ctx)
local hash = get_branch_parent()
if hash then
invoke({'git', 'difftool', '-d', hash})
2023-09-22 21:56:23 +00:00
end
end,
{}
)