LuaLS / LuaLS/lua-language-server

When defining global variable using `---@class _G` + `---@field`, no autocompletion in `_G.*` for those injected fields

Open
#2,714 1 comment 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Lua
Stars
4.4k
Forks
442
PR merge metrics
No merged PRs in 30d

Description

How are you using the lua-language-server?

Visual Studio Code Extension (sumneko.lua)

Which OS are you using?

Windows

What is the issue affecting?

Completion

Expected Behaviour

The _G class is defined in extension's basic.lua, representing the global vairable. I expected the following should merge the defined fields into the global variable.

---@class _G
---@field player_min integer
---@field player_max integer

print(p--[[should have autocompletion]])
print(_G.p--[[should have autocompletion]])
Actual Behaviour

LuaLs recognized the injected field in the hover preview of _G, however no autocompletion for those fields are provided, unless I write _G._G.*.

---@class _G
---@field player_min integer
---@field player_max integer

print(p--[[no autocompletion]])
print(_G.p--[[no autocompletion]])
print(_G._G.p--[[!!! has autocompletion !!!]])
Reproduction steps

Use the code snippet provided above.

Additional Notes

LuaLS actually recognized the injected fields in the hover preview of _G, just the autocompletion is not working as expected.
image

Meanwhile the workaround is the use ---@type annotation for global variable. However as discussed here, unless the global variable is of table type, otherwise any default value assigned to it will be misleading as the global variable may not be a const.

Log File

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the completion behavior in the Visual Studio Code extension using the provided Lua snippet, then inspect the extension's basic.lua definition for _G and the completion path. Done means fields declared with @field on _G appear in both global and _G.* completion without requiring _G._G.*.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.