Olical / aniseed

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

[Bug] Aniseed compile potentially conflicts with `impatient.nvim` #68

Closed bangedorrunt closed 2 years ago

bangedorrunt commented 2 years ago

Using impatient.nvim give me significant startup improvement, however this also give me a weird and/or unknow bug as shown in the following

https://user-images.githubusercontent.com/1514823/134770663-9710587e-3bce-4f52-aa66-99be73d37b0b.mov

Actual behavior: a same file was loaded 4 times

Expected behavior: file is loaded normally

The config which caused the issue

;; core/macros.fnl
(fn fn? [obj]
  "Returns true if the object is a function
  This only works at compilation time"
  (and (list? obj) (or (= (->str (first obj)) :hashfn)
                       (= (->str (first obj)) :fn))))

(fn pug [val prefix?]
  (let [inter-compile-uid (_G.os.date "%s")
        name (if prefix?
                  (.. (->str (gensym prefix?)) inter-compile-uid)
                  (.. (->str (gensym :pug)) inter-compile-uid))]
  `(do
     (tset _G ,name ,val)
     ,name)))

(fn vlua [what prefix?]
  `(.. "v:lua." ,(pug what prefix?) "()"))

(fn command! [name expr]
  (let [name (->str name)]
     (if (fn? expr)
       `(vim.cmd (string.format "command! %s call %s" ,name ,(vlua expr)))
       `(vim.cmd (string.format "command! %s %s" ,name ,expr)))))

;; lsp/init.fnl
(import-macros {: nmap! : noremap! : buf-noremap!
                : augroup! : buf-augroup! : autocmd! : buf-autocmd!
                : command! : buf-command! : lua-buf-command!
                : pug : vlua} :core.macros)
(fn reload-lsp []
  (lsp.stop_client (vim.lsp.get_active_clients))
  (vim.cmd :edit))

(fn open-lsp-log []
  (let [path (vim.lsp.get_log_path)]
    (vim.cmd (.. "edit " path))))

(command! :LspLog #(open-lsp-log))
(command! :LspRestart #(reload-lsp))

Remove impatient.nvim plugin, the configuration works as normal

Quote from Aniseed creator:

... that sounds like that's going to introduce a bunch of breaking changes for aniseed since I was in charge of loading your lua before 😦 If that's doing some -> bytecode stuff and loading it automatically weird things might start happening (like what you're seeing) but I'm not sure yet ...

bangedorrunt commented 2 years ago

Hmm, it's weird, now I have the same issue with impatient.nvim plugin already removed. The macro command! mentioned above still apply in this case. Possibly caused by latest fennel update? afaik, hotpot hasn't updated to latest fennel yet.

Olical commented 2 years ago

Hmm there could be issues, Fennel is changing a LOT right now as they move towards v1, so they're making breaking changes in the run up. I think I've insulated you from the environment changes with Aniseed though.

They did change some actual compiler things too, the one that comes to mind is (when ...) used to produce a single branch if that would return absolutely nothing (not even nil) in the else case because Lua lets you do that (in the same way it lets you return many values from one function). Now (when ...) returns a two branch if and the else contains return nil basically, so you always get a value, even if it's nil.

I prefer that and I think it's the right move, but it could be a breaking change if you're expecting absolutely nothing returned from some functions in some cases.

bangedorrunt commented 2 years ago

Hmm, interesting, this could be the case then. https://github.com/bakpakin/Fennel/commit/176c90b0695a5e3880b2fc856d6a445119a7e7ee

bangedorrunt commented 2 years ago

Update: I use built-in nvim.ex.command for the command macro (instead of vim.cmd) and the issue is resolved. There actually must be something weird with macros evaluation since your rewritten.

(fn command [name expr]
  (if (fn? expr)
      `(nvim.ex.command_ ,(->str name) :call ,(vlua expr))
      `(nvim.ex.command_ ,(->str name) ,expr)))

(fn buf-command [name expr]
  (if (fn? expr)
      `(nvim.ex.command_ :-buffer ,(->str name) :call ,(vlua expr))
      `(nvim.ex.command_ :-buffer ,(->str name) ,expr)))
Olical commented 2 years ago

How's this look on develop for you now, out of interest? I've added some extra options and defaults to force the module macros to produce static ready Lua even if you're compiling in a dynamic env or where the module already exists.

I used to assume if the module is already loaded and you try to compile it you're doing an interactive eval, so it produced dynamic eval targeted Lua. Now I force it to produce static ready Lua in more scenarios.

Olical commented 2 years ago

It's working for me and shaved off about 20-50ms! That plus Aniseed's new module system which produces MUCH more efficient and minimal Lua now. (in line with what you'd hand write anyway)

bangedorrunt commented 2 years ago

@Olical first of all, kudos to your hardworking. When I saw what you said above, I couldn't wait to try it out. I'm happy to say that my issue is fixed, though it might be too soon to speak. Also, it feels snappier when startup. You're genius 🧙‍♂️

bangedorrunt commented 2 years ago

@Olical This is the reason why my autocmd called multiple times. Now we can safely put this issue at ease https://github.com/wbthomason/packer.nvim/pull/636