summaryrefslogtreecommitdiff
path: root/script
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2022-01-21 17:46:13 +0800
committer最萌小汐 <sumneko@hotmail.com>2022-01-21 17:46:13 +0800
commit9b67d5bee1cf7a7e2d86d14543ea5e93f40396fa (patch)
tree729d29eae92e50dbf8f9de5e0c2869a8b07a704b /script
parent4d538055a120ff39fe40e9c73bb40df5645c50e4 (diff)
downloadlua-language-server-9b67d5bee1cf7a7e2d86d14543ea5e93f40396fa.zip
fix plugin
Diffstat (limited to 'script')
-rw-r--r--script/plugin.lua46
1 files changed, 25 insertions, 21 deletions
diff --git a/script/plugin.lua b/script/plugin.lua
index 7f2165db..677644ef 100644
--- a/script/plugin.lua
+++ b/script/plugin.lua
@@ -7,25 +7,28 @@ local await = require 'await'
---@class plugin
local m = {}
-function m.showError(err)
+function m.showError(scp, err)
if m._hasShowedError then
return
end
m._hasShowedError = true
- client.showMessage('Error', lang.script('PLUGIN_RUNTIME_ERROR', m.pluginPath, err))
+ client.showMessage('Error', lang.script('PLUGIN_RUNTIME_ERROR', scp:get('pluginPath'), err))
end
-function m.dispatch(event, ...)
- if not m.interface then
+function m.dispatch(event, uri, ...)
+ local ws = require 'workspace'
+ local scp = ws.getScope(uri)
+ local interface = scp:get('pluginInterface')
+ if not interface then
return false
end
- local method = m.interface[event]
+ local method = interface[event]
if type(method) ~= 'function' then
return false
end
local clock = os.clock()
tracy.ZoneBeginN('plugin dispatch:' .. event)
- local suc, res1, res2 = xpcall(method, log.error, ...)
+ local suc, res1, res2 = xpcall(method, log.error, uri, ...)
tracy.ZoneEnd()
local passed = os.clock() - clock
if passed > 0.1 then
@@ -34,36 +37,34 @@ function m.dispatch(event, ...)
if suc then
return true, res1, res2
else
- m.showError(res1)
+ m.showError(scp, res1)
end
return false, res1
end
-function m.isReady()
- return m.interface ~= nil
-end
-
---@async
-local function checkTrustLoad()
+---@param scp scope
+local function checkTrustLoad(scp)
+ local pluginPath = scp:get('pluginPath')
local filePath = LOGPATH .. '/trusted'
local trusted = util.loadFile(filePath)
local lines = {}
if trusted then
for line in util.eachLine(trusted) do
lines[#lines+1] = line
- if line == m.pluginPath then
+ if line == pluginPath then
return true
end
end
end
- local _, index = client.awaitRequestMessage('Warning', lang.script('PLUGIN_TRUST_LOAD', m.pluginPath), {
+ local _, index = client.awaitRequestMessage('Warning', lang.script('PLUGIN_TRUST_LOAD', pluginPath), {
lang.script('PLUGIN_TRUST_YES'),
lang.script('PLUGIN_TRUST_NO'),
})
if not index then
return false
end
- lines[#lines+1] = m.pluginPath
+ lines[#lines+1] = pluginPath
util.saveFile(filePath, table.concat(lines, '\n'))
return true
end
@@ -72,7 +73,8 @@ end
function m.init(scp)
await.call(function () ---@async
local ws = require 'workspace'
- m.interface = {}
+ local interface = {}
+ scp:set('pluginInterface', interface)
local pluginPath = ws.getAbsolutePath(scp.uri, config.get(scp.uri, 'Lua.runtime.plugin'))
log.info('plugin path:', pluginPath)
@@ -83,20 +85,22 @@ function m.init(scp)
if not pluginLua then
return
end
- m.pluginPath = pluginPath
- local env = setmetatable(m.interface, { __index = _ENV })
+
+ scp:set('pluginPath', pluginPath)
+
+ local env = setmetatable(interface, { __index = _ENV })
local f, err = load(pluginLua, '@'..pluginPath, "t", env)
if not f then
log.error(err)
- m.showError(err)
+ m.showError(scp, err)
return
end
- if not client.isVSCode() and not checkTrustLoad() then
+ if not client.isVSCode() and not checkTrustLoad(scp) then
return
end
local suc, err = xpcall(f, log.error, f)
if not suc then
- m.showError(err)
+ m.showError(scp, err)
return
end