Skip to content

Conversation

@gwuen
Copy link
Contributor

@gwuen gwuen commented Apr 13, 2025

When g:vimtex_doc_handlers holds a lambda expression or bare Lua function, the iterator itself will become a Funcref. To avoid E704, this Funcref variable name must start with a capital letter.

In practice, this means one can directly pass in a Lua function:

vim.g.vimtex_doc_handlers = { MyVimtexDocHandler }

Instead of having to create a Vim wrapper function, which can be call()ed directly by its name:

function! MyVimtexDocHandler(context)
  return v:lua.MyVimtexDocHandler(a:context)
endfunction

let g:vimtex_doc_handlers = ['MyVimtexDocHandler']

When `g:vimtex_doc_handlers` holds a lambda expression or bare Lua
function, the iterator itself will become a Funcref. To avoid E704,
this Funcref variable name must start with a capital letter.
@gwuen
Copy link
Contributor Author

gwuen commented Apr 13, 2025

By the way, I'd be really happy if someone can explain to me why :h lua-call clearly says:

v:lua without a call is not allowed in a Vimscript expression: Funcrefs cannot represent Lua functions.

While the error message suggests that the Lua function I passed is now held as a Funcref:

E704: Funcref variable name must start with a capital: l:handler

Is this because the bare Lua function is treated the same as a lambda expression when handled inside Vim script? This is at least the behavior I observed, but I couldn't find any conclusive information in the Neovim docs or online.

So if my reasoning in the commit text is wrong, feel free to correct me :)


And a note for anyone considering using a Lua function: From inside Lua, there's absolutely no way1 to modify the context dictionary passed to the handler outside its scope, see :h lua-vimscript:

Objects passed over this [Lua-Vimscript] bridge are COPIED (marshalled): there are no "references".

So one will want to implement all the doc-handling logic inside the function, not relying on handlers down the list to open the selected candidate. Alternatively, use a Vimscript wrapper to modify the dictionary.

Footnotes

  1. Please let me know if you think there is!

@lervag
Copy link
Owner

lervag commented Apr 17, 2025

Sorry for the slow response!

When g:vimtex_doc_handlers holds a lambda expression or bare Lua function, the iterator itself will become a Funcref. To avoid E704, this Funcref variable name must start with a capital letter.

Yes, that's right, and I think your change is both safe and useful! Thanks!

By the way, I'd be really happy if someone can explain to me why :h lua-call clearly says: …

I'm sorry, I can't explain that.

And a note for anyone considering using a Lua function: From inside Lua, there's absolutely no way to modify the context dictionary passed to the handler outside its scope, see :h lua-vimscript: …

Thanks, that's useful to know. And yes, I think you are right that there is no way to change the vimscript reference of the context dictionary here if you are moving over to Lua.


In any case, thanks for the contribution!

lervag added a commit that referenced this pull request Apr 17, 2025
@lervag lervag closed this Apr 17, 2025
@gwuen
Copy link
Contributor Author

gwuen commented Apr 18, 2025

Sorry for the slow response!

No worries at all! I appreciate your feedback. I'm genuinely impressed that you've been actively maintaining this plugin for so many years – huge thanks for your dedication!

@gwuen gwuen deleted the fix/lambda-as-doc-handler branch April 18, 2025 01:11
@lervag
Copy link
Owner

lervag commented Apr 18, 2025

No worries at all! I appreciate your feedback. I'm genuinely impressed that you've been actively maintaining this plugin for so many years – huge thanks for your dedication!

Great, and thanks for the kind words! It's been fun to keep working on this. 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants