1

Regenerate nvim config

This commit is contained in:
2024-06-02 03:29:20 +02:00
parent 75eea0c030
commit ef2e28883d
5576 changed files with 604886 additions and 503 deletions

View File

@ -0,0 +1,170 @@
local open_floating_window = require("lazygit.window").open_floating_window
local project_root_dir = require("lazygit.utils").project_root_dir
local get_root = require("lazygit.utils").get_root
local is_lazygit_available = require("lazygit.utils").is_lazygit_available
local is_symlink = require("lazygit.utils").is_symlink
local open_or_create_config = require("lazygit.utils").open_or_create_config
local fn = vim.fn
LAZYGIT_BUFFER = nil
LAZYGIT_LOADED = false
vim.g.lazygit_opened = 0
local prev_win = -1
local win = -1
local buffer = -1
--- on_exit callback function to delete the open buffer when lazygit exits in a neovim terminal
local function on_exit(job_id, code, event)
if code ~= 0 then
return
end
LAZYGIT_BUFFER = nil
LAZYGIT_LOADED = false
vim.g.lazygit_opened = 0
vim.cmd("silent! :checktime")
if vim.api.nvim_win_is_valid(prev_win) then
vim.api.nvim_win_close(win, true)
vim.api.nvim_set_current_win(prev_win)
prev_win = -1
if vim.api.nvim_buf_is_valid(buffer) and vim.api.nvim_buf_is_loaded(buffer) then
vim.api.nvim_buf_delete(buffer, { force = true })
end
buffer = -1
win = -1
end
end
--- Call lazygit
local function exec_lazygit_command(cmd)
if LAZYGIT_LOADED == false then
-- ensure that the buffer is closed on exit
vim.g.lazygit_opened = 1
vim.fn.termopen(cmd, { on_exit = on_exit })
end
vim.cmd("startinsert")
end
local function lazygitdefaultconfigpath()
-- lazygit -cd gives only the config dir, not the config file, so concat config.yml
return fn.substitute(fn.system("lazygit -cd"), "\n", "", "") .. "/config.yml"
end
local function lazygitgetconfigpath()
local default_config_path = lazygitdefaultconfigpath()
-- if vim.g.lazygit_config_file_path is a table, check if all config files exist
if vim.g.lazygit_config_file_path then
if type(vim.g.lazygit_config_file_path) == "table" then
for _, config_file in ipairs(vim.g.lazygit_config_file_path) do
if fn.empty(fn.glob(config_file)) == 1 then
print("lazygit: custom config file path: '" .. config_file .. "' could not be found. Returning default config")
return default_config_path
end
end
return vim.g.lazygit_config_file_path
elseif fn.empty(fn.glob(vim.g.lazygit_config_file_path)) == 0 then
return vim.g.lazygit_config_file_path
else
print("lazygit: custom config file path: '" .. vim.g.lazygit_config_file_path .. "' could not be found. Returning default config")
return default_config_path
end
else
print("lazygit: custom config file path is not set, option: 'lazygit_config_file_path' is missing")
-- any issue with the config file we fallback to the default config file path
return default_config_path
end
end
--- :LazyGit entry point
local function lazygit(path)
if is_lazygit_available() ~= true then
print("Please install lazygit. Check documentation for more information")
return
end
prev_win = vim.api.nvim_get_current_win()
win, buffer = open_floating_window()
local cmd = "lazygit"
-- set path to the root path
_ = project_root_dir()
if vim.g.lazygit_use_custom_config_file_path == 1 then
local config_path = lazygitgetconfigpath()
if type(config_path) == "table" then
config_path = table.concat(config_path, ",")
end
cmd = cmd .. " -ucf '" .. config_path .. "'" -- quote config_path to avoid whitespace errors
end
if path == nil then
if is_symlink() then
path = project_root_dir()
end
else
if fn.isdirectory(path) then
cmd = cmd .. " -p " .. path
end
end
exec_lazygit_command(cmd)
end
--- :LazyGitCurrentFile entry point
local function lazygitcurrentfile()
local current_dir = vim.fn.expand("%:p:h")
local git_root = get_root(current_dir)
lazygit(git_root)
end
--- :LazyGitFilter entry point
local function lazygitfilter(path)
if is_lazygit_available() ~= true then
print("Please install lazygit. Check documentation for more information")
return
end
if path == nil then
path = project_root_dir()
end
prev_win = vim.api.nvim_get_current_win()
win, buffer = open_floating_window()
local cmd = "lazygit " .. "-f " .. path
exec_lazygit_command(cmd)
end
--- :LazyGitFilterCurrentFile entry point
local function lazygitfiltercurrentfile()
local current_file = vim.fn.expand("%")
lazygitfilter(current_file)
end
--- :LazyGitConfig entry point
local function lazygitconfig()
local config_file = lazygitgetconfigpath()
if type(config_file) == "table" then
vim.ui.select(
config_file,
{ prompt = "select config file to edit" },
function (path)
open_or_create_config(path)
end
)
else
open_or_create_config(config_file)
end
end
return {
lazygit = lazygit,
lazygitcurrentfile = lazygitcurrentfile,
lazygitfilter = lazygitfilter,
lazygitfiltercurrentfile = lazygitfiltercurrentfile,
lazygitconfig = lazygitconfig,
project_root_dir = project_root_dir,
}

View File

@ -0,0 +1,127 @@
local fn = vim.fn
-- store all git repositories visited in this session
local lazygit_visited_git_repos = {}
-- TODO:check if the repo isa git repo
local function append_git_repo_path(repo_path)
if repo_path == nil or not fn.isdirectory(repo_path) then
return
end
for _, path in ipairs(lazygit_visited_git_repos) do
if path == repo_path then
return
end
end
table.insert(lazygit_visited_git_repos, tostring(repo_path))
end
--- Strip leading and lagging whitespace
local function trim(str)
return str:gsub('^%s+', ''):gsub('%s+$', '')
end
local function get_root(cwd)
local status, job = pcall(require, 'plenary.job')
if not status then
return fn.system('git rev-parse --show-toplevel')
end
local gitroot_job = job:new({
'git',
'rev-parse',
'--show-toplevel',
cwd=cwd
})
local path, code = gitroot_job:sync()
if (code ~= 0) then
return nil
end
return table.concat(path, "")
end
--- Get project_root_dir for git repository
local function project_root_dir()
-- always use bash on Unix based systems.
local oldshell = vim.o.shell
if vim.fn.has('win32') == 0 then
vim.o.shell = 'bash'
end
local cwd = vim.loop.cwd()
local root = get_root(cwd)
if root == nil then
vim.o.shell = oldshell
return nil
end
local cmd = string.format('cd "%s" && git rev-parse --show-toplevel', fn.fnamemodify(fn.resolve(fn.expand('%:p')), ':h'), root)
-- try symlinked file location instead
local gitdir = fn.system(cmd)
local isgitdir = fn.matchstr(gitdir, '^fatal:.*') == ''
if isgitdir then
vim.o.shell = oldshell
append_git_repo_path(gitdir)
return trim(gitdir)
end
-- revert to old shell
vim.o.shell = oldshell
local repo_path = fn.getcwd(0, 0)
append_git_repo_path(repo_path)
-- just return current working directory
return repo_path
end
--- Check if lazygit is available
local function is_lazygit_available()
return fn.executable('lazygit') == 1
end
local function is_symlink()
local resolved = fn.resolve(fn.expand('%:p'))
return resolved ~= fn.expand('%:p')
end
local function open_or_create_config(path)
if fn.empty(fn.glob(path)) == 1 then
-- file does not exist
-- check if user wants to create it
local answer = fn.confirm(
"File "
.. path
.. " does not exist.\nDo you want to create the file and populate it with the default configuration?",
"&Yes\n&No"
)
if answer == 2 then
return nil
end
if fn.isdirectory(fn.fnamemodify(path, ":h")) == false then
-- directory does not exist
fn.mkdir(fn.fnamemodify(path, ":h"), "p")
end
vim.cmd("edit " .. path)
vim.cmd([[execute "silent! 0read !lazygit -c"]])
vim.cmd([[execute "normal 1G"]])
else
vim.cmd("edit " .. path)
end
end
return {
get_root = get_root,
project_root_dir = project_root_dir,
lazygit_visited_git_repos = lazygit_visited_git_repos,
is_lazygit_available = is_lazygit_available,
is_symlink = is_symlink,
open_or_create_config = open_or_create_config,
}

View File

@ -0,0 +1,89 @@
local api = vim.api
--- open floating window with nice borders
local function open_floating_window()
local floating_window_scaling_factor = vim.g.lazygit_floating_window_scaling_factor
-- Why is this required?
-- vim.g.lazygit_floating_window_scaling_factor returns different types if the value is an integer or float
if type(floating_window_scaling_factor) == 'table' then
floating_window_scaling_factor = floating_window_scaling_factor[false]
end
local status, plenary = pcall(require, 'plenary.window.float')
if status and vim.g.lazygit_floating_window_use_plenary and vim.g.lazygit_floating_window_use_plenary ~= 0 then
local ret = plenary.percentage_range_window(floating_window_scaling_factor, floating_window_scaling_factor, {winblend=vim.g.lazygit_floating_window_winblend})
return ret.win_id, ret.bufnr
end
local height = math.ceil(vim.o.lines * floating_window_scaling_factor) - 1
local width = math.ceil(vim.o.columns * floating_window_scaling_factor)
local row = math.ceil(vim.o.lines - height) / 2
local col = math.ceil(vim.o.columns - width) / 2
local border_opts = {
style = 'minimal',
relative = 'editor',
row = row - 1,
col = col - 1,
width = width + 2,
height = height + 2,
}
local opts = { style = 'minimal', relative = 'editor', row = row, col = col, width = width, height = height }
local topleft, top, topright, right, botright, bot, botleft, left
local window_chars = vim.g.lazygit_floating_window_border_chars
if type(window_chars) == 'table' and #window_chars == 8 then
topleft, top, topright, right, botright, bot, botleft, left = unpack(window_chars)
else
topleft, top, topright, right, botright, bot, botleft, left = '','', '', '', '','', '', ''
end
local border_lines = { topleft .. string.rep(top, width) .. topright }
local middle_line = left .. string.rep(' ', width) .. right
for _ = 1, height do
table.insert(border_lines, middle_line)
end
table.insert(border_lines, botleft .. string.rep(bot, width) .. botright)
-- create a unlisted scratch buffer for the border
local border_buffer = api.nvim_create_buf(false, true)
-- set border_lines in the border buffer from start 0 to end -1 and strict_indexing false
api.nvim_buf_set_lines(border_buffer, 0, -1, true, border_lines)
-- create border window
local border_window = api.nvim_open_win(border_buffer, true, border_opts)
vim.api.nvim_set_hl(0, "LazyGitBorder", { link = "Normal", default = true })
vim.cmd('set winhl=NormalFloat:LazyGitBorder')
-- create a unlisted scratch buffer
if LAZYGIT_BUFFER == nil or vim.fn.bufwinnr(LAZYGIT_BUFFER) == -1 then
LAZYGIT_BUFFER = api.nvim_create_buf(false, true)
else
LAZYGIT_LOADED = true
end
-- create file window, enter the window, and use the options defined in opts
local win = api.nvim_open_win(LAZYGIT_BUFFER, true, opts)
vim.bo[LAZYGIT_BUFFER].filetype = 'lazygit'
vim.cmd('setlocal bufhidden=hide')
vim.cmd('setlocal nocursorcolumn')
vim.api.nvim_set_hl(0, "LazyGitFloat", { link = "Normal", default = true })
vim.cmd('setlocal winhl=NormalFloat:LazyGitFloat')
vim.cmd('set winblend=' .. vim.g.lazygit_floating_window_winblend)
-- use autocommand to ensure that the border_buffer closes at the same time as the main buffer
local cmd = [[autocmd WinLeave <buffer> silent! execute 'hide']]
vim.cmd(cmd)
cmd = [[autocmd WinLeave <buffer> silent! execute 'silent bdelete! %s']]
vim.cmd(cmd:format(border_buffer))
return win, LAZYGIT_BUFFER
end
return {
open_floating_window = open_floating_window,
}

View File

@ -0,0 +1,93 @@
local pickers = require("telescope.pickers")
local finders = require("telescope.finders")
local actions = require("telescope.actions")
local action_state = require("telescope.actions.state")
local conf = require("telescope.config").values
local lazygit_utils = require("lazygit.utils")
local function open_lazygit(prompt_buf)
local entry = action_state.get_selected_entry()
vim.fn.execute('cd ' .. entry.value)
local cmd = [[lua require"lazygit".lazygit(nil)]]
vim.api.nvim_command(cmd)
vim.cmd('stopinsert')
vim.cmd([[execute "normal i"]])
vim.fn.feedkeys('j')
vim.api.nvim_buf_set_keymap(0, 't', '<Esc>', '<Esc>', {noremap = true, silent = true})
end
local lazygit_repos = function(opts)
local displayer = require("telescope.pickers.entry_display").create {
separator = "",
-- TODO: make use of telescope geometry
items = {
{width = 4},
{width = 55},
{remaining = true},
},
}
local repos = {}
for _, v in pairs(lazygit_utils.lazygit_visited_git_repos) do
if v == nil then
goto skip
end
local index = #repos + 1
local entry =
{
idx = index,
value = v:gsub("%s", ""),
-- retrieve git repo name
repo_name= v:gsub("%s", ""):match("^.+/(.+)$"),
}
table.insert(repos, index, entry)
::skip::
end
pickers.new(opts or {}, {
prompt_title = "lazygit repos",
finder = finders.new_table {
results = repos,
entry_maker = function(entry)
local make_display = function()
return displayer
{
{entry.idx},
{entry.repo_name},
}
end
return {
value = entry.value,
ordinal = string.format("%s %s", entry.idx, entry.repo_name),
display = make_display,
}
end,
},
sorter = conf.generic_sorter(opts),
attach_mappings = function(prompt_buf, _)
actions.select_default:replace(function ()
-- for what ever reason any attempt to open an external window (such as lazygit)
-- shall be done after closing the buffer manually
actions.close(prompt_buf)
open_lazygit()
end
)
return true
end
}):find()
end
return require("telescope").register_extension({
exports = {
lazygit = lazygit_repos,
}
})