Merge pull request #161 from jamestrew/refactor/git-branch-specific-marks

refactor: improve git branch specific marks impl
This commit is contained in:
ThePrimeagen 2022-02-16 12:44:37 -07:00 committed by GitHub
commit b2bb0d6f2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 21 deletions

View File

@ -133,7 +133,10 @@ global_settings = {
tmux_autoclose_windows = false, tmux_autoclose_windows = false,
-- filetypes that you want to prevent from adding to the harpoon list menu. -- filetypes that you want to prevent from adding to the harpoon list menu.
excluded_filetypes = { "harpoon" } excluded_filetypes = { "harpoon" },
-- set marks specific to each git branch inside git repository
mark_branch = false,
} }
``` ```

View File

@ -46,8 +46,9 @@ local function merge_table_impl(t1, t2)
end end
end end
local function mark_config_key() local function mark_config_key(global_settings)
if HarpoonConfig.mark_branch then global_settings = global_settings or M.get_global_settings()
if global_settings.mark_branch then
return utils.branch_key() return utils.branch_key()
else else
return utils.project_key() return utils.project_key()
@ -66,12 +67,10 @@ end
local function ensure_correct_config(config) local function ensure_correct_config(config)
log.trace("_ensure_correct_config()") log.trace("_ensure_correct_config()")
local projects = config.projects local projects = config.projects
if projects[mark_config_key()] == nil then local mark_key = mark_config_key(config.global_settings)
log.debug( if projects[mark_key] == nil then
"ensure_correct_config(): No config found for:", log.debug("ensure_correct_config(): No config found for:", mark_key)
mark_config_key() projects[mark_key] = {
)
projects[mark_config_key()] = {
mark = { marks = {} }, mark = { marks = {} },
term = { term = {
cmds = {}, cmds = {},
@ -79,19 +78,16 @@ local function ensure_correct_config(config)
} }
end end
local proj = projects[mark_config_key()] local proj = projects[mark_key]
if proj.mark == nil then if proj.mark == nil then
log.debug( log.debug("ensure_correct_config(): No marks found for", mark_key)
"ensure_correct_config(): No marks found for",
mark_config_key()
)
proj.mark = { marks = {} } proj.mark = { marks = {} }
end end
if proj.term == nil then if proj.term == nil then
log.debug( log.debug(
"ensure_correct_config(): No terminal commands found for", "ensure_correct_config(): No terminal commands found for",
mark_config_key() mark_key
) )
proj.term = { cmds = {} } proj.term = { cmds = {} }
end end
@ -169,6 +165,7 @@ function M.setup(config)
["enter_on_sendcmd"] = false, ["enter_on_sendcmd"] = false,
["tmux_autoclose_windows"] = false, ["tmux_autoclose_windows"] = false,
["excluded_filetypes"] = { "harpoon" }, ["excluded_filetypes"] = { "harpoon" },
["mark_branch"] = false,
}, },
}, expand_dir(c_config), expand_dir(u_config), expand_dir(config)) }, expand_dir(c_config), expand_dir(u_config), expand_dir(config))

View File

@ -11,11 +11,20 @@ function M.project_key()
end end
function M.branch_key() function M.branch_key()
return string.gsub( -- `git branch --show-current` requires Git v2.22.0+ so going with more
vim.loop.cwd() .. "-" .. vim.fn.system("git branch --show-current"), -- widely available command
"\n", local branch = M.get_os_command_output({
"" "git",
) "rev-parse",
"--abbrev-ref",
"HEAD",
})[1]
if branch then
return vim.loop.cwd() .. "-" .. branch
else
return M.project_key()
end
end end
function M.normalize_path(item) function M.normalize_path(item)