Skip to content

Commit fc81682

Browse files
authored
Merge branch 'master' into TIMONz1535-patch-3
2 parents 4af79e3 + 4396730 commit fc81682

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

changelog.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@
1414
* `NEW` Test CLI: `--name=<testname>` `-n=<testname>`: run specify unit test
1515
* `FIX` Fixed the error that the configuration file pointed to by the `--configpath` option was not read and loaded.
1616
* `FIX` Fixed the comment calculating in docs `---@param a string?Comment` - now its `Comment` instead of `omment`.
17+
* `NEW` `---@class` supports attribute `partial`, which will not check missing inherited fields [#3023](https://github.com/LuaLS/lua-language-server/issues/3023)
18+
```lua
19+
---@class Config
20+
---@field a number
21+
22+
---@class (partial) Config.P: Config
23+
---@field b number
24+
25+
---@type Config.P[]
26+
local cfgs = {}
27+
cfgs[1] = { b = 1 } -- no warning
28+
cfgs[2] = {} -- only warns missing `b`
29+
```
30+
This enables the previous missing field check behavior before [#2970](https://github.com/LuaLS/lua-language-server/issues/2970)
1731

1832
## 3.13.5
1933
`2024-12-20`

script/core/completion/completion.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,7 @@ local function insertEnum(state, pos, src, enums, isInArray, mark)
13511351
description = description,
13521352
kind = define.CompletionItemKind.Function,
13531353
insertText = insertText,
1354+
insertTextFormat = 2,
13541355
}
13551356
elseif src.type == 'doc.enum' then
13561357
---@cast src parser.object

script/core/diagnostics/missing-fields.lua

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ return function (uri, callback)
2626
local className = def.class[1]
2727
if not sortedDefs[className] then
2828
sortedDefs[className] = {}
29+
-- check if this class is a `partial` class
30+
-- a partial class will not check missing inherited fields
31+
local class = vm.getGlobal('type', className)
32+
---@cast class -nil
33+
for _, set in ipairs(class:getSets(uri)) do
34+
if set.type == 'doc.class'
35+
and vm.docHasAttr(set, 'partial')
36+
then
37+
sortedDefs[className].isPartial = true
38+
break
39+
end
40+
end
2941
end
3042
local samedefs = sortedDefs[className]
3143
samedefs[#samedefs+1] = def
@@ -41,8 +53,8 @@ return function (uri, callback)
4153
for className, samedefs in pairs(sortedDefs) do
4254
local missedKeys = {}
4355
for _, def in ipairs(samedefs) do
44-
local fields = vm.getFields(def)
45-
if #fields == 0 then
56+
local fields = samedefs.isPartial and def.fields or vm.getFields(def)
57+
if not fields or #fields == 0 then
4658
goto continue
4759
end
4860

@@ -78,6 +90,12 @@ return function (uri, callback)
7890
end
7991
end
8092
::continue::
93+
94+
if not samedefs.isPartial then
95+
-- if not partial class, then all fields in this class have already been checked
96+
-- because in the above uses `vm.getFields` to get all fields
97+
break
98+
end
8199
end
82100

83101
if #missedKeys == 0 then

test/diagnostics/missing-fields.lua

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,4 +462,38 @@ local function f(b) end
462462
f <!{y = 1}!>
463463
]]
464464

465+
-- partial class
466+
467+
TEST[[
468+
---@class A
469+
---@field x number
470+
471+
---@class (partial) B: A
472+
473+
---@type B
474+
local t = {}
475+
]]
476+
477+
TEST[[
478+
---@class A
479+
---@field x number
480+
481+
---@class (partial) B: A
482+
---@field y number
483+
484+
---@type B
485+
local t = <!{}!>
486+
]]
487+
488+
TEST[[
489+
---@class A
490+
---@field x number
491+
492+
---@class (partial) B: A
493+
---@field y number
494+
495+
---@type B
496+
local t = {y = 1}
497+
]]
498+
465499
--

0 commit comments

Comments
 (0)