Skip to content

Commit 9b67d5b

Browse files
committed
fix plugin
1 parent 4d53805 commit 9b67d5b

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

script/plugin.lua

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,28 @@ local await = require 'await'
77
---@class plugin
88
local m = {}
99

10-
function m.showError(err)
10+
function m.showError(scp, err)
1111
if m._hasShowedError then
1212
return
1313
end
1414
m._hasShowedError = true
15-
client.showMessage('Error', lang.script('PLUGIN_RUNTIME_ERROR', m.pluginPath, err))
15+
client.showMessage('Error', lang.script('PLUGIN_RUNTIME_ERROR', scp:get('pluginPath'), err))
1616
end
1717

18-
function m.dispatch(event, ...)
19-
if not m.interface then
18+
function m.dispatch(event, uri, ...)
19+
local ws = require 'workspace'
20+
local scp = ws.getScope(uri)
21+
local interface = scp:get('pluginInterface')
22+
if not interface then
2023
return false
2124
end
22-
local method = m.interface[event]
25+
local method = interface[event]
2326
if type(method) ~= 'function' then
2427
return false
2528
end
2629
local clock = os.clock()
2730
tracy.ZoneBeginN('plugin dispatch:' .. event)
28-
local suc, res1, res2 = xpcall(method, log.error, ...)
31+
local suc, res1, res2 = xpcall(method, log.error, uri, ...)
2932
tracy.ZoneEnd()
3033
local passed = os.clock() - clock
3134
if passed > 0.1 then
@@ -34,36 +37,34 @@ function m.dispatch(event, ...)
3437
if suc then
3538
return true, res1, res2
3639
else
37-
m.showError(res1)
40+
m.showError(scp, res1)
3841
end
3942
return false, res1
4043
end
4144

42-
function m.isReady()
43-
return m.interface ~= nil
44-
end
45-
4645
---@async
47-
local function checkTrustLoad()
46+
---@param scp scope
47+
local function checkTrustLoad(scp)
48+
local pluginPath = scp:get('pluginPath')
4849
local filePath = LOGPATH .. '/trusted'
4950
local trusted = util.loadFile(filePath)
5051
local lines = {}
5152
if trusted then
5253
for line in util.eachLine(trusted) do
5354
lines[#lines+1] = line
54-
if line == m.pluginPath then
55+
if line == pluginPath then
5556
return true
5657
end
5758
end
5859
end
59-
local _, index = client.awaitRequestMessage('Warning', lang.script('PLUGIN_TRUST_LOAD', m.pluginPath), {
60+
local _, index = client.awaitRequestMessage('Warning', lang.script('PLUGIN_TRUST_LOAD', pluginPath), {
6061
lang.script('PLUGIN_TRUST_YES'),
6162
lang.script('PLUGIN_TRUST_NO'),
6263
})
6364
if not index then
6465
return false
6566
end
66-
lines[#lines+1] = m.pluginPath
67+
lines[#lines+1] = pluginPath
6768
util.saveFile(filePath, table.concat(lines, '\n'))
6869
return true
6970
end
@@ -72,7 +73,8 @@ end
7273
function m.init(scp)
7374
await.call(function () ---@async
7475
local ws = require 'workspace'
75-
m.interface = {}
76+
local interface = {}
77+
scp:set('pluginInterface', interface)
7678

7779
local pluginPath = ws.getAbsolutePath(scp.uri, config.get(scp.uri, 'Lua.runtime.plugin'))
7880
log.info('plugin path:', pluginPath)
@@ -83,20 +85,22 @@ function m.init(scp)
8385
if not pluginLua then
8486
return
8587
end
86-
m.pluginPath = pluginPath
87-
local env = setmetatable(m.interface, { __index = _ENV })
88+
89+
scp:set('pluginPath', pluginPath)
90+
91+
local env = setmetatable(interface, { __index = _ENV })
8892
local f, err = load(pluginLua, '@'..pluginPath, "t", env)
8993
if not f then
9094
log.error(err)
91-
m.showError(err)
95+
m.showError(scp, err)
9296
return
9397
end
94-
if not client.isVSCode() and not checkTrustLoad() then
98+
if not client.isVSCode() and not checkTrustLoad(scp) then
9599
return
96100
end
97101
local suc, err = xpcall(f, log.error, f)
98102
if not suc then
99-
m.showError(err)
103+
m.showError(scp, err)
100104
return
101105
end
102106

0 commit comments

Comments
 (0)