summaryrefslogtreecommitdiff
path: root/script
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2021-09-30 17:13:55 +0800
committer最萌小汐 <sumneko@hotmail.com>2021-09-30 17:13:55 +0800
commitd15a62ec1f0957eb464c7f5c08e0b8bcad6bd487 (patch)
treea23eeba2e639ce1faf7bc77f6bce310cdcd98bdc /script
parent908ff9da629ad6cc307a3621ce0c742049db1a77 (diff)
downloadlua-language-server-d15a62ec1f0957eb464c7f5c08e0b8bcad6bd487.zip
ask for trusting plugin
Diffstat (limited to 'script')
-rw-r--r--script/client.lua5
-rw-r--r--script/plugin.lua77
2 files changed, 56 insertions, 26 deletions
diff --git a/script/client.lua b/script/client.lua
index e77eaaae..1ade4a9e 100644
--- a/script/client.lua
+++ b/script/client.lua
@@ -87,16 +87,19 @@ end
---@param message string
---@param titles string[]
---@return string action
+---@return integer index
function m.awaitRequestMessage(type, message, titles)
proto.notify('window/logMessage', {
type = define.MessageType[type] or 3,
message = message,
})
+ local map = {}
local actions = {}
for i, title in ipairs(titles) do
actions[i] = {
title = title,
}
+ map[title] = i
end
local item = proto.awaitRequest('window/showMessageRequest', {
type = type,
@@ -106,7 +109,7 @@ function m.awaitRequestMessage(type, message, titles)
if not item then
return nil
end
- return item.title
+ return item.title, map[item.title]
end
---@param type message.type
diff --git a/script/plugin.lua b/script/plugin.lua
index 21e05292..e66e410f 100644
--- a/script/plugin.lua
+++ b/script/plugin.lua
@@ -2,6 +2,7 @@ local config = require 'config'
local util = require 'utility'
local client = require 'client'
local lang = require 'language'
+local await = require 'await'
---@class plugin
local m = {}
@@ -49,34 +50,60 @@ local function resetFiles()
end
end
-function m.init()
- local ws = require 'workspace'
- m.interface = {}
-
- local pluginPath = ws.getAbsolutePath(config.get 'Lua.runtime.plugin')
- log.info('plugin path:', pluginPath)
- if not pluginPath then
- return
- end
- local pluginLua = util.loadFile(pluginPath)
- if not pluginLua then
- return
- end
- m.pluginPath = pluginPath
- local env = setmetatable(m.interface, { __index = _ENV })
- local f, err = load(pluginLua, '@'..pluginPath, "t", env)
- if not f then
- log.error(err)
- m.showError(err)
- return
+local function checkTrustLoad()
+ local trusted = util.loadFile(LOGPATH / 'trusted') or ''
+ local lines = {}
+ for line in util.eachLine(trusted) do
+ lines[#lines+1] = line
+ if line == m.pluginPath then
+ return true
+ end
end
- local suc, err = xpcall(f, log.error, f)
- if not suc then
- m.showError(err)
- return
+ local _, index = client.awaitRequestMessage('Warning', lang.script('PLUGIN_TRUST_LOAD', m.pluginPath), {
+ lang.script('PLUGIN_TRUST_YES'),
+ lang.script('PLUGIN_TRUST_NO'),
+ })
+ if not index then
+ return false
end
+ lines[#lines+1] = m.pluginPath
+ util.saveFile(LOGPATH / 'trusted', table.concat(trusted, '\n'))
+ return true
+end
+
+function m.init()
+ await.call(function ()
+ local ws = require 'workspace'
+ m.interface = {}
+
+ local pluginPath = ws.getAbsolutePath(config.get 'Lua.runtime.plugin')
+ log.info('plugin path:', pluginPath)
+ if not pluginPath then
+ return
+ end
+ local pluginLua = util.loadFile(pluginPath)
+ if not pluginLua then
+ return
+ end
+ m.pluginPath = pluginPath
+ local env = setmetatable(m.interface, { __index = _ENV })
+ local f, err = load(pluginLua, '@'..pluginPath, "t", env)
+ if not f then
+ log.error(err)
+ m.showError(err)
+ return
+ end
+ if not checkTrustLoad() then
+ return
+ end
+ local suc, err = xpcall(f, log.error, f)
+ if not suc then
+ m.showError(err)
+ return
+ end
- resetFiles()
+ resetFiles()
+ end)
end
return m