garymjr / nvim-snippets

Snippet support using native neovim snippets
MIT License
207 stars 14 forks source link

Bug: `require("snippet.util").reload_file` does not work when reloaded file is not for the current filetype #47

Closed chrisgrieser closed 1 month ago

chrisgrieser commented 1 month ago

I, or rather my plugin nvim-scissors, uses require("snippet.util").reload_file a lot to reload snippets. I noticed that sometimes reload functions appears to fail.

I took a look at the reloading function, and could not determine any issues with it.

With a lot of trial and error, I could pin down that the issue is fixed by clearing the cache for the respective filetype via require("snippets").clear_cache(filetypeOfReloadedFile). Running that function before reloading the file results in the reloading function working 100% of the time.

My assumption is, since nvim-snippets cache is grouped by filetype and reload_file apparently only updates the currently loaded files, that the issue is due to nvim-scissors switching filetypes. In particular, the editing of the snippets happens in a special buffer with the filetype scissors-snippet, not the filetype the currently edited snippet file is for. So the moment reloading happens, the filetype is "wrong", and updating Snippets.loaded_snippets is in vain, since it gets overwritten by the (outdated) snippets from the cache.

And since the call to hot-reloading takes place at the moment nvim-scissors closes its editing buffer, there is apparently a race condition, between nvim-scissors returning to the original buffer, and nvim-snippets' cache overwriting the loaded snippets. Which would explain why the issue only occurs irregularly.

chrisgrieser commented 1 month ago

I noticed another issue, namely global snippets. The current implementation of the file-reloading only clears the cache of the active filetype, so when you update your global snippets, they also are not updated correctly.

I think the best method to deal with all these cases is to simply fully clear the cache. And since that is also trivial to implement, I made a small PR for it.