LuaLS / LuaLS/lua-language-server

Add `upvalue` semantic token modifier

Offen
#3,456 0 Kommentare 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

LuaLS should expose whether a variable reference is an upvalue through a new upvalue semantic token modifier.

Currently, local variables and upvalues are both reported as:

semantic token type: variable
modifiers:           -

while globals are already distinguished using:

semantic token type: variable
modifiers:           global

This makes it impossible for editors such as VS Code to visually distinguish local variables from upvalues.

Example

local function outer()
    local value = 123

    return function()
        print(value) -- upvalue
    end
end

Currently the value reference inside the inner function is:

variable

It would be useful for LuaLS to report it as:

variable + upvalue

while the declaration remains:

variable

Proposed semantic token model

variable           -> local variable
variable + upvalue -> upvalue
variable + global  -> global variable
parameter          -> function parameter

For example:

local foo = 1

local function outer(bar)
    local baz = 2

    return function(qux)
        print(foo)    -- upvalue
        print(bar)    -- upvalue
        print(baz)    -- upvalue
        print(qux)    -- parameter
        print(print)  -- global
    end
end

would produce approximately:

foo   -> variable + upvalue
bar   -> variable + upvalue
baz   -> variable + upvalue
qux   -> parameter
print -> variable + global

Ordinary locals would remain simply:

variable

Why a modifier?

A modifier seems preferable to introducing a new semantic token type because upvalue describes a property of a variable reference rather than a fundamentally different kind of symbol.

It also fits the existing LuaLS representation of globals:

variable + global

This gives editor themes a simple way to distinguish all four cases:

variable
variable + upvalue
variable + global
parameter

Scope

The upvalue modifier should be applied to references resolved to a variable declared in an enclosing lexical scope.

For example:

local x = 1

local function f()
    print(x)
end

would be:

x declaration -> variable
x reference   -> variable + upvalue

Nested closures should work recursively.

Motivation

This would allow VS Code themes to use different colors for locals, parameters, upvalues, and globals, making lexical scope much easier to understand in closure-heavy Lua/LuaJIT code.

LuaLS already performs the necessary lexical-scope resolution; this proposal would expose that information through semantic tokens.

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

Das Issue nennt keine Dateien oder Tests. Beginne damit, die Erzeugung semantischer Tokens in LuaLS und die für Variablenreferenzen verwendete Auflösung des lexikalischen Gültigkeitsbereichs zu finden; verfolge, wie der bestehende globale Modifier zugewiesen wird. Erledigt ist die Aufgabe, wenn Referenzen, die zu Variablen eines einschließenden Gültigkeitsbereichs aufgelöst werden, einen Upvalue-Modifier erhalten, Deklarationen und gewöhnliche lokale Variablen unverändert bleiben und verschachtelte Closures durch Tests abgedeckt sind.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
lua
Bereich
devtools
Issue-Typ
Feature
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Aktiv
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
52/100

Neue Issues direkt in Ihr Postfach

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