Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add base dirs (#73) #179

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ global_settings = {

-- set marks specific to each git branch inside git repository
mark_branch = false,

-- all marks that belong under a base directory will be shown in the harpoon menu for that base directory
-- this feature does not work if mark_branch is true
-- to clear the list of base_dirs, specify an empty table (do not remove the base_dirs key entirely)
base_dirs = { }
}
```

Expand Down
9 changes: 9 additions & 0 deletions lua/harpoon/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ HarpoonConfig = HarpoonConfig or {}
-- tbl_deep_extend does not work the way you would think
local function merge_table_impl(t1, t2)
for k, v in pairs(t2) do
if (k == 'base_dirs') then
Copy link
Author

@tg0h tg0h Apr 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • if I add an or condition to include "menu"
  • and assuming that menu = {} (and not simply removed entirely) in the harpoon setup call
    then Selectively cache config #47 can also be fixed ... fyi @jesseleite

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heyo! So if I delete my custom menu = { ... } config (to revert to harpoon defaults in my setup method), would I still have to go and bust the cache myself somewhere? Hoping not 🙏

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yups,

  • originally, if you did this to set up your menu
require("harpoon").setup({
    menu = {
        width = vim.api.nvim_win_get_width(0) - 4,
    }
-- other settings ... 
})
  • later to remove your menu settings, you can do this
-- need to call setup again and pass an empty table to the menu key to clear the menu settings
require("harpoon").setup({
    menu = {}
-- other settings ... 
})
  • unfortunately this won't work (removing the menu key entirely)
-- need to call setup again and pass an empty table to the menu key to clear the menu settings
require("harpoon").setup({
-- other settings ... 
})

no need to mess about with deleting the harpoon cache


From my preliminary testing, providing menu={} clears up the menu settings without needing to mess about with the cache.

This is caused by the current deep merge implementation which doesn't merge nicely. The nicer solution would be to clean up the deep merge function. The work around is to detect if we are attempting to merge the menu key and simply overwrite the 'left' table. Haven't created a PR for this yet though ... but should be simple enough to add an or condition and PR ...

Copy link
Contributor

@jesseleite jesseleite Apr 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, though I disagree with having to leave menu = {} to clear some cache. What I'm suggesting in #47 is to altogether avoid caching configs that don't benefit from caching in the first place. Should people really have to provide menu = {} in order to revert back to setup function defaults for said config? 🙂

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, agree that it's not ideal. Seems like the deep merge function would also be a good excuse to have a go at implementing unit tests ... #30 ... will get round to it if I get round to it :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-- we want the base_dirs from t2 (user config) to overwrite t1
-- eg if a base dir is removed from user config
t1[k] = v
end
if type(v) == "table" then
if type(t1[k]) == "table" then
merge_table_impl(t1[k], v)
Expand Down Expand Up @@ -160,6 +165,7 @@ function M.setup(config)
local complete_config = merge_tables({
projects = {},
global_settings = {
["base_dirs"] = {},
["save_on_toggle"] = false,
["save_on_change"] = true,
["enter_on_sendcmd"] = false,
Expand All @@ -169,6 +175,9 @@ function M.setup(config)
},
}, expand_dir(c_config), expand_dir(u_config), expand_dir(config))

-- make global settings available so the utils module can use it at this point
HarpoonConfig.global_settings = complete_config.global_settings

-- There was this issue where the vim.loop.cwd() didn't have marks or term, but had
-- an object for vim.loop.cwd()
ensure_correct_config(complete_config)
Expand Down
2 changes: 1 addition & 1 deletion lua/harpoon/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ function M.nav_file(id)
local mark = Marked.get_marked_file(idx)
local filename = mark.filename
if filename:sub(1, 1) ~= "/" then
filename = vim.loop.cwd() .. "/" .. mark.filename
filename = utils.project_key() .. "/" .. mark.filename
end
local buf_id = vim.fn.bufnr(filename, true)
local set_row = not vim.api.nvim_buf_is_loaded(buf_id)
Expand Down
17 changes: 16 additions & 1 deletion lua/harpoon/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,22 @@ local M = {}
M.data_path = data_path

function M.project_key()
return vim.loop.cwd()
-- get the base dir if found, if not, return cwd
local global_settings = HarpoonConfig.global_settings
local baseDirs = global_settings.base_dirs
local cwd = vim.loop.cwd()

if (baseDirs) then
for _, baseDir in pairs(baseDirs) do
local childDir = Path:new(cwd):make_relative(baseDir)
if (baseDir == "" or childDir == cwd) then
else
return baseDir
end
end
end

return cwd
end

function M.branch_key()
Expand Down