Skip to content

Commit 73be83c

Browse files
authored
Merge pull request #2532 from fesily/automatic-infer-function-param-type
add infer function param type
2 parents f388b95 + 87c83c3 commit 73be83c

File tree

27 files changed

+179
-94
lines changed

27 files changed

+179
-94
lines changed

locale/en-us/setting.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,12 @@ When checking the type of union type, ignore the `nil` in it.
293293
294294
When this setting is `false`, the `number|nil` type cannot be assigned to the `number` type. It can be with `true`.
295295
]]
296+
config.type.inferParamType =
297+
[[
298+
When a parameter type is not annotated, it is inferred from the function's call sites.
299+
300+
When this setting is `false`, the type of the parameter is `any` when it is not annotated.
301+
]]
296302
config.doc.privateName =
297303
'Treat specific field names as private, e.g. `m_*` means `XXX.m_id` and `XXX.m_type` are private, witch can only be accessed in the class where the definition is located.'
298304
config.doc.protectedName =

locale/pt-br/setting.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,12 @@ When checking the type of union type, ignore the `nil` in it.
293293
294294
When this setting is `false`, the `number|nil` type cannot be assigned to the `number` type. It can be with `true`.
295295
]]
296+
config.type.inferParamType = -- TODO: need translate!
297+
[[
298+
When the parameter type is not annotated, the parameter type is inferred from the function's incoming parameters.
299+
300+
When this setting is `false`, the type of the parameter is `any` when it is not annotated.
301+
]]
296302
config.doc.privateName = -- TODO: need translate!
297303
'Treat specific field names as private, e.g. `m_*` means `XXX.m_id` and `XXX.m_type` are private, witch can only be accessed in the class where the definition is located.'
298304
config.doc.protectedName = -- TODO: need translate!

locale/zh-cn/setting.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@ config.type.weakNilCheck =
292292
293293
此设置为 `false` 时,`numer|nil` 类型无法赋给 `number` 类型;为 `true` 是则可以。
294294
]]
295+
config.type.inferParamType =
296+
[[
297+
未注释参数类型时,参数类型由函数传入参数推断。
298+
299+
如果设置为 "false",则在未注释时,参数类型为 "any"。
300+
]]
295301
config.doc.privateName =
296302
'将特定名称的字段视为私有,例如 `m_*` 意味着 `XXX.m_id` 与 `XXX.m_type` 是私有字段,只能在定义所在的类中访问。'
297303
config.doc.protectedName =

locale/zh-tw/setting.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@ When checking the type of union type, ignore the `nil` in it.
292292
293293
When this setting is `false`, the `number|nil` type cannot be assigned to the `number` type. It can be with `true`.
294294
]]
295+
config.type.inferParamType = -- TODO: need translate!
296+
[[
297+
未注释参数类型时,参数类型由函数传入参数推断。
298+
299+
如果设置为 "false",则在未注释时,参数类型为 "any"。
300+
]]
295301
config.doc.privateName = -- TODO: need translate!
296302
'Treat specific field names as private, e.g. `m_*` means `XXX.m_id` and `XXX.m_type` are private, witch can only be accessed in the class where the definition is located.'
297303
config.doc.protectedName = -- TODO: need translate!

script/client.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ local function searchPatchInfo(cfg, rawKey)
278278
}
279279
end
280280

281-
---@param uri uri
281+
---@param uri? uri
282282
---@param cfg table
283283
---@param change config.change
284284
---@return json.patch?
@@ -330,7 +330,7 @@ local function makeConfigPatch(uri, cfg, change)
330330
return nil
331331
end
332332

333-
---@param uri uri
333+
---@param uri? uri
334334
---@param path string
335335
---@param changes config.change[]
336336
---@return string?

script/config/template.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ local template = {
397397
['Lua.type.castNumberToInteger'] = Type.Boolean >> true,
398398
['Lua.type.weakUnionCheck'] = Type.Boolean >> false,
399399
['Lua.type.weakNilCheck'] = Type.Boolean >> false,
400+
['Lua.type.inferParamType'] = Type.Boolean >> false,
400401
['Lua.doc.privateName'] = Type.Array(Type.String),
401402
['Lua.doc.protectedName'] = Type.Array(Type.String),
402403
['Lua.doc.packageName'] = Type.Array(Type.String),

script/core/command/autoRequire.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ end
132132

133133
---@async
134134
return function (data)
135+
---@type uri
135136
local uri = data.uri
136137
local target = data.target
137138
local name = data.name
@@ -158,5 +159,7 @@ return function (data)
158159
end
159160

160161
local offset, fmt = findInsertRow(uri)
161-
applyAutoRequire(uri, offset, name, requireName, fmt)
162+
if offset and fmt then
163+
applyAutoRequire(uri, offset, name, requireName, fmt)
164+
end
162165
end

script/core/completion/completion.lua

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ end
147147

148148
local function findParent(state, position)
149149
local text = state.lua
150+
if not text then
151+
return
152+
end
150153
local offset = guide.positionToOffset(state, position)
151154
for i = offset, 1, -1 do
152155
local char = text:sub(i, i)
@@ -675,6 +678,7 @@ local function checkGlobal(state, word, startPos, position, parent, oop, results
675678
end
676679

677680
---@async
681+
---@param parent parser.object
678682
local function checkField(state, word, start, position, parent, oop, results)
679683
if parent.tag == '_ENV' or parent.special == '_G' then
680684
local globals = vm.getGlobalSets(state.uri, 'variable')
@@ -955,8 +959,7 @@ local function checkFunctionArgByDocParam(state, word, startPos, results)
955959
end
956960
end
957961

958-
local function isAfterLocal(state, startPos)
959-
local text = state.lua
962+
local function isAfterLocal(state, text, startPos)
960963
local offset = guide.positionToOffset(state, startPos)
961964
local pos = lookBackward.skipSpace(text, offset)
962965
local word = lookBackward.findWord(text, pos)
@@ -965,6 +968,8 @@ end
965968

966969
local function collectRequireNames(mode, myUri, literal, source, smark, position, results)
967970
local collect = {}
971+
local source_start = source and smark and (source.start + #smark) or position
972+
local source_finish = source and smark and (source.finish - #smark) or position
968973
if mode == 'require' then
969974
for uri in files.eachFile(myUri) do
970975
if myUri == uri then
@@ -978,8 +983,8 @@ local function collectRequireNames(mode, myUri, literal, source, smark, position
978983
if not collect[info.name] then
979984
collect[info.name] = {
980985
textEdit = {
981-
start = smark and (source.start + #smark) or position,
982-
finish = smark and (source.finish - #smark) or position,
986+
start = source_start,
987+
finish = source_finish,
983988
newText = smark and info.name or util.viewString(info.name),
984989
},
985990
path = relative,
@@ -1006,8 +1011,8 @@ local function collectRequireNames(mode, myUri, literal, source, smark, position
10061011
if not collect[open] then
10071012
collect[open] = {
10081013
textEdit = {
1009-
start = smark and (source.start + #smark) or position,
1010-
finish = smark and (source.finish - #smark) or position,
1014+
start = source_start,
1015+
finish = source_finish,
10111016
newText = smark and open or util.viewString(open),
10121017
},
10131018
path = path,
@@ -1034,8 +1039,8 @@ local function collectRequireNames(mode, myUri, literal, source, smark, position
10341039
if not collect[path] then
10351040
collect[path] = {
10361041
textEdit = {
1037-
start = smark and (source.start + #smark) or position,
1038-
finish = smark and (source.finish - #smark) or position,
1042+
start = source_start,
1043+
finish = source_finish,
10391044
newText = smark and path or util.viewString(path),
10401045
}
10411046
}
@@ -1097,6 +1102,9 @@ end
10971102

10981103
local function checkLenPlusOne(state, position, results)
10991104
local text = state.lua
1105+
if not text then
1106+
return
1107+
end
11001108
guide.eachSourceContain(state.ast, position, function (source)
11011109
if source.type == 'getindex'
11021110
or source.type == 'setindex' then
@@ -1392,6 +1400,9 @@ end
13921400

13931401
local function checkEqualEnum(state, position, results)
13941402
local text = state.lua
1403+
if not text then
1404+
return
1405+
end
13951406
local start = lookBackward.findTargetSymbol(text, guide.positionToOffset(state, position), '=')
13961407
if not start then
13971408
return
@@ -1493,6 +1504,9 @@ local function tryWord(state, position, triggerCharacter, results)
14931504
return
14941505
end
14951506
local text = state.lua
1507+
if not text then
1508+
return
1509+
end
14961510
local offset = guide.positionToOffset(state, position)
14971511
local finish = lookBackward.skipSpace(text, offset)
14981512
local word, start = lookBackward.findWord(text, offset)
@@ -1518,7 +1532,7 @@ local function tryWord(state, position, triggerCharacter, results)
15181532
checkProvideLocal(state, word, startPos, results)
15191533
checkFunctionArgByDocParam(state, word, startPos, results)
15201534
else
1521-
local afterLocal = isAfterLocal(state, startPos)
1535+
local afterLocal = isAfterLocal(state, text, startPos)
15221536
local stop = checkKeyWord(state, startPos, position, word, hasSpace, afterLocal, results)
15231537
if stop then
15241538
return
@@ -1530,8 +1544,10 @@ local function tryWord(state, position, triggerCharacter, results)
15301544
checkLocal(state, word, startPos, results)
15311545
checkTableField(state, word, startPos, results)
15321546
local env = guide.getENV(state.ast, startPos)
1533-
checkGlobal(state, word, startPos, position, env, false, results)
1534-
checkModule(state, word, startPos, results)
1547+
if env then
1548+
checkGlobal(state, word, startPos, position, env, false, results)
1549+
checkModule(state, word, startPos, results)
1550+
end
15351551
end
15361552
end
15371553
end
@@ -1592,6 +1608,9 @@ end
15921608

15931609
local function checkTableLiteralField(state, position, tbl, fields, results)
15941610
local text = state.lua
1611+
if not text then
1612+
return
1613+
end
15951614
local mark = {}
15961615
for _, field in ipairs(tbl) do
15971616
if field.type == 'tablefield'
@@ -1610,9 +1629,11 @@ local function checkTableLiteralField(state, position, tbl, fields, results)
16101629
local left = lookBackward.findWord(text, guide.positionToOffset(state, position))
16111630
if not left then
16121631
local pos = lookBackward.findAnyOffset(text, guide.positionToOffset(state, position))
1613-
local char = text:sub(pos, pos)
1614-
if char == '{' or char == ',' or char == ';' then
1615-
left = ''
1632+
if pos then
1633+
local char = text:sub(pos, pos)
1634+
if char == '{' or char == ',' or char == ';' then
1635+
left = ''
1636+
end
16161637
end
16171638
end
16181639
if left then
@@ -1801,6 +1822,7 @@ local function getluaDocByContain(state, position)
18011822
return result
18021823
end
18031824

1825+
---@return parser.state.err?, parser.object?
18041826
local function getluaDocByErr(state, start, position)
18051827
local targetError
18061828
for _, err in ipairs(state.errs) do
@@ -2008,7 +2030,7 @@ local function tryluaDocByErr(state, position, err, docState, results)
20082030
for _, doc in ipairs(vm.getDocSets(state.uri)) do
20092031
if doc.type == 'doc.class'
20102032
and not used[doc.class[1]]
2011-
and doc.class[1] ~= docState.class[1] then
2033+
and docState and doc.class[1] ~= docState.class[1] then
20122034
used[doc.class[1]] = true
20132035
results[#results+1] = {
20142036
label = doc.class[1],

script/core/diagnostics/undefined-doc-name.lua

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,6 @@ return function (uri, callback)
1313
return
1414
end
1515

16-
local function hasNameOfGeneric(name, source)
17-
if not source.typeGeneric then
18-
return false
19-
end
20-
if not source.typeGeneric[name] then
21-
return false
22-
end
23-
return true
24-
end
25-
2616
guide.eachSource(state.ast.docs, function (source)
2717
if source.type ~= 'doc.extends.name'
2818
and source.type ~= 'doc.type.name' then
@@ -35,8 +25,7 @@ return function (uri, callback)
3525
if name == '...' or name == '_' or name == 'self' then
3626
return
3727
end
38-
if #vm.getDocSets(uri, name) > 0
39-
or hasNameOfGeneric(name, source) then
28+
if #vm.getDocSets(uri, name) > 0 then
4029
return
4130
end
4231
callback {

script/core/highlight.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ local function checkInIf(state, source, text, position)
6363
local endA = endB - #'end' + 1
6464
if position >= source.finish - #'end'
6565
and position <= source.finish
66-
and text:sub(endA, endB) == 'end' then
66+
and text and text:sub(endA, endB) == 'end' then
6767
return true
6868
end
6969
-- 检查每个子模块
@@ -83,7 +83,7 @@ local function makeIf(state, source, text, callback)
8383
-- end
8484
local endB = guide.positionToOffset(state, source.finish)
8585
local endA = endB - #'end' + 1
86-
if text:sub(endA, endB) == 'end' then
86+
if text and text:sub(endA, endB) == 'end' then
8787
callback(source.finish - #'end', source.finish)
8888
end
8989
-- 每个子模块

script/fs-utility.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ function dfs:__div(filename)
128128
return new
129129
end
130130

131+
---@package
131132
function dfs:_open(index)
132133
local paths = split(self.path, '[/\\]')
133134
local current = self.files
@@ -147,6 +148,7 @@ function dfs:_open(index)
147148
return current
148149
end
149150

151+
---@package
150152
function dfs:_filename()
151153
return self.path:match '[^/\\]+$'
152154
end
@@ -291,6 +293,7 @@ local function fsIsDirectory(path, option)
291293
if path.type == 'dummy' then
292294
return path:isDirectory()
293295
end
296+
---@cast path -dummyfs
294297
local status = fs.symlink_status(path):type()
295298
return status == 'directory'
296299
end
@@ -347,6 +350,7 @@ local function fsSave(path, text, option)
347350
return false
348351
end
349352
if path.type == 'dummy' then
353+
---@cast path -fs.path
350354
local dir = path:_open(-2)
351355
if not dir then
352356
option.err[#option.err+1] = '无法打开:' .. path:string()
@@ -385,6 +389,7 @@ local function fsLoad(path, option)
385389
return nil
386390
end
387391
else
392+
---@cast path -dummyfs
388393
local text, err = m.loadFile(path)
389394
if text then
390395
return text
@@ -407,6 +412,7 @@ local function fsCopy(source, target, option)
407412
end
408413
return fsSave(target, sourceText, option)
409414
else
415+
---@cast source -dummyfs
410416
if target.type == 'dummy' then
411417
local sourceText, err = m.loadFile(source)
412418
if not sourceText then

script/gc.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
local util = require 'utility'
22

33
---@class gc
4-
---@field _list table
4+
---@field package _list table
55
local mt = {}
66
mt.__index = mt
77
mt.type = 'gc'
88
mt._removed = false
99

10+
---@package
1011
mt._max = 10
1112

1213
local function destroyGCObject(obj)

0 commit comments

Comments
 (0)