-
-
Notifications
You must be signed in to change notification settings - Fork 358
table literals with descriptions #2128
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
Comments
Currently, you can achieve this if you create a new class for the cat field:
|
yes |
Mind elaborating a bit on this? If I understood correctly, you don't want to define the |
yes |
Well, why not? That's how many do it. In the end, the annotations are just comments. These affect just your autocomplete suggestions, not code behavior. |
I haven't come up with a better syntax yet. For example, in the solution you provided, I can't decide where the comment ends. (Translated by ChatGPT) |
could this perhaps be done when you spread the thing to multiple lines similarly to how it's possibel to add descriptions to enums? @sumneko |
Hello, i am also very interested in this feature. In my use case there are many functions that have "named arguments" they just receive a table as first arg and take the values from there. If someone wants to give me pointers where to start i would be glad to help implement this. |
Maybe you could support syntax such as this: -- Leading whitespace is insignificant and removed/ignored by the LSP.
-- Also if a line ends with `,` or `;`, ignore/remove it from the description as well.
---@field cat {
--- name: string the name of the cat
--- age: integer the age of the cat
---}
---@param cat {
--- name: string the name of the cat,
--- age: integer the age of the cat,
---} Then just keep a count of balanced braces to find the end. Or, maybe: ---@field cat {
--- @field name string the name of the cat
--- @field age integer the age of the cat
---}
---@param cat {
--- @field name string the name of the cat
--- @field age integer the age of the cat
---} |
Using a I understand the argument here that the ---@class uv.fs_stat.result.time
---@field sec integer
---@field nsec integer
---@class uv.fs_stat.result
---
---@field dev integer
---@field mode integer
---@field nlink integer
---@field uid integer
---@field gid integer
---@field rdev integer
---@field ino integer
---@field size integer
---@field blksize integer
---@field blocks integer
---@field flags integer
---@field gen integer
---@field atime uv.fs_stat.result.time
---@field mtime uv.fs_stat.result.time
---@field ctime uv.fs_stat.result.time
---@field birthtime uv.fs_stat.result.time
---@field type string
---@class uv.fs_stat.result.date
---@field year integer
---@field month integer
---@field day integer (instead of |
I totally agree with tomlau10 here. We do the same in MWSE: https://github.com/MWSE/MWSE/blob/master/misc/package/Data%20Files/MWSE/core/meta/lib/tes3.lua#L34 --- @param params tes3.addArmorSlot.params
function tes3.addArmorSlot(params) end
---Table parameter definitions for `tes3.addArmorSlot`.
--- @class tes3.addArmorSlot.params
--- @field slot number Armor slot number. A number greater than 10 to configure a slot for.
--- @field name string The human-readable name for the armor slot.
--- @field key string? *Optional*. The key placed in the `tes3.armorSlot` table. If no key is provided, the name will be used.
--- @field weight number? *Default*: `0`. A stand-in for the armor base weight value, typically controlled by a GMST (e.g. iHelmWeight).
--- @field scalar number? *Default*: `0.1`. A multiplier with range 0.0-1.0 that controls how much of an item's armor value applies to a character's overall armor rating. For comparison, standard chest armor uses 0.3, helmets, greaves and pauldrons use 0.1, and gauntlets use 0.05.
|
The current convention is to just come up with a name and create classes (unless you only have 2-3 fields and are sure that you don't need descriptions), but it still might be nice to have a shorthand for certain cases (mainly function args/kwargs where the table type is unique and has a small number of fields that need descriptions). It's effectively an anonymous type, or an extension of a function's type/signature. But it's not a necessary feature and we can do without it for now (since we have the |
Is there a reason something like this wouldn't be a good idea for a small number of fields that are functioning as named arguments? --- @param opts table ...
--- @param opts.name string ...
--- @param opts.age integer ... or --- @param opts table ...
--- @field opts.name string ...
--- @field opts.age integer ... This would maintain nearly the same annotation conventions without adding a lot of complexity for cases when there are only 2–3 fields. Classes certainly work, but it feels like it adds a level of visual noise and the cognitive load that may not be necessary. I'd prefer to be able to read through it linearly without bouncing between different annotation blocks |
Here maybe the problems (just my guess)
---@param opts integer
---@field opts.name string ???
---@param optsA table
---@field optsB.name string ???
---@param opts table
---@field opts.a table
---@field opts.b table
---@field opts.a.name integer ???
---@field opts.b.name integer ??? the complexity here seems exponential 😅 |
Uh oh!
There was an error while loading. Please reload this page.
I expect something like this or something similar to this to work but it doesn't
also when I e.g. split it up onto multiple lines
---@field cat { ["name"]: string # the name of the cat, ["age"]: integer # the age of the cat }
everything is working fine without the descriptions
The text was updated successfully, but these errors were encountered: