Skip to content

Commit 647971c

Browse files
authored
Merge branch 'master' into generic-pattern1
2 parents 1e51dd0 + 34319c7 commit 647971c

File tree

7 files changed

+123
-68
lines changed

7 files changed

+123
-68
lines changed

script/config/template.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ local template = {
223223
'||', '&&', '!', '!=',
224224
'continue',
225225
}),
226-
['Lua.runtime.plugin'] = Type.String,
227-
['Lua.runtime.pluginArgs'] = Type.Array(Type.String),
226+
['Lua.runtime.plugin'] = Type.Or(Type.String, Type.Array(Type.String)) ,
227+
['Lua.runtime.pluginArgs'] = Type.Or(Type.Array(Type.String), Type.Hash(Type.String, Type.String)),
228228
['Lua.runtime.fileEncoding'] = Type.String >> 'utf8' << {
229229
'utf8',
230230
'ansi',

script/plugin.lua

Lines changed: 81 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,37 @@ function m.showError(scp, err)
1818
client.showMessage('Error', lang.script('PLUGIN_RUNTIME_ERROR', scp:get('pluginPath'), err))
1919
end
2020

21+
---@alias plugin.event 'OnSetText' | 'OnTransformAst'
22+
23+
---@param event plugin.event
2124
function m.dispatch(event, uri, ...)
2225
local scp = scope.getScope(uri)
23-
local interface = scp:get('pluginInterface')
24-
if not interface then
25-
return false
26-
end
27-
local method = interface[event]
28-
if type(method) ~= 'function' then
26+
local interfaces = scp:get('pluginInterfaces')
27+
if not interfaces then
2928
return false
3029
end
31-
local clock = os.clock()
32-
tracy.ZoneBeginN('plugin dispatch:' .. event)
33-
local suc, res1, res2 = xpcall(method, log.error, uri, ...)
34-
tracy.ZoneEnd()
35-
local passed = os.clock() - clock
36-
if passed > 0.1 then
37-
log.warn(('Call plugin event [%s] takes [%.3f] sec'):format(event, passed))
38-
end
39-
if suc then
40-
return true, res1, res2
41-
else
42-
m.showError(scp, res1)
30+
local failed = 0
31+
local res1, res2
32+
for i, interface in ipairs(interfaces) do
33+
local method = interface[event]
34+
if type(method) ~= 'function' then
35+
return false
36+
end
37+
local clock = os.clock()
38+
tracy.ZoneBeginN('plugin dispatch:' .. event)
39+
local suc
40+
suc, res1, res2 = xpcall(method, log.error, uri, ...)
41+
tracy.ZoneEnd()
42+
local passed = os.clock() - clock
43+
if passed > 0.1 then
44+
log.warn(('Call plugin event [%s] takes [%.3f] sec'):format(event, passed))
45+
end
46+
if not suc then
47+
m.showError(scp, res1)
48+
failed = failed + 1
49+
end
4350
end
44-
return false, res1
51+
return failed == 0, res1, res2
4552
end
4653

4754
---@async
@@ -75,53 +82,72 @@ end
7582
local function initPlugin(uri)
7683
await.call(function () ---@async
7784
local scp = scope.getScope(uri)
78-
local interface = {}
79-
scp:set('pluginInterface', interface)
85+
local interfaces = {}
86+
scp:set('pluginInterfaces', interfaces)
8087

8188
if not scp.uri then
8289
return
8390
end
84-
85-
local pluginPath = ws.getAbsolutePath(scp.uri, config.get(scp.uri, 'Lua.runtime.plugin'))
86-
log.info('plugin path:', pluginPath)
87-
if not pluginPath then
91+
---@type string[]|string
92+
local pluginConfigPaths = config.get(scp.uri, 'Lua.runtime.plugin')
93+
if not pluginConfigPaths then
8894
return
8995
end
90-
91-
--Adding the plugins path to package.path allows for requires in files
92-
--to find files relative to itself.
93-
local oldPath = package.path
94-
local path = fs.path(pluginPath):parent_path() / '?.lua'
95-
if not package.path:find(path:string(), 1, true) then
96-
package.path = package.path .. ';' .. path:string()
96+
local args = config.get(scp.uri, 'Lua.runtime.pluginArgs')
97+
if type(pluginConfigPaths) == 'string' then
98+
pluginConfigPaths = { pluginConfigPaths }
9799
end
100+
for i, pluginConfigPath in ipairs(pluginConfigPaths) do
101+
local myArgs = args
102+
if args then
103+
for k, v in pairs(args) do
104+
if pluginConfigPath:find(k, 1, true) then
105+
myArgs = v
106+
break
107+
end
108+
end
109+
end
98110

99-
local pluginLua = util.loadFile(pluginPath)
100-
if not pluginLua then
101-
log.warn('plugin not found:', pluginPath)
102-
package.path = oldPath
103-
return
104-
end
111+
local pluginPath = ws.getAbsolutePath(scp.uri, pluginConfigPath)
112+
log.info('plugin path:', pluginPath)
113+
if not pluginPath then
114+
return
115+
end
105116

106-
scp:set('pluginPath', pluginPath)
117+
--Adding the plugins path to package.path allows for requires in files
118+
--to find files relative to itself.
119+
local oldPath = package.path
120+
local path = fs.path(pluginPath):parent_path() / '?.lua'
121+
if not package.path:find(path:string(), 1, true) then
122+
package.path = package.path .. ';' .. path:string()
123+
end
107124

108-
local env = setmetatable(interface, { __index = _ENV })
109-
local f, err = load(pluginLua, '@'..pluginPath, "t", env)
110-
if not f then
111-
log.error(err)
112-
m.showError(scp, err)
113-
return
114-
end
115-
if not client.isVSCode() and not checkTrustLoad(scp) then
116-
return
117-
end
118-
local pluginArgs = config.get(scp.uri, 'Lua.runtime.pluginArgs')
119-
local suc, err = xpcall(f, log.error, f, uri, pluginArgs)
120-
if not suc then
121-
m.showError(scp, err)
122-
return
123-
end
125+
local pluginLua = util.loadFile(pluginPath)
126+
if not pluginLua then
127+
log.warn('plugin not found:', pluginPath)
128+
package.path = oldPath
129+
return
130+
end
124131

132+
scp:set('pluginPath', pluginPath)
133+
134+
local interface = setmetatable({}, { __index = _ENV })
135+
local f, err = load(pluginLua, '@' .. pluginPath, "t", interface)
136+
if not f then
137+
log.error(err)
138+
m.showError(scp, err)
139+
return
140+
end
141+
if not client.isVSCode() and not checkTrustLoad(scp) then
142+
return
143+
end
144+
local suc, err = xpcall(f, log.error, f, uri, myArgs)
145+
if not suc then
146+
m.showError(scp, err)
147+
return
148+
end
149+
interfaces[#interfaces+1] = interface
150+
end
125151
ws.resetFiles(scp)
126152
end)
127153
end

script/vm/compiler.lua

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,35 @@ local function compileLocal(source)
10901090
end
10911091
end
10921092
end
1093+
if not hasDocArg
1094+
and func.parent.type == 'local' then
1095+
local refs = func.parent.ref
1096+
local findCall
1097+
if refs then
1098+
for i, ref in ipairs(refs) do
1099+
if ref.parent.type == 'call' then
1100+
findCall = ref.parent
1101+
break
1102+
end
1103+
end
1104+
end
1105+
if findCall and findCall.args then
1106+
local index
1107+
for i, arg in ipairs(source.parent) do
1108+
if arg == source then
1109+
index = i
1110+
break
1111+
end
1112+
end
1113+
if index then
1114+
local callerArg = findCall.args[index]
1115+
if callerArg then
1116+
hasDocArg = true
1117+
vm.setNode(source, vm.compileNode(callerArg))
1118+
end
1119+
end
1120+
end
1121+
end
10931122
if not hasDocArg then
10941123
vm.setNode(source, vm.declareGlobal('type', 'any'))
10951124
end

test/diagnostics/await-in-sync.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ end
119119

120120
TEST [[
121121
local function f(cb)
122-
cb()
122+
<!cb!>()
123123
end
124124
125125
local function af()

test/diagnostics/redundant-parameter.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ print(1, 2, 3, 4, 5)
9494

9595
TEST [[
9696
local function f(callback)
97-
callback(1, 2, 3)
97+
callback(<!1!>, <!2!>, <!3!>)
9898
end
9999
f(function () end)
100100
]]

test/hover/init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,15 @@ function string.lower(s: string|number)
276276
-> string
277277
]]
278278

279-
-- 不根据传入值推测参数类型
279+
-- 根据传入值推测参数类型
280280
TEST [[
281281
local function x(a, ...)
282282
end
283283
284284
<?x?>(1, 2, 3, 4, 5, 6, 7)
285285
]]
286286
[[
287-
function x(a: any, ...any)
287+
function x(a: integer, ...any)
288288
]]
289289

290290
TEST [[

test/signature/init.lua

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ end
8888
8989
x(1, 2, 3, <??>
9090
]]
91-
{'function x(a: any, <!...any!>)'}
91+
{'function x(a: integer, <!...any!>)'}
9292

9393
TEST [[
9494
(''):sub(<??>
@@ -106,7 +106,7 @@ end
106106
107107
f(1, 'string<??>')
108108
]]
109-
{'function f(a: any, <!b: any!>, c: any)'}
109+
{'function f(a: integer, <!b: string!>, c: any)'}
110110

111111
TEST [[
112112
pcall(function () <??> end)
@@ -156,7 +156,7 @@ end
156156
157157
f({},<??>)
158158
]]
159-
{'function f(a: any, <!b: any!>, c: any)'}
159+
{'function f(a: table, <!b: any!>, c: any)'}
160160

161161
TEST [[
162162
for _ in pairs(<??>) do
@@ -188,31 +188,31 @@ end
188188
189189
x( aaaa <??>, 2)
190190
]]
191-
{"function x(<!a: any!>, b: any)"}
191+
{"function x(<!a: any!>, b: integer)"}
192192

193193
TEST [[
194194
local function x(a, b)
195195
end
196196
197197
x(<??> aaaa , 2)
198198
]]
199-
{'function x(<!a: any!>, b: any)'}
199+
{'function x(<!a: any!>, b: integer)'}
200200

201201
TEST [[
202202
local function x(a, b)
203203
end
204204
205205
x(aaaa ,<??> 2)
206206
]]
207-
{'function x(a: any, <!b: any!>)'}
207+
{'function x(a: any, <!b: integer!>)'}
208208

209209
TEST [[
210210
local function x(a, b)
211211
end
212212
213213
x(aaaa , 2 <??>)
214214
]]
215-
{'function x(a: any, <!b: any!>)'}
215+
{'function x(a: any, <!b: integer!>)'}
216216

217217
TEST [[
218218
local fooC

0 commit comments

Comments
 (0)