LuaLS / LuaLS/lua-language-server
Feature Request: Capture table keys similar to capturing generic types
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Lua
- Estrellas
- 4.4k
- Forks
- 442
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Descripción
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
```
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
El issue no nombra archivos del repositorio, pruebas ni puntos de entrada. Empieza por localizar la captura de tipos genéricos y el manejo de anotaciones existentes, y luego sigue cómo se representan las claves de las tablas y los alias. Se considera terminado cuando `keyof queries` conserva las claves de la tabla de entrada para las tablas devueltas y valida los valores independientes o los alias contra esas claves.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- lua
- Área
- devtools
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100