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

[Annotations] How to set a default value for a function parameter?

Open punkochel opened this issue 2 years ago • 3 comments

---@param name string
---@param age? number
function addPerson(name, age)
    age = age or 18
end

How can I specify that the age parameter is optional and has a default value of 18? So that in the popup-menu it looks something like this:

function addPerson(name: string [,age = 18: number])

Or

function addPerson(name: string, age = 18: number)

punkochel avatar Sep 28 '23 05:09 punkochel

This is not yet supported. I swear this has been requested before… but I cannot find any existing issues. This discussion is somewhat related: https://github.com/LuaLS/lua-language-server/discussions/1813

carsakiller avatar Sep 29 '23 03:09 carsakiller

A default for optional @fields in a @class would be nice as well. I currently document it as a comment on the field/param.

firas-assaad avatar Oct 06 '23 10:10 firas-assaad

I second this! Right now, I also include it in the description line for the specific @field or @param but a built-in way would indeed be very helpful.

Right now with it'd look something like this w\ your example:

---@param name string
---@param age? number Default: 18
function addPerson(name, age)
    age = age or 18
end

A supported way could look like any number of ways

---@param age?: 18 number
---@param age? number|18
---@param age? number = 18

or

---@param age? number
---@default 18

where the optional @default would always refer to the annotation line above and raise a warning if it's not an optional field/param - or somewhere along these lines.. with the most intuitive (to me) would probably be allowing actual values to be set as types, so this option: ---@param age? number|18.

Arxareon avatar Oct 29 '23 06:10 Arxareon