Skip to content

Commit 35810de

Browse files
authored
Merge branch 'master' into TIMONz1535-patch-4
2 parents 5f0efc4 + 8a6d9db commit 35810de

File tree

9 files changed

+104
-32
lines changed

9 files changed

+104
-32
lines changed

changelog.md

+15
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@
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` Generic return can be optional.
17+
* `FIX` Fixed the comment calculating in docs `---@param a string?Comment` - now its `Comment` instead of `omment`.
18+
* `NEW` `---@class` supports attribute `partial`, which will not check missing inherited fields [#3023](https://github.com/LuaLS/lua-language-server/issues/3023)
19+
```lua
20+
---@class Config
21+
---@field a number
22+
23+
---@class (partial) Config.P: Config
24+
---@field b number
25+
26+
---@type Config.P[]
27+
local cfgs = {}
28+
cfgs[1] = { b = 1 } -- no warning
29+
cfgs[2] = {} -- only warns missing `b`
30+
```
31+
This enables the previous missing field check behavior before [#2970](https://github.com/LuaLS/lua-language-server/issues/2970)
1732

1833
## 3.13.5
1934
`2024-12-20`

script/cli/doc/export.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function export.getLocalPath(uri)
5959
local local_file_uri = uri
6060
local i, j = local_file_uri:find(DOC)
6161
if not j then
62-
return '[FORIEGN]'..uri
62+
return '[FOREIGN] '..uri
6363
end
6464
return local_file_uri:sub( j + 1 )
6565
end

script/core/completion/completion.lua

+1
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

+20-2
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

script/core/semantic-tokens.lua

+9-9
Original file line numberDiff line numberDiff line change
@@ -902,23 +902,23 @@ return function (uri, start, finish)
902902
return
903903
end
904904
if start <= comm.start and comm.finish <= finish then
905+
-- the same logic as in buildLuaDoc
905906
local headPos = (comm.type == 'comment.short' and comm.text:match '^%-%s*[@|]()')
906-
or (comm.type == 'comment.long' and comm.text:match '^@()')
907+
or (comm.type == 'comment.long' and comm.text:match '^%s*@()')
907908
if headPos then
908-
local atPos
909-
if comm.type == 'comment.short' then
910-
atPos = headPos + 2
911-
else
912-
atPos = headPos + #comm.mark
909+
-- absolute position of `@` symbol
910+
local startOffset = comm.start + headPos
911+
if comm.type == 'comment.long' then
912+
startOffset = comm.start + headPos + #comm.mark - 2
913913
end
914914
results[#results+1] = {
915915
start = comm.start,
916-
finish = comm.start + atPos - 2,
916+
finish = startOffset,
917917
type = define.TokenTypes.comment,
918918
}
919919
results[#results+1] = {
920-
start = comm.start + atPos - 2,
921-
finish = comm.start + atPos - 1 + #comm.text:match('%S*', headPos),
920+
start = startOffset,
921+
finish = startOffset + #comm.text:match('%S*', headPos) + 1,
922922
type = define.TokenTypes.keyword,
923923
modifieres = define.TokenModifiers.documentation,
924924
}

script/parser/compile.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ local function skipComment(isAction)
590590
local result, right = resolveLongString '*/'
591591
pushLongCommentError(left, right)
592592
State.comms[#State.comms+1] = {
593-
type = 'comment.long',
593+
type = 'comment.clong',
594594
start = left,
595595
finish = right,
596596
text = result,

script/parser/luadoc.lua

+20-16
Original file line numberDiff line numberDiff line change
@@ -1712,10 +1712,9 @@ local function trimTailComment(text)
17121712
end
17131713

17141714
local function buildLuaDoc(comment)
1715-
local text = comment.text
1716-
local startPos = (comment.type == 'comment.short' and text:match '^%-%s*@()')
1717-
or (comment.type == 'comment.long' and text:match '^@()')
1718-
if not startPos then
1715+
local headPos = (comment.type == 'comment.short' and comment.text:match '^%-%s*@()')
1716+
or (comment.type == 'comment.long' and comment.text:match '^%s*@()')
1717+
if not headPos then
17191718
return {
17201719
type = 'doc.comment',
17211720
start = comment.start,
@@ -1724,42 +1723,47 @@ local function buildLuaDoc(comment)
17241723
comment = comment,
17251724
}
17261725
end
1727-
local startOffset = comment.start
1726+
-- absolute position of `@` symbol
1727+
local startOffset = comment.start + headPos
17281728
if comment.type == 'comment.long' then
1729-
startOffset = startOffset + #comment.mark - 2
1729+
startOffset = comment.start + headPos + #comment.mark - 2
17301730
end
17311731

1732-
local doc = text:sub(startPos)
1732+
local doc = comment.text:sub(headPos)
17331733

1734-
parseTokens(doc, startOffset + startPos)
1734+
parseTokens(doc, startOffset)
17351735
local result, rests = convertTokens(doc)
17361736
if result then
17371737
result.range = math.max(comment.finish, result.finish)
17381738
local finish = result.firstFinish or result.finish
17391739
if rests then
17401740
for _, rest in ipairs(rests) do
1741-
rest.range = comment.finish
1742-
finish = rest.firstFinish or result.finish
1741+
rest.range = math.max(comment.finish, rest.finish)
1742+
finish = rest.firstFinish or rest.finish
17431743
end
17441744
end
1745-
local cstart = text:find('%S', finish - comment.start)
1746-
if cstart and cstart < comment.finish then
1745+
1746+
-- `result` can be a multiline annotation or an alias, while `doc` is the first line, so we can't parse comment
1747+
if finish >= comment.finish then
1748+
return result, rests
1749+
end
1750+
1751+
local cstart = doc:find('%S', finish - startOffset)
1752+
if cstart then
17471753
result.comment = {
17481754
type = 'doc.tailcomment',
1749-
start = cstart + comment.start,
1755+
start = startOffset + cstart,
17501756
finish = comment.finish,
17511757
parent = result,
1752-
text = trimTailComment(text:sub(cstart)),
1758+
text = trimTailComment(doc:sub(cstart)),
17531759
}
17541760
if rests then
17551761
for _, rest in ipairs(rests) do
17561762
rest.comment = result.comment
17571763
end
17581764
end
17591765
end
1760-
end
17611766

1762-
if result then
17631767
return result, rests
17641768
end
17651769

script/vm/sign.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local vm = require 'vm.vm'
55
---@class vm.sign
66
---@field parent parser.object
77
---@field signList vm.node[]
8-
---@field docGenric parser.object[]
8+
---@field docGeneric parser.object[]
99
local mt = {}
1010
mt.__index = mt
1111
mt.type = 'sign'
@@ -17,7 +17,7 @@ end
1717

1818
---@param doc parser.object
1919
function mt:addDocGeneric(doc)
20-
self.docGenric[#self.docGenric+1] = doc
20+
self.docGeneric[#self.docGeneric+1] = doc
2121
end
2222

2323
---@param uri uri
@@ -268,7 +268,7 @@ end
268268
function vm.createSign()
269269
local genericMgr = setmetatable({
270270
signList = {},
271-
docGenric = {},
271+
docGeneric = {},
272272
}, mt)
273273
return genericMgr
274274
end

test/diagnostics/missing-fields.lua

+34
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)