Skip to content

Commit d5279fa

Browse files
committed
Alias markdown hover does not repeat description
1 parent 8d12152 commit d5279fa

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
@@ -1512,6 +1512,7 @@ TEST {
15121512
{
15131513
path = 'a.lua',
15141514
content = [[
1515+
---Description comment
15151516
---@alias A
15161517
---| 1 # comment1
15171518
---| 2 # comment2
@@ -1528,10 +1529,33 @@ local x: 1|2
15281529
---
15291530
15301531
#### A:
1532+
Description comment
1533+
15311534
- `1` — comment1
15321535
- `2` — comment2]]
15331536
}
15341537

1538+
TEST {
1539+
{
1540+
path = 'a.lua',
1541+
content = [[
1542+
---Description comment
1543+
---@alias A number
1544+
1545+
---@type A
1546+
local <?x?>
1547+
]]
1548+
},
1549+
hover = [[
1550+
```lua
1551+
local x: number
1552+
```
1553+
1554+
---
1555+
1556+
Description comment]]
1557+
}
1558+
15351559
TEST {
15361560
{
15371561
path = 'a.lua',

0 commit comments

Comments
 (0)