Format files

This commit is contained in:
Dheepak Krishnamurthy
2021-07-07 21:37:27 -06:00
parent e8611a38ba
commit 480e1c1c05

View File

@@ -8,220 +8,211 @@ vim.g.lazygit_opened = 0
--- Strip leading and lagging whitespace --- Strip leading and lagging whitespace
local function trim(str) local function trim(str)
return str:gsub("^%s+", ""):gsub("%s+$", "") return str:gsub('^%s+', ''):gsub('%s+$', '')
end end
--- Check if lazygit is available --- Check if lazygit is available
local function is_lazygit_available() local function is_lazygit_available()
return fn.executable("lazygit") == 1 return fn.executable('lazygit') == 1
end end
--- Get project_root_dir for git repository --- Get project_root_dir for git repository
local function project_root_dir() local function project_root_dir()
-- always use bash -- always use bash
local oldshell = vim.o.shell local oldshell = vim.o.shell
vim.o.shell = 'bash' vim.o.shell = 'bash'
-- try submodule first -- try submodule first
local gitdir = fn.system('cd "' .. fn.expand('%:p:h') .. '" && git rev-parse --show-superproject-working-tree') local gitdir = fn.system('cd "' .. fn.expand('%:p:h') .. '" && git rev-parse --show-superproject-working-tree')
if gitdir ~= "" then if gitdir ~= '' then
return trim(gitdir) return trim(gitdir)
end end
-- try file location first -- try file location first
local gitdir = fn.system('cd "' .. fn.expand('%:p:h') .. '" && git rev-parse --show-toplevel') local gitdir = fn.system('cd "' .. fn.expand('%:p:h') .. '" && git rev-parse --show-toplevel')
local isgitdir = fn.matchstr(gitdir, '^fatal:.*') == "" local isgitdir = fn.matchstr(gitdir, '^fatal:.*') == ''
if isgitdir then if isgitdir then
return trim(gitdir) return trim(gitdir)
end end
-- try symlinked file location instead -- try symlinked file location instead
gitdir = fn.system('cd "' .. fn.fnamemodify(fn.resolve(fn.expand('%:p')), ':h') .. '" && git rev-parse --show-toplevel') gitdir = fn.system(
isgitdir = fn.matchstr(gitdir, '^fatal:.*') == "" 'cd "' .. fn.fnamemodify(fn.resolve(fn.expand('%:p')), ':h') .. '" && git rev-parse --show-toplevel')
if isgitdir then isgitdir = fn.matchstr(gitdir, '^fatal:.*') == ''
return trim(gitdir) if isgitdir then
end return trim(gitdir)
end
-- revert to old shell -- revert to old shell
vim.o.shell = oldshell vim.o.shell = oldshell
-- just return current working directory -- just return current working directory
return fn.getcwd(0, 0) return fn.getcwd(0, 0)
end end
--- on_exit callback function to delete the open buffer when lazygit exits in a neovim terminal --- on_exit callback function to delete the open buffer when lazygit exits in a neovim terminal
local function on_exit(job_id, code, event) local function on_exit(job_id, code, event)
if code == 0 then if code == 0 then
-- Close the window where the LAZYGIT_BUFFER is -- Close the window where the LAZYGIT_BUFFER is
vim.cmd("silent! :q") vim.cmd('silent! :q')
LAZYGIT_BUFFER = nil LAZYGIT_BUFFER = nil
LAZYGIT_LOADED = false LAZYGIT_LOADED = false
vim.g.lazygit_opened = 0 vim.g.lazygit_opened = 0
end end
end end
--- Call lazygit --- Call lazygit
local function exec_lazygit_command(cmd) local function exec_lazygit_command(cmd)
if LAZYGIT_LOADED == false then if LAZYGIT_LOADED == false then
-- ensure that the buffer is closed on exit -- ensure that the buffer is closed on exit
vim.g.lazygit_opened = 1 vim.g.lazygit_opened = 1
vim.fn.termopen(cmd, { on_exit = on_exit }) vim.fn.termopen(cmd, { on_exit = on_exit })
end end
vim.cmd "startinsert" vim.cmd 'startinsert'
end end
--- open floating window with nice borders --- open floating window with nice borders
local function open_floating_window() local function open_floating_window()
local floating_window_scaling_factor = vim.g.lazygit_floating_window_scaling_factor local floating_window_scaling_factor = vim.g.lazygit_floating_window_scaling_factor
-- Why is this required? -- Why is this required?
-- vim.g.lazygit_floating_window_scaling_factor returns different types if the value is an integer or float -- 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 if type(floating_window_scaling_factor) == 'table' then
floating_window_scaling_factor = floating_window_scaling_factor[false] floating_window_scaling_factor = floating_window_scaling_factor[false]
end end
local status, plenary = pcall(require, 'plenary.window.float') 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 if status and vim.g.lazygit_floating_window_use_plenary and vim.g.lazygit_floating_window_use_plenary ~= 0 then
plenary.percentage_range_window(floating_window_scaling_factor, floating_window_scaling_factor) plenary.percentage_range_window(floating_window_scaling_factor, floating_window_scaling_factor)
return return
end end
local height = math.ceil(vim.o.lines * floating_window_scaling_factor) - 1 local height = math.ceil(vim.o.lines * floating_window_scaling_factor) - 1
local width = math.ceil(vim.o.columns * floating_window_scaling_factor) local width = math.ceil(vim.o.columns * floating_window_scaling_factor)
local row = math.ceil(vim.o.lines - height) / 2 local row = math.ceil(vim.o.lines - height) / 2
local col = math.ceil(vim.o.columns - width) / 2 local col = math.ceil(vim.o.columns - width) / 2
local border_opts = { local border_opts = {
style = "minimal", style = 'minimal',
relative = "editor", relative = 'editor',
row = row - 1, row = row - 1,
col = col - 1, col = col - 1,
width = width + 2, width = width + 2,
height = height + 2, height = height + 2,
} }
local opts = { local opts = { style = 'minimal', relative = 'editor', row = row, col = col, width = width, height = height }
style = "minimal",
relative = "editor",
row = row,
col = col,
width = width,
height = height,
}
local topleft, topright, botleft, botright local topleft, topright, botleft, botright
local corner_chars = vim.g.lazygit_floating_window_corner_chars local corner_chars = vim.g.lazygit_floating_window_corner_chars
if type(corner_chars) == "table" and #corner_chars == 4 then if type(corner_chars) == 'table' and #corner_chars == 4 then
topleft, topright, botleft, botright = unpack(corner_chars) topleft, topright, botleft, botright = unpack(corner_chars)
else else
topleft, topright, botleft, botright = '', '', '', '' topleft, topright, botleft, botright = '', '', '', ''
end end
local border_lines = {topleft .. string.rep('', width) .. topright} local border_lines = { topleft .. string.rep('', width) .. topright }
local middle_line = '' .. string.rep(' ', width) .. '' local middle_line = '' .. string.rep(' ', width) .. ''
for i = 1, height do for i = 1, height do
table.insert(border_lines, middle_line) table.insert(border_lines, middle_line)
end end
table.insert(border_lines, botleft .. string.rep('', width) .. botright) table.insert(border_lines, botleft .. string.rep('', width) .. botright)
-- create a unlisted scratch buffer for the border -- create a unlisted scratch buffer for the border
local border_buffer = api.nvim_create_buf(false, true) 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 -- 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) api.nvim_buf_set_lines(border_buffer, 0, -1, true, border_lines)
-- create border window -- create border window
local border_window = api.nvim_open_win(border_buffer, true, border_opts) local border_window = api.nvim_open_win(border_buffer, true, border_opts)
vim.cmd('set winhl=Normal:Floating') vim.cmd('set winhl=Normal:Floating')
-- create a unlisted scratch buffer -- create a unlisted scratch buffer
if LAZYGIT_BUFFER == nil then if LAZYGIT_BUFFER == nil or vim.fn.bufwinnr(LAZYGIT_BUFFER) > 0 then
LAZYGIT_BUFFER = api.nvim_create_buf(false, true) LAZYGIT_BUFFER = api.nvim_create_buf(false, true)
else else
LAZYGIT_LOADED = true LAZYGIT_LOADED = true
end end
-- create file window, enter the window, and use the options defined in opts -- create file window, enter the window, and use the options defined in opts
local _ = api.nvim_open_win(LAZYGIT_BUFFER, true, opts) local _ = api.nvim_open_win(LAZYGIT_BUFFER, true, opts)
vim.bo[LAZYGIT_BUFFER].filetype = 'lazygit' vim.bo[LAZYGIT_BUFFER].filetype = 'lazygit'
vim.cmd('setlocal bufhidden=hide') vim.cmd('setlocal bufhidden=hide')
vim.cmd('setlocal nocursorcolumn') vim.cmd('setlocal nocursorcolumn')
vim.cmd('set winblend=' .. vim.g.lazygit_floating_window_winblend) 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 -- 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']] local cmd = [[autocmd WinLeave <buffer> silent! execute 'hide']]
vim.cmd(cmd) vim.cmd(cmd)
cmd = [[autocmd WinLeave <buffer> silent! execute 'silent bdelete! %s']] cmd = [[autocmd WinLeave <buffer> silent! execute 'silent bdelete! %s']]
vim.cmd(cmd:format(border_buffer)) vim.cmd(cmd:format(border_buffer))
end end
--- :LazyGit entry point --- :LazyGit entry point
local function lazygit(path) local function lazygit(path)
if is_lazygit_available() ~= true then if is_lazygit_available() ~= true then
print("Please install lazygit. Check documentation for more information") print('Please install lazygit. Check documentation for more information')
return return
end end
if path == nil then if path == nil then
path = project_root_dir() path = project_root_dir()
end end
open_floating_window() open_floating_window()
local cmd = "lazygit" local cmd = 'lazygit'
if not vim.env.GIT_DIR then if not vim.env.GIT_DIR then
cmd = cmd .. " -g \"" .. path .. "/.git/\"" cmd = cmd .. ' -g "' .. path .. '/.git/"'
end end
if not vim.env.GIT_WORK_TREE then if not vim.env.GIT_WORK_TREE then
cmd = cmd .. " -w \"" .. path .. "\"" cmd = cmd .. ' -w "' .. path .. '"'
end end
exec_lazygit_command(cmd) exec_lazygit_command(cmd)
end end
--- :LazyGitFilter entry point --- :LazyGitFilter entry point
local function lazygitfilter(path) local function lazygitfilter(path)
if is_lazygit_available() ~= true then if is_lazygit_available() ~= true then
print("Please install lazygit. Check documentation for more information") print('Please install lazygit. Check documentation for more information')
return return
end end
if path == nil then if path == nil then
path = project_root_dir() path = project_root_dir()
end end
open_floating_window() open_floating_window()
local cmd = "lazygit " .. "-f " .. path local cmd = 'lazygit ' .. '-f ' .. path
exec_lazygit_command(cmd) exec_lazygit_command(cmd)
end end
--- :LazyGitConfig entry point --- :LazyGitConfig entry point
local function lazygitconfig() local function lazygitconfig()
local os = fn.substitute(fn.system('uname'), '\n', '', '') local os = fn.substitute(fn.system('uname'), '\n', '', '')
local config_file = "" local config_file = ''
if os == "Darwin" then if os == 'Darwin' then
config_file = "~/Library/Application Support/jesseduffield/lazygit/config.yml" config_file = '~/Library/Application Support/jesseduffield/lazygit/config.yml'
else else
config_file = "~/.config/jesseduffield/lazygit/config.yml" config_file = '~/.config/jesseduffield/lazygit/config.yml'
end
if fn.empty(fn.glob(config_file)) == 1 then
-- file does not exist
-- check if user wants to create it
local answer = fn.confirm('File ' .. config_file
.. ' 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 end
if fn.empty(fn.glob(config_file)) == 1 then if fn.isdirectory(fn.fnamemodify(config_file, ':h')) == false then
-- file does not exist -- directory does not exist
-- check if user wants to create it fn.mkdir(fn.fnamemodify(config_file, ':h'))
local answer = fn.confirm("File " .. config_file .. " 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(config_file, ":h")) == false then
-- directory does not exist
fn.mkdir(fn.fnamemodify(config_file, ":h"))
end
vim.cmd('edit ' .. config_file)
vim.cmd([[execute "silent! 0read !lazygit -c"]])
vim.cmd([[execute "normal 1G"]])
else
vim.cmd('edit ' .. config_file)
end end
vim.cmd('edit ' .. config_file)
vim.cmd([[execute "silent! 0read !lazygit -c"]])
vim.cmd([[execute "normal 1G"]])
else
vim.cmd('edit ' .. config_file)
end
end end
return { return { lazygit = lazygit, lazygitfilter = lazygitfilter, lazygitconfig = lazygitconfig }
lazygit = lazygit,
lazygitfilter = lazygitfilter,
lazygitconfig = lazygitconfig,
}