lua-language-server icon indicating copy to clipboard operation
lua-language-server copied to clipboard

Returning module type from require wrapper

Open tech-meppem opened this issue 2 years ago • 3 comments

I cannot figure out a way to make a function return the type information of a module. Basically, we have a wrapper function for require, which takes 2 params, the first being the repository, the second being the module name. It's a weird legacy thing we have to deal with in our code. I was just wondering how I can annotate that function to return the module type of the second parameter. It can basically ignore the first parameter. I've added paths to the library & paths etc... and the typing does work when specifying using normal require, just not the custom wrapper.

essentially the code of the wrapping function (not exact, but like I said, the first param can be ignored):

function custom_require(repo, modname)
    return require(modname)
end

I was hoping it could do something similar to the following annotation, but I have not been able to do so.

---@param repo string
---@param modname string
---@return module<modname>

The only way I've got it to work functionally is to use the plugin feature to replace the text, but this screws up the colouring / highlighting of the entire file.

plugin matcher: "()local([^\n]+)custom_require%([^\n,]+,([^\n%)]+)%)()" with "---@module " .. mod .. "\nlocal" .. varDec .. "require(" .. mod .. ")". (where varDec is the second group match, and mod is the third, the first being the position)

And the "specials" don't help in this case, as that tries to use the first param, not second.

tech-meppem avatar Jun 28 '22 15:06 tech-meppem

This plugin just need to add a ---@module above the code, it dose not need to modify the custom_require itself

---@module "xxxx" -- add this line
local xx = custom_require(xx, "xxxx") -- don't modify the origin code

but this screws up the colouring / highlighting of the entire file.

Plugin should not have such a problem. Maybe it is a bug. I will test it later.

sumneko avatar Jun 28 '22 16:06 sumneko

I was hoping it could do something similar to the following annotation, but I have not been able to do so.

I can implement this feature, but I need to determine the format

sumneko avatar Jun 29 '22 07:06 sumneko

This plugin just need to add a ---@module above the code, it dose not need to modify the custom_require itself

---@module "xxxx" -- add this line
local xx = custom_require(xx, "xxxx") -- don't modify the origin code

but this screws up the colouring / highlighting of the entire file.

Plugin should not have such a problem. Maybe it is a bug. I will test it later.

After multiple tries of things, I got the plugin to work (for the most part) by doing 2 matches / replacements, instead of 1. ()local([^\n]+)custom_require%([^\n,]+,([^\n%)]+)%)() which adds "---@module" .. mod .. "\n" in front, and then ()custom_require%([^\n,]+,([^\n%)]+)%)() which is replaced with require(%s). This way, it supports both if you use local = require() or just require(). Doing both of these in 1 replace doesn't work for me (well, functionally it does, but it screws up the colours), but 2 separate ones does. Thanks for your help

tech-meppem avatar Jun 29 '22 09:06 tech-meppem