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

fix: properly handle directory changes #614

Open
wants to merge 3 commits into
base: harpoon2
Choose a base branch
from

Conversation

cosmicboots
Copy link

@cosmicboots cosmicboots commented Jul 2, 2024

This change clears out the in-memory harpoon data and re-read it from the disk whenever a DirChanged event is triggered. This makes sure the in memory data always reflects the harpoon lists for the current working directory.

Fixes #612

This change clears out the in-memory harpoon data and re-read it from
the disk whenever a `DirChanged` event is triggered. This makes sure the
in memory data always reflects the harpoon lists for the current working
directory.
@pcyman
Copy link

pcyman commented Jul 7, 2024

Tested this and it fixes #612 for me.
Hope it gets merged!

@stroiman
Copy link

stroiman commented Jul 11, 2024

I finally got around to test this, and based on the initial testing, it works as expected. (I submitted #612)

E.g. I have keyboard shortcuts to open my init.lua in either a new tab, or new window, and set the local dir for better file navigation - e.g. telescope find files show either project-related files or files in my modularised configuration (:tabnew +tcd\ %:h:p $MYVIMRC<cr> / :vsplit +lcd\ %:p:h $MYVIMRC<cr> for tabs/windows respectively).

When switching between a project-window and config window, the harpoon list shows the relevant marks, i.e. project-related or config related marks, and the list stays persisted after I quit and reopen neovim (I believe that was the original issue).

I will drop my own branch, and point to this in my lazy config until it gets merged.

pattern = "*",
callback = function(ev)
if ev.event == "DirChanged" then
self:reset()
Copy link

@stroiman stroiman Jul 11, 2024

Choose a reason for hiding this comment

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

Suggestion for minor technical improvement:

As far as I can tell from reading the code, this change seems to reload the entire state. The data model supports having lists multiple lists by working dir, so perhaps rather than completely reloading state, just load the state for the new dir if it doesn't already exist in memory, to optimize loading? Then the new file is only loaded the first time you switch to a new directory.

Having said that, the harpoon file is so small that reading it in when changing tab or window is not a real problem. I don't experience any delays when using this branch, i.e. changing tabs is instantaneous.

Copy link
Author

Choose a reason for hiding this comment

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

I'm inclined to leave it reloading from the disk completely. Reading from a file is fast enough the user shouldn't ever notice it, so it seems like a fairly negligible performance cost when considering what it would take to properly store all directory states in memory.

One big issue I could see with caching directory states in memory is garbage collection. When do we remove a directory's state from memory? Keeping them indefinitely is asking for memory leaks (albeit small) for anyone who a long running neovim instance when working with all their projects. It's also not obvious to me what a good heuristic would be to clear them out from memory (only keep 10..?, keep for a time..?).

Ultimately, I don't see the potential performance gains being worth complicating the state management logic further.

@mike-jl
Copy link

mike-jl commented Jul 21, 2024

This doesn't really work as expected (at least under the hood). After switching working directory, there are two lists stored, both with the default name. To make it work, you also need to set self.lists = {} in the reset method.
Other than that, it works great and i implemented the feature based on your code in my extension harpoonEx.

@cosmicboots
Copy link
Author

@mike-jl good catch! Fixed in 09e58f9.

Tested and works from a user perspective as before, but now we're not dragging around old lists with us when we change directories.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants