Olical / aniseed

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

Compiling with macros #20

Closed steelsojka closed 3 years ago

steelsojka commented 3 years ago

This is more of a help request than an issue. I'm using aniseed for a plugin written in fennel. It's for using nvim-treesitter to generate documentation for various languages. I'm using macros (I'm new to them) for the templating language. Anyways, I tried "requireing" or importing the macros, but I don't get any lua output when using the compile script (for the file using the macros). I get an error when trying to evaluate the file locally, saying it couldn't find the macro module.

Here is the branch I'm working with for a more in depth look https://github.com/nvim-treesitter/nvim-tree-docs/tree/rewrite-in-fennel

File using the macros: fnl/nvim-tree-docs/specs/javascript/jsdoc.fnl

(require-macros "nvim-tree-docs.macros")

(doc-spec {:spec "jsdoc" :lang "javascript"})

(fn get-param-name [ctx param]
  (if param.default_value
    (string.format "%s=%s"
                   (ctx.get-text param.name)
                   (ctx.get-text param.default_value))
    (ctx.get-text param.name)))

(template function
  "/**"
  [" *" (%= name) "description"]
  #(when $.export
     " * @export")
  #(each [i param $.parameters]
    [" * @param" #(get-param-name $ param) "{any} - The" (%= name param)])
  #(when $.return_statement
     " * returns {any} The result")
  " */")

(template variable
  "/**"
  " * Description"
  #(when $.export " * @export")
  " * @type {any}"
  " */")

Macro file: fnl/nvim-tree-docs/macros.fnl

(local modsym (gensym))

(fn doc-spec [config module]
  `(local ,modsym {:get-spec #(-> ,(tostring config.spec))})
   (tset (. (require "nvim-tree-docs.template") :loaded-specs)
         (.. config.lang "_" config.spec)
         ,modsym))

(fn template [kind ...]
  `(tset ,modsym ,(tostring kind)
     (fn [context#]
       (local output# [])
       (each [_# line# (ipairs ,[...])]
         (table.insert output# (context#.eval-line line# context#)))
       output#)))

(fn %= [key tbl default]
  `#(let [tbl# (or ,tbl $)]
      (or ($.get-text (. tbl# ,(tostring key)) default)))

(fn %? [key]
  `#(. $1 ,(tostring key)))

{: template
 : %=
 : %?}
Olical commented 3 years ago

So macro requires are relative to your CWD of the Lua instance I think, so you might have better luck with (require-macros "fnl.nvim-tree-docs.macros"). You can also add directories to the fennel path (fennel.path which has the same format as Lua's path IIRC).

So I think it's a path issue more than anything? See if you can get it to work by using more absolute paths (even if that's not ideal) then we can work out how to make that work nicer if it's beginning to work 🙂

steelsojka commented 3 years ago

Changing the path to fnl.nvim-tree-docs.macros worked! I knew it had to be something along those lines. I didn't see anything in the docs for fennel about how those paths were resolved. Thanks again and by the way aniseed has a very pleasant experience. Thanks for all the effort!