LuaLS / LuaLS/lua-language-server

Difference in behavior between table literal, dictionary, and class in terms of completion

Open
#2,217 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Maybe more of a feature request than a bug, but I noticed some interesting differences between classes, dictionaries, and table literals in terms of completions.

Using this code as a base:
```lua
---@class TestClass
---@field test_group TestGroup

---@class TestGroup
---@field test_field string

---@alias TestTableDictionary {["test_group"]: {["test_field"]: string}}

---@alias TestTableLiteral {test_group: {test_field: string}}

---@type TestClass
local test_class
local test_group = "test_group"

local test1 = test_class["test_group"].test_field

---@type TestTableDictionary
local test_table_dictionary
local test_group = "test_group"

local test2 = test_table_dictionary[test_group].test_field

---@type TestTableLiteral
local test_table_literal
local test_group = "test_group"

local test3 = test_table_literal[test_group].test_field
```

I would expect the completion for test1, test2, and test3 to be equivalent. However there are some interesting differences.

For test 1 if we do this `local test1 = test_class[test_group].test_field` the type of test1 is unknown and there is no completion suggestions for `test_class[test_group].` Both `test_class["test_group"].test_field` and `test_class.test_group.test_field` work correctly.

For test 2 `test_table_dictionary[test_group].test_field` works correctly. `test_table_dictionary.test_group.test_field` gives the right value but does not show any completion suggestions like the classes do.

For test 3 the table literal is the best of all worlds. You can do ` test_table_literal[test_group].test_field` as well as get completions on test_table_literal.test_group.test_field.

As I am using classes in a lot of places and they are useful as they can be spread across multiple lines and the fields can have longer descriptions it would be awesome to see the ability to do something like `test_class[test_group].test_field` for classes

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

The issue names no source files or tests; start by tracing the language server's completion handling for class fields, dictionary aliases, and table literals using the three Lua examples. Done means equivalent indexed and dotted expressions provide the expected type and completion suggestions.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
developer-experience
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.