Skip to content

Commit 3f998af

Browse files
committed
Alias markdown hover does not repeat description
1 parent 8cd8bab commit 3f998af

2 files changed

Lines changed: 45 additions & 14 deletions

File tree

script/core/hover/description.lua

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,11 @@ local function lookUpDocComments(source)
212212
return table.concat(lines, '\n')
213213
end
214214

215-
local function tryDocClassComment(source)
215+
local function tryDocClassComment(source, types)
216+
types = types or {['doc.class'] = true}
217+
216218
for _, def in ipairs(vm.getDefs(source)) do
217-
if def.type == 'doc.class'
218-
or def.type == 'doc.alias'
219-
or def.type == 'doc.enum' then
219+
if types[def.type] then
220220
local comment = getBindComment(def)
221221
if comment then
222222
return comment
@@ -238,7 +238,9 @@ local function buildEnumChunk(docType, name, uri)
238238
end
239239
local enums = {}
240240
local types = {}
241-
local lines = {}
241+
local lines = {('#### %s:'):format(name)}
242+
local commentTypes = {['doc.enum'] = true, ['doc.alias'] = true}
243+
242244
for _, tp in ipairs(vm.getDefs(docType)) do
243245
types[#types+1] = vm.getInfer(tp):view(guide.getUri(docType))
244246
if tp.type == 'doc.type.string'
@@ -247,20 +249,22 @@ local function buildEnumChunk(docType, name, uri)
247249
or tp.type == 'doc.type.code' then
248250
enums[#enums+1] = tp
249251
end
250-
local comment = tryDocClassComment(tp)
252+
253+
local comment = tryDocClassComment(tp, commentTypes)
251254
if comment then
252255
for line in util.eachLine(comment) do
253256
lines[#lines+1] = line
254257
end
255258
end
256259
end
260+
257261
if #enums == 0 then
258262
return nil
259263
end
260-
if #lines > 0 and lines[#lines] ~= "" then
264+
if #lines > 1 then
261265
lines[#lines+1] = ""
262266
end
263-
lines[#lines+1] = ('#### %s:'):format(name)
267+
264268
for _, enum in ipairs(enums) do
265269
local suffix = (enum.default and ' (default)')
266270
or (enum.additional and ' (additional)')
@@ -410,21 +414,24 @@ local function tryDocComment(source, raw)
410414
md:add('md', getFunctionCommentMarkdown(source, raw))
411415
source = source.parent
412416
end
417+
413418
local comment = lookUpDocComments(source)
414419
md:add('md', comment)
420+
415421
if source.type == 'doc.alias' then
416422
local enums = buildEnumChunk(source, source.alias[1], guide.getUri(source))
423+
or tryDocClassComment(source, {['doc.alias'] = true})
424+
417425
md:add('md', enums)
418-
end
419-
if source.type == 'doc.enum' then
426+
elseif source.type == 'doc.enum' then
420427
local enums = buildEnumChunk(source, source.enum[1], guide.getUri(source))
428+
or tryDocClassComment(source, {['doc.enum'] = true})
429+
421430
md:add('md', enums)
422431
end
432+
423433
local result = md:string()
424-
if result == '' then
425-
return nil
426-
end
427-
return result
434+
return result ~= '' and result or nil
428435
end
429436

430437
---@async

test/crossfile/hover.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,7 @@ TEST {
14811481
{
14821482
path = 'a.lua',
14831483
content = [[
1484+
---Description comment
14841485
---@alias A
14851486
---| 1 # comment1
14861487
---| 2 # comment2
@@ -1497,10 +1498,33 @@ local x: 1|2
14971498
---
14981499
14991500
#### A:
1501+
Description comment
1502+
15001503
- `1` — comment1
15011504
- `2` — comment2]]
15021505
}
15031506

1507+
TEST {
1508+
{
1509+
path = 'a.lua',
1510+
content = [[
1511+
---Description comment
1512+
---@alias A number
1513+
1514+
---@type A
1515+
local <?x?>
1516+
]]
1517+
},
1518+
hover = [[
1519+
```lua
1520+
local x: number
1521+
```
1522+
1523+
---
1524+
1525+
Description comment]]
1526+
}
1527+
15041528
TEST {
15051529
{
15061530
path = 'a.lua',

0 commit comments

Comments
 (0)