lua-language-server icon indicating copy to clipboard operation
lua-language-server copied to clipboard

Better global

Open sumneko opened this issue 5 years ago • 3 comments

I'm going to do some work for global variable distinguishing environments. At present, my idea is as follows:

---@global global -- the table will be merged into current globals. the second `global` can be omitted
local t = {
    X = 1,
    Y = 2,
    Z = 3,
}

print(X) -- `X` is `t.X`
---@global module1, module2 -- the table will be merged into environment `module1` and `module2`
local t = {
    X = 1,
    Y = 2,
    Z = 3,
}

print(X) -- `x` is undefined

---@environment module1, global -- set the current environment can visit `module1` and `global`
print(X) -- `X` is `t.X`

---@environment module1
print(X) -- `X` is `t.X` and `print` is undefined

EDIT:

---@partial class _G -- the table will be merged into class `_G`. the default environment is class `_G`
local t = {
    X = 1,
    Y = 2,
    Z = 3,
}

print(X) -- `X` is `t.X`
---@class module1
local t = {
    X = 1,
    Y = 2,
    Z = 3,
}

print(X) -- `x` is undefined

---@environment module1, _G -- set the current environment can visit class `module1` and `_G`
print(X) -- `X` is `t.X`

---@environment module1
print(X) -- `X` is `t.X` and `print` is undefined

sumneko avatar Dec 05 '20 06:12 sumneko

Hello, thanks for your work on this project, is there any progress on this issue recently?

ZegWe avatar Nov 16 '23 11:11 ZegWe

What's the best way to handle environments until we get first class support for them?

I'm currently thinking about writing a plugin that just emits setlocal <field> <nil> with an @type doc class annotation for every field in my environment class (which is rather small anyways) in the files that make use of that environment

emmericp avatar Feb 03 '24 16:02 emmericp

I love this 👍

sarahsturgeon avatar Jun 06 '24 08:06 sarahsturgeon