Request: Limit "Fields cannot be injected" to exact classes
還沒有人認領這個 Issue。
評估
研究方向
首先使用 LuaLS 3.17.1 執行提供的 NeoVim 重現,並比較 @class 與 @class (exact)。接著追蹤 inject-field 診斷的套用方式;完成的標準是,非精確類別和精確類別可以依照提議獨立控制,同時文件所述的補全和查找行為仍然可用。
由索引模型根據 Issue 內容生成。
描述
How are you using the lua-language-server?
NeoVim
Which OS are you using?
Windows
What is the issue affecting?
Annotations
Expected Behaviour
The docs say:
Marking the class as (exact) means fields cannot be injected after the definition.
So when I use @class, I expect to not get any injection warnings unless I use (exact). I see this came up before in #2710, so I'd add: I should be able to get a warning for injections in exact classes while silencing warnings in non-exact classes.
Actual Behaviour
Any time I add a field outside of the constructor (and sometimes even ones that are defined in my class constructor), I get this warning:
Fields cannot be injected into the reference of
ClassXforfield_y. To do so, use---@classforinstance_z.
The @class annotation makes lua too strict. I'm using lua because I want to be able to inject fields.
Using annotations has multiple effects:
- It improves completion.
- It improves symbol lookups like goto definition.
- It does type checking.
But it's not great that adding annotations to get 1+2 results in tons of warnings from 3.
I understand that people may not want to add (exact) all over their codebase, but I don't want to add ---@field [any] any to all of mine.
I'd propose a change to diagnostic flags to allow independently controlling these two cases:
inject-field: suppress inject field diagnostic for non-exact classes.inject-field-exact: suppress inject field diagnostic for exact classes.
Reproduction steps
Using neovim and installed luals with Mason. Version: pkg:github/luals/[email protected]
-- Stripped-down version of https://github.com/rxi/classic/blob/master/classic.lua with an annotation added to be close to real-world class usage.
--
-- classic
--
-- Copyright (c) 2014, rxi
--
-- This module is free software; you can redistribute it and/or modify it under
-- the terms of the MIT license. See LICENSE for details.
--- @class Object
local Object = {}
Object.__index = Object
function Object:new()
end
function Object:extend()
local cls = {}
for k, v in pairs(self) do
if k:find("__") == 1 then
cls[k] = v
end
end
cls.__index = cls
cls.super = self
setmetatable(cls, self)
return cls
end
function Object:__call(...)
local obj = setmetatable({}, self)
obj:new(...)
return obj
end
-- /end classic
--- @class Item : Object
local Item = Object:extend()
function Item:new(id, category)
self.id = id
self.category = category
self.count = 0
end
function Item:Print()
print("Item", self.id, self.category, self.count)
end
--- @param item Item
function FlagDebugItem(item)
item:Print() -- with the annotation, this completes. Yay!
item.is_cheater = true -- Warning :( "Fields cannot be injected into the reference of `Item` for `is_cheater`."
end
If I configure luals to add inject-field to the Lua.diagnostics.disable list, then the warning goes away. But the warning doesn't come back when I make Item an exact class with this annotation: --- @class (exact) Item : Object
Workaround: Add a fake field of any type:
--- @class Item : Object
--- @field [any] any
local Item = Object:extend()
Additional Notes
No response
Log File
[WARN][2026-02-23 11:41:41] ...lsp/handlers.lua:564 "More than 100000 files have been scanned. The current scanned directory is `D:\\code\\project\\br`. Please see the [FAQ](https://luals.github.io/wiki/faq/#how-can-i-improve-startup-speeds) to see how you can include fewer files. It is also possible that your [configuration is incorrect](https://luals.github.io/wiki/faq#why-is-the-server-scanning-the-wrong-folder)."
[WARN][2026-02-23 14:32:31] .../lua/vim/lsp.lua:52 "method textDocument/typeDefinition is not supported by any of the servers registered for the current buffer"
[START][2026-02-23 15:29:24] LSP logging initiated
[ERROR][2026-02-23 15:29:24] ...m/lsp/client.lua:620 "LSP[lua_ls]" "Cannot find request with id 125 whilst attempting to cancel"
- 主要語言
- Lua
- 星號
- 4.4k
- 分支
- 449
- 平均合併
- 15 天 6 小時
- 30 天內合併 PR
- 4
環境準備
- 提供 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
LuaLS/lua-language-server 的其他 Issue
-
Witch未關閉
難度 1/5 1 小時以內 新手友好度 88/100
LuaLS/lua-language-server#3471 ·
-
enhancement
難度 2/5 1-3 小時 新手友好度 62/100
LuaLS/lua-language-server#1776 ·
-
難度 4/5 3-5 天 新手友好度 35/100
LuaLS/lua-language-server#3470 ·
-
難度 3/5 1-2 天 新手友好度 68/100
LuaLS/lua-language-server#3469 ·
-
難度 3/5 1-2 天 新手友好度 68/100
LuaLS/lua-language-server#3468 ·
查看 LuaLS/lua-language-server 的全部 Issue
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 70/100
維護者通常 1 天內回覆
-
documentation enhancement
難度 2/5 1-3 小時 新手友好度 62/100
EmirErtorer/ink-away.koplugin#38 · 1 則留言 ·
-
Feature Request
難度 2/5 1-3 小時 新手友好度 62/100
Questie/QuestieTrace#92 ·
維護者通常 1 天內回覆
-
bug
難度 1/5 1 小時以內 新手友好度 85/100
維護者通常 1 天內回覆
-
RadlNavi Routing V2.3.5 Rechts/Links Kombinationen halb unterdrückt可能已有人在做 @thomas-munichways 於 1 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 62/100