LuaLS / LuaLS/lua-language-server

Function Overloading Overhaul (`@function` annotation)

Aberta
#1,456 22 comentários 24 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

enhancement
Linguagem predominante
Lua
Estrelas
4.4k
Forks
442
Métricas de merge de PRs
Nenhum PR com merge em 30d

Descrição

# The Problem
I think function overloading needs some changes in order for it to really function in the way that most people would find useful. This is especially problematic with event systems, as I and [others](https://github.com/sumneko/lua-language-server/discussions/1452) have encountered.

# Example
Currently, let's say I have the following function that I want to provide an overload for:
```lua
function Shape:listen(event, callback) end
```

## Using `@overload`
The first logical option is to use [`@overload`](https://github.com/sumneko/lua-language-server/wiki/Annotations#overload):
```lua
---@class Shape
local Shape = {}

---Subscribe to an event on this shape
---@param event "Destroy"
---@param callback fun(self: Shape)
---@overload fun(event: "Repair", callback: fun(self: Shape, amount: number))
function Shape:listen(event, callback) end
```
But there is a problem, when using methods (`:`), the first parameter only gets completions for the first `@param` and ignores the `@overload` entirely.
![image](https://user-images.githubusercontent.com/61925890/183683092-958d747c-0979-4834-b172-3559a658994a.png)
Ok, so for testing, let's replace the method (`:`) with a static function (`.`):
```lua
---@class Shape
local Shape = {}

---Subscribe to an event on this shape
---@param event "Destroy"
---@param callback fun(self: Shape)
---@overload fun(event: "Repair", callback: fun(self: Shape, amount: number))
function Shape.listen(event, callback) end
```
This still isn't great, we are still offered both callbacks even though the info we have entered only matches the `@overload`. At least the first parameter was completed this time.
![image](https://user-images.githubusercontent.com/61925890/183684285-b6b125ba-c6b1-487a-8dec-1a4c5b5f2c2f.png)

## Multiple Definitions
So then maybe we try defining multiple functions where each `event` param has the type set to the event name we are looking for:
```lua
---@class Shape
local Shape = {}

---Subscribe to an event on this shape
---@param event "Destroy"
---@param callback fun(self: Shape)
function Shape:listen(event, callback) end

---Subscribe to an event on this shape
---@param event "Repair"
---@param callback fun(self: Shape, amount: number)
function Shape:listen(event, callback) end
```
Now, even as methods (`:`) we are receiving correct completions for the first parameter... nice! However, we are still receiving two completions for the callback - there is no narrowing happening. The completion also shows the `event` as `"Destroy"`, which is incorrect for our second definition as we have only allowed `"Repair"`.
![image](https://user-images.githubusercontent.com/61925890/183685902-164bb942-f11c-4dd6-bac5-dacb45af0891.png)
At least when defining the function twice, we are able to write a separate description for each of them as well as their `@param`s and `@return`s. However, we receive a warning saying that we have a duplicate field.

# Proposed Solution
See @flrgh['s idea](https://github.com/LuaLS/lua-language-server/issues/1456#issuecomment-1210963693) to add a `@function` annotation to add more in-depth support for defining functions overall.

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Direção de pesquisa

Comece pelos exemplos de @overload e de múltiplas definições da issue e, em seguida, leia a proposta vinculada de @flrgh para a anotação @function. O trabalho estará concluído quando as definições de funções derem suporte à anotação proposta com conclusões corretas específicas de eventos e callbacks, sem o aviso de definição duplicada descrito aqui.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
lua
Domínio
devtools
Tipo de issue
Funcionalidade
Dificuldade
5/5
Tempo estimado
Mais de uma semana
Status de atividade
Pouca atividade
Clareza
Precisa de esclarecimento
Facilidade para iniciantes
35/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.