LuaLS / LuaLS/lua-language-server

Adding ---@class breaks auto completion

Open
#3,327 3 comments 0 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?

Linux

### What is the issue affecting?

Completion

### Expected Behaviour

I'm running into an issue where adding the `---@class` annotation to a local variable seems to break auto completion. It should appear correctly even if the ---@class is affixed to a local variable.

### Actual Behaviour

The autocomplete variables and functions are not appearing.

### Reproduction steps
Here is a simple example of creating a new player class with the issue.
```lua
-- Here is a simple example of creating a new player class with the issue
-- Uncommenting the line below *breaks* the autocompletion for p.new()
-- ---@class err.Player
local Player = {}
Player.__index = Player

local function mixin_xy(o)
o.x = 0
o.y = 0

o.set_position = function(self, x, y)
self.x = x
self.y = y
end

return o
end

function Player.new()
local self = {}
self.happy = true
self = mixin_xy(self)

return setmetatable(self, Player)
end

local p = Player.new()
-- `p:set_position(), p.x, p.y, p.happy` no longer showing in auto complete if `---@class err.Player` is uncommented
```

When `---@class err.Player` is **uncommented**, and above `local Player = {}`:
Image

When `---@class err.Player` is removed or commented:

Image

The above image is the expected behaviour. However, when `---@class err.Player` is added above `local Player = {}` it no longer works.

### Additional Notes

_No response_

### 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 issue in the Visual Studio Code extension using the Lua example in the report, comparing completion with and without the `---@class err.Player` annotation. Trace the completion handling for class annotations and verify that `p:set_position()`, `p.x`, `p.y`, and `p.happy` appear when the annotation is present.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.