Olical / aniseed

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

def values do not update when required multiple times within a NeoVim session #79

Closed katawful closed 2 years ago

katawful commented 2 years ago

I'm writing a colorscheme using Aniseed, and while using defs with modules is very convenient, if I were to update the colorscheme with different options (like doing: :colorscheme my_scheme_dark) during runtime, the color values I define with def are not updated like I expect (unlike defonce values). Snippet:

(module colorscheme.colors)
(if (= colorschemeStyle :dark)
    (def color_table {1 :some_dark_color ...})
    (def color_table {1 :some_light_color ...}))

(module colorscheme.syntax
  {autoload {colors colorscheme.colors}})
(defn init []
  (highlight_func_using_color (. colors.color_table 1) ...))

(module colorscheme.main)
(defn init [opt1 opt2]
... colorscheme init stuff
  ((. (require :colorscheme.syntax) :init)))

; the following is in /colors and is compiled without aniseed
; its used to integrate with :h colorscheme
((. (require :colorscheme.main) :init) :opt1 :opt2)

I set the different color styles mostly using Vim methods. I compile a raw fennel file in /colors to the necessary Lua file which passes the color options to my main file's init function. These values do get passed properly doing some basic debug printing, but when it gets to the modules that contain my color variables these never actually update during runtime, only on boot. This makes me think that this is a bug in how def works (or maybe how autoload/require works?), since the documentation suggests that def values can be overwritten on next eval:

`(defonce name value)`
  Like `def` but will only be defined once. Useful for defining stateful
  values that you don't want reset every time you evaluate the file with
  Conjure. You can store things like window or process IDs in here.

I have tried setting def values within a function that calls whenever these modules get requires/autoloaded, but that doesn't properly export these variables even though they do update properly.

katawful commented 2 years ago

I worked around it by using defn functions that return the values that I need to be updated. Not ideal, but it works

Olical commented 2 years ago

Sorry about the lack of response, I'm away this week and have been really struggling for time recently. I never intended to support def etc from anywhere other than the top level of the file in the exact same way Clojure doesn't intend for people to use def to redefine things. That's just not how it's designed.

The best approach would be to use (defonce state {}) or something similar then update values in that state table that people can access, in my opinion. I've never tested nested defs and do not intend for it to work fundamentally. Sorry if this causes any issues! And maybe the main problem here is documentation, I could be explicit about what these macros are for.

katawful commented 2 years ago

That's understandable. My workaround is very similar to what you're suggesting anyways, but more explicit documentation would be useful for sure when you have the time

Olical commented 2 years ago

Added some more notes to the help text, hope that helps people in the future! Thanks for pointing this out!