Skip to content

Commit 2f24f62

Browse files
committed
Improve rendering of alias/enum descriptions
Previously if an alias contained a class reference as one of its variants, we would paste the class's description into the alias description, and not display the class as one of the variants. This fixes both of those issues.
1 parent 96bcbfd commit 2f24f62

File tree

2 files changed

+88
-16
lines changed

2 files changed

+88
-16
lines changed

script/core/hover/description.lua

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,15 @@ 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 {
217+
['doc.class'] = true,
218+
['doc.alias'] = true,
219+
['doc.enum'] = true,
220+
}
221+
216222
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
223+
if types[def.type] then
220224
local comment = getBindComment(def)
221225
if comment then
222226
return comment
@@ -236,31 +240,50 @@ local function buildEnumChunk(docType, name, uri)
236240
if not docType then
237241
return nil
238242
end
243+
local concreteType = {
244+
['doc.type.string'] = true,
245+
['doc.type.integer'] = true,
246+
['doc.type.number'] = true,
247+
['doc.type.boolean'] = true,
248+
}
249+
local enumLike = {
250+
['doc.enum'] = true,
251+
['doc.alias'] = true
252+
}
253+
239254
local enums = {}
240255
local types = {}
241256
local lines = {}
242-
for _, tp in ipairs(vm.getDefs(docType)) do
257+
258+
for i, tp in ipairs(vm.getDefs(docType)) do
243259
types[#types+1] = vm.getInfer(tp):view(guide.getUri(docType))
244-
if tp.type == 'doc.type.string'
245-
or tp.type == 'doc.type.integer'
246-
or tp.type == 'doc.type.boolean'
247-
or tp.type == 'doc.type.code' then
248-
enums[#enums+1] = tp
249-
end
250-
local comment = tryDocClassComment(tp)
251-
if comment then
252-
for line in util.eachLine(comment) do
253-
lines[#lines+1] = line
260+
261+
local ty = tp.type
262+
263+
if i == 1 and not concreteType[ty] then
264+
local comment = tryDocClassComment(tp)
265+
266+
if comment then
267+
for line in util.eachLine(comment) do
268+
lines[#lines+1] = line
269+
end
254270
end
255271
end
272+
273+
if not enumLike[ty] then
274+
enums[#enums+1] = tp
275+
end
256276
end
257-
if #enums == 0 then
277+
278+
if #enums == 0 or (#enums == 1 and (enums[1].comment == nil or enums[1].comment == "")) then
258279
return nil
259280
end
281+
260282
if #lines > 0 and lines[#lines] ~= "" then
261283
lines[#lines+1] = ""
262284
end
263285
lines[#lines+1] = ('#### %s:'):format(name)
286+
264287
for _, enum in ipairs(enums) do
265288
local suffix = (enum.default and ' (default)')
266289
or (enum.additional and ' (additional)')

test/crossfile/hover.lua

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,33 @@ function f(x: string|"选项1"|"选项2")
400400
401401
---
402402
403+
#### x:
404+
- `string`
405+
- `"选项1"` — 注释1
406+
- `"选项2"` (default) — 注释2]]
407+
}
408+
409+
TEST {
410+
{
411+
path = 'a.lua',
412+
content = '',
413+
},
414+
{
415+
path = 'b.lua',
416+
content = [[
417+
---@param x
418+
---| "选项1" # 注释1
419+
---| > "选项2" # 注释2
420+
function <?f?>(x) end
421+
]]
422+
},
423+
hover = [[
424+
```lua
425+
function f(x: "选项1"|"选项2")
426+
```
427+
428+
---
429+
403430
#### x:
404431
- `"选项1"` — 注释1
405432
- `"选项2"` (default) — 注释2]]
@@ -690,6 +717,27 @@ function f()
690717
comment1
691718
comment2]]}
692719

720+
TEST {{ path = 'a.lua', content = '', }, {
721+
path = 'b.lua',
722+
content = [[
723+
---@param a boolean|string # xxx
724+
local function <?f?>(a)
725+
end
726+
]]
727+
},
728+
hover = [[
729+
```lua
730+
function f(a: boolean|string)
731+
```
732+
733+
---
734+
735+
@*param* `a` — xxx
736+
737+
#### a:
738+
- `boolean`
739+
- `string`]]}
740+
693741
TEST {{ path = 'a.lua', content = '', }, {
694742
path = 'b.lua',
695743
content = [[
@@ -710,6 +758,7 @@ function f(a: boolean)
710758
@*param* `a` — xxx
711759
712760
#### a:
761+
- `boolean`
713762
- `true` — ttt
714763
- `false` — fff]]}
715764

0 commit comments

Comments
 (0)