Skip to content

Commit 9b5ac53

Browse files
committed
fix #1409
1 parent 35c3ffc commit 9b5ac53

File tree

4 files changed

+62
-75
lines changed

4 files changed

+62
-75
lines changed

changelog.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# changelog
22

3+
## 3.5.3
4+
* `FIX` [#1409](https://github.com./sumneko/lua-language-server/issues/1409)
5+
36
## 3.5.2
47
`2022-8-1`
58
* `FIX` [#1395](https://github.com./sumneko/lua-language-server/issues/1395)

script/core/hover/return.lua

+1-31
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,6 @@
11
local vm = require 'vm.vm'
22
local guide = require 'parser.guide'
33

4-
---@param source parser.object
5-
---@return integer
6-
local function countReturns(source)
7-
local n = 0
8-
9-
local docs = source.bindDocs
10-
if docs then
11-
for _, doc in ipairs(docs) do
12-
if doc.type == 'doc.return' then
13-
for _, rtn in ipairs(doc.returns) do
14-
if rtn.returnIndex and rtn.returnIndex > n then
15-
n = rtn.returnIndex
16-
end
17-
end
18-
end
19-
end
20-
end
21-
22-
local returns = source.returns
23-
if returns then
24-
for _, rtn in ipairs(returns) do
25-
if #rtn > n then
26-
n = #rtn
27-
end
28-
end
29-
end
30-
31-
return n
32-
end
33-
344
---@param source parser.object
355
---@return parser.object[]
366
local function getReturnDocs(source)
@@ -51,7 +21,7 @@ local function getReturnDocs(source)
5121
end
5222

5323
local function asFunction(source)
54-
local num = countReturns(source)
24+
local _, _, num = vm.countReturnsOfFunction(source)
5525
if num == 0 then
5626
return nil
5727
end

script/vm/function.lua

+42-27
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@ end
1919
---@param func parser.object
2020
---@return integer min
2121
---@return number max
22+
---@return integer def
2223
function vm.countParamsOfFunction(func)
2324
local min = 0
2425
local max = 0
26+
local def = 0
2527
if func.type == 'function' then
2628
if func.args then
2729
max = #func.args
30+
def = max
2831
for i = #func.args, 1, -1 do
2932
local arg = func.args[i]
3033
if arg.type == '...' then
@@ -44,6 +47,7 @@ function vm.countParamsOfFunction(func)
4447
if func.type == 'doc.type.function' then
4548
if func.args then
4649
max = #func.args
50+
def = max
4751
for i = #func.args, 1, -1 do
4852
local arg = func.args[i]
4953
if arg.name and arg.name[1] =='...' then
@@ -55,56 +59,58 @@ function vm.countParamsOfFunction(func)
5559
end
5660
end
5761
end
58-
return min, max
62+
return min, max, def
5963
end
6064

6165
---@param node vm.node
6266
---@return integer min
6367
---@return number max
68+
---@return integer def
6469
function vm.countParamsOfNode(node)
65-
local min, max
70+
local min, max, def
6671
for n in node:eachObject() do
6772
if n.type == 'function'
6873
or n.type == 'doc.type.function' then
6974
---@cast n parser.object
70-
local fmin, fmax = vm.countParamsOfFunction(n)
75+
local fmin, fmax, fdef = vm.countParamsOfFunction(n)
7176
if not min or fmin < min then
7277
min = fmin
7378
end
7479
if not max or fmax > max then
7580
max = fmax
7681
end
82+
if not def or fdef > def then
83+
def = fdef
84+
end
7785
end
7886
end
79-
return min or 0, max or math.huge
87+
return min or 0, max or math.huge, def or 0
8088
end
8189

8290
---@param func parser.object
8391
---@param onlyDoc? boolean
8492
---@param mark? table
8593
---@return integer min
8694
---@return number max
95+
---@return integer def
8796
function vm.countReturnsOfFunction(func, onlyDoc, mark)
8897
if func.type == 'function' then
89-
---@type integer?
90-
local min
91-
---@type number?
92-
local max
98+
---@type integer?, number?, integer?
99+
local min, max, def
93100
local hasDocReturn
94101
if func.bindDocs then
95102
local lastReturn
96103
local n = 0
97-
---@type integer?
98-
local dmin
99-
---@type number?
100-
local dmax
104+
---@type integer?, number?, integer?
105+
local dmin, dmax, ddef
101106
for _, doc in ipairs(func.bindDocs) do
102107
if doc.type == 'doc.return' then
103108
hasDocReturn = true
104109
for _, ret in ipairs(doc.returns) do
105110
n = n + 1
106111
lastReturn = ret
107112
dmax = n
113+
ddef = n
108114
if (not ret.name or ret.name[1] ~= '...')
109115
and not vm.compileNode(ret):isNullable() then
110116
dmin = n
@@ -123,19 +129,25 @@ function vm.countReturnsOfFunction(func, onlyDoc, mark)
123129
if dmax and (not max or (dmax > max)) then
124130
max = dmax
125131
end
132+
if ddef and (not def or (ddef > def)) then
133+
def = ddef
134+
end
126135
end
127136
if not onlyDoc and not hasDocReturn and func.returns then
128137
for _, ret in ipairs(func.returns) do
129-
local rmin, rmax = vm.countList(ret, mark)
138+
local rmin, rmax, ddef = vm.countList(ret, mark)
130139
if not min or rmin < min then
131140
min = rmin
132141
end
133142
if not max or rmax > max then
134143
max = rmax
135144
end
145+
if not def or ddef > def then
146+
def = ddef
147+
end
136148
end
137149
end
138-
return min or 0, max or math.huge
150+
return min or 0, max or math.huge, def or 0
139151
end
140152
if func.type == 'doc.type.function' then
141153
return vm.countList(func.returns)
@@ -147,52 +159,55 @@ end
147159
---@param mark? table
148160
---@return integer min
149161
---@return number max
162+
---@return integer def
150163
function vm.countReturnsOfCall(func, args, mark)
151164
local funcs = vm.getMatchedFunctions(func, args, mark)
152-
---@type integer?
153-
local min
154-
---@type number?
155-
local max
165+
---@type integer?, number?, integer?
166+
local min, max, def
156167
for _, f in ipairs(funcs) do
157-
local rmin, rmax = vm.countReturnsOfFunction(f, false, mark)
168+
local rmin, rmax, rdef = vm.countReturnsOfFunction(f, false, mark)
158169
if not min or rmin < min then
159170
min = rmin
160171
end
161172
if not max or rmax > max then
162173
max = rmax
163174
end
175+
if not def or rdef > def then
176+
def = rdef
177+
end
164178
end
165-
return min or 0, max or math.huge
179+
return min or 0, max or math.huge, def or 0
166180
end
167181

168182
---@param list parser.object[]?
169183
---@param mark? table
170184
---@return integer min
171185
---@return number max
186+
---@return integer def
172187
function vm.countList(list, mark)
173188
if not list then
174-
return 0, 0
189+
return 0, 0, 0
175190
end
176191
local lastArg = list[#list]
177192
if not lastArg then
178-
return 0, 0
193+
return 0, 0, 0
179194
end
180195
if lastArg.type == '...'
181196
or lastArg.type == 'varargs' then
182-
return #list - 1, math.huge
197+
return #list - 1, math.huge, #list
183198
end
184199
if lastArg.type == 'call' then
185200
if not mark then
186201
mark = {}
187202
end
188203
if mark[lastArg] then
189-
return #list - 1, math.huge
204+
return #list - 1, math.huge, #list
190205
end
191206
mark[lastArg] = true
192-
local rmin, rmax = vm.countReturnsOfCall(lastArg.node, lastArg.args, mark)
193-
return #list - 1 + rmin, #list - 1 + rmax
207+
local rmin, rmax, rdef = vm.countReturnsOfCall(lastArg.node, lastArg.args, mark)
208+
return #list - 1 + rmin, #list - 1 + rmax, #list - 1 + rdef
194209
end
195-
return #list, #list
210+
return #list, #list, #list
196211
end
197212

198213
---@param func parser.object

test/hover/init.lua

+16-17
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,6 @@ local config = require 'config'
55

66
rawset(_G, 'TEST', true)
77

8-
local accept = {
9-
['local'] = true,
10-
['setlocal'] = true,
11-
['getlocal'] = true,
12-
['setglobal'] = true,
13-
['getglobal'] = true,
14-
['field'] = true,
15-
['method'] = true,
16-
['string'] = true,
17-
['number'] = true,
18-
['integer'] = true,
19-
['doc.type.name'] = true,
20-
['doc.class.name'] = true,
21-
['function'] = true,
22-
}
23-
248
---@diagnostic disable: await-in-sync
259
function TEST(script)
2610
return function (expect)
@@ -312,7 +296,6 @@ end
312296
]]
313297
[[
314298
function x()
315-
-> unknown
316299
]]
317300

318301
TEST [[
@@ -2137,3 +2120,19 @@ local <?x?> = 1 << 2
21372120
[[
21382121
local x: integer = 4
21392122
]]
2123+
2124+
TEST [[
2125+
local function test1()
2126+
return 1, 2, 3
2127+
end
2128+
2129+
local function <?test2?>()
2130+
return test1()
2131+
end
2132+
]]
2133+
[[
2134+
function test2()
2135+
-> integer
2136+
2. integer
2137+
3. integer
2138+
]]

0 commit comments

Comments
 (0)