Olical / aniseed

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

Evaluation of a form in a module hides modules vars from neovim #77

Closed nbardiuk closed 3 years ago

nbardiuk commented 3 years ago

I am using aniseed to configure some autogroups in my neovim. After recent plugin update I've started noticing lua errors when opening buffers.

Below is minimal example how I define autocmd:

(module repro)

(defn group-body []
  (vim.notify "entering"))

(vim.schedule
  #(vim.cmd
     "augroup testing
      autocmd!
      autocmd BufEnter * lua require('repro')['group-body']()
      augroup end"))

This setup works in general but breaks when I evaluate any expression using conjure/aniseed inside this repro module, like simple (+ 1 1)

...E5108: Error executing lua [string ":lua"]:1: attempt to call field 'group-body' (a nil value)

It looks like when I evaluate expression in repro module the group-body function dissapears from vim runtime.

Is there a better way to define vim autocmd that uses fennel code and supports conjure/aniseed evaluations?

Versions:

NVIM v0.5.1
Build type: Release
LuaJIT 2.1.0-beta3
Olical commented 3 years ago

It will be called group_body in the compiled Lua, not group-body, this is a quirk of Fennel, kind of. Lua doesn't support names with hyphens in them so Fennel compiles all names to Lua compatible equivalents which we have to bear in mind when referencing at runtime from something other than Fennel. You should be able to see the name in the compiled Lua output.

This should do the trick: lua require('repro')['group_body']()

nbardiuk commented 3 years ago

Sorry, my reproduction example is a bit missleading, I actually don't have dashes in my actual config.

This autogroup setup works in everyday situations, it breaks only if I try to evaluate some expression in the modules buffer using conjure/aniseed.

Olical commented 3 years ago

Hm okay, odd! And just to confirm, do the filename and module names line up in your real code? Because if your file is foo/bar.fnl and your module name isn't foo.bar you'll also run into issues.

nbardiuk commented 3 years ago

Yes, in real code everything is in ~/.config/nvim/fnl/init.fnl and module name is init.

Olical commented 3 years ago

So, just a hunch, but is it still broken after the very latest commits I made in the last ~30 mins? I just fixed something around interactive evals + the module macros. It was sometimes putting the module macros into static compilation mode which produces efficient Lua (for writing to static Lua files) but wipes the module table which could cause the behaviour you were seeing.

Olical commented 3 years ago

Which would explain it breaking semi-recently since I only recently started to distinguish between "static" and "dynamic" evals / module macros which focus on different things. One being efficiency and wiping whatever is there already, the other augmenting whatever is currently in the environment with smarter, slower, dynamic code.

nbardiuk commented 3 years ago

Perfect timing! With the latest commit it works like a charm. Thank you!

Olical commented 3 years ago

Wonderful! Sorry if it caused you issues for a while though!