Olical / aniseed

Neovim configuration and plugins in Fennel (Lisp compiled to Lua)
https://discord.gg/wXAMr8F
The Unlicense
606 stars 28 forks source link

Source fennel changes in running neovim instance? #96

Closed stefanvanburen closed 2 years ago

stefanvanburen commented 2 years ago

Hi,

More of a question than an issue, really — with an init.vim setup, it's easy to pull in changes to a running neovim instance from an edited config via source $MYVIMRC. Is anyone aware of an easy way to do this with aniseed? I was thinking I'd be able to do something like:

:lua require('aniseed.env').init({ module = 'dotfiles.init', compile = true })

But that doesn't seem to work. Didn't see any obvious solutions in the other issues here so thought I'd ask! Thanks again for the excellent tool.

harrygallagher4 commented 2 years ago

Calling aniseed.env.init again will only compile your changes. Lua caches requires though, so require'dotfiles.init' (done internally when you call env.init) won't actually run your init module again.

(there's a ton of text below here so tl;dr: there's no good way to reload your entire configuration if it's spread out over multiple files)

If you use Conjure, you can run an entire buffer/file with \eb where <localleader>=\, this is what I do, but obviously it depends on you already using Conjure (which is great btw, if you don't use it I'd highly recommend it!)

You could also compile changes with aniseed.env.init and then manually run the compiled file with :luafile ~/.config/nvim/lua/dotfiles/init.lua. Again, this only works for a single file, any requires within the file will use the cache. Also :luafile doesn't update the cache so any modules depending on the re-run module will still have the old version cached.

plenary.nvim has a module reloader that can be used to re-init a single module AND update the module cache. This means you can re-run your dotfiles.init module, but any modules it depends on will still be cached. So if you make changes to a single file those can be reloaded pretty easily this way

require'aniseed.env'.init({ module = 'dotfiles.init', compile = true })
require'plenary.reload'.reload_module('dotfiles.init') -- or whatever module you've changed

good solution here

Well, while I was writing this horrendously long explanation I decided to write something to unload all dotfiles.* modules and then call init again. I'll leave the rest of the explanation there though.

(do
  (each [k _ (ipairs package.loaded)]
    (when (string.match k "^dotfiles%..+")
      (tset package.loaded k nil)))
  ((. (require :aniseed.env) :init) {:module :dotfiles.init :compile true}))
stefanvanburen commented 2 years ago

thanks for the detailed write-up @harrygallagher4!

trying things out, plenary.reload seems to do the trick, module-by-module.

with regards to your good solution, I'm assuming you'd need to eval that form in conjure in a running neovim instance to reload everything? any thoughts on a way to make that into a mapping? I'd love to be able to reload from anywhere with, for instance, <leader>so.

harrygallagher4 commented 2 years ago

Yeah I'd just turn it into a function and map it the same way you'd map any lua/fennel function. If you're on a newer version of neovim (since Jan. 6) vim.keymap.set can directly map functions.

(defn aniseed-reload []
  (each [k _ (ipairs package.loaded)]
    (when (string.match k "^dotfiles%..+")
      (tset package.loaded k nil)))
  ((. (require :aniseed.env) :init) {:module :dotfiles.init :compile true}))

(vim.keymap.set :n :<leader>so aniseed-reload)

; without vim.keymap.set
(global map_functions {:aniseed_reload aniseed-reload})
(vim.api.nvim_set_keymap :n :<leader>so "<cmd>lua map_functions.aniseed_reload()<cr>")
stefanvanburen commented 2 years ago

awesome! yeah, I'm still using v0.6.1 so I needed to use the second approach. The only thing additionally I needed to change was swapping ipairs for pairs in the each form. Seems to work for me, commit here!:

https://github.com/stefanvanburen/dotfiles/commit/21876b6312df9ed2118bf8f0af60dddf3a27f0f4

Going to close this out as this discussion has solved my issue, thanks a lot @harrygallagher4 😃