LuaLS / LuaLS/lua-language-server

Add `upvalue` semantic token modifier

オープン
#3,456 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
Lua
スター
4.4k
フォーク
442
PR マージ指標
30日以内にマージされた PR はありません

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Issue にはファイル名もテストも記載されていません。まず、LuaLS のセマンティックトークン生成と、変数参照に使用される字句スコープの解決を見つけ、既存のグローバル modifier がどのように割り当てられているかを追跡してください。外側のスコープの変数に解決された参照に upvalue modifier が付与され、宣言と通常のローカル変数は変更されず、ネストした closure がテストでカバーされれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
lua
領域
devtools
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
活発
明瞭さ
おおむね明確
初心者へのやさしさ
52/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。