@@ -7,25 +7,28 @@ local await = require 'await'
7
7
--- @class plugin
8
8
local m = {}
9
9
10
- function m .showError (err )
10
+ function m .showError (scp , err )
11
11
if m ._hasShowedError then
12
12
return
13
13
end
14
14
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 ))
16
16
end
17
17
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
20
23
return false
21
24
end
22
- local method = m . interface [event ]
25
+ local method = interface [event ]
23
26
if type (method ) ~= ' function' then
24
27
return false
25
28
end
26
29
local clock = os.clock ()
27
30
tracy .ZoneBeginN (' plugin dispatch:' .. event )
28
- local suc , res1 , res2 = xpcall (method , log .error , ... )
31
+ local suc , res1 , res2 = xpcall (method , log .error , uri , ... )
29
32
tracy .ZoneEnd ()
30
33
local passed = os.clock () - clock
31
34
if passed > 0.1 then
@@ -34,36 +37,34 @@ function m.dispatch(event, ...)
34
37
if suc then
35
38
return true , res1 , res2
36
39
else
37
- m .showError (res1 )
40
+ m .showError (scp , res1 )
38
41
end
39
42
return false , res1
40
43
end
41
44
42
- function m .isReady ()
43
- return m .interface ~= nil
44
- end
45
-
46
45
--- @async
47
- local function checkTrustLoad ()
46
+ --- @param scp scope
47
+ local function checkTrustLoad (scp )
48
+ local pluginPath = scp :get (' pluginPath' )
48
49
local filePath = LOGPATH .. ' /trusted'
49
50
local trusted = util .loadFile (filePath )
50
51
local lines = {}
51
52
if trusted then
52
53
for line in util .eachLine (trusted ) do
53
54
lines [# lines + 1 ] = line
54
- if line == m . pluginPath then
55
+ if line == pluginPath then
55
56
return true
56
57
end
57
58
end
58
59
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 ), {
60
61
lang .script (' PLUGIN_TRUST_YES' ),
61
62
lang .script (' PLUGIN_TRUST_NO' ),
62
63
})
63
64
if not index then
64
65
return false
65
66
end
66
- lines [# lines + 1 ] = m . pluginPath
67
+ lines [# lines + 1 ] = pluginPath
67
68
util .saveFile (filePath , table.concat (lines , ' \n ' ))
68
69
return true
69
70
end
72
73
function m .init (scp )
73
74
await .call (function () --- @async
74
75
local ws = require ' workspace'
75
- m .interface = {}
76
+ local interface = {}
77
+ scp :set (' pluginInterface' , interface )
76
78
77
79
local pluginPath = ws .getAbsolutePath (scp .uri , config .get (scp .uri , ' Lua.runtime.plugin' ))
78
80
log .info (' plugin path:' , pluginPath )
@@ -83,20 +85,22 @@ function m.init(scp)
83
85
if not pluginLua then
84
86
return
85
87
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 })
88
92
local f , err = load (pluginLua , ' @' .. pluginPath , " t" , env )
89
93
if not f then
90
94
log .error (err )
91
- m .showError (err )
95
+ m .showError (scp , err )
92
96
return
93
97
end
94
- if not client .isVSCode () and not checkTrustLoad () then
98
+ if not client .isVSCode () and not checkTrustLoad (scp ) then
95
99
return
96
100
end
97
101
local suc , err = xpcall (f , log .error , f )
98
102
if not suc then
99
- m .showError (err )
103
+ m .showError (scp , err )
100
104
return
101
105
end
102
106
0 commit comments