How does type hinting work when using require
?
#1110
-
Hi everyone 👋 I am getting confortable with Neovim and I recently started to develop my own lua modules, in order to create custom snippets with luasnip and treesitter. When adding my snippets, I see that I am a bit lost on how this works in lua or with the LSP. I would like to know if it's possible to get autocompletion for something like Sorry if I am too much of a newbie for being clear. I would be happy to help you help me! Thank you for your time and specially to |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 7 replies
-
Hey! |
Beta Was this translation helpful? Give feedback.
-
Okay I'm pumped! As I see after cloning your repo, the simplest way to add type hints for the snippets would be to cast when returning. I would be happy to contrbute to better type hinting for Luasnip, but I need to understand the code better. Here is a first try: -- In lua/luasnip/init.lua
...
---@class LSLazyTable
---@field expand_or_jumpable fun(): boolean
---@field expand_or_locally_jumpable fun(): boolean
---@field locally_jumpable fun(dir: number): boolean
---@field jumpable fun(dir: number): boolean
---@field expandable fun(): boolean
---@field in_snippet fun(): boolean
---@field expand fun(): boolean
---@field snip_expand fun(snippet: any, opts: table): unknown
---@field s fun(context: any, nodes: any, opts: any): unknown
---@field etc...
---@cast ls LSLazyTable
return ls A more elegant solution would be to leverage the |
Beta Was this translation helpful? Give feedback.
-
Hi @L3MON4D3 👋 I tried to play around this morning so I can contribute to type hinting Luasnip :) However, as I try to move up the code to type hint the data flow, I stumble on some of your code which makes difficult for me to guess the type you are expecting. For example, in local by_prio = {
snippets = {
-- stores sorted keys, eg 1=1000, 2=1010, 3=1020,..., used for
-- quick iterating.
order = {},
},
autosnippets = {
order = {},
},
} Can you help me understand what is the type of this class or table? I think it will take me some time to be more comfortable with the code 😅 Thanks a lot for your time! |
Beta Was this translation helpful? Give feedback.
-
Hello @L3MON4D3 👋 I finished my first draft that gives Luasnip some autocompletion and type hinting. I cloned your repo, checked out to a new branch Sorry for my noobness in submitting PR, I'm still unfamiliar with them. Can't wait to discuss the type hints with you! |
Beta Was this translation helpful? Give feedback.
-
Et voilà for the result so far: |
Beta Was this translation helpful? Give feedback.
Hey!
I think this may be down to
extras/init.lua
being a simple table, whilels
is not.The reason behind this is that
ls
contains a bunch of entries from other modules, and we want to make these accessible lazily to decrease the upfront cost of loading all of them.As to how luals could be made aware of them, maybe type-hints? But that would be quite a large endeavour 😅