LuaLS / LuaLS/lua-language-server

Feature Request: Capture table keys similar to capturing generic types

Aperta
#2,058 1 commento 12 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

enhancement
Lingua principale
Lua
Stelle
4.4k
Fork
442
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

I like [TypeScript's `keyof` operator](https://www.typescriptlang.org/docs/handbook/2/keyof-types.html) that basically suggests that the value of a given string must match one of the properties/keys of a specified object.

In Lua, a type declared as `@type keyof queries` would mean it is a string whose value is a key found in the `queries` table.

This is useful because I can change just the value types if the keys of a table parameter were to be captured and specified as the target for a returned table type.

Here's an example of what I currently have to show the problem:

```lua
---@param queries table
---@return table
local function QueryMany(queries)
local results = {}

for key, query in pairs(queries) do
results[key] = Query(query) -- this function returns `QueryResult?`
end

return results
end
```

So, you can see that the `queries` argument is a table, and the keys should be exactly the same in the returned table, but only the values change.

Therefore, when I hover over the returned table in VS Code it doesn't know the keys in the table and only shows the key as being a string.

In the example above, the new annotation would look like this:

```lua
---@param queries table
---@return table
```

Currently, I need to create custom aliases or cast the returned tables manually when using the `QueryMany` example function to add the desired intelli-sense in VS Code. This is a problem because the function is designed to be dynamically reusable with arbitrary table arguments.

For context, this is how I would use the above function:
```lua
local results = QueryMany({
xSpacing = "frame.dimensions.xSpacing",
ySpacing = "frame.dimensions.ySpacing",
vDirection = "frame.behavior.vDirection",
hDirection = "frame.behavior.hDirection",
})
```
(**in summary:** _the table is transformed from a key-value pair of `string`/`string` to `string`/`QueryResult?` and there is no way of telling LuaLS this without losing the field/key names_)

You could also support `keyof` for standard aliases and types:

```lua
---@type keyof queries
local queryName = "" -- the value of queryName must match a field key found in the queries table

-- or as an alias:
---@alias QueryName keyof queries
```

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Nell’issue non sono indicati file del repository, test o punti di ingresso. Inizia individuando la gestione esistente della cattura dei tipi generici e delle annotazioni, quindi traccia come vengono rappresentate le chiavi delle tabelle e gli alias. Il lavoro è completo quando `keyof queries` preserva le chiavi della tabella di input per le tabelle restituite e convalida i valori autonomi o gli alias rispetto a tali chiavi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
lua
Ambito
devtools
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.