Skip to content

Commit 9471213

Browse files
committed
1 parent d060e87 commit 9471213

File tree

7 files changed

+103
-40
lines changed

7 files changed

+103
-40
lines changed

script/core/hover/arg.lua

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ local function asFunction(source, oop)
5555
end
5656
end
5757

58-
local function asDocFunction(source)
58+
local function asDocFunction(source, oop)
5959
if not source.args then
6060
return ''
6161
end
@@ -69,15 +69,19 @@ local function asDocFunction(source)
6969
arg.extends and infer.searchAndViewInfers(arg.extends) or 'any'
7070
)
7171
end
72-
return table.concat(args, ', ')
72+
if oop then
73+
return table.concat(args, ', ', 2)
74+
else
75+
return table.concat(args, ', ')
76+
end
7377
end
7478

7579
return function (source, oop)
7680
if source.type == 'function' then
7781
return asFunction(source, oop)
7882
end
7983
if source.type == 'doc.type.function' then
80-
return asDocFunction(source)
84+
return asDocFunction(source, oop)
8185
end
8286
return ''
8387
end

script/core/hover/init.lua

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local util = require 'utility'
66
local findSource = require 'core.find-source'
77
local markdown = require 'provider.markdown'
88
local infer = require 'core.infer'
9+
local guide = require 'parser.guide'
910

1011
---@async
1112
local function getHover(source)
@@ -15,14 +16,14 @@ local function getHover(source)
1516
local descMark = {}
1617

1718
---@async
18-
local function addHover(def, checkLable)
19+
local function addHover(def, checkLable, oop)
1920
if defMark[def] then
2021
return
2122
end
2223
defMark[def] = true
2324

2425
if checkLable then
25-
local label = getLabel(def)
26+
local label = getLabel(def, oop)
2627
if not labelMark[tostring(label)] then
2728
labelMark[tostring(label)] = true
2829
md:add('lua', label)
@@ -38,27 +39,34 @@ local function getHover(source)
3839
end
3940
end
4041

42+
local oop
4143
if infer.searchAndViewInfers(source) == 'function' then
4244
local hasFunc
4345
for _, def in ipairs(vm.getDefs(source)) do
46+
if guide.isOOP(def) then
47+
oop = true
48+
end
4449
if def.type == 'function'
4550
or def.type == 'doc.type.function' then
4651
hasFunc = true
47-
addHover(def, true)
52+
addHover(def, true, oop)
4853
end
4954
end
5055
if not hasFunc then
51-
addHover(source, true)
56+
addHover(source, true, oop)
5257
end
5358
else
54-
addHover(source, true)
59+
addHover(source, true, oop)
5560
for _, def in ipairs(vm.getDefs(source)) do
61+
if guide.isOOP(def) then
62+
oop = true
63+
end
5664
local isFunction
5765
if def.type == 'function'
5866
or def.type == 'doc.type.function' then
5967
isFunction = true
6068
end
61-
addHover(def, isFunction)
69+
addHover(def, isFunction, oop)
6270
end
6371
end
6472

script/core/hover/label.lua

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ local files = require 'files'
1111
local guide = require 'parser.guide'
1212

1313
local function asFunction(source, oop)
14-
if oop == nil then
15-
oop = guide.isOOP(source, oop)
16-
end
1714
local name = buildName(source, oop)
1815
local arg = buildArg(source, oop)
1916
local rtn = buildReturn(source)
@@ -28,20 +25,6 @@ local function asFunction(source, oop)
2825
return table.concat(lines, '\n')
2926
end
3027

31-
local function asDocFunction(source, oop)
32-
local name = buildName(source, oop)
33-
local arg = buildArg(source, oop)
34-
local rtn = buildReturn(source)
35-
local lines = {}
36-
lines[1] = string.format('%s%s %s(%s)'
37-
, ''
38-
, oop and 'method' or 'function'
39-
, name or ''
40-
, arg)
41-
lines[2] = rtn
42-
return table.concat(lines, '\n')
43-
end
44-
4528
local function asDocTypeName(source)
4629
local defs = vm.getDefs(source)
4730
for _, doc in ipairs(defs) do
@@ -195,7 +178,8 @@ end
195178

196179
---@async
197180
return function (source, oop)
198-
if source.type == 'function' then
181+
if source.type == 'function'
182+
or source.type == 'doc.type.function' then
199183
return asFunction(source, oop)
200184
elseif source.type == 'local'
201185
or source.type == 'getlocal'
@@ -217,8 +201,6 @@ return function (source, oop)
217201
elseif source.type == 'number'
218202
or source.type == 'integer' then
219203
return asNumber(source)
220-
elseif source.type == 'doc.type.function' then
221-
return asDocFunction(source, oop)
222204
elseif source.type == 'doc.type.name' then
223205
return asDocTypeName(source)
224206
elseif source.type == 'doc.field.name' then

script/core/hover/name.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ local function asGlobal(source)
4646
return guide.getKeyName(source)
4747
end
4848

49-
local function asDocFunction(source)
49+
local function asDocFunction(source, oop)
5050
local doc = guide.getParentType(source, 'doc.type')
5151
or guide.getParentType(source, 'doc.overload')
5252
if not doc or not doc.bindSources then
5353
return ''
5454
end
5555
for _, src in ipairs(doc.bindSources) do
56-
local name = buildName(src)
56+
local name = buildName(src, oop)
5757
if name ~= '' then
5858
return name
5959
end
@@ -89,7 +89,7 @@ function buildName(source, oop)
8989
return asTableField(source) or '', oop
9090
end
9191
if source.type == 'doc.type.function' then
92-
return asDocFunction(source), oop
92+
return asDocFunction(source, oop), oop
9393
end
9494
if source.type == 'doc.field' then
9595
return asDocField(source), oop

script/parser/guide.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,7 @@ function m.isInString(ast, position)
11601160
end)
11611161
end
11621162

1163-
function m.isOOP(source, oop)
1163+
function m.isOOP(source)
11641164
if source.type == 'setmethod'
11651165
or source.type == 'getmethod' then
11661166
return true

script/parser/luadoc.lua

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,9 @@ local function parseTypeUnitLiteralTable()
480480
return typeUnit
481481
end
482482

483-
local function parseTypeUnit(parent, content)
483+
local parseTypeUnit
484+
485+
local function parseDocFunction(parent, content)
484486
if content == 'async' then
485487
local tp, cont = peekToken()
486488
if tp == 'name' then
@@ -494,12 +496,17 @@ local function parseTypeUnit(parent, content)
494496
end
495497
end
496498
end
497-
local result
498499
if content == 'fun' then
499-
result = parseTypeUnitFunction()
500+
return parseTypeUnitFunction()
500501
end
501-
if content == '{' then
502-
result = parseTypeUnitLiteralTable()
502+
end
503+
504+
function parseTypeUnit(parent, content)
505+
local result = parseDocFunction(parent, content)
506+
if not result then
507+
if content == '{' then
508+
result = parseTypeUnitLiteralTable()
509+
end
503510
end
504511
if not result then
505512
result = {
@@ -916,7 +923,8 @@ end
916923

917924
local function parseOverload()
918925
local tp, name = peekToken()
919-
if tp ~= 'name' or name ~= 'fun' then
926+
if tp ~= 'name'
927+
or (name ~= 'fun' and name ~= 'async') then
920928
pushError {
921929
type = 'LUADOC_MISS_FUN_AFTER_OVERLOAD',
922930
start = getFinish(),
@@ -928,7 +936,7 @@ local function parseOverload()
928936
local result = {
929937
type = 'doc.overload',
930938
}
931-
result.overload = parseTypeUnitFunction()
939+
result.overload = parseDocFunction(result, name)
932940
if not result.overload then
933941
return nil
934942
end

test/crossfile/hover.lua

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,3 +1072,64 @@ global G: A {
10721072
}
10731073
```]]
10741074
}
1075+
1076+
TEST {
1077+
{
1078+
path = 'a.lua',
1079+
content = [[
1080+
---@overload fun(self, a)
1081+
function C:<?f?>(a, b) end
1082+
]]
1083+
},
1084+
hover = [[
1085+
```lua
1086+
method C:f(a: any, b: any)
1087+
```
1088+
1089+
---
1090+
1091+
```lua
1092+
method C:f(a: any)
1093+
```]]
1094+
}
1095+
1096+
TEST {
1097+
{
1098+
path = 'a.lua',
1099+
content = [[
1100+
---@overload fun(self, a)
1101+
function C.<?f?>(a, b) end
1102+
]]
1103+
},
1104+
hover = [[
1105+
```lua
1106+
function C.f(a: any, b: any)
1107+
```
1108+
1109+
---
1110+
1111+
```lua
1112+
function C.f(self: any, a: any)
1113+
```]]
1114+
}
1115+
1116+
TEST {
1117+
{
1118+
path = 'a.lua',
1119+
content = [[
1120+
---@async
1121+
---@overload async fun(self, a)
1122+
function C:<?f?>(a, b) end
1123+
]]
1124+
},
1125+
hover = [[
1126+
```lua
1127+
async method C:f(a: any, b: any)
1128+
```
1129+
1130+
---
1131+
1132+
```lua
1133+
async method C:f(a: any)
1134+
```]]
1135+
}

0 commit comments

Comments
 (0)