LuaLS / LuaLS/lua-language-server

Proposal: Add @constructor Tag for Class Constructors in Lua LSP

Offen
#3,063 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
Lua
Sterne
4.4k
Forks
442
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

This Lua LSP could benefit from a new @constructor tag, which would be used when defining classes, especially with libraries. Currently, we have to do something like this:

---@class myCls
---@overload fun(a: integer, b: integer): myCls
local myCls = class "myCls"

---@param a integer
---@param b integer
function myCls:__init(a, b)
     self.a = a
     self.b = b
end
local instance = myCls(10, 20)
print(instance)

As you can see, we need to create an overload for the variable, which makes the typing process quite repetitive. To address this, we could introduce a new @constructor tag that would automatically handle this:

---@class myCls
local myCls = class "myCls" -- No need for an overload anymore.

-- Defining this method as the constructor:
---@constructor
---@param a integer
---@param b integer
function myCls:__init(a, b)
     self.a = a
     self.b = b
end
local instance = myCls(10, 20) -- Type checking would work correctly and show the constructor parameters.
print(instance)

This approach would make the code cleaner and more intuitive, reducing the need for repetitive annotations and improving the overall developer experience.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, das bestehende Verhalten von @class und @overload anhand der Beispiele in diesem Issue zu überprüfen; es werden keine Dateien, Tests oder Einstiegspunkte genannt. Als erledigt sollte gelten, dass die Kennzeichnung von init mit @constructor die Konstruktorparameter für Aufrufe wie myCls(10, 20) bereitstellt, ohne einen separaten overload zu erfordern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
lua
Bereich
devtools
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.