Format files

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

View File

@@ -8,12 +8,12 @@ vim.g.lazygit_opened = 0
--- Strip leading and lagging whitespace
local function trim(str)
return str:gsub("^%s+", ""):gsub("%s+$", "")
return str:gsub('^%s+', ''):gsub('%s+$', '')
end
--- Check if lazygit is available
local function is_lazygit_available()
return fn.executable("lazygit") == 1
return fn.executable('lazygit') == 1
end
--- Get project_root_dir for git repository
@@ -25,20 +25,21 @@ local function project_root_dir()
-- try submodule first
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)
end
-- try file location first
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
return trim(gitdir)
end
-- try symlinked file location instead
gitdir = fn.system('cd "' .. fn.fnamemodify(fn.resolve(fn.expand('%:p')), ':h') .. '" && git rev-parse --show-toplevel')
isgitdir = fn.matchstr(gitdir, '^fatal:.*') == ""
gitdir = fn.system(
'cd "' .. fn.fnamemodify(fn.resolve(fn.expand('%:p')), ':h') .. '" && git rev-parse --show-toplevel')
isgitdir = fn.matchstr(gitdir, '^fatal:.*') == ''
if isgitdir then
return trim(gitdir)
end
@@ -54,7 +55,7 @@ end
local function on_exit(job_id, code, event)
if code == 0 then
-- Close the window where the LAZYGIT_BUFFER is
vim.cmd("silent! :q")
vim.cmd('silent! :q')
LAZYGIT_BUFFER = nil
LAZYGIT_LOADED = false
vim.g.lazygit_opened = 0
@@ -68,7 +69,7 @@ local function exec_lazygit_command(cmd)
vim.g.lazygit_opened = 1
vim.fn.termopen(cmd, { on_exit = on_exit })
end
vim.cmd "startinsert"
vim.cmd 'startinsert'
end
--- open floating window with nice borders
@@ -94,26 +95,19 @@ local function open_floating_window()
local col = math.ceil(vim.o.columns - width) / 2
local border_opts = {
style = "minimal",
relative = "editor",
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 opts = { style = 'minimal', relative = 'editor', row = row, col = col, width = width, height = height }
local topleft, topright, botleft, botright
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)
else
topleft, topright, botleft, botright = '', '', '', ''
@@ -136,7 +130,7 @@ local function open_floating_window()
vim.cmd('set winhl=Normal:Floating')
-- 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)
else
LAZYGIT_LOADED = true
@@ -160,57 +154,58 @@ end
--- :LazyGit entry point
local function lazygit(path)
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
end
if path == nil then
path = project_root_dir()
end
open_floating_window()
local cmd = "lazygit"
local cmd = 'lazygit'
if not vim.env.GIT_DIR then
cmd = cmd .. " -g \"" .. path .. "/.git/\""
cmd = cmd .. ' -g "' .. path .. '/.git/"'
end
if not vim.env.GIT_WORK_TREE then
cmd = cmd .. " -w \"" .. path .. "\""
cmd = cmd .. ' -w "' .. path .. '"'
end
exec_lazygit_command(cmd)
end
--- :LazyGitFilter entry point
local function lazygitfilter(path)
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
end
if path == nil then
path = project_root_dir()
end
open_floating_window()
local cmd = "lazygit " .. "-f " .. path
local cmd = 'lazygit ' .. '-f ' .. path
exec_lazygit_command(cmd)
end
--- :LazyGitConfig entry point
local function lazygitconfig()
local os = fn.substitute(fn.system('uname'), '\n', '', '')
local config_file = ""
if os == "Darwin" then
config_file = "~/Library/Application Support/jesseduffield/lazygit/config.yml"
local config_file = ''
if os == 'Darwin' then
config_file = '~/Library/Application Support/jesseduffield/lazygit/config.yml'
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")
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
if fn.isdirectory(fn.fnamemodify(config_file, ':h')) == false then
-- directory does not exist
fn.mkdir(fn.fnamemodify(config_file, ":h"))
fn.mkdir(fn.fnamemodify(config_file, ':h'))
end
vim.cmd('edit ' .. config_file)
vim.cmd([[execute "silent! 0read !lazygit -c"]])
@@ -220,8 +215,4 @@ local function lazygitconfig()
end
end
return {
lazygit = lazygit,
lazygitfilter = lazygitfilter,
lazygitconfig = lazygitconfig,
}
return { lazygit = lazygit, lazygitfilter = lazygitfilter, lazygitconfig = lazygitconfig }