Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@function annotation for class prototype members #2415

Closed
hinell opened this issue Nov 15, 2023 · 6 comments
Closed

@function annotation for class prototype members #2415

hinell opened this issue Nov 15, 2023 · 6 comments

Comments

@hinell
Copy link

hinell commented Nov 15, 2023

How are you using the lua-language-server?

NeoVim

Which OS are you using?

Linux

What is the issue affecting?

Annotations

Expected Behaviour

Note

This is not a bug, but a feature request

--- $HOME/.local/nvim/lua/Clipboard.lua

--- Return size of the clipboard (max 1)
--- @return number size of the clipboard
function M.Clipboard.prototype:size()
  return #self
end

-- Instantiate
local cb = M.Clipboard:new() 
print(cb:size())
--       ^^^^^^^
-- LLS doesn't recognize :size() as method of an instance currently
-- there is no way to declare there is such

Consider an example above. Currently, there is no way to tell LLS to recognize prototype based class method as actual instance method. I request LLS to provide support for this field automatically or preferrably via ability to declare @function type with params description.

Design consideration: in JSDoc for instance, there is @public tag, see: google-closure-compiler @public

Workaround

Currently, it's possible to workaround the issue temporarily by

--- Clipboard instance
--- @class M.ClipboardInstance 
--- @field print fun(self): nil
--- @field size fun(self): number

--- @class M.Clipboard
--- @return M.ClipboardInstance
function M.Clipboard:new ()
-- ...
end
@hinell hinell changed the title @method stati Class prototype members Nov 15, 2023
@Frityet
Copy link
Contributor

Frityet commented Nov 22, 2023

Lua doesn't have prototype built in like that, the equivalent being __index, so I don't see why or how it would be implemented

@hinell
Copy link
Author

hinell commented Nov 22, 2023

@Frityet We just need annotation like @function to be able to fully specify types of functions. That would be enough. Currently we are limited to @field fun(x, y, z): ...

@Frityet
Copy link
Contributor

Frityet commented Nov 22, 2023

@Frityet We just need annotation like @function to be able to fully specify types of functions. That would be enough. Currently we are limited to @field fun(x, y, z): ...

How would function be any different to

---@type fun(c: T): R
local function f(c) end

@hinell
Copy link
Author

hinell commented Nov 22, 2023

@Frityet We need to be able to define custom function types with full description of their params. fun(...): ... doesn't allow that. E.g.:

--- @function myCustomFnType
--- @param xyz string my param
--- @param bar number[]|nil optional param


--- @type myCustomFnType
local _wtf = nil

_wtf("is going on?", { 99, 88 }) -- no error by LLS

@hinell hinell changed the title Class prototype members @function annotation for class prototype members Nov 23, 2023
@carsakiller
Copy link
Collaborator

Adding a @function annotation is the conclusion we last came to in #1456. Your use case is slightly different from #1456's original problem, but they need the same solution.

@hinell
Copy link
Author

hinell commented Nov 23, 2023

Closing as duplicate #1456

@hinell hinell closed this as completed Nov 23, 2023
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

No branches or pull requests

3 participants