Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better global #283

Open
sumneko opened this issue Dec 5, 2020 · 3 comments
Open

Better global #283

sumneko opened this issue Dec 5, 2020 · 3 comments
Labels
help wanted Extra attention is needed
Milestone

Comments

@sumneko
Copy link
Collaborator

sumneko commented Dec 5, 2020

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
@ZegWe
Copy link

ZegWe commented Nov 16, 2023

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

@emmericp
Copy link
Contributor

emmericp commented Feb 3, 2024

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

@brandonsturgeon
Copy link

I love this 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants